C Programming Concepts and Techniques
C Programming Concepts and Techniques
Chapter : 1 ......................................................................................................... 1
1.1 Introduction: ................................................................................................ 1
1.2 Header Files: ................................................................................................ 2
1.3 Variables in C: ............................................................................................. 3
[Link] Variable........................................................................................ 5
[Link] Variable ...................................................................................... 6
[Link] Variable........................................................................................ 7
[Link] Variable ................................................................................ 9
[Link] Variable ................................................................................. 10
1.4. DataTypes : ............................................................................................... 11
[Link] Data Types: .......................................................................... 11
[Link].Integer Data types: ......................................................................... 13
[Link].Character Data Type: ..................................................................... 17
[Link].Float Data Types: ........................................................................... 19
1.5 KEYWORDS:............................................................................................ 21
1.6 IDENTIFIER : ........................................................................................... 21
1.7 Comments in C .......................................................................................... 22
1.7.1 Single Line Comments: ........................................................................ 22
1.7.2. Multi line Comments: ......................................................................... 23
1.8 Format Specifier: ....................................................................................... 24
1.8.1 Character Format Specifier: (%c) ..................................................... 24
1.8.2 Signed Integer Format Specifier : (%d, %i) ...................................... 25
1.8.3 Unsigned Integer Format Specifier:(%u) .......................................... 25
1.8.4 Floating point Format Specifier:(%f, %e) ......................................... 26
1.8.5 Unsigned Octal Number For Integer Format Spectifier: (%o) ........... 27
1.8.6 Unsigned hexadecimal for Integer Format Specifier :(%x) ............... 27
1.8.7 String Printing:(%s) .......................................................................... 28
1.8.8 Double Floating point Number(%lf) ................................................. 28
1.9 Escape Squences: ....................................................................................... 29
1.10 Operators: ................................................................................................ 29
Types of Operators: ...................................................................................... 29
1.10.1 Arithmetic Operator: ....................................................................... 30
1.10.2 Assignment Operator :(=)................................................................ 31
1.10.3 Relational Operator: ........................................................................ 32
1.10.4 Logical Operator: ............................................................................ 33
1.10.5 Bitwise Operator: ............................................................................ 34
1.10.6 Conditional operator: ...................................................................... 38
1.10.7 Modify Operator: ............................................................................ 39
1.10.8 Special Operator: ............................................................................ 41
1.11 Constants: ................................................................................................ 42
1.12 C Tokens? ................................................................................................ 44
CHAPTER :2 ................................................................................................... 46
2.1Control Statements: ..................................................................................... 46
2.1.1 Sequential Statement: ........................................................................... 46
2.1.2 Selection statement: ............................................................................. 46
[Link] If Statement:................................................................................... 46
[Link] IF-Else Statement: .......................................................................... 48
[Link] Else- if Ladder statement ............................................................... 49
[Link] Nested If Statement: ....................................................................... 50
[Link] Switch Case Statement: .................................................................. 52
2.1.3 Iteration / Looping: ............................................................................. 55
[Link] While Loop : .................................................................................. 55
[Link] Do-while Loop: .............................................................................. 56
[Link] For Loop: ....................................................................................... 58
2.2 Nested Loop: .............................................................................................. 59
2.3 Break Statement: ........................................................................................ 60
2.4 Continue Statement: ................................................................................... 61
2.5 Goto Statement: ......................................................................................... 62
Chapter : 3 ....................................................................................................... 63
3.1 Functions: .................................................................................................. 63
3.1.1 No Arguments with No Return Values ................................................. 65
[Link] Arguments with No Return values ............................................... 66
[Link] Arguments with Return values: ................................................... 67
[Link] Arguments with Return Values: ..................................................... 68
3.2 Call by Value : ........................................................................................... 69
3.3 Call by Reference:...................................................................................... 70
3.4 Recursion: .................................................................................................. 71
CHAPTER : 4 .................................................................................................. 72
4.1 Arrays: ....................................................................................................... 72
[Link] Dimensional Array: ...................................................................... 74
[Link] Dimensional array: ....................................................................... 77
CHAPTER : 5 .................................................................................................. 80
[Link]: ...................................................................................................... 80
[Link] and Pointer: ................................................................................ 82
[Link] of Pointers: ................................................................................. 83
[Link] to Function: .............................................................................. 85
CHAPTER : 6 .................................................................................................. 85
[Link]:....................................................................................................... 85
[Link] the String................................................................................ 87
[Link].h ...................................................................................................... 88
[Link] String functions ............................................................................. 88
CHAPTER : 7 .................................................................................................. 93
[Link]: ................................................................................................... 93
[Link] Structure: ..................................................................................... 94
[Link] of Structures:.................................................................................... 95
[Link]: ........................................................................................................ 97
7.5.Type_def: ................................................................................................... 98
[Link]:............................................................................................ 101
CHAPTER : 8 ................................................................................................ 101
[Link] Handling : ......................................................................................... 101
C Language
Chapter : 1
1.1Introduction:
The C Programming Language was designed by Dennis Ritchie at Bell
Laborators in the early 1970s
Standardized in 1989 by ANSI (American National Standards Institute)
known as ANSI C
A C Development environment includes:
1) System Libraries and headers:
A set of standard libraries and their header files
2) Application Source:
Application Source and header files
3) Compiler:
Converts source to object code for a specific platform.
4) Linker:
Resolves external references and produces the executables module.
Program 1:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello");
getch();
}
1
Output:
Hello
1.2Header Files:
Usually, all C programs begin with include statements starting with a
#(hash / pound)
The symbol # is a directive for the preprocessor
That means, these statements are processed before the compilation
process begins
Header file contains the definition of predefined functions.
Example:
1. #include<stdio.h> - standard Input and Output - scanf, printf
2. #include<conio.h> - console input and output - clrscr(), getch()
3. #include<math.h> - mathematical operations - cos(), tan(), pow()
4. #include<string.h>- character - strlen(), strcmp(), strcpy()
Program 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the Number\n");
scanf("%d",&a);
printf(" value of A : %d",a);
getch();
}
Output:
Enter the number:
5
Value of A: 5
2
Program 3:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x;
clrscr();
x=pow(2,8);
printf("%d",x);
getch();
}
Output:
256
1.3Variables in C:
A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it
can be easily identified.
1. type variable_list;
Here, a, b, c are variables. The int, float, char are the data types.
3
We can also provide values while declaring the variables as given below
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
Types of Variables in C :
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
4
[Link] Variable
1. void function1(){
2. int x=10;//local variable
3. }
Program 4:
#include<stdio.h>
#include<conio.h>
int fun();
void main()
{
int a,b;
clrscr();
a=3;
b=4;
printf("%d%d\n",a,b);
fun();
getch();
}
int fun()
{
int c;
c=5;
printf("%d",c);
return 0;
}
5
Output:
34
5
[Link] Variable
Program 5:
#include<stdio.h>
#include<conio.h>
int a,b=10;
void fun1();
void fun2();
void main()
{
clrscr();
printf("global a=%d\n",a);
printf("global b=%d\n\n",b);
fun1();
fun2();
getch();
}
void fun1()
6
{
printf("Fun1 Global a=%d\n",a);
printf("Fun1 Global b=%d\n\n",b);
}
void fun2()
{
int a=5;
printf("fun2 a=%d\n",a);
}
Output:
Global a=0
Global b=10
Fun1 global a=0
Fun1 global b=10
Fun2 a=5
[Link] Variable
A variable that is declared with the static keyword is called static [Link]
retains its value between multiple function calls.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
If you call this function many times, the local variable will print the same
value for each function call, e.g, 11,11,11 and so on. But the static variable will
print the incremented value in each function call, e.g. 11, 12, 13 and so on.
7
Program 6:
#include <stdio.h>
#include<conio.h>
void fun1();
void main()
{
clrscr();
fun1();
fun1();
fun1();
getch();
}
void fun1()
{
int a=1;
static int b=100;
printf("a=%d\n",a);
printf("b=%d\n",b);
a++;
b++;
}
Output:
a=1
b=100
a=1
b=101
a=1
b=102
8
[Link] Variable
All variables in C that are declared inside the block, are automatic variables by
default. We can explicitly declare an automatic variable using auto keyword.
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
Program 7:
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i=1;
clrscr();
{
auto int i=2;
{
auto int i=3;
printf("%d\n",i);
}
printf("%d\n",i);
}
printf("%d\n",i);
getch();
}
Output:
3
2
1
9
[Link] Variable
Program 8:
#include<stdio.h>
#include<conio.h>
extern int i=1;
int fun();
void main()
{
int i=3;
clrscr();
printf("%d\n",i);
fun();
getch();
}
int fun()
{
printf("%d",i);
return 0;
}
Output:
3
1
10
[Link] :
What is Data Types?
It is a representation of data. Int, float, char, .. etc.,
It is mandatory to use datatype for variable.
Data type represent two things about a variable.
1. What type of data is allowed to store
2. How much memory is required to store a data
Classification:Data type classified into three types.
1. Primitive char, int, float, void
2. Derived Array, String. Pointer
3. User Defined Structure, Union, Type_def, Enum.
[Link] Data Types:
1. Char It Represent a single character.
2. Int It Represent an integer value.
3. Float It Represent an decimal point value.
4. Void It Represent Nothing.
Char 1 Byte
Int 2 Byte 1 Byte = 8 bits
Float 4 Byte
Program 9:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
float b=10.5;
char c='m';
clrscr();
printf("The Integer Value : %d \n",a);
printf("The Float Value : %f \n",b);
printf("The Character Value : %c \n",c);
11
getch();
}
Output:
The Integer Value : 10
The Float Value : 10.500000
The Character Value : m
Signed
2- or 4-
Int bytes Bytes
Unsigned
Integer
Signed
Long 4 Bytes
Unsigned
Signed
char 1 Byte
Primitive Unsigned
Data
Types Float 4 Bytes
Double 8 Bytes
Float
Long 10 Bytes
Double
void
12
[Link].Integer Data types:
Signed
2 Bytes
Short
Unsigned
Signed
2- or 4-
Int bytes Bytes
Integer Unsigned
Signed
Long 4 Bytes
Unsigned
Signed Short:
It allow signed and unsigned value. (Both +ve and -ve value)
It occupy 2 Bytes in the memory location of a variable.
Don’t need to define Explicitly.
Unsigned short:
It allow only unsigned value(only +ve values)
It occupy 2 Bytes in the memory location of a variable.
You have to define Explicitly.
13
Data Measurements:
Data Measurement Size
Bit Single Binary Bit (0 and 1)
Byte 8 bits
Kilobyte (kb) 1024 Bytes
Megabyte (mb) 1024 kb
Gigabyte (gb) 1024 mb
Terabyte (tb) 1024 gb
0 Bit
0 1 1 1 1 0 0 0 Byte
0 0 0 0 0 0 0 0 0
16 bits =2 Byte
1 1 1 1 1 1 1 1 256
216
65536 Range
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 65536
Unsigned Int:(65536)
Value Range 0 to 65535
SingnedInt :
14
65536 / 2 = -32768 to +32767 (…+3,+2,+1,0,1,2,3….)
Program 10:
#include<stdio.h>
#include<conio.h>
void main()
{
short a=32767;
unsigned short b=65535;
clrscr();
printf("The Signed short is :%d\n",a);
printf("The unsigned short is :%u",b);
getch();
}
Output:
The signed short is : 32767
The unsigned short is : 65535
Short Integer:
15
Short Integer:
Signed short 2 Bytes -32768 to 32767
Unsigned short 2 Bytes 0 to 65535
Program 11:
#include<stdio.h>
#include<conio.h>
void main()
{
short a=32770;
unsigned short b=65540;
clrscr();
printf("The signed value is : %d \n",a);
printf("The unsigned value is : %u",b);
getch();
}
Output:
The signed value is : -32766
The unsigned value is : 4
16
Short Integer:
Signed short 2 Bytes -32768 to 32767
Unsigned short 2 Bytes 0 to 65535
Integer:
Signed int 2 Bytes -32768 to 32767
Unsigned int 2 Bytes 0 to 65535
Long Integer:
Signed long 4 Bytes -2,147,483,648 to
2,147,483,647
Unsigned long 4 Bytes 0 to 4,294,967,295
Signed
char 1 Byte
Unsigned
Unsigned Char:
1Byte 256 0 to 255 Range of value
Signed Char :
1 Byte 256/2 -128 to +127
Character System:
Using Character System we can represent all the character in an
constant integer format.
ASCII – American Standard Code for Information Interchange.
17
Program 12:
#include<stdio.h>
#include<conio.h>
void main()
{
18
char t='s';
clrscr();
printf("The Character is : %c\n",t);
printf("The ASCII Value is : %d",t);
getch();
}
Output:
The character is : s
The ASCII Value is : 115
1.2E-38 TO
Float 4 bytes %f
3.4E+38
19
2.3E-308 TO
Double 8 Bytes %lf
1.7E+308
3.4E-4932 TO
Long double 10 Bytes %Lf
1.1E+4932
Program 13:
#include<stdio.h>
#include<conio.h>
void main()
{
float a=57.89;
double b=1234.56;
long double c;
clrscr();
c=a+b;
printf("The First value is : %f\n",a);
printf("The second value is : %lf\n",b);
printf("The sum of two values are : %Lf",c);
getch();
}
Output:
The First value is : 57.889999
The second value is : 1234.560000
The sum of two values are : 1292.449999
20
1.5 KEYWORDS:
Keywords serves as basic building blocks for program statements. All
keywords have fixed meanings and cannot be changed.
Keywords cannot be used as normal identifier.
Keywords are written in lowercase letters.
For examples of keywords used in c.
1.6 IDENTIFIER :
C identifier refers to name used to identify variable, function, structures
or any other user defined item or entity.
Identifier must be unique.
Identifier names must be different from keywords.
For example : You cannot use double as an identifier because double is a
keyword
Rules For an Identifier:
An identifier can only have alphanumeric charaters(a-z,A-Z,0-
9)&Underscore( _ )
The First character of an identifier can only contain alphabet (a-z, A-Z) or
Underscore ( _ ).
For Example : int roll_num
Identifier are also case sensitive in C.
For Example :
Name and Name are two different identifiers in C
21
Program 14:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // int- keyword
a=10; // a,b,c- Identifier
b=25;
c=a+b;
printf("Addition = %d",c);
getch();
}
Output:
Addition = 35
1.7 Comments in C
Comments in C language are used to provide information about lines
of code. It is widely
Used for documenting code. There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments
22
Program 15:
#include<stdio.h>
#include<conio.h>
void main()
{
//printing information
clrscr();
printf("HELLO");
getch();
}
Output:
HELLO
Program 16:
#include<stdio.h>
#include<conio.h>
void main()
{
/* printing
information */
clrscr();
printf("HELLO");
getch();
}
Output:
Hello
23
1.8 Format Specifier:
What is Format specifier?
The Format Specifier is used during Input and Output.
It is a way to tell the compiler what type of data is in a variable
during taking input using scanf() or Printing using Printf()
Some Examples are %c, %d, %f, Etc.
24
{
char ch='B';
clrscr();
printf("%c\n",ch);
getch();
}
Output:
B
25
unsigned int a=-10, b=10;
clrscr();
printf("%u\n",a);
printf("%u\n",b);
getch();
}
Output:
65526
10
26
1.8.5 Unsigned Octal Number For Integer Format Spectifier: (%o)
Program 21:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=67;
clrscr();
printf("%o\n",a);
getch();
}
Output:
103
Program 22:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf("%x\n",a);
getch();
}
27
Output:
f
Output:
Welcome
Output:
45.650000
28
1.9 Escape Squences:
1.10 Operators:
We define operator as a symbols that helps us to perform specific
mathematical and logical computations on operand.
For example,
C=a+b;
Here ‘+’ is the operator known as Additional operator.
‘a’ and ‘b’ are the operands.
The addition operator tells the compiler to add both of the
Operand.
Types of Operators:
Arithmetic Operator. (Binary operator)
Assignment Operator
Relational Operator
Logical Operator
Bit wise Operator
Conditional operator. (Ternary Operator)
Increment / decrement Operator. (Unary Operator)
Special Operator.
29
1.10.1 Arithmetic Operator:
Addition +
Subtraction -
Multiplication *
Division / (Quotient)
Modulus % (Reminder)
Program 25:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Addition : %d \n",a+b);
printf("Subtraction : %d \n",a-b);
printf("Multiplication : %d \n",a*b);
printf("Division : %d \n",a/b);
printf("Modulus : %d",a%b);
getch();
}
Output:
Enter Two Numbers:
5
2
Addition : 7
30
Subtraction : 3
Multiplication : 10
Division : 2
Modulus : 1
a=a+b; a+=b;
a=a-b; a-=b;
a=a*b; a*=b;
a=a/b; a/=b;
a=a%b; a%=b;
Program 26:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=10;
b=20;
printf("A=%d\n",a);
printf("B=%d\n",b);
a=a+b; //a+=b;
printf("A=%d\n",a);
getch();
}
Output:
A=10
31
B=20
A=30
Output:
Enter Two values:
5
4
32
A is Greater
Logical OR(||):
It return true when one or both of the condition in
consideration is satisfied. Otherwise it return false.
Logical Not(!):
It return true the condition in consideration is not satisfied.
Otherwise it return false. For example !a return true if a=0
Program 28:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20,c=30,d=40;
clrscr();
//Logical AND
if(a>b&&c<d) //||
printf("A is greater than B AND D is greater than c");
else
printf("Logicaal AND function does not satisfied");
getch();
}
33
Output:
A is greater than B AND D is greater than c
Logical NOT:
Program 29:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=4; //a=0;
clrscr();
//Logical NOt
if(!a)
printf("A is Zero");
else
printf("A is non zero");
getch();
}
Output:
A is non zero
A B A|B
1 1 1
0 0 0
1 0 1
Bitwise XOR(^):
It Takes two numbers as operand and does XOR on every
bit of two numbers.
The result of XOR is 1 only if both bits are different.
For Example: A=5, B=4(binary value for 5=101 and for
4=100)
A B A^B
1 1 0
0 0 0
1 0 1
35
Left Shift(<<):
It takes two numbers, left shifts the bits of the first operand,
the second operand decides the number of places to shift.
For Example: A=5, B=2(binary value for 5=101 Result = 20)
A 0 0 0 0 0 1 0 1
A<<B 0 0 0 1 0 1 0 0
Right Shift(>>):
It takes two numbers, Right shifts the bits of the first
operand, the second operand decides the number of places to
shift.
For Example: A=5, B=2(binary value for 5=101 Result = 1)
A 0 0 0 0 0 1 0 1
A<<B 0 0 0 0 0 0 0 1
A ~A
0 1
0 1
0 1
0 1
0 1
1 0
0 1
1 0
36
Program 30:
Bitwise AND,OR,XOR
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=4;
clrscr();
printf("A & B is : %d\n",a&b);
printf("A | B is : %d\n",a|b);
printf("A ^ B is : %d\n",a^b);
getch();
}
Output:
A & B is : 4
A | B is : 5
A ^ B is : 1
Bitwise Not
Program 32:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
clrscr();
printf("A is : %d\n",~a);
getch();
}
Output:
A is : -6
Output:
5
Modify Operator(unary)
Increment(++) Decrement(--)
39
Program 34:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10, b=2;
clrscr();
printf("a++ = %d\n",a++);
printf("a = %d\n",a);
printf("++a = %d\n",++a/b);
printf("a = %d\n",a);
printf("a-- = %d\n",a--);
printf("a = %d\n",a);
printf("--a = %d\n",--a);
getch();
}
Output:
a++ = 10
a = 11
++a = 6
a = 12
a-- = 12
a = 11
--a = 10
40
1.10.8 Special Operator:
Sizeof():
It return the size of:
1. Variable
2. Data type
3. Expression
4. pointer
Program 35:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*b;
float c,*d;
clrscr();
printf("Address of A is : %u",&a);
b=&a;
printf("\n%u",b);
printf("\n sizeof c : %d",sizeof(c));
printf("\n sizeof :%d",sizeof(char));
printf("\n sizeof : %d",sizeof(++a));
printf("\n sizeof : %d",sizeof(*d));
41
getch();
}
Output:
Address of A is : 65524
65524
Sizeofc : 4
Sizeof : 1
Sizeof : 2
Sizeof : 4
1.11 Constants:
Constants are affixed values that do not change during the execution of a
program.
C supports several types of constants.
o Numeric Constants
o Character Constants
Constants
Numeric Character
Constants Constants
42
Integer Constant :
Integer constant refers to sequence of digits.
There are three types of integers, namely
Integer Constant
Decimal Integer:
Consist of a set of digits, 0 through 9 proceded by on optional – or+ sign
For example:
234 -456 0 3465677 +23
Embedded spaces, commas and non digit characters are not permitted
between two digits.
For Example :
15, 345 $45678
Octal Integer:
Any combination of digitd from the set 0 through 7 with a leading 0.
For Example:
089 0 07654 023
Hexadecimal Integer:
Asequence of digits proceded by 0x or 0X considered by hexadecimal
integer
Alphabets A through F or a through f.
The letters A through F represent the numbers 10 through 15.
For example:
0x2 0x4F 0Xbcd 0x
43
Real Constants:
Integer number are inadequate to represent quantities that vary
continuously, such as distances, heights, temperatures, prices and
so on.
For Example:-
17.548 such numbers are called real or floating point
constants.
1.12 C Tokens?
C Tokens are the basic building blocks in C language which are
constructed together to write a C program.
Every Smallest individual units in a C program are known as C tokens.
44
Types of C Tokens:
C has six types of tokens.
1. Keywords : Ex - float, int, double, for, while
2. Identifiers : Ex - Main, amount
3. Constants : Ex - 12.3, 3456
4. Strings : Ex - “COMPUTER”, “SCIENCE”
5. Special Symbols : Ex - [, ], {, }, (, )
6. Operators : Ex - +, -, *, /
Program 36:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // int- keyword
a=10; // a,b,c- Identifier
b=25; //{,},(,) – Special Symbols
c=a+b;
printf("Addition = %d",c);
getch();
}
Output:
Addition = 35
45
CHAPTER :2
2.1Control Statements:
In a program, a control structure determines he order in which
statement are executed.
The following are the basic control structures in the programming
language:
1. Sequential
2. Selection
3. Looping / Iteration
46
Syntax:
If(condition)
{
Statements;
}
Program 37:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// if statement
if(a>b)
{
printf("A is Greater");
}
getch();
}
Output:
Enter the Two Numbers:
20
10
A is Greater
47
[Link] IF-Else Statement:
The if-else statement checks for the condition and if the
condition is satisfied it executes the statement of the IF block
Otherwise it executes the Else block.
Syntax:
If(condition)
{
Statements;
}
else
{
Statements;
}
Program 38:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// if – Else statement
if(a>b)
{
printf("A is Greater");
}
else
{
printf("B is Greater");
}
getch();
}
Output:
20
10
A is Greater
48
10
20
B is Greater
Syntax:
If(condition)
{
Statements;
}
else if
{
Statements;
}
else
{
Statements;
}
Program 39:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// Else- if Ladder statement
if(a>b)
{
printf("A is Greater");
}
else if(a<b)
{
printf("B is Greater");
49
}
else
{
printf("A and B are Equal");
}
getch();
}
Output:
20
10
A is Greater
10
20
B is Greater
10
10
A and B are Equal
50
{
Statements;
}
}
else
{
Statements;
}
Program 40:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// Nested if statement
if(a>b)
{
printf("A is Greater");
if(a>100)
{
printf(" and A is Greater than 100");
}
else
{
51
printf(" but A is lesser than 100");
}
}
else
{
printf("B is Greater");
}
getch();
}
Output:
120
90
A is Greater and A is Greater than 100
90
80
A is Greater but A is lesser than 100
80
90
B is Greater
52
Syntax:
Switch(Expression)
{
case 1: statements;
break;
case 2: statements;
break;
case 3: statements;
break;
default :
statements;
}
Program 41:
#include<stdio.h>
#include<conio.h>
void main()
{
int num, a=10, b=5;
clrscr();
printf("[Link]\n [Link]\n [Link]\n [Link]");
printf("\n\n choose your option:");
scanf("%d",&num);
switch(num)
{
case 1:
printf("Addition is : %d",a+b);
break;
53
case 2:
printf("Subtraction is :%d",a-b);
break;
case 3:
printf("Multiplication is :%d",a*b);
break;
case 4:
printf("Division is : %d",a/b);
break;
default:
printf("Choose the correct options...");
}
getch();
}
Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division
54
Choose Your Option: 4
Division : 2
Program 42:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
55
while(i<=10)
{
printf("%d\n",i);
i++;
}
getch();
}
Output :
1
2
3
4
5
6
7
8
9
10
56
Program 43:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("%d\n",i);
i++;
}
while(i<=10);
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10
57
[Link] For Loop:
The for Loop like this.
Syntax:
For(initialization; condition; increment / decrement)
{
Statement;
}
Program 44:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
getch();
}
Output:
1
2
3
4
5
6
58
7
8
9
10
void main()
{
int i,j,n;
clrscr();
printf("enter the n value\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
59
{
printf("%d\t",(i*j));
}
printf("\n");
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
void main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
if(i==3)
break;
printf("\n%d",i);
60
}
printf("\nEnded");
getch();
}
Output:
0
1
2
Ended
void main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
if(i==3)
continue;
printf("\n%d",i);
}
printf("\nEnded");
getch();
61
}
Output:
0
1
2
4
5
6
7
8
9
10
Chapter : 3
3.1 Functions:
63
Syntax:
Return_typefunction_name(parameter list)
{
Body of the function(function definition)
}
Program 49:
#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
clrscr();
add(7,8);
getch();
}
int add(int a, int b)
{
int c=a+b;
printf("The Addition value of: %d",c);
return(c);
}
Output:
The Addition value of : 15
Types of Functions:
No Arguments with No Return values
With Arguments with No Return values
With Arguments with Return values
No Arguments with Return values
64
3.1.1 No Arguments with No Return Values
Syntax:
Void function_name(void)
{
---------
---------
}
Program 50:
#include<stdio.h>
#include<conio.h>
void cel();
void main()
{
clrscr();
cel();
getch();
}
void cel()
{
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("add=%d",c);
}
Output:
Enter two numbers:
5
65
5
Add=10
66
[Link] Arguments with Return values:
Syntax:
int function_name(char b)
{
--------
--------
return a;
}
Program 52:
#include<stdio.h>
#include<conio.h>
int cel(char);
void main()
{
clrscr();
printf("ASCII=%d",cel('A'));
getch();
}
int cel(char a)
{
return a;
}
Output:
ASCII : 65
67
[Link] Arguments with Return Values:
Syntax:
Float function_name(void)
{
------
------
return b;
}
Program 53:
#include<stdio.h>
#include<conio.h>
float cel();
void main()
{
clrscr();
printf("Division=%f",cel());
getch();
}
float cel()
{
float a,b,c;
printf("enter two value");
scanf("%f%f",&a,&b);
c=a/b;
return c;
}
68
Output:
Enter Two values :
10.6
2
Division : 5.300000
void c_value(int x)
{
x=x+10;
printf("value of x: %d\n",x);
}
void main()
{
// Call by value
int a=10;
clrscr();
c_value(a); //calling function
printf("value of A After Calling : %d ",a);
getch();
}
69
Output:
Value of x: 20
Value of A After Calling : 10
void main()
{
// Call by reference
int a=10;
clrscr();
c_value(&a); //calling function
printf("value of A After Calling : %d ",a);
getch();
}
Output :
Value of x : -12
Value of A After calling : 20
70
3.4 Recursion:
Program 56:
#include<stdio.h>
#include<conio.h>
void rev(int);
void main()
{
clrscr();
rev(10);
getch();
}
void rev(int n)
{
if(n<=0)
printf("Ended");
else
{
printf("%d\n",n);
rev(n-1);
}
}
Output:
10
9
8
7
6
5
71
4
3
2
1
Ended
CHAPTER : 4
4.1 Arrays:
What is an Array?
Array stores a collection of related values of a same data type.
So, Using primitive variable we can store only one value at a time. We
can’t store multiple value in that variable.
If use primitive variable to store 100 number of students register number
we have to use 100 variables.
For that only we are using Array.
By using array we can store N number of value in a single variable of
same data type.
For example int stud_no[20]; This hold 20 student Number.
Syntax:
Datatype identifier [size of array];
For example:
int ABC[3];
72
Memory Allocation for this Array:
0 1 2
10 20 30
Output:
10
20
Program 58:
#include<stdio.h>
#include<conio.h>
void main()
{ // 0 1 2 3 4
73
int i,array[5]={10,20,30,40,50};
clrscr();
for( i=0;i<=4;i++)
{
printf("%d\n",array[i]);
}
getch();
}
Output:
10
20
30
40
50
Types of Array:
Arrays are classified in to two Types:
1. One Dimensional Array
2. Two Dimensional Array
1 2 3 4 5
74
Array having one subscript value is called one Dimensional Array.
Eg: int a[5] ={1,2,3,4,5};
It is also used as Linear Array.
Initialization of one Dimensional Array:
1. Compile time Initialization
2. Run Time Initialization
75
4
5
78
Program 62:
#include<stdio.h>
#include<conio.h>
void main()
{
// Run time Initialization
int arr[4][3];
int i,j;
clrscr();
printf("Enter values are:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
printf("\n");
}
printf("The values are:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
79
}
Output:
Enter values are :
1
2
3
4
5
6
7
8
9
10
11
12
The values are:
1 2 3
4 5 6
7 8 9
10 11 12
CHAPTER : 5
[Link]:
A Pointer is a variable that contains the address of the another variable.
In other Words, it points to the location of a variable and can indirectly
access the variable.
Pointers are declared using the * symbol.
* Asterisk
Eg : int *p;
80
For Example: a b
void main() 5 5
{
6550 6557
int a=5, b;
int *p; p
p=&a; 6550
printf(“%u”,p);
b=*p; *p=5 6559
printf(“%d”,b);
}
Program 63:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p;
clrscr();
p=&a;
printf("Addres of a:%u\n",p);
printf("value of a : %d\n",a);
printf("value of a:%d\n",++*p);
printf("value of a: %d",a);
getch();
}
81
Output:
Address of a:65524
Value of a:10
Value of a:11
Value of a:11
int *P; P
printf(“%u”,P); 6780
printf(“%d”,*P);
}
Program 64:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={2,4,6,8,10};
int *p,i;
clrscr();
p=&arr[0];
for(i=0;i<5;i++)
printf("%u\n",*(p+i)+10);
82
getch();
}
Output:
12
14
16
18
20
[Link] of Pointers:
Array:
Array is a collection value of same type.
Eg: int arr[5];
Array of Pointers:
Array of Pointer is a collection of address of same type.
Eg: int *p[5];
Sample Program:
0 1 2
void main()
10 20 30
{
6550 6552 6554
int arr[3]={10,20,30};
ar
int *p[3]; r
6550
0 1 2
int i;
for(i=0;i<3;i++) 6550 6552 6554
P
{ 6540 6542 6544
6540
P[i]=&arr[i];
Printf(“%d”,*p[i]);
83
}
}
Program 65:
#include<stdio.h>
#include<conio.h>
void main()
{
//Array of Pointers
int arr[5]={10,20,30,40,50};
int *p[5],i;
clrscr();
for(i=0;i<5;i++)
{
p[i]=&arr[i];
printf("%d\n",*p[i]);
}
getch();
}
Output:
10
20
30
40
50
84
[Link] to Function:
Program 66:
#include<stdio.h>
#include<conio.h>
// pointer to function
int add(int,int);
void main()
{
int (*p)(int,int);
clrscr();
p=&add;
printf("%d",p(10,20));
getch();
}
int add(int x, int y)
{
int z=x+y;
return z;
}
Output:
30
CHAPTER : 6
[Link]:
String is a array of character or a collection of character.
CHARACTER:
It represent a single letter within the single codes.
85
Eg: ‘a’, ‘e’, ‘i’, ‘o’, ‘u’
STRING:
It represent a collection of character within double codes.
Eg.”hello”
Declaration syntax:
Char string_name[size of the string];
Initialization:
Using single character constant.
Eg: char STR[10]=”hello”;
0 1 2 3 4 5 6 7 8 9
H e l l o \0
6550 6551 52 53 54 55 56 57 58 59
Memory Allocation:
Char str[10]={‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
Char str[10]=”Hello”;
Format Specifier :
The format Specifier “%s” used for accessing the string till it encounted ‘\0’.
Example:
Char str[10] = “Hello”;
Printf(“%s”,str);
Program 67:
#include<stdio.h>
#include<conio.h>
void main()
86
{
char str1[10]={'H', 'e', 'l', 'l', 'o', '\0'};
char str2[10]="Hello";
clrscr();
printf("%c\n",str1[0]);
printf("%s",str2);
getch();
}
Output:
H
Hello
H e l l o \0
6551 52 53 54 55 56 57 58 59 60
6551
str
Program 68:
#include<stdio.h>
#include<conio.h>
void main()
87
{
char str[20];
clrscr();
printf("Enter your name:");
gets(str);
printf("%s",str);
getch();
}
Output:
Enter your name : Abi karthika
Abi karthika
[Link].h
C provides a set of built in function to manipulate the strings.
That means we can perform some operation on string such as joining a
string, comparing…etc.
All the string handling function are stored in the header file called
string.h
88
Strlen(): used to find the length
Strcpy(): used for copying the source string into Destination string
Program 69:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();
Strcpy:
Program 70:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20]="welcome";
char str2[20];
clrscr();
strcpy(str2,str1);
printf("%s", str2);
getch();
}
Output:
Welcome
strcat:
program 71:
#include<stdio.h>
90
#include<conio.h>
#include<string.h>
void main()
{
char str1[20]="Hi Guys";
char str2[20]="welcome";
clrscr();
strcat(str1,str2);
printf("%s", str1);
getch();
}
Output:
Hi guyswelcome
Strcmp:
Program 72:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20]="Mall";
char str2[20]="Mall";
int n;
clrscr();
n=strcmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else if(n>0)
91
printf("string1 is greater");
else
printf("string2 is greater");
getch();
}
Output:
Both strings are equal
Strstr:
Program 73:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
char b[20];
clrscr();
printf("enter the string");
gets(a);
printf("enter the word to be find");
gets(b);
printf("%s",strstr(a,b));
getch();
}
Output:
Enter string Ram is a good boy
Enter the word to be find a
a good boy
92
CHAPTER : 7
Sample program:
Num Name avg
int num;
char name[10]; 6550
Float avg; s
};
Struct student s;
Program 74:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int rollno;
char name[10];
float average;
}a;
void main()
{
clrscr();
strcpy([Link],"Arun");
[Link]=1201;
93
[Link]=98.7;
printf("student roll no %d\n",[Link]);
printf("student name %s\n",[Link]);
printf("student average %f\n",[Link]);
getch();
}
Output:
Student rollno:1201
Student name : Arun
Student Average : 98.699997
[Link] Structure:
Program 75:
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
};
struct student s1,s2;
void main()
{
clrscr();
printf("enter the 1st student no");
scanf("%d",&[Link]);
printf("enter the 1st student name");
scanf("%s",[Link]);
94
printf("enter the 2nd student no");
scanf("%d",&[Link]);
printf("enter the2nd student name");
scanf("%s",[Link]);
printf("\nRollno\t\tName\n");
printf("%d\t\t",[Link]);
printf("%s",[Link]);
printf("\n%d\t\t",[Link]);
printf("%s",[Link]);
getch();
}
Output:
Enter the 1st student no: 1201
Enter the 1st student name: Raji
Enter the 2nd student no: 1202
Enter the 2nd student name: Raj
Rollno Name
1201 Raji
1202 Raj
[Link] of Structures:
Program76:
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
95
};
struct student s[3];
void main()
{
int i;
clrscr();
for (i=0;i<3;i++)
{
printf("Enter the no:");
scanf("%d",&s[i].rollno);
printf("Enter the name : ");
scanf("%s",s[i].name);
}
printf("\nRollno\t\tName");
for(i=0;i<3;i++)
{
printf("\n%d\t\t",s[i].rollno);
printf("%s",s[i].name);
}
getch();
}
Output:
Enter the no: 1
Enter the name :rajesh
Enter the no: 2
Enter the name :rajee
Enter the no: 3
Enter the name :raj
96
Rollno Name
1 rajesh
2 rajee
3 raj
[Link]:
sample program:
Union un 20.2
{
int a;
char b;
float c;
o
};
Union un o;
Void main()
{
o.a=10;
o.b=’A’;
o.c=20.2;
}
Program77:
#include<stdio.h>
#include<conio.h>
union un
{
int a;
char b;
97
float c;
};
union un o;
void main()
{
clrscr();
printf("%d\n",sizeof(o));
o.a=10;
o.b='b';
o.c=20.5;
printf("%f",o.c);
printf("\n%c",o.b);
printf("\n%d",o.a);
getch();
}
Output:
4
20.500000
0
7.5.Type_def:
Program78:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int num;
num a=10;
clrscr();
98
printf("%d",a);
getch();
}
Output :
10
Program79:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int a;
a num[5]={1,2,3,4,5};
clrscr();
printf("%d",num[2]);
getch();
}
Output:
3
Program80:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int* num;
num a,b;
int a1;
a=&a1;
99
clrscr();
printf("%u",a);
getch();
}
Output:
65522
Program81:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct stud
{
int a;
char b[10];
}st;
void main()
{
st s1;
clrscr();
s1.a=1201;
strcpy(s1.b,"Arun");
printf("%d\n",s1.a);
printf("%s",s1.b);
getch();
}
Output:
1201
Arun
100
[Link]:
Program82:
#include<stdio.h>
#include<conio.h>
void main()
{
enum days { sun=10,mon=20,tue=30,wed=40,thur=50,fri=60, sat=70};
enum days d;
clrscr();
d=fri;
printf("%d",d);
getch();
}
Output:
60
CHAPTER : 8
[Link] Handling :
Program 83:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
fp=fopen("[Link]","w");
if(fp==NULL)
printf("Does not Exit");
101
else
{
printf("File created");
}
fprintf(fp,"THis is my first program in file handling");
fclose(fp);
getch();
}
Output:
File created
Program84:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
char str[50];
clrscr();
fp=fopen("[Link]","r"); //name,mode
if(fp==NULL)
printf("Does not Exit");
else
{
printf("File created\n");
}
while(fscanf(fp,"%s",str)!=EOF)
102
{ //End Of File
printf("%s ", str);
}
fclose(fp);
getch();
}
Output:
File created
This is my first program in file handling
Program85:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
int a=10;
clrscr();
fp=fopen("[Link]","w"); //name,mode
if(fp==NULL)
printf("Does not Exit");
else
{
printf("File created\n");
}
fprintf(fp,"Hi Guys %d",a);
fclose(fp);
103
getch();
}
Output :
File created
Program 86:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
int rollno;
char name[20];
clrscr();
printf("Enter the rollno& name");
scanf("%d",&rollno);
scanf("%s",&name);
fp=fopen("[Link]","w"); //name,mode
fprintf(fp,"ROLLNO :%d \n NAME : %s",rollno,name);
fclose(fp);
getch();
}
Output:
Enter the rollno& name
121
raji
104
105