2. Basics of C Programming
Overview of C – C Character Set –
Identifiers and Keywords – Declaration
– Data Types – Type Qualifiers and
Type Modifiers –Variables and
Constants – Structure of a C Program –
Executing a C Program – Operators
and Expressions – Decision-Making
and Looping Statements.
L.NIVETHA AP/CSE,KNCET
3. C is a general purpose, block
structured, procedural, case sensitive, free
flow, portable, and high level Programming
Language
C having both the feature of high level
language as well as low level language
programming
so it is called as middle level programming
language.
C is a general purpose programming
language
so we have to create application to all the
purposes.
Like medical, banking, library …..etc
L.NIVETHA AP/CSE,KNCET
4. ‘C’ is most popular programming language,
It is developed by Dennis Ritchie at AT & T’s
Bell laboratory at USA in 1972.
Low level language:
It is in terms of 0’s and 1’s ,
‘C’ has certain features of low “level language”,
Using this 0’s and 1’s to create a program that is
called low level language program’s
High level language:
It is look like a normal English.
It is most compatible with human language.
Hear we using a to z alphabet and numbers
0-9 to make a program's called high level
language program
L.NIVETHA AP/CSE,KNCET
5. ‘C’ is general purpose ,structured
programming language.
‘C’ is powerful and efficient and flexible.
‘C’ run’s into different operating system.
‘C’ programs are fast and efficient.
L.NIVETHA AP/CSE,KNCET
6. The character set are used to represent
information.
Also the characters used to write ‘C’
program , It is basically two types.
1. Source character set
2. Execution character set
C character
set
Alphabets
Execution
character set
Source
character set
White spaces
Special
characters
Digits
Escape
sequence
L.NIVETHA AP/CSE,KNCET
7. Identifiers are names given to the
different program elements,
Example: - variables , functions , arrays…etc
Rules for naming identifiers:
1. It consist of letters and digits.
2. First character must be letter, or begin with _
3. _ underscore also consider as character.
4. Both upper/lower character accepted.
5. No special character allowed.
6. identifier cannot be keyword.
L.NIVETHA AP/CSE,KNCET
8. Keywords are reserved words whose meaning
has already been explained to the compiler .
The keywords are also called reserved words.
We won’t declare keyword as a variable or a
function name.
Example:
auto , int , long , if , static , void …. etc
L.NIVETHA AP/CSE,KNCET
9. The tokens are referred as individual text
and punctuation in a passage of text.
L.NIVETHA AP/CSE,KNCET
C tokens
Identifier
s
main()
Total
avg
keyword
s
int
double
for
constant
s
39
3.14
strings
“college
”
Operato
rs
+ -
Special
symbol
s
# $ %
10. variable is a an identifiers that is used to
store some specified type of information.
Rules for naming the variables:
1. Variable name can be character , character
with number, _ underscore with number.
2. First character must be alphabet or
underscore.
3. Variable name with in 8 character.
4. Blank space not allowed.
5. Special symbol not allowed.
L.NIVETHA AP/CSE,KNCET
11. Variable declaration:
syntax: data_type v1,v2,v3…….etc;
Example:
int a, b;
Initializing variables:
Syntax: data_type variable=value;
Example:
int radios=10;
float cutoff=198.4;
char c=‘a’;
L.NIVETHA AP/CSE,KNCET
12. Data type is the type of data ,That are going
to access within the program.
C support different data types.
Each data type having predefined memory
requirement and storage representation.
C support following four classes of data
types.
L.NIVETHA AP/CSE,KNCET
13. C data types
Primary User defined Derived Empty
Char
Int
Float
double
typedef
Enum
Structure
union
Arrays
Pointer
function
void
Data type Description Memory
bytes
Control
strings
Example
int integer
numbers
2 bytes %d int a=20;
float Decimal
point
numbers
4 bytes %f float b=20.1
char Single
character
1 byte %c char s= ‘ n ‘;
double Double 8 bytes %if double
L.NIVETHA AP/CSE,KNCET
14. Integer:
An integer type (int ) represents signed
whole numbers . without decimal point.
They can be positive or negative numbers.
The range is -2 , 147 to +2 , 147
1. Normal integer numbers:
It is a signed whole numbers it may be
positive or negative
Int a = 27
Int b = -23
int c = 3435
L.NIVETHA AP/CSE,KNCET
15. Definition:
Floating point type represent numbers with
fractional part (or) real numbers with
decimal point.
They can be positive or negative numbers, It
will take 4 bytes of memory.
Example:
float a = 3.14
float b = -15.45
L.NIVETHA AP/CSE,KNCET
16. character is a single alphabet stored by using
single quotes.
string are represented as near by set of
character with in double quotes.
char s=‘a’;
char name[20]=“ram”;
L.NIVETHA AP/CSE,KNCET
17. double data type store both integer as well
as floating point data item.
It will allocate 8 byte of memory space.
Example :
double s=232444;
L.NIVETHA AP/CSE,KNCET
18. Typedef data type:
syntax: typedef data type variablename;
Example:
typedef int mark;
mark m1,m2,m3;
Range
Signed Int -32,768 to +32, 767
Signed Char -128 to +127
Unsigned char 0 to 255
Unsigned int 0 to 65,767
L.NIVETHA AP/CSE,KNCET
19. Short , long , signed , unsigned.
Data type Size in bytes Control
string
Char signed and
unsigned
1 %c
Int signed 2 %d
Unsigned int 2 %u
Signed short int
Unsigned short int
1 %d
Long int 4 %id
Unsigned long int 4 %iu
float 4 %f
double 8 %if
Long double 10 %if
L.NIVETHA AP/CSE,KNCET
20. 1. Type declaration:
syntax:
typedef data_type variable name;
Example:
typedef int mark;
mark m1,m2,m3;
2. Enumerated data type:
L.NIVETHA AP/CSE,KNCET
21. 2. Enumerated data type:
C provides user defined data type called
enumerated data type ,it attaché names to
numbers ,here we access the enum variable by
using index position.
syntax:
enum variable {valu1, value2,
value3……etc value n };
Example:1
enum day {mon , tue , wed , ….. , sun }
printf(“mon :%d”, mon);
Output:
0
Example:2
enum day w_st, w_end;
w_st=mon , w_end=sat;
L.NIVETHA AP/CSE,KNCET
23. The item whose value cannot change at
the time of execution are called constant.
‘C’ constants
Real constant
Integer
constant
Character
constant
Numeric
constant
String constant
Character
constant
L.NIVETHA AP/CSE,KNCET
24. 1. Integer constant:
It is formed with the use of sequence of digit
without decimal point , that is .
Decimal digit - 0 to 9
Octal digit - 0 to 7
Hexadecimal digit - 0 to 9 A, B, C, D, E, F
Rules:
It must be contain one digit
Decimal point not allowed,
It can either negative or positive,
Range is -32,768 to +32, 767.
No special character and blank spaces allowed with
in an integer literals
EXAMPLE:
const int mark=90 , avg=75 , total=10A1;
#define rollno 301;
L.NIVETHA AP/CSE,KNCET
25. 2. Real constant:
It contain sequence of digits with decimal
point.
Rules:
It must be contain one decimal digit
It can either negative or positive,
Range is -32,768 to +32, 767.
No special character and blank spaces
allowed with in an float literals
Example:
const float distance =120.8;
#define pi 3.14;
L.NIVETHA AP/CSE,KNCET
26. Character constant:
1. Single character constant:
The character constant contain single character with in single quotes.
Syntax:
const data type variable= ‘ single character ‘;
Example:
const char initial= ‘ s ‘ ;
2. String constant:
The string constant contains more than set of character with in
double quotes.
Syntax:
const data type variable= “ string “;
Example:
const char department[5]=“IT”;
4. Declaring a variable as volatile:
Volatile data also one of constant data, it’s value modified only
with in the program, also we could not change from external.
Syntax:
volatile data type variable=constant ;
Example:
volatile const int year=2007 ;
L.NIVETHA AP/CSE,KNCET
27. Delimeters:
Delimeters are symbols, it not perform any
operation, only it separate the variable and
statements.
#
,
:
;
()
[]
{}
L.NIVETHA AP/CSE,KNCET
28. OPERATORS used in C Programs
Presented by,
L.Nivetha,AP/CSE,
KNCET
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NAMAKKAL - TRICHY MAIN ROAD, THOTTIAM, TRICHY-621 215
L.NIVETHA AP/CSE,KNCET
29. 1. An operator specifies the operation to be
applied to it’s operand.
2. Based on the number of operator present in
an expression, the expression are classified
as simple and compound expression.
3. An operator is symbol that specifies an
operation to be performed on the operands.
Example
A+B A,B-Operands
+Operator
L.NIVETHA AP/CSE,KNCET
30. 1.Classification based on number of operand
2. classification based on role of an operator.
Classification based on number of operand
1. Unary arithmetic:
It require only one operand. like unary plus, unary
minus
Example: +a , -b
2. Binary arithmetic:
The binary operator operates on two operands. It
require two operand towards it’s left and right.
Example: a+b
3. Ternary Operator
A ternary operator operates on three operands.
conditional operator (?) is an ternary operator in C.
Example: big = a>b ? a : b ;
L.NIVETHA AP/CSE,KNCET
32. Arithmetic Operations like addition,
Subtraction, Multiplication, division ….etc.
it can be performed by using arithmetic
operator.
The arithmetic operator available in c are.
+ - addition operator
- - subtraction operator
/ - Division operator
* - Multiplication operator
% - Modulo division operator
L.NIVETHA AP/CSE,KNCET
34. Relational Operator:
It is used to compare two or more
operands , operands may be variable ,
constants or expression.
< - less than
<= - less than or equal to
>= -grater than or equal to
> -grater than
= = - is equal to
! = - not equal to
L.NIVETHA AP/CSE,KNCET
35. #include <stdio.h>
void main()
{
int a , b ;
printf("Enter two numbers: ");
scanf( "%d %d “ , &a, &b);
if(a>b)
printf( “A is biggest number” );
else
printf( “B is biggest number” );
}
L.NIVETHA AP/CSE,KNCET
36. logical operators are used to combine the
results of two or more conditions,
And produce the result based on types of
logical operator.
&& logical AND –
it will execute set of statement if two
condition are true,
associativity rule is left to Right.
Syntax:
(exp1)&&(exp2)
Example:
(a>c)&&(a>b)
L.NIVETHA AP/CSE,KNCET
37. || logical OR
It will execute set of statement if any one of
the condition is true.
Associativity rule is Left to right.
Syntax:
(exp1)||(exp2)
Example:
(a>b)||(a>d)
! Logical NOT
Here if operand is false to produce true , if
operand is true it produce false output.
associativity is Right to Left
29!=29
L.NIVETHA AP/CSE,KNCET
38. #include <stdio.h>
void main()
{
int a,b,c;
printf("Enter three numbers: ");
scanf( "%d %d %d", &a, &b,&c);
if(a>b)&&(a>c)
printf("A is biggest number”);
else if(b>c)
printf("B is biggest number”);
else
printf(“c is biggest number”);
}
L.NIVETHA AP/CSE,KNCET
39. Assignment operator used assign a values to a variable.
Syntax: variable= expression or value;
Assignment operator:
= - simple assignment
+= - assign sum
- = - assign difference
/= - assign quotient
%= - assign modulus
*= - assign product
Example:
int mark, total.
Mark=10;
Total=a+b;
Compound assignment:
x+ =y x=x+y;
Nested assignment
Y=x=20
L.NIVETHA AP/CSE,KNCET
40. Here we using increment operator (+ +) it
represent adding one to the variable,
decrement operator(- -) it represent
subtracting one to the variable ,
These operator are called unary operator.
Because it access with single operand.
++ x - pre increment
-- X - pre decrement
X ++ - post increment
X -- -post decrement
L.NIVETHA AP/CSE,KNCET
41. Conditional operator itself checks the
condition and executes the statement
depending on the condition,
Here if the condition is true then set exp1
value otherwise set exp2 value as result
Syntax: variable = condition ? Exp1:exp2;
Example:
#include<stdio.h>
void main()
{
int a=5 , b=4 , big ;
big=a>b ? a : b;
printf( “%d” , big) ;
}
L.NIVETHA AP/CSE,KNCET
42. Bitwise operator used to manipulate the data
at bit level,
it operates on integers only.
it not applicable to float or real.
operator meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement
L.NIVETHA AP/CSE,KNCET
43. Bitwise AND (&)
Here operate with two operand bit by bit.
Truth table for & is:
x= 7= 0000 0111
y= 8 = 0000 1000
OUTPUT: 0000 0000
Bitwise OR ( |)
x= 7= 0000 0111
y= 8 = 0000 1000
8421
OUTPUT:0000 1111
Bitwise exclusive OR (^)
x= 7= 0000 0111
y= 8 = 0000 1000
OUTPUT: 0000 1111
& 0 1
0 0 0
1 0 1
| 0 1
0 0 1
1 1 1
^ 0 1
0 0 1
1 1 0
L.NIVETHA AP/CSE,KNCET
44. Operators Meaning
, Comma operator used for
separation of variable
sizeof Size of operator used for find
the total bytes of a variable
Syntax: sizeof(var)
& and * & Symbol represent address
of the variable
* Symbol represent value of
the variable
. And --> It represent access element
from the structure.
L.NIVETHA AP/CSE,KNCET
45. Arithmetic expression are evaluated by
using it’s precedence(PEMDAS).
When an expression contains more than
one operator , then the order of evaluation
depends on it’s operator precedence.
Parentheses having highest precedence , it
is executed first with in the expression.
Example:
5*(5+3) = 40
In this expression first execute
parenthesis , then multiply with five and
produce 40 as result.
L.NIVETHA AP/CSE,KNCET
46. Next highest precedence is exponentiation
Next highest precedence is multiplication
and division ( * , / , % )
The lowest precedence is addition and
subtraction.( + , -)
Operator with the same precedence are
evaluated from left to right.
EXAMPLE:
Workout this expression and write correct
answer .
3 – 9*(3 + 7)+7 * 3 – 1
L.NIVETHA AP/CSE,KNCET
47. It is defined as interconnection of operands
and operator.
Based on the operator present in the
expression that is classified as
1. Simple expression
2. Compound expression.
Here we convert the various equation of
math's and physics equation into expression
Syntax: variable=expression;
Example : int a=2,b=3,c=4;
Sum=a+b*c; 20
12+2=14
L.NIVETHA AP/CSE,KNCET
48. Branching statement are used to transfer
the program control from one point to
another point.
They are classified as
1. Conditional statement – selection statement.
Conditional branching also known as
selection statement.
In this the program control transferred
from one point to another point based on
the condition.
L.NIVETHA AP/CSE,KNCET
49. 2. Unconditional statement – jumping
statement.
Unconditional statement also known as
jumping statement
Here program control transferred from one
point to another point without checking any
condition.
L.NIVETHA AP/CSE,KNCET
50. ‘c’ control statements
i. Sequential structure:
instruction are executed in sequence
i=i+1;
j=j+1;
ii. Selection structure:
if(x>y)
i=i+1;
else
J=j+1;
L.NIVETHA AP/CSE,KNCET
53. if is a decision making statement.
It is used to control the flow of execution,
so to test logically whether the condition is
true or false.
Syntax:
if(condition)
true false
{
true statements;
}
condition
True statement
L.NIVETHA AP/CSE,KNCET
54. #include <stdio.h>
void main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d %d ", &a, &b);
if(a>b)
{
printf("A is biggest number”);
}
}
L.NIVETHA AP/CSE,KNCET
55. It is basically two way decision making statement,
Here it check the condition if the condition is true
means it execute the true statements,
If it is false execute another set of statement.
Syantax:
if(condition)
true
false
{
true statement;
}
else
{
false statement;
}
conditio
n
True
statement
False
statement
L.NIVETHA AP/CSE,KNCET
56. #include <stdio.h>
void main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d %d ", &a, &b);
if(a>b)
printf("A is biggest number”);
else
printf("B is biggest number”);
}
L.NIVETHA AP/CSE,KNCET
57. It is defined as we can write a entire
if…else statement in another if…else
statement called nesting.
true
false
true false
Conditio
n 1
Conditi
on 2
true
statement 2
True
statement 1
False
statement 2
L.NIVETHA AP/CSE,KNCET
59. #include<stdio.h>
Void main()
{
int a,b,c;
printf(“enter three values:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)&&(a>c)
{
printf(“a is biggest number”);
}
else if(b>c)
{
printf(“b is biggest number”);
}
else
{
printf(“c is biggest number”);
} L.NIVETHA AP/CSE,KNCET
60. The if…else statement contain more than
two are three if…else statement with in this
called nested if….else statement.
(Or) it is called as if …else ladder.
true false
true false
true
false
conditi
on1
Condition
3
conditi
on2
True statement
1
True
statement 2
True
statement 3
False
statement 3
L.NIVETHA AP/CSE,KNCET
61. Write a c program to check whether the given number is +ve or _ve Or zero.
#include<stdio.h>
#include<conio.h>
Void main()
{
int num;
printf(“Enter the number”);
scanf(“%d”, &num);
clrscr();
if(num==0)
printf(“The number is zero”);
else if(num>0)
printf(“+ve number ”);
else if(num<0)
printf(“-ve number ”);
else
printf(“Enter valid number ”);
return(o);
getch();
}
L.NIVETHA AP/CSE,KNCET
62. It is a multi way decision statement ,
Using the value of a given variable and it
check with case values,
if it found execute those block of values ,
if not found it execute default statement.
L.NIVETHA AP/CSE,KNCET
65. The jump statement transfer the control
from one point to another point without
checking any condition. The following jump
statement used in C language.
1. goto statement
2. break statement
3. Continue statement
4. return statement
L.NIVETHA AP/CSE,KNCET
66. goto statement:
The goto statement are used to transfer the
control unconditionally from one point to
another point with in the function.
It provide a highly unstructured way of
transferring the program control from one
point to another point.
It often makes the program control difficult
to understand and modify.
Syntax:
goto label;
L.NIVETHA AP/CSE,KNCET
67. The goto statement is always used in joining
with an identifier-labeled statement.
The goto statement at the time of execution
transfers the program control to an
identifier-labeled statement, The label name
used in the goto statement .
The goto statement can be used to make a
forward jump as well as backward jump.
The goto statement transfer control any
where with in the function, but it no way to
take the control out of the function.
L.NIVETHA AP/CSE,KNCET
69. Break is a loop control statement .
The break statement is used to terminate the
loop
when the break statement enter inside a loop
,then the loop is exit immediately
The break statement can be used in both
while and for loop.
L.NIVETHA AP/CSE,KNCET
72. The continue statement rejects all the
remaining statements in the current iteration
of the for loop
and moves the control back to the top of the
loop.
When the statement continue is entered into
any C loop control automatically passes to
the beginning of the loop
L.NIVETHA AP/CSE,KNCET
75. The return statement terminate the execution
of a function
It returns the control to the calling function.
A return statement without an expression can
appear only in a function whose return type is
void.
Example :
return();
Or return(expression);
L.NIVETHA AP/CSE,KNCET
76. The loop is defined as the block of
statements which are repeatedly executed for
a certain number of time.
1. while statement(entry check loop)(top tested
loop).
2. Do while statement(bottom tested loop)(exit
check loop).
3. for statement.
L.NIVETHA AP/CSE,KNCET
77. The while loop is an entry controlled loop
statement ,It means the condition is
evaluated first , if it is true repeatedly execute
the body of the loop until the condition
become false.
Syntax: false
while(condition)
{ true
…….
body of the loop;
…….
}
conditio
n
Body of the loop
L.NIVETHA AP/CSE,KNCET
79. The do…while statement is exit control
statement , Here the body of the statement
executed one’s after that check the condition.
Syntax:
do
{
……
body of the loop; true
……
}
while (condition)
false
Body of the loop
conditio
n
L.NIVETHA AP/CSE,KNCET
81. This is another one of looping structure, it
is execute set of instructions repeatedly until
the condition become false.
Syntax:
for(initialization sec ;condition checking ;
increment / decrement)
{
……
body of the loop;
……
true
}
Initialization
Increment /
decrement
Body of the
loop
conditio
n
L.NIVETHA AP/CSE,KNCET
82. #include<stdio.h>
Void main()
{
int n, i, sum=0;
printf("Enter n value ");
scanf("%d" , &n);
for(i=1;n>=i ; i++)
{
sum=sum +i;
}
printf("%d", sum);
}
L.NIVETHA AP/CSE,KNCET
83. A constant is an entity whose value
remains the same throughout the execution
of a program.
Constant are classified as:
1. Literal constant
2. Qualified constant
3. Symbolic Constant
L.NIVETHA AP/CSE,KNCET
85. 1. Integer literal constant:
It is formed with the use of sequence of digit without
decimal point , that is .
Decimal digit - 0 to 9
Octal digit - 0 to 7
Hexadecimal digit - 0 to 9 A, B, C, D, E, F
Rules:
It must be contain one digit
Decimal point not allowed,
It can either negative or positive,
Range is -32,768 to +32, 767.
No special character and blank spaces allowed with in an
integer literals
EXAMPLE:
int mark=90 , avg=75 , total=10A1;
L.NIVETHA AP/CSE,KNCET
86. 2. Real constant:
It contain sequence of digits with decimal
point.
Rules:
It must be contain one decimal digit
It can either negative or positive,
Range is -32,768 to +32, 767.
No special character and blank spaces
allowed with in an float literals
Example:
float distance =120.8;
L.NIVETHA AP/CSE,KNCET
87. Character literal constant:
1. Single character literal constant:
The character constant contain single character with in
single quotes.
Syntax:
data type variable= ‘ single character ‘;
Example:
char initial= ‘ s ‘ ;
2. String literal constant:
The string constant contains more than set of
character with in double quotes.
Syntax:
data type variable= “ string “;
Example:
char department[5]=“IT”;
L.NIVETHA AP/CSE,KNCET
88. Qualified constant are created by using
const keyword .
Example:
const char a=‘s’;
const int a=10;
if we declare normal variable and assign
10, like int a = 10; then this variable
allocate 2byte memory space and store 10
with In those space.
consider the statement const int a =10;
the usage of qualified constant is after
storing value 10 those memory block is
locked, also could not allow new value with in
this.
L.NIVETHA AP/CSE,KNCET
89. Symbolic constant are created with the
help of the #define preprocessor.
Example :
#define pi 3.14
Here pi is a symbolic constant or
called as macro constant.
L.NIVETHA AP/CSE,KNCET
90. A type qualifier cannot affect the range of
the values, but affect the arithmetic
properties of the declared object.
There are two types:
1. Const - declaring an object by using const
then that value cannot be modified during
the execution.
2. Volatile – this object has some special
properties.
L.NIVETHA AP/CSE,KNCET
91. Declaring a variable as volatile:
Volatile data also one of constant data, it’s
value modified only with in the program, also
we could not change from external.
Syntax:
volatile data type
variable=constant ;
Example:
volatile int year=2007 ;
L.NIVETHA AP/CSE,KNCET
92. A type modifier modifies the range of the
object.
There are following types:
1. Signed ( negative number )
2. Unsigned ( zero or positive number )
3. Short
4. Long
L.NIVETHA AP/CSE,KNCET
93. Short , long , signed , unsigned.
Data type Size in bytes Control
string
Char signed and
unsigned
1 %c
Int signed 2 %d
Unsigned int 2 %u
Signed short int
Unsigned short int
1 %d
Long int 4 %id
Unsigned long int 4 %iu
float 4 %f
double 8 %if
Long double 10 %if
L.NIVETHA AP/CSE,KNCET