Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Welcome

To
The
World
Of

‘C’
Introduction to C

M.Srikanth,
Project Engineer,
CDAC,Hyderabad.

2
AGENDA

● History of C Language
● Low Level and High Level Languages
● Characteristics of C
● Simple C Program
● Preprocessor Directives
● C overview

3
C: History
➢ Root of the modern language is
ALGOL 1960. It’s first computer
language to use a block structure.

➢ It gave concept of “structured


programming”.

➢ In 1967, Martin Richards developed


a language, BCPL (Basic Combined
Programming Language)

4
C: History
➢ In 1970,by Ken Thompson created a
language called as ‘B’.
➢ It used to create early version of Unix.

➢ In 1972,by Dennis Ritchie introduced


new language called as ‘C’ .

5
C: History
● Developed in the 1970s at AT & T Bell Laboratories by
Dennis Ritchie.
● Initially designed for programming in Unix operating
System.
● C Language was derived from B language. B language was
adopted from BCPL(Basic Combined Programming
Language).
● In 1989, the standard for C language was introduced which
is known as ANSI C.
● Most of the compilers confront to this standard.

6
C Pitfall
C is not object oriented!
Can’t “hide” data as “private” or “protected” fields
You can follow standards to write C code that looks object-oriented,
but you have to be disciplined – will the other people working on your
code also be disciplined?

7
Program:- A Set of instructions which
when carried out by processor for some
Specific input, generates specific output.

Programming language:- A specific


manner of writing a program with some
Predefined rules, symbols & their use as
a part of language.

8
Programming Languages
● Machine Level Language
– 0’s and 1’s
– Not Portable
● Assembly Language
– Use english like words
● High Level Language
– Not concerened with low level details.

9
Characteristics of C
● Middle Level Language and Robust.
● Efficient and general purpose programming
language.
● Powerful.
● Users can add their own library functions.
● Structured Programming Language.
– Improves clarity,quality by extensive use of
subroutines.

10
Translators
● Assembler
● Compiler
● Interpreter

11
Basic Structure of C Programs
Documentation Section
Preprocessor Directives
Global Variables
int main()
{
Local variables
--------------
}
func1()
{
--------------
}

12
Basic structure of ‘C’
1) Documentation Section :-
It has set of comment lines(name of
program, author details).
What is Comment line??
 To guide a programmer. To write a note for
function,operation,logic in between a
program.
 Non-executable statement.
 Can’t be nested.
e.g:- /* Hello /* abc */ Hi */
ERROR.
13
Example for use of comments
/*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/


Only /* … */ for comments – no // like Java or C++

14
14
Comments on comments

int main()
{
int a=5,b;
b=fun(a);
return 0;
}

/* Output of the following code


int fun(int x) {
return x+42; /* return the result */
}
*/

15
15 CS 3090: Safety Critical Programming in
C
Comments on comments

Can’t nest comments within comments
– /* is matched with the very next */ that comes along

Don’t use /* … */ to comment out code – it won’t work if
the commented-out code contains comments

/* Comment out the following code


int f(int x) {
return x+42; /* return the result */ Only this will
} be
*/ commented
out

Single Line Comments //

16
16
Preprocessor Directives
● # include
To access the functions which are stored in
library, it is necessary to tell the compiler ,
about the file to be accessed.

Syntax :
#include <stdio.h>

17
Global Variables
➢ Some variables that are used in more than
one function, such variables (global
variables) declared in the global declaration
section.

➢ It also declares all the user-defined function.

18
A Simple C Program:
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 int main()
6 {
7 printf( "Welcome to C!\n" );
8
9 return 0;
10 }

Welcome to C!

• #include <stdio.h>
– Preprocessor directive

Tells computer to load contents of a certain file
– <stdio.h> allows standard input/output
operations
19
A Simple C Program:
• int main()
– C programs contain one or more functions,
exactly one of which must be main
– Parenthesis is used to indicate a function

– int means that main "returns" an integer value


– Braces ({ and }) indicate a block

The bodies of all functions must be contained in
braces

20
C Program Compilation

A C program consists of source code in one or more files

Each source file is run through the preprocessor and compiler,
resulting in a file containing object code

Object files are tied together by the linker to form a single
executable program

Source code Preprocessor/ Object code


file1.c Compiler file1.o

Source code Preprocessor/ Object code


file2.c Compiler file2.o

Libraries
Linker

Executable code
a.out
21
Separate compilation

Advantage: Quicker compilation
– When modifying a program, a programmer typically edits only
a few source code files at a time.
– With separate compilation, only the files that have been
edited since the last compilation need to be recompiled when
re-building the program.
– For very large programs, this can save a lot of time.

22
The preprocessor

The preprocessor takes your source code and – following
certain directives that you give it – tweaks it in various ways
before compilation.

A directive is given as a line of source code starting with the #
symbol

The preprocessor works in a very crude, “word-processor”
way, simply cutting and pasting –
it doesn’t really know anything about C!

Your Enhanced and


source obfuscated Object
code source code code

Preprocessor Compiler

23
24
25
©1992-2010 by Pearson Education, Inc. All
Rights Reserved.
26
Memory Concepts
● Variables
● Variable names correspond to locations in the computer's

memory.
● Every variable has a name, a type, a size and a value.

● Whenever a new value is placed into a variable (through

scanf, for example), it replaces (and destroys) the


previous value.
● Reading variables from memory does not change them

● A visual representation.

integer1 45

27
28
29
30
31
32
33
C OPERATORS, OPERANDS, EXPRESSION &
STATEMENTS

 An operator specifies an operation to be performed that yields a


value.
 Operators are symbols which take one or more operands or
expressions and perform arithmetic or logical computations.
 Operands are variables or expressions which are used in conjunction
with operators to evaluate the expression.
 Combination of operands and operators form an expression.
 Expressions are sequences of operators, operands, and punctuators that
specify a computation.
 Evaluation of expressions is based on the operators that the expressions
contain and the context in which they are used.
 Expression can result in a value and can produce execution sequence .
Categories of operators
● Arithmetic operators
● Assignment operators
● Increment and decrement operators
● Relational operators
● Logical operators
● Conditional operators
● Sizeof operator
● Bitwise operators
● other operators

35
Arithmetic operators
● Unary operators
+x -y
● Binary operators
+ - * / %

● % operator cannot be used with floating point numbers.

36
37
38
● As in algebra, it is acceptable to place unnecessary
parentheses in an expression to make the
expression clearer.
● These are called redundant parentheses.

39
40
Assignment operators
● =
● +=
● -=
● *=
● /=
● %=

41
Increment and Decrement operators
● ++x
● --x

42
Relational operators
● <
● <=
● ==
● !=
● >
● >=

43
Logical and Boolean
● &&
● ||
● !

44
Conditonal operator
● Test Expression ? expression1:expression2

● example
● Max= a>b ? a:b

45
sizeof operator
● sizeof(int)

46
Bitwise operators
● &
● |
● ~
● <<
● >>
● ^

47
48
49
Precedence and Associativity

50
Thank You

51

You might also like