C Programming Lecture Notes
C Programming Lecture Notes
PROGRAMMING RULES:
All statements in ‘C’ program should be written in lower case letters.
Blank spaces may be inserted between the words. It is not used while declaring a variable,
keyword, constant and function.
The program statement can write anywhere between the two braces following the declaration
part.
The user can also write one or more statements in one line separating them with a
semicolon(;).
C TOKENS:
The tokens are usually referred as individual text and punctuation in a passage of text.
The ‘C’ language program can contain the individual units called the C tokens and has
the following types.
1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Operators
6. Special symbols
1. IDENTIFIERS
Identifiers are names given to various program elements, such as variables, functions and
arrays etc.
Rules:
Identifiers consist of letters and digits in any order.
The first character must be a letter/character or may begin with underscore( _ ).
Both upper/lower cases are permitted although uppercase character is not equivalent to
corresponding lowercase character.
The underscore ‘ _’ can also be used and is considered as a letter.
An identifier can be of any length while most of the ‘C’ compiler recognizes only the first
31 characters.
No space and special symbols are allowed between the identifier.
The identifier cannot be a keyword.
Valid identifiers:
STDNAME, SUB, Y2K.
Invalid identifiers:
STD NAME, Return, $stay, 7rno.
2. KEYWORDS:
There are certain reserved words called keywords that have standard and predefined
meaning in ‘C’ language which cannot be changed and they are the basic building blocks
for program statements.
Keywords are must written in lower case.
The ‘C’ keywords are listed below.
1. auto 9. double 17. int 25. struct
2. break 10. else 18. long 26. switch
3. case 11. enum 19. register 27. typedef
4. char 12. extern 20. return 28. union
5. const 13. float 21. short 29. unsigned
6. continue 14. for 22. signed 30. void
7. default 15. goto 23. sizeof 31. volatile
www.annauniversityplus.com
8. do 16. if 24. static 32. while
Data types:
Data type is the type of the data, that are going to access within the program.
Each data type may have predefined memory requirement and storage representation.
Primary User defined Derived Empty
char Arrays
int pointer void
float typedef structures
double union
The bytes occupied by each of the primary data types are
Data type Description Memory Control Ex
string
int Integer quantity 2 bytes %d int a=39;
char Single character 1 byte %c char s = ‘n’;
float Floating pointing no’s. 4 bytes %f float f=29.77
double Double floating pointing 8 bytes %lf double d=
no’s. 29771770776
All C compilers supports the five fundamental data types called int, char, float, double
and void.
The primary data types are divided into the following.
INTEGER TYPE
Integers are the numbers with the supported range.
Usually the integers occupy one word of storage typically 16 or 32 bits.
The size of the integer depends upon the system.
signed unsigned
int unsigned int
short int unsigned short int
long int unsigned long int
CHARACTER TYPE
Characters are generally stored in 8 bits and a single character can be defined as char data
type.
char signed char unsigned
FLOAT TYPE char
The floating point numbers are generally stored in 32 bits with the 6 digits of precision.
The double datatype uses the 64bits with the 14 digits of precision.
float double long double
EMPTY DATA TYPE
The void is the null data type in ‘C’ language.
This is generally specified with the function which has no arguments.
void
VARIABLES:
A variable is an identifier that is used to represent some specified type of information.
Variable name give to relate the program.
i) Variable declaration:
We must declare them in a program, and this declaration tells the compiler what the
variable name and type of the data that the variable will hold.
Syntax: datatype variable-1,variable-2,…,variable-n;
Example: int code; char sex; float price;
ii) Initializing variables:
Initialization of variables can be done using the assignment operator ( = ).
Syntax: Datatype Variable = value;
Example: int i=29;
www.annauniversityplus.com
SCOPE OF VARIABLES:
Local variables:
The variables which are defined inside a main function block or inside a function are
called local variables.
It is declared within blocks.
Example:
void main()
{
int i,j;
/* body of the function */
}
Global variables:
The variables that are declared before the function main() are called the global variables.
Example:
int a=5,b=2;
void main()
{
fun();
}
void fun()
{
int sum;
sum = a+b;
}
STORAGE CLASSES:
1. Auto:
They are called as the automatic because their memory space is automatically allocated
as the variable is declared.
It is optional keyword.
Syntax: Storage_class_type data_type var1, var2……var n;
Example: auto int a,b;
2. Static:
The static variables are the variables for which the contents of the variables will be
retained throughout the program.
These are permanent within the function in which they are declared.
Syntax: Storage_class_type data_type var1, var2,…….varn;
Example: static int a,b;
3. Extern:
The external variables are declared out of the main() function the availability of these
variables are throughout the program and that is both in main program and inside the user
defined functions.
Syntax: Storage_class_type data_type var1, var2,……var n;
Example: extern int a, b;
4. Register:
Registers are special storage areas within a computer’s central processing unit.
The actual arithmetic and logical operation that comprise a program are carried out
within these registers.
Syntax: Storage_class_type data_type var1, var2, ………..var n;
Example: register int a,g;
www.annauniversityplus.com
CONSTANTS:
The item whose values cannot be changed during the execution of program are called
constants.
a) Numeric constants:
i) Integer constants:
An integer constant formed with the sequence of digits.
There are three types of integer constants which forms different number system.
Syntax: const datatype identifier=value;
Example: const int M = 95;
Rules:
It must have atleast one value.
Decimal point is not allowed.
It can be either positive or negative.
ii) Real constants:
A real constant is made up of a sequence of numeric digits with presence of a decimal
point.
Real constants serve as represent quantities such as distance, heights, temperatures, etc.
Syntax: const datatype identifier=value;
Example: const float distance =126.5;
Rules:
A real constant must have one digit.
A real constant must have decimal point.
No commas or blank spaces are allowed.
b) Character constants:
i) Single character constants:
The character constant contains a single character enclosed within pair of single inverted
commas(‘) both pointing to the left.
Syntax: const datatype identifier= value;
Example: const char c=’s’,gender=’F’;
ii) String constants:
A string constant is a sequence of characters enclosed in double quotes the characters
may be letters, numbers, special characters and blank spaces etc.
Syntax: const datatype identifier= value;
Example: const char c[10]=”senthil”;
Delimiters:
These are the symbols, which has some syntactic meaning and has got significance.
Symbol Name Meaning
# Hash Pre-processor directive
, Comma separate list of variables
: Colon Label
; Semi colon Statement end
() Parenthesis Function and Expression
{} Braces Creating blocks
[] Square bracket Arrays
Statements:
Statements can be defined as set of declaration or sequence of action. Statement causes
the computer to perform some action.
1. Assignment statements:
Assignment operator used for assigning values to the variables.
www.annauniversityplus.com
Example: basic =3890;
2. Null statements:
A statement without any characters and it has only semicolon is called null statement.
Example: ; (null statement)
3. Block of statements:
Block contains several statements that are enclosed within a pair of braces {}.
These can be any of expression, assignments and keywords etc.
Example:
{
int a=890;
float b= 89.9;
printf(“%d%f”, a, b);
}
4. Expression statements:
These consist of expressions and can be arithmetic, relational or logical.
Example:
a= 29
B=a+77;
fun(a,b);
OPERATORS
An operator is as symbol that specifies an operation to be performed on the operands.
The data items are called operands.
Example: a+b
‘+’ operator and a, b are the operands.
Types of operators:
1) Arithmetic Operator (+, -,*,/,%,)
2) Relational Operator (<,>,<=,>=,!=,==)
3) Logical Operators (&&.||,!)
4) Assignment Operator ( = )
5) Increment & decrement Operator (++,--)
6) Conditional (or) Ternary Operator (? , : )
7) Bitwise Operator (&,!,|,^)
8) Special Operator (, sizeof,&&*, .&->)
1) Arithmetic operators:
Basic arithmetic operation like addition, subtraction, multiplication, and division.
Operator Meaning Example A=9, B=5
+ addition C=a+b C=14
- subtraction C=a-b C=4
* multiplication C=a*b C=45
/ division C=a/b C=1
% Modulo C=a%b C=4
Example:
//Arithmetic Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Arithmetic Operators");
www.annauniversityplus.com
printf("\nEnter the A & B Value:");
scanf("%d%d",&a,&b);
printf("A=%d\nB=%d",a,b);
c=a+b;
printf("\nAddition = %d",c);
c=a-b;
printf("\nSubtraction = %d",c);
c=a*b;
printf("\nMultiplication = %d",c);
c=a/b;
printf("\nDivision = %d",c);
c=a%b;
printf("\nModulo = %d",c);
getch();
}
OUTPUT:
Arithmetic Operators
Enter the A & B Value:9
5
A=9
B=5
Addition = 14
Subtraction = 4
Multiplication = 45
Division = 1
Modulo = 4
2) Relational operators:
Relational operators are used to compare two or more operands.
Operands may be variables, constants or expression.
The value of relational expression is either one or zero.
Relational operators are used in decision making process.
Operator Meaning Example A=9, B=5
< Less than C=a<b C=0
> Greater than C=a>b C=1
<= Less than or Equal to C=a<=b C=0
>= Greater than or equal to C=a>=b C=1
== Equal to C=a==b C=0
!= Not equal to C=a!=b C=1
Example:
//Relational Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Relational Operators");
printf("\nEnter the A & B Value:");
www.annauniversityplus.com
scanf("%d%d",&a,&b);
printf("A=%d\nB=%d",a,b);
c=a>b;
printf("\nGreaterthan (>)= %d",c);
c=a<b;
printf("\nLessthan (<)= %d",c);
c=a>=b;
printf("\nGreaterthan or equal (>=)= %d",c);
c=a<=b;
printf("\nLessthan or equal (<=)= %d",c);
c=a==b;
printf("\nEqual to (==)= %d",c);
getch();
}
OUTPUT:
Relational Operators
Enter the A & B Value:9
5
A=9
B=5
Greaterthan (>)= 1
Lessthan (<)= 0
Greaterthan or equal (>=)= 1
Lessthan or equal (<=)= 0
Equal to (==)= 0
3) Logical operators:
Logical operators are used to combine the results of two or more conditions.
The logical operators are &&, ||,!
Syntax: (exp1)&&(exp2)
Operator Meaning Example A=9, B=5, C=2
&& Logical AND d=(a>b)&&(a>c) d=1
|| Logical OR d=(a>b)||(a>c) d=1
! Logical NOT d=!(a>=b) d=0
Example:
//Logic Operators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Logic Operators");
printf("\nEnter the A,B,C Value:");
scanf("%d%d%d",&a,&b,&c);
printf("A=%d\nB=%d\nC=%d",a,b,c);
d=(a>b)&&(a>c);
printf("\nLogic AND\n((a>b)&&(a>c)) = %d",d);
d=(a>b)||(a>c);
printf("\nLogic OR\n((a>b)||(a>c)) = %d",d);
d=!(a>=b);
printf("\nLogic NOT\n(!(a>=b)) = %d",d);
getch(); www.annauniversityplus.com
}
OUTPUT:
Logic Operators
Enter the A,B,C Value:9
5
2
A=9
B=5
C=2
Logic AND
((a>b)&&(a>c)) = 1
Logic OR
((a>b)||(a>c)) = 1
Logic NOT
(!(a>=b)) = 0
4) Assignment operators:
Assignment operators are used to assign a value or an expression or a value of a variable
to another variable.
Example: x=5;
i) Compound assignment:
Compound assignment operators to assign a value to a variable in order to assign a new
value to a variable after performing a specified operation.
Example: x+ = y
ii) Nested assignments:
‘C’ language has got distinct features in assignment called nested assignment.
Syntax:
var1=var2=…………..var-n= single variable or expression or value;
Example: i=j=k=l;
x=y=z=(i+j+k);
6) Conditional operator:
It checks the condition and executes the statement depending on the condition.
It is like as if…else statement.
Syntax:
Condition?exp1:exp2;
Example:
int a=5,b=3,big;
big=a>b?a:b;
printf(“%d is big”,big);
For all the above operators in all possible combinations of bits as shown below.
A B A|B A&B A^B ~A
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0
Example:
//Bitwise OPerators
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Bitwise Operators");
printf("\nEnter the A & B Value:");
scanf("%d%d",&a,&b);
printf("A=%d\nB=%d",a,b);
www.annauniversityplus.com
c=a&b;
printf("\nBitwise AND (&) = %d",c);
c=a|b;
printf("\nBitwise OR (|) = %d",c);
c=a^b;
printf("\nBitwise EX-OR (^) = %d",c);
c=~a;
printf("\nOne's Complement or Bitwise NOT (~) = %d",c);
c=a<<2;
printf("\nLeft Shift (<<)= %d",c);
c=a>>2;
printf("\nRight Shift (>>)= %d",c);
getch();
}
OUTPUT:
Bitwise Operators
Enter the A & B Value:7
3
A=7
B=3
Bitwise AND (&) = 3
Bitwise OR (|) = 7
Bitwise EX-OR (^) = 4
One's Complement or Bitwise NOT (~) = -8
Left Shift (<<)= 28
Right Shift (>>)= 1
8) The special operator:
1. Comma operator(,):
It is used to separate the statement elements such as variables, constants or expression
etc.
Example: val = (a=3, b=9, c=77, a+c);
2. Sizeof() operator:
it is a unary operator, that returns the length in bytes of the specified variable, and it is
very useful to find the bytes occupied by the specified variable in the memory.
It is a compile time operator.
Syntax:
sizeof(var);
Ex:
void main()
{
int a;
printf(“size of variable a is……..%d”, sizeof(a));
}
Output:
size of variable a is…………2
3. Pointer operators:
&---This symbol specifies the address of the variable.
* --- This symbol specifies the value of the variable.
4. Member selection operators:
. and -- > : These symbols used to access the elements from a structure.
EXPRESSIONS:
An expression represents data item such as variables, constants and are interconnected
with operators as per thewww.annauniversityplus.com
syntax of the language.
An expression is evaluated using assignment operator.
Syntax: variable = expression;
Example: x=a*b-c;
Arithmetic operator’s precedence:
Arithmetic operators are evaluated from the left to right using the precedence of operators
when the expression is written without the parameters.
High:* / %
Low: + -
First phase: the highest priority operators are evaluated in the expression.
Second phase: the lowest priority operators are evaluated in the expression.
Type conversion:
It refers to the process of changing an entity of one data type into another.
i) Implicit conversion:
It is an automatic type conversion. In a mixed type expression data of one or more subtypes
can be converted to a super type as needed at runtime so that the program will run correctly.
Example:
int c;
float f=3.5;
c=f;
printf(“%d”,c);
OUTPUT:
3
ii) Explicit conversion:
The explicit conversion can be made possible to convert one data type to another by
forcefully and it is different from the implicit conversion.
Syntax: var1=(datatype)var2;
Ex: int a=10;
float(a);
Where a=10;
float (a) will contain 10.000000
Input www.annauniversityplus.com
Output Input Output
getchar() putchar() scanf() printf()
getc() putc() fscanf() fprintf()
gets() puts()
getch()
UNFORMATTED INPUT/OUTPUT STATEMENTS
1) getchar() function:
A single character can be given to the computer using ‘C’ input library function getchar().
Syntax: char variable = getchar();
Example: char x;
x= getchar();
The getchar() function is written in standard I/O library.
It reads a single character from a standard input device.
This function do not require any arguments, through a pair of empty parenthesis, must follow
the statement getchar().
2) putchar() function:
The putchar() function is used to display one character at a time on the standard output
device.
Syntax: putchar(char variable);
Example: char x;
putchar(x);
3) getc() function:
This is used to accept a single character from the standard input to a character variable.
Syntax: char variable = getc();
Example: char c;
c= getc();
4) putc() function:
This is used to display a single character in a character variable to standard output device.
Syntax: putc(char variable);
Example: char c;
putc(c);
5) gets() function:
The gets() function is used to read the string from the standard input device.
Syntax: gets(char type of array variable);
Example: gets(s);
6) puts() function:
The puts() function is used to display/write the string to the standard output device(Monitor).
Syntax: puts(char type of array variable);
Example: puts(s);
7) getch() function:
The getch reads a single character directly from the keyboard without echoing to the screen.
Syntax: int getch(void);
8) getche() function:
The getche reads a single character from the keyboard and echoes it to the current text
window.
Syntax: int getche(void);
www.annauniversityplus.com
if(condition is true) #include<stdio.h>
{ Start #include<conio.h>
True statements; void main()
} {
Read A,B
Next Statement; int a,b;
clrscr();
True scanf(“%d%d”,&a,&b);
if(A>B)
if(a>b)
Print “A is Big”
{
False printf(“A is big”);
}
printf(“\nIt is simple if”);
getch();
Print “It is simple if” }
Stop
Properties:
If the condition is true then the simple or compound condition statements are executed.
If the condition is false it does not do anything.
The condition is given in parenthesis and must be evaluated as true or false.
If a compound structure is provided it must be enclosed in opening and closing braces.
b) The if-else statement:
It is basically two way decision making statement and always used in conjunction with
condition.
It is used to control the flow of execution and also used to carry out the logical test and
then pickup one of the two possible actions depending on the logical test.
Syntax Flow Chart Example
if(condition) //Biggest of 2 Numbers
{ Start #include<stdio.h>
True statements; #include<conio.h>
} Read A,B void main()
else {
{ int a,b;
False statements; FALSE TRUE clrscr();
if(A>B)
} scanf(“%d%d”,&a,&b);
Next Statement; if(a>b)
PRINT “B IS BIG” PRINT “A IS BIG” {
printf(“A is big”);
}
else
{
printf(“B is big”);
}
Print “It is if…else”
printf(“\nIt is if…else”);
getch();
Stop }
FALSE TRUE
if(A>B||C>B)
FALSE TRUE
if(A>C)
PRINT “B IS BIG”
PRINT “C IS BIG” PRINT “A IS BIG”
Stop
Syntax Example
if(condition 1) //Biggest of 3 Numbers
{ #include<stdio.h>
if(condition 2) #include<conio.h>
{ void main()
True statement2; {
} int a,b,c;
else clrscr();
{ scanf(“%d%d%d”,&a,&b,&c);
False statement2; if(a>b||c>b)
} {
} if(a>c)
else {
{ printf(“A is big”);
False statement1; }
} else
Next Statement; {
printf(“C is big”);
}
}
else
{
printf(“B is big”);
}
printf(“\nIt is Nested if…else”);
getch();
}
d) if……else ladder:
Nested if statements can become quite complex.
More than three alternatives is not consistent to determine the logical structure of if
statement.
In situations, you can use the nested if as the else if ladder.
Flow Chart: www.annauniversityplus.com
Start
Read A,B,C,D
FALSE TRUE
Syntax Example
if(condition 1) //Biggest of 4 Numbers
{ #include<stdio.h>
Statement 1; #include<conio.h>
} void main()
else if(condition 2) {
{ int a,b,c,d;
Statement 2; clrscr();
} scanf(“%d%d%d%d”,&a,&b,&c,&d);
else if(condition 3) if(a>b&&a>c&&a>d)
{ {
Statement 3; printf(“A is big”);
} }
else else if(b>c&&b>d)
{ {
Default- statements; printf(“B is big”);
} }
Next Statement; else if(c>d)
{
printf(“C is big”);
}
else
{
printf(“D is big”);
}
printf(“\nIt is if…else Ladder”);
getch();
}
CH=3
Read CH Print C=A*B
CH=4
Print C=A/B
switch(CH)
Stop
Syntax:
Syntax Example
switch(expression) //Arithmetic operations using switch case
{ #include<stdio.h>
case constant-1: #include<conio.h>
Block 1; void main()
break; {
case constant-2: int a=10,b=20,ch,d;
Block 2; clrscr();
break; printf("1.ADD\n2.SUB\n3.MUL\n4.DIV");
. printf("\nEnter your choice:");
. scanf("%d",&ch);
default: switch(ch)
Default block; {
break; case 1:
} printf("%d",a+b);
Next Statement; break;
case 2:
printf("%d",a-b);
break;
case 3:
printf("%d",a*b);
break;
case 4:
printf("%d",a/b);
break;
default:
printf("\nEnter the correct choice");
www.annauniversityplus.com
break;
}
printf("\nIt is switch case");
getch();
}
Rules:
The expression in switch statement must be an integer value or a character constant.
No real numbers are used in an expression.
The case labels must be constants.
The switch can be nested.
Nested switch() case:
‘C’ supports the nested switch() statements. The inner switch() statement can be a
part of an outer switch() statement.
The inner and outer switch() case constants may be same.
No conflict arises even if they are same.
Comparison of switch() case and nested if:
Switch() case Nested if
The switch() can test only constant values. The if can evaluate relational or logical
expressions.
No two case statements have identical Same conditions may be repeated for number of
constants in the same switch. times.
Character constants are automatically Character constants are automatically converted
converted to integers. to integers
In switch() case statement, nested if can be In nested if statements switch() case can be used.
used.
LOOPING STATEMENTS:
1. while loop:
It is a repetitive control structure, used to execute the statements within the body until the
condition becomes false.
The while loop is an entry controlled loop statement means the condition is evaluated first
and it is true then the body of the loop is executed.
After executing the body of the loop the condition is once again evaluated and if it is true,
the body is executed once again the process of repeated execution of the body of the loop
until the condition becomes false and the control is transferred out of the loop.
The body of the loop may have one or more statements the blocking with the braces are
www.annauniversityplus.com
needed only if the body contains two or more statements.
Syntax Example OUTPUT
while(condition) void main() Sum of n Numbers
{ { Enter the Limit Value:10
Body of the loop; int n,i,sum=0;
} clrscr(); Sum = 55
printf("Sum of n Numbers");
printf("\nEnter the Limit Value:");
scanf("%d",&n);
i=0;
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nSum = %d",sum);
getch();
}
2. do……while loop:
The while loop makes a test of condition before the loop is executed.
In some situations it may be necessary to execute the body of the loop before the test condition
is performed, such a situation the do……while loop is useful.
It is also repetitive control structure and executes the body of the loop once irrespective of the
condition then it checks the condition and continues the execution until the condition is false.
Here the statements within the body of the loop are executed once, then it evaluates for the
condition if it is true, then it executes body until the condition becomes false.
Syntax Example OUTPUT
do void main() Sum of n Numbers
{ { Enter the Limit Value:10
Body of the loop; int n,i,sum=0;
}while(condition); clrscr(); Sum = 55
printf("Sum of n Numbers");
printf("\nEnter the Limit Value:");
scanf("%d",&n);
i=0;
do
{
sum=sum+i;
i++;
}while(i<=n);
printf("\nSum = %d",sum);
getch();
}
While Do……while
This is the top tested loop. This is the bottom tested loop.
The condition is first tested if the condition is It executes the body once after it checks the
true then the block is executed until the condition condition if it is true the body is executed
becomes false. until the condition becomes false.
Loop will not be executed if the condition is Loop is executed at least once even though
false. the condition is false.
//Sum of n Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Sum of n Numbers"); www.annauniversityplus.com
printf("\nEnter the Limit Value:");
scanf("%d",&n);
for(i=0;i<=n;i++)
sum=sum+i;
printf("\nSum = %d",sum);
getch();
}
OUTPUT:
Sum of n Numbers
Enter the Limit Value:10
Sum = 55
Sum = 10
Average = 3.33
Root1 = 463.00
Root2 = -466.33
www.annauniversityplus.com
UNIT-3
ARRAYS and STRINGS
Arrays:
An array is a collection of similar data items, which are stored under a common name.
Each element of an array is stored in successive locations of the memory.
Data items can be int, float, char values of data types.
The elements of the array are known as members of the array.
Arrays are declared using the symbol square bracket”[ ]” (or) subscript.
Need for Array:
A single variable store only one value.
If we want to store more values means we need to declare more variables.
This problem solved using array.
Types of array:
Arrays can be classified into
1. One Dimensional Array
2. Two Dimensional Array
3. Multi Dimensional Array
Array initialization:
The array initialized in two ways:
1. At compile time
2. At run time
1. At compile time:
o The array elements are initialized at the time of declaration.
Syntax: Datatype array_name[]={list of values};
Example: int marks[3]={45,67,89};
Array index Element values
marks[0] 45
marks [1] 67
marks [2] 89
2. At run time:
o The array elements are initialized at the time of program running.
o The “for” loop is used to initialize values for array element.
Example:
i) Using for loop to initialize value “0” to all the elements.
www.annauniversityplus.com
int a[10],i;
for(i=0;i<10;i++)
a[i]=0;
ii) Using scanf() function to initialize different values.
int a[5],i;
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
//One Dimensional array Sum and average Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,sum=0;
float avg=0.0;
clrscr();
printf("Sum and average of given Numbers");
printf("\nEnter the Limit Value:");
scanf("%d",&n);
printf("\nEnter the Values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=(float)sum/n;
printf("\nSum = %d",sum);
printf("\nAverage = %.2f",avg);
getch();
}
OUTPUT:
Sum and average of given Numbers
Enter the Limit Value:5
Enter the Values:45
2
3
10
5
Sum = 65
Average = 13.00
That is represented as
Columns
Column-0 Column-1 Column-2
Rows
Syntax: www.annauniversityplus.com
Datatype array_name[row_size][column_size];
Initialization:
1. At compile time:
The values can be initialized at the time of declaration.
Syntax:
Datatype array_name[row_size][column_size]={List of Values};
Example:
A[2][2]={{1,2}{3,4}};
Or
A[2][2]={1,2,3,4};
2. At run time:
The values can be initialized at the time of running.
Run time initialization can be do with the scanf() function.
Example:
int a[5][5],I,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);
Example:
//Two Dimensional array or Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],j,i,k;
clrscr();
printf("Matrix Multiplication");
printf("\nEnter the A matrix 4 values:");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the B matrix 4 values:");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nResult Matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
OUTPUT:
Matrix Multiplication
Enter the A matrix 4 values:1
2
3 www.annauniversityplus.com
4
Enter the B matrix 4 values:5
6
7
8
Result Matrix:
19 22
43 50
Strings:
Group of (or) Collection of (or) Array of characters are called strings.
The strings are specified with pair of double quotes (“ “).
Where declaring a string it will take null character “\0” at the end of string.
This null character cannot visible.
Syntax: char array_name[];
Initialization:
The values of string can bewww.annauniversityplus.com
initialized at run time or compile time.
The string values are initialized at compile time:
Syntax: char array_name[]=”values”;
Example: char name[]=”Senthil”;
The string values are initialized at run time using scanf():
Syntax:
char array_name[];
scanf(“%s”,array_name);
Example:
char name[];
scanf(“%s”,name);
Reading and Writing:
Reading & writing the string using scanf() & printf() function through the “%s” control string.
Also use the gets() and puts() functions.
Syntax:
Read Write
scanf(“%s”,array_name); printf(“%s”,array_name);
gets(array_name); puts(array_name);
Example:
char name[15];
printf(“Enter the name:”);
scanf(“%s”,name);
printf(“\nName:%s”,name);
OUTPUT:
Enter the name:Senthil
Name:Senthil
4. strcmp():
This function is used to compares two strings and find out whether they are same or different.
The two strings are compared character by character until end of one string is reached.
When two strings are equal it will give value “Zero” otherwise give any other value.
Syntax: int var=strcmp(string1,string2);
Example:
char name1[]=”Kalai”,name2[]=”Malai”,name3[]=”Kalai”;
int I,j;
i=strcmp(name1,name2);
j=strcmp(name1,name3);
printf(“Comparison of %s & %s is %d”,name1,name2,i);
printf(“\nComparison of %s & %s is %d”,name1,name3,j);
OUTPUT:
Comparison of Kalai & Malai is 1
Comparison of Kalai & Kalai is 0
5. strrev():
This function is used to reverse a string.
Syntax: strrev(string1);
Example:
char str1[]=”Senthil”;
strrev(str1);
printf(“Reversed String=%s”,str1);
OUTPUT:
lihtneS
6. strlwr():
This function is used to converts the characters of string into lower case characters.
Syntax: strlwr(string1);
Example:
char str1[]=”SenThiL”;
strlwr(str1);
printf(“Converted lower case:%s”,str1);
OUTPUT:
Converted lower case:senthil
7. strupr():
This function is used to converts the characters of string into upper case characters.
Syntax: strupr(string1);
Example:
char str1[]=”SenThiL”;
strupr(str1);
printf(“Converted upper case:%s”,str1);
OUTPUT:
Converted upper case:SENTHIL
It like as bubble near with another bubble so that it is called bubble sort.
Diagram:
www.annauniversityplus.com
Example:
//Bubble Sorting
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={35,75,45,5,25},j,i,temp=0;
clrscr();
printf("Bubble Sorting");
printf("\nUnsorted Elements:\n");
for(i=0;i<5;i++)
printf("%d\t",a[i]);
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\nSorted Elements:\n");
for(i=0;i<5;i++)
printf("%d\t",a[i]);
getch();
}
OUTPUT:
Bubble Sorting
Unsorted Elements:
35 75 45 5 25
Sorted Elements:
5 25 35 45 75
Searching:
To determine if the particular data item is present in that collection.
Which data item to find is called the “key”.
Our task the key element matches in the collection of data items.
Types of Search:
1) Sequential (or) Linear search
2) Binary search
1) Sequential (or) Linear search:
It is the search the key element in sequentially.
That is search element matches check with element by element.
If the searching element is present in at last of the list means it will take more time.
www.annauniversityplus.com
Example:
//Linear or Sequential Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={4,21,36,14,62,5,10},key,i,t=0;
clrscr();
printf("Linear or Sequential Search");
printf("\nList of Elements:\n");
for(i=0;i<7;i++)
printf("%d\t",a[i]);
printf("\nEnter the search key:");
scanf("%d",&key);
for(i=0;i<7;i++)
{
if(a[i]==key)
{
t=1;
printf("\nThe %d is present in %d position",key,i+1);
break;
}
}
if(t==0)
printf("\nThe Element is not present\n");
getch();
}
OUTPUT:
Linear or Sequential Search
List of Elements:
4 21 36 14 62 5 10
Enter the search key:62
The 62 is present in 5 position
UNIT-4
FUNCTIONS:
A function is a set of instructions that are used to perform specified task.
Types of Functions:
i) User defined functions
ii) Built in functions
User Defined Functions:
The user defined functions has to be written by the programmer.
Built in functions:
These functions are not required to be written by the programmer.
Built in functions also called as pre defined (or) library functions.
Need for user defined function:
The program becomes too large & complex.
The users cannot go through at glance.
www.annauniversityplus.com
The task of debugging, testing & maintenance becomes difficult.
Elements of functions:
The functions contain following three elements:
1) Function declaration
2) Function Definition
3) Function calling
Example:
1) Function Declaration:
The function can be declared before they are defined.
The parameters must match.
Syntax:
Return_type function_name(Parameter list);
Return_type Datatype of the return value
Function_name Name of the function
www.annauniversityplus.com
Parameter list List of parameters
Example:
Int add(int x, int y); or void add();
2) Function Definition:
The function definition is the actual body of the function.
It is the process of specifying and establishing the operation.
Syntax:
Return_type function_name(parameter list)
{
Body of the function;
Return data;
}
3) Function call:
A function can be called by specifying its name, followed by a list of parameters.
The function definition may return a value to function call.
A semicolon (;) is used at the end of the statement.
Syntax:
Function_name(); (or) function_name(parameter);
Value=function_name(parameter);
Example:
Fun(); or fun(a,b);
C=fun(a,b);
Parameters:
Parameters provide the data communication between the function call & function definition.
Parameters are also called as arguments.
There are 2 types:
i) Actual parameters
ii) Formal parameters.
Actual and formal parameters must match.
i) Actual parameters: These parameters are present in the function call.
ii) Formal parameters: These are present in the function definition.
Example:
Void main()
{ void sum(int x, int y)
……. {
Body of the function;
…… }
Sum(a,b);
…..
……
}
Return Statement:
Value (or) information is returned from the function definition to the function call.
A function may contain more than one return statement.
Return type must be present in definition.
Syntax:
Return; (or) return(value (or) expression);
Example:
www.annauniversityplus.com
int first() int second()
{ {
….. ……
…. …
Return x*x; return a;
} }
Function prototypes:
The functions are classified into the following types depending on arguments present or not
and the value is returned or not.
These are called as function prototypes.
a) Function without arguments & no return values
b) Function with arguments & no return vales
c) Function with arguments & with return vales
d) Function without arguments & with return vales
Example:
void sum();
void main()
{
sum();
} www.annauniversityplus.com
void sum()
{
int a,b,c;
printf("enter the two values:");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum =%d",c);
}
Output:
enter the two values:10
20
sum =30
b) Function with arguments & no return values:
In this prototype, data is transferred from function call to the function definition.
The value does not return the function call so it is called one-way communication.
Syntax:
Example:
void sum(int,int);
void main()
{
int a,b;
printf("enter the two values:");
scanf("%d%d",&a,&b);
sum(a,b);
}
void sum(int x, int y)
{
int z;
z=x+y;
printf("sum=%d",z);
}
OUTPUT: www.annauniversityplus.com
enter the two values:10
20
sum =30
Syntax:
Example:
void main()
{
int a,b,c;
clrscr();
printf(“enter the two values:”);
scanf(“%d%d”,&a,&b);
c=sum(a,b);
printf(“sum=%d”,c);
getch();
}
void sum(int x, int y)
{
int z;
z=x+y;
return z;
}
Output:
Enter the values: 5 10
Sum=15
d) Function without arguments & with return values:
www.annauniversityplus.com
Here, no data is transferred between function call & function definition.
But the value is returned from the function definition to the function cal.
So it is called as one way communication.
Syntax:
Example:
void main()
{
int a,b,c;
clrscr();
c=sum();
printf(“sum=%d”,c);
getch();
}
void sum(int x, int y)
{
int x,y,z;
printf(“enter the two values:”);
scanf(“%d%d”,&x,&y);
z=x+y;
return z;
}
OUTPUT:
Enter the values: 5 10
Sum=15
i) Call by Value:
This method copies the values of actual parameters into the formal parameters of the
function.
The changes of the formal parameters cannot affect the actual parameters.
www.annauniversityplus.com
Because formal parameters are photocopy of the actual parameters.
Syntax:
void main()
{
…………
swap(a,b);
……….
}
swap(int a,int b)
{
int temp;
………….
}
Example:
void swap1(int,int);
void main()
{
int a=10,b=20;
clrscr();
printf("before swap: a=%d\t b=%d",a,b);
swap1(a,b);
printf("\nafter swap: a=%d\t b=%d",a,b);
}
void swap1(int x, int y)
{
int t;
t=x;
x=y;
y=t;
printf("\nIn function a=%d\t b=%d",x,y);
}
OUTPUT:
before swap: a=10 b=20
In function a=20 b=10
after swap: a=10 b=20
Example:
void swap(int*,int*);
void main()
{
int a=10,b=20;
clrscr();
printf("before swap: a=%d\t b=%d",a,b);
swap(&a,&b);
printf("\nafter swap: a=%d\t b=%d",a,b);
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
OUTPUT:
before swap: a=10 b=20
after swap: a=20 b=10
Recursion:
Recursion takes the form of function that calls itself.
A process being performed where one of the instructions is to repeat the process.
It is similar to looping.
Recursion is the process of calling the same function itself again and again until some
condition is satisfied.
Syntax:
void function1();
{
---
---
function1();
}
Tower of Hanoi:
The Tower of Hanoi is a mathematical game or puzzle.
It consists of three rods, and a number of disks of different
sizes which can slide onto any rod.
The puzzle starts with the disks in a neat stack in ascending
order of size on one rod, the smallest at the top, thus making a
conical shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the
stacks and placing it on top of another stack i.e. a disk can only
www.annauniversityplus.com
be moved if it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves
required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.
Example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
void main()
{
int x,y=2;
printf("\nEnter the number:");
scanf("%d",&x); www.annauniversityplus.com
printf("\nThe squareroot of %d is %f",x,sqrt(x));
printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));
printf("\nThe ceiling of 6.7 is %f",ceil(6.7));
printf("\nThe floor of 6.7 is %f",floor(6.7));
printf("\nThe absolute value of -6 is %d",abs(-6));
printf("\nThe value of sin 45 is %f",sin(45));
printf("\nThe uppercase of 'a' is %c",toupper('a'));
printf("\nThe uppercase of 97 is %c",toupper(97));
getch();
}
OUTPUT:
Enter the number:6
The squareroot of 6 is 2.449490
The value of 6 power 2 is 36.000000
The ceiling of 6.7 is 7.000000
The floor of 6.7 is 6.000000
The absolute value of -6 is 6
The value of sin 45 is 0.850904
The uppercase of 'a' is A
The uppercase of 97 is A
Pointers:
A pointer is a variable that is used to store the address of another variable.
It is declared like other variables and also it is always denoted by asterisk “*” operator.
Each variable has two attributes:
o Address
o Value
It can be used to access and manipulate data stored in the
memory.
Ordinary Variable:
int a=5;
aaccess the value
&a access the address of variable.
a variable
5 value
4002 address
Syntax: Data_type *pointer_variable;
Example: int *p;
Advantages:
1. It is increase the speed of execution.
2. It is saving memory space.
3. It enables us to access the memory directly.
4. It is also provide an alternate way to access an array.
5. Multiple data items can receive in the function.
Initializing pointer:
The process of assigning the address of a variable to a pointer variable is called initialization.
The location of the variablewww.annauniversityplus.com
in system memory.
This can be achieved through the ampersand(&) symbol.
The ampersand(&) symbol is an address operator.
It takes the address of the variable.
Pointers must be initializing with assigning address of the variable.
Example:
void main()
{
int a=10,*p;
p=&a;
printf("\n*p=%d\tp=%d\t&p=%d\t&a=%d",*p,p,&p,&a);
}
OUTPUT:
*p=10 p=-12 &p=-14 &a=-12
*pvalue of the address stored in pointer variable.
Paddress of the variable.
&paddress of the pointer variable.
&aaddress of the variable.
a variable *p
10 value -12
-12 address -14
Pointer Arithmetic:
C pointer is an address, which is a numeric value.
Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.
There are four arithmetic operators that can be used on pointers: ++, --, +, and -
Data type Initial Address operation address after operation bytes of data type
char 4000 ++ 4001 1
char 4000 -- 3999 1
int 4000 ++ 4002 2
int 4000 -- 3998 2
float 4000 ++ 4004 4
float 4000 -- 3996 4
Incrementing a Pointer
Incrementing pointer is generally used in array.
The variable pointer to access each succeeding element of the array.
Incrementing pointer variable depends upon datatype of the pointer variable.
Example:
void main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
ptr = var;
for ( i = 0; i < 3; i++)
{
printf("\nAddress of var[%d] = %u", i, ptr );
printf("\nValue of var[%d] = %d", i, *ptr );
ptr++;
} www.annauniversityplus.com
}
OUTPUT:
Address of var[0] = 65520
Value of var[0] = 10
Address of var[1] = 65522
Value of var[1] = 100
Address of var[2] = 65524
Value of var[2] = 200
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its value by the
number of bytes of its data type as shown below:
Example:
void main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
ptr = &var[3-1];
for ( i = 3; i > 0; i--)
{
printf("Address of var[%d] = %u\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
ptr--;
}
}
OUTPUT:
Address of var[3] = 65524
Value of var[3] = 200
Address of var[2] = 65522
Value of var[2] = 100
Address of var[1] = 65520
Value of var[1] = 10
Unit-5
Structures and Union
Structures:
Structure is a compound datatype.
It stores different type of data items.
It is used to store dissimilar data items.
Structure is creating with the “struct” keyword.
It is the one of the storage unit.
Different type of data items can be store in different memory space.
The elements of structure are called members.
It must be declare and defined.
INT FLOAT
Structures
CHAR DOUBLE
Example:
//Student Details using Structure
struct stud
{
int regno,m1,m2,m3,total;
char name[20],result[5];
};
void main()
{
struct stud s1,s2;
printf("\nStructures");
printf("\nEnter the Regno, Name:");
scanf("%d%s",&s1.regno,s1.name);
printf("\nEnter the Mark1, Mark2, Mark3:");
scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);
s1.total=s1.m1+s1.m2+s1.m3;
if(s1.m1<50||s1.m2<50||s1.m3<50)
strcpy(s1.result,"Fail");
else
strcpy(s1.result,"Pass");
s2=s1; //Assigning structure
printf("Regno\tName\tMark1\tMark2\tMark3\tTotal\tResult\n");
printf("%d\t%s\t%d\t%d\t%d\t%d\t%s",s1.regno,s1.name,s1.m1,s1.m2,s1.m3,s1.total,s1.result);
printf("\nStructure--2");
printf("\nRegno\tName\tMark1\tMark2\tMark3\tTotal\tResult\n");
printf("%d\t%s\t%d\t%d\t%d\t%d\t%s",s2.regno,s2.name,s2.m1,s2.m2,s2.m3,s2.total,s2.result);
getch();
}
OUTPUT:
Enter the Regno, Name:345
kumar
Enter the Mark1, Mark2, Mark3:67
78
61
Regno Name Mark1 Mark2 Mark3 Total Result
345 kumar 67 78 61 206 Pass
Structure--2
Regno Name Mark1 Mark2 Mark3 Total Result
345 kumar 67 78 61 206 Pass
Example:
//Employee Details using Structure
void dummy(float *a)
{
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
struct employee1
{
int empno;
www.annauniversityplus.com
char name[10];
float salary;
};
void main()
{
struct employee1 e[10];
int i,n;
printf("\nEnter the no.of Employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the employee empno,name,salary:");
scanf("%d%s%f",&e[i].empno,e[i].name,&e[i].salary);
}
printf("\nEmployee Details\n");
printf("\nEMPNO\tNAME\tSALARY");
for(i=0;i<n;i++)
printf("\n%d\t%s\t%.2f",e[i].empno,e[i].name,e[i].salary);
getch();
}
OUTPUT:
Enter the no.of Employees:2
Enter the employee empno,name,salary:23
Raj
5000
Enter the employee empno,name,salary:24
Kumar
3500
Employee Details
EMPNO NAME SALARY
23 Raj 5000.00
24 Kumar 3500.00
Union
• A Union is a collection of different data items, which are stored under a common name.
• Here same memory is shared by its members.
• The keyword union is used to define a union.
• Size of union is equal to the size of largest member.
• Memory allocated is shared by individual members of union.
• The address is same for all the members of a union.
• Altering the value of any of the member will alter other member values.
• Only one member can be accessed at a time.
• Only the first member of a union can be initialized.
www.annauniversityplus.com
Syntax:
union union_name
{
union element1;
union element2;
…………………
};
Example:
union result
{
int mark;
float avg;
char grade;
};
union result s;
Structure Union
1.The keyword struct is used to define a structure 1. The keyword union is used to define a union.
2. The size of structure is greater than or equal to the 2. Size of union is equal to the size of largest
sum of sizes of its members. member.
3. Each member within a structure is assigned 3. Memory allocated is shared by individual
unique storage area of location. members of union.
4. The address of each member will be in ascending 4. The address is same for all the members of a
order. union.
5 Altering the value of a member will not affect 5. Altering the value of any of the member will
other members of the structure. alter other member values.
6. Individual member can be accessed at a time 6. Only one member can be accessed at a time.
7. Several members of a structure can initialize at 7. Only the first member of a union can be
once. initialized.
Example:
void dummy(float *a)
{
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
union employee1
{
int empno;
char name[10];
float salary;
};
void main()
{
union employee1 e[10];
int i,n;
printf("\nEnter the no.of Employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the employee empno,name,salary:");
scanf("%d%s%f",&e[i].empno,e[i].name,&e[i].salary);
} www.annauniversityplus.com
printf("\nEmployee Details\n");
printf("\nEMPNO\tNAME\tSALARY");
for(i=0;i<n;i++)
printf("\n%d\t%s\t%.2f",e[i].empno,e[i].name,e[i].salary);
getch();
}
OUTPUT:
Enter the no.of Employees:2
Enter the employee empno,name,salary:23
Raj
5000
Enter the employee empno,name,salary:24
Kumar
3500
Employee Details
EMPNO NAME SALARY
16384 5000.00
-16384 3500.00
Preprocessor:
It is a program that processes the source program before compilation.
The C preprocessor executes before a program is compiled.
Preprocessor directives begin with # and only white-space characters and comments may
appear before a preprocessor directive on a line.
It operates under the following directives
1. File Inclusion
2. Macro substitution
3. Conditional inclusion
File Inclusion:
• It is used to include some file that contains functions or some definitions.
• Copy of a specified file included in place of the directive
Syntax:
#include<filename> Searches standard library for file
(or) #include“filename” Searches for user-defined files
Example:
#include<stdio.h>
#include<conio.h>
#include "addition.txt"
void main()
{
int a,b;
printf("\nEnter the numbers:");
scanf("%d%d",&a,&b);
printf("The Value is %d",add(a,b));
getch();
}
addition.txt
int add(int a,int b)
{
return(a+b);
}
OUTPUT:
Enter the numbers:7 www.annauniversityplus.com
4
The Value is 11
Macro Substitution:
• It is used to define and use integer, string, or identifier in the source program
• The three forms of macros are
– Simple Macro
– Argumented Macro
– Nested Macro
1) Simple Macro:
It is used to define some constants.
Syntax
#define identifier string/integer
Example:
#include<stdio.h>
#include<conio.h>
#define pi 3.14
#define CITY "chennai"
void main()
{
printf("The Value is %f",2*pi);
printf("\nThe Value CITY is %s",CITY);
getch();
}
OUTPUT:
The Value is 6.280000
The Value CITY is chennai
2) Argumented Macro
• It is used to define some complex forms in the source program.
Syntax:
#define identifier (v1,v2,….) string/integer
Example:
#include<stdio.h>
#include<conio.h>
#define cube(n) (n*n*n)
void main()
{
printf("The Value of 3 cube is %d",cube(3));
getch();
}
OUTPUT:
The Value of 3 cube is 27
3) Nested Macro
Here one macro is used by another macro.
Example:
#include<stdio.h>
#include<conio.h>
#define a 3
#define sq a*a
void main()
{
printf("The Value is %d",sq);
getch();
www.annauniversityplus.com
}
Output:
The Value is 9
Conditional Inclusion:
It is used to include some conditional statements.
Example:
#include<stdio.h>
#include<conio.h>
#define a 3
#ifdef a
#define c a+5
#endif
void main()
{
printf("\nThe value C is %d",c);
getch();
}
Output:
The value C is 8
www.annauniversityplus.com