Sei sulla pagina 1di 123

1 C LANGUAGE

PROGRAMMING IN
C LANGUAGE

ABOUT THE AUTHOR

Bhupendra Singh Mandloi, an MCA from RGPV, Bhopal,


M.Tech from Mewar University, Chittorgarh (Raj.),
presently works as an Assistant Professor in IPS
Academy, Indore.
2 C LANGUAGE

CONTENT
Chapter 1 INTRODUCTION TO C LANGUAGE
1.1 HISTORY OF C LANGUAGE

1.2 STRUCTURE OF C PROGRAM

1.3 VARAIBLE

1.4 DATA TYPES

1.5 SPECIFIERS

Chapter 2 OPERATORS
2.1 ARITHMETIC OPERATOR

2.2 RELATIONAL OPERATOR

2.3 LOGICAL OPERATOR

2.4 BITWISE OPERATOR

2.5 SIZEOF OPERATOR

2.6 COMMA OPERATOR

2.7 ASSIGNMENT OPERATOR

2.8 DOT OPERATOR

Chapter 3 CONTROL STATEMENT


3.1 INTRODUCTION

3.2 IF STATEMENT

3.3 IF-ELSE STATEMENT

3.4 SWICH-CASE STATEMENT

3.5 BREAK & CONTINUE STATEMENT

Chapter 4 LOOPS
4.1 INTRODUCTION
3 C LANGUAGE

4.2 FOR LOOP

4.3 WHILE LOOP

4.4 DO-WHILE LOOP

4.5 BREAK & CONTINUE

4.6 NESTING

Chapter 5 ARRAY
5.1 1-D ARRAY

5.2 2D ARRAYS

5.3 POINTER

Chapter 6 FUNCTION
6.1 INTRODUCTION

6.2 RECURSION

6.3 GLOBAL VARIABLE

6.4 STRING

Chapter 7 STRUCTURE & UNION


7.1 STRUCTURE

7.2 UNION

7.3 FILE HANDLING


4 C LANGUAGE

Chapter 1 INTRODUCTION TO C LANGUAGE


1.1 HISTORY OF C LANGUAGE
The history of C programming language is quite interesting. C language
was developed by Dennis Ritchie in year 1972 at AT&T (American
Telephone & Telegraph) Bell laboratory in USA.C language was
originally developed for implementation of UNIX operating system. C is
the result of a development process that started with an older language
called BCPL (basic common programming language). BCPL was
developed by Martin Richards, and it influenced a language called B,
which was invented by Ken Thompson. B led to the development of C in
the1972.
Dennis Ritchie also known as father of C language and C language also
known as mother language because most of the languages follow the
syntax of c language. C is also known as middle level language because
C supports some good features of low level language and some features
of high level language.
Reason behind the name of language

1. Most of the features of C language were derived from the earlier


language B.
2. B language was derived from BCPL.
3. BCPL was derived from CPL (common Programming Language).
4. As many of the features were derived from “B” Language
that’s why it was named as “C”.
5 C LANGUAGE

C language Timeline

Programming Development
Developed by
Language Year
International
ALGOL 1960
Group
BCPL 1967 Martin Richards
B 1970 Ken Thompson
C 1972 Dennis Ritchie

1.2 STRUCTURE OF C PROGRAM


1. Header files
2. Global variables or global function declaration
3. Main function
4. Local variables declaration
5. Functions or instructions
6. User define function definitions
Header files:-header files contain functions declarations and definitions
of standard library functions.
6 C LANGUAGE

Example: - <stdio.h> here .h is an extension of header files, stdio means


standard input output. Header files come into angular braces. Using
preprocessor or macro we can load header files in our program.

#include is a preprocessor here space is a optional between # and


include.
#include or # include or # include
So finally header file is looking like this
# include<stdio.h>
Header files cannot terminate by semicolon, we can load any number of
header files and there is no rule for order of header files.
# include<stdio.h>
# include<conio.h>
# include<math.h>
# include<string.h>
1.3 VARAIBLES

Global variable:-A variable which is declared outside of any function


called global variable and scope of global variable is in full program in
which it is declared.
Main function:-main is a special user defines function which is call
inside compiler that‟s why every program execution starts from main
function. main function is not available in any header files because main
is user define function.
7 C LANGUAGE

Local variables declaration:-A variable which is declared inside


functions called local variable and scope of local variable is only inside
function where it is declared.
Functions or instructions:-any valid statements are called instructions
it also provided by functions also.
Users define function definition:-definitions of user define functions
come outside of main function. Later we will discuss about user define
functions in details.
Every c program must have main function without main function
programming is not possible in c language. There is no maximum limit
for function but there is minimum limit for function, we must require at
least one function that is main function. Every program execution starts
from the main function. Every c program must be saved by .c extension.
For example
Test.c
void main()
{
}
1.4 DATA TYPES
Data types:-In the real world data has many forms like 500, 50.50, A,
Amit, and so on.
Basic four data types of c language
integer---------500--------- whole number
float-----------50.50-------- floating number
8 C LANGUAGE

character------A --------- non-numeric value


void -------- mainly used for function only
all data types are keyword in c language must comes into lower case
letter, keywords are reserve words computer already known the
predefine meaning of keywords.
For 16-bit machine
Data types keyword Size(in bytes) Range
character char 1 -128 to 127
integer Int 2 -32768 to 32767
long long 4 -2,147,483,648
to 2,147,483,647
float float 4 3.4E-38 to
3.4E+38
double double 8
1.7E-308 to
1.7E+308

void void 0 NA

Variable Declaration:-variables are used to store some data or value.


Syntax for variable declaration:-

type or data type variable name;


9 C LANGUAGE

Variable name:-variable name is a any combination of alphabets {A-Z,


a-z}, digits {0-9} and a special symbol underscore {_}.
But we cannot start variable name with digits.

Example:-
int a;
int a123;
int abc;
int _123;
int _;
int 123a;(wrong)

Input/output functions:-output function write message or data onto


screen or output device, input function read message or data from
keyboard or any other resources (like from file).
Here printf() is a output function.
Example1:-A.c
void main()
{
printf(“well-come to c language”);
10 C LANGUAGE

getch();
}
Output:-
Well-come to c language

Example2:-A.c
void main()
{
printf(“well-come to c language”);
printf(“Thanks for visit”);
getch();
}
Output:-
Well-come to c languageThanks for visit
Example3:-A.c
void main()
{
printf(“well-come to c language\n”);
printf(“Thanks for visit”);
getch();
}
11 C LANGUAGE

Output:-
Well-come to c language
Thanks for visit

Example4:-A.c
void main()
{
printf(“well-come to c language”);
printf(“\nThanks for visit”);
getch();
}
Output:-
Well-come to c language
Thanks for visit
Example5:-A.c
void main()
{
printf(“well-come to c language\t”);
printf(“Thanks for visit”);
getch();
12 C LANGUAGE

Output:-

Well-come to c language Thanks for visit


Example6:-A.c
void main()
{
printf(“well-come to c language”);
printf(“\tThanks for visit”);
getch();
}
Output:-
Well-come to c language Thanks for visit
Example7:-A.c
void main()
{
printf(“well-come to c language\tThanks for visit”);
getch();
}
13 C LANGUAGE

Output:-

Well-come to c language Thanks for visit

Example8:-A.c
void main()
{
printf(“Well-come to c language\nThanks for visit”);
getch();
}
Output:-
Well-come to c language
Thanks for visit

Syntax of printf function for display value of variable

printf(“conversion specifier ”,variablename);

1.5 SPECIFIERS
Conversion specifier for different data types
14 C LANGUAGE

Data type conversion specifier


int %d
long %ld
float %f
Double %lf
Char %c

Example9:-A.c
void main() {
int a,b;
printf(“%d”,a);
printf(“%d”,b);
getch();
}
Output:-
Garbadge value
Garbadge value
Example10:-A.c
void main() {
int a,b;
a=100;
b=200;
15 C LANGUAGE

printf(“%d\n”,a);
printf(“%d”,b);
getch(); }
Output:-
100
200
Example11:-A.c
void main()
{
int a,b;
a=100;
b=200;
printf(“a=%d\n”,a);
printf(“b=%d”,b);
getch();
}
Output:-
a=100
b=200
Example12:-A.c
void main()
16 C LANGUAGE

{
int a,b;
a=100;
b=200;
printf(“Value of a=%d\n”,a);
printf(“Value of b=%d”,b);
getch();
}
Output:-
Value of a=100
Value of b=200
Example13:-A.c
void main()
{
int a=100,b=200;
printf(“Value of a=%d\n Value of b=%d”,a,b);
getch();
}
Output:-
Value of a=100
Value of b=200
17 C LANGUAGE

Reading primitive data as an input from keyboard use scanf function.


Syntax of scanf function is ,

scanf(“Conversion specifier only”,address of variable);

Example14:-A.c
void main()
{
int a,b;
printf(“Enter any two numbers\n”);
scanf(“%d”,&a);
scanf(“%d”,&b);
printf(“\nValue of a=%d\n Value of b=%d”,a,b);
getch();
}
Output:-
Enter any two numbers
100
200
Value of a=100
18 C LANGUAGE

Value of b=200

Chapter 2 OPERATORS
Operators:-If we want to perform some business related operations, we
need operators. Operator need variable for operate.
19 C LANGUAGE

Types of operators:-
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Bitwise operator
5. Sizeof operator
6. Comma operator
7. Assignment operator
8. Dot operator

2.1 ARITHMETIC OPERATOR

Arithmetic operator divided into two parts.


(a) Binary Arithmetic:-An operator required at least two
operands for operation called binary arithmetic operator.

Example: - +,-,*, /, %.

Operator meaning example Result


+ addition 100+200 300
- subtraction 500-100 400
* multiplication 5*5 25
/ division 10/3 3
% modulas 5%3 2

Example1:-program for binary arithmetic operation.


20 C LANGUAGE

void main() {
int a,b,c;
printf("Enter two no\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d\n",c);
c=a-b;
printf("sub=%d\n",c);
c=a*b;
printf("mul=%d\n",c);
c=a/b;
printf("div=%d\n",c);
c=a%b;
printf("mod=%d\n",c);
getch();
}
Example2:- program for binary arithmetic operation.
void main()
{
int a,b;
printf("Enter two no\n");
scanf("%d%d",&a,&b);
printf("sum=%d\n",a+b);
printf("sub=%d\n",a-b);
printf("mul=%d\n",a*b);
printf("div=%d\n",a/b);
printf("mod=%d\n",a%b);
getch(); }
21 C LANGUAGE

(b) Unary arithmetic: - An operator required at least and at


most single variable for operation called unary arithmetic
operator.
Example: - ++,--
Example1:-program for unary ++ operator.
void main()
{
int a=1;
printf("%d\n",a++);
printf("%d\n",++a);
printf("%d\n",a++);
printf("%d\n",++a);
printf("%d\n",a);
getch();
}
Example2:-program for unary -- operator.
void main()
{
int a=1;
printf("%d\n",a--);
printf("%d\n",--a);
printf("%d\n",a--);
printf("%d\n",--a);
printf("%d\n",a);
getch();
}

2.2 RELATIONAL OPERATOR


22 C LANGUAGE

Relational operator returns Boolean type result that means true,


false but in c language true means 1 and false means 0.
operator meaning example Result
< Less than 5<10 1
> Greater than 5>10 0
<= Less than or equal to 5<=10 1
>= greater than or equal to 5>=10 0
== Equal to equal to 5==10 0
!= Not equal to 5!=10 1

Example1:-program for relational operators.


void main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("%d<%d is %d\n",a,b,a<b);
printf("%d>%d is %d\n",a,b,a>b);
printf("%d<=%d is %d\n",a,b,a<=b);
printf("%d>=%d is %d\n",a,b,a>=b);
printf("%d==%d is %d\n",a,b,a==b);
printf("%d!=%d is %d",a,b,a!=b);
getch();
}
2.3 LOGICAL OPERATOR

Logical operators are used to conjunction of relational operators, it


also return Boolean type values same as relational operators.
23 C LANGUAGE

Types of logical operators: - There are three types of logical


operators.
(a) Logical AND (&&)
(b) Logical OR (||)
(c) Logical NOT (!)

Logical AND (&&):-logical AND operator return true if


both conditions are true otherwise return false.

Table for Logical AND

Condition1 Condition1 Condition1 &&Condition2


T T T
T F F
F T F
F F F

Logical OR (||):-logical OR operator return false if both


conditions are false otherwise return true.

Table for Logical OR

Condition1 Condition1 Condition1 ||Condition2


T T T
T F T
F T T
F F F
24 C LANGUAGE

Logical NOT:-It convert the reverse of the result return


by relational operator.
Example1:-program of logical operators.
void main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
printf(" %d\n",a<b&&b>c);
printf("%d\n",a<b||b>c);
printf("%d\n",!(a<b));
getch();
}

2.4 BITWISE OPERATOR

Bitwise operator operates on particular binary bits than produce


result according to bit value and applied operators.
Types of bitwise operator:-
(a) Bitwise AND (&)
(b) Bitwise OR (|)
(c) Bitwise XOR (^)
(d) Bitwise Negation (~)
(e) Bitwise Leftshift (<<)
(f) Bitwise RightShift (>>)
Bitwise AND (&):-bitwise AND operator produce bit value 1as
result if both the bits where operator applies are 1 otherwise
produce 0.
25 C LANGUAGE

Bitwise OR (|):-bitwise OR operator produce bit value 0 as


result if both the bits where operator applies are 0 otherwise
produce 1.
Bitwise XOR (^):-bitwise XOR operator produce bit value 0 as
result if both the bits where operator applies are same (like both
are 0 or 1) otherwise produce 1.

Bitwise Negation (~):-bitwise negation converts input value into its


equivalent 1‟s complement.

Example:-

Variable Decimal Binary value


value
int a,b; a=15 1111
b=9 1001
(a&b) 1001(9 in decimal)
int a,b; a=15 1111
b=9 1001
(a|b) 1111(15 in decimal)
int a,b; a=15 1111
b=9 1001
(a^b) 0110(10 in decimal )
int a; a=15 ~a 1111111111110000 (-20 in decimal)
26 C LANGUAGE

Example1:-program for bitwise operator.


void main()
{
int a,b;
a=15,b=9;
printf("Result of bitwise AND is %d\n",a&b);
printf("Result of bitwise OR is %d\n",a|b);
printf("Result of bitwise XOR is %d\n",a^b);
printf("Result of bitwise Negation is %d",~a);
getch();
}
Left shift and Right shift:-both operators are used to shift bits, left shift
is used for shifting bits into left direction and right shift are shifting in
right direction.
For example1:-For left shift.
Case1:-
int a=15;
0000000000001111
After shifting in left
a<<2;
0000000000001111--00
27 C LANGUAGE

Discarded bits Original value Inserted


bits
00 0000000000111100(60 in decimal) 00

Case2:-
int a=-15;
if input value is negative than inserted bits will be 1.

For example2:-For left shift.


Case1:-
int a=15;
0000000000001111
After shifting in left
a>>2;
00--->0000000000000011
Inserted bits Original value Discarded bits
00 00000000000011(3 in decimal) 11

Case2:-
int a=-15;

If input value is negative than inserted bits will be 1.


28 C LANGUAGE

Example1:-program for left shift and right shift.


void main()
{
int a=15;
printf("left shift for positive no. is %d\n",a<<2);
printf("right shift for positive no. is %d\n",a>>2);
a=-15;
printf("left shift for negativ no. is %d\n",a<<2);
printf("right shift for negativ no. is %d",a>>2);
getch();
}
2.5 SIZEOF OPERATOR

sizeof operator return occupied byte by the variable or data types.


Example1:-program for finding size of data type.
void main()
{
printf("size of charactor is %d Byte\n",sizeof(char));
printf("size of integer is %d Byte\n",sizeof(int));
printf("size of long is %d Byte\n",sizeof(long));
printf("size of float is %d Byte\n",sizeof(float));
printf("size of double is %d Byte\n",sizeof(double));
getch();
}
29 C LANGUAGE

Example2:-program for finding size of data type.

void main()
{
char c;
int i;
long l;
float f;
double d;
printf("size of charactor is %d Byte\n",sizeof(c));
printf("size of integer is %d Byte\n",sizeof(i));
printf("size of long is %d Byte\n",sizeof(l));
printf("size of float is %d Byte\n",sizeof(f));
printf("size of double is %d Byte\n",sizeof(d));
getch();
}
Example3:- program for finding size of data type.

void main()
{
char c;
int i,size;
long l;
float f;
double d;
size=sizeof(c);
printf("size of charactor is %d Byte\n",size);
30 C LANGUAGE

size=sizeof(i);
printf("size of integer is %d Byte\n",size);
size=sizeof(l);
printf("size of long is %d Byte\n",size);
size=sizeof(f);
printf("size of float is %d Byte\n",size);
size=sizeof(d);
printf("size of double is %d Byte\n",size);
getch();
}

2.6 COMMA OPERATOR

Comma operator is used for separation.


Like int a,b,c;
a=1,b=100,c=1000;
Example1:-
void main()
{
int a,b,c;
a=1,b=10,c=100;
}
2.7 ASSIGNMENT OPERATOR

assignment operator is used for initializing variables.


Like a=100; a=b;
Example1:-
void main()
{
int a,b,c;
31 C LANGUAGE

a=10;
b=a;
c=b;
}

2.8 DOT OPERATOR

dot operator or membership operator is used for accessing structure


members, union members and accessing class members (oops).

Chapter 3 CONTROL STATEMENT


32 C LANGUAGE

3.1 INTRODUCTION
Control statements are used to control flow of execution or control
statements are used to take some decisions, like validations and security.
Control statements play very important roll where something should be
happened before satisfying some constraints or conditions, after that
result should be generated according to the constraints satisfaction or
dissatisfaction. Without using control statements nobody can develop a
scalable software or application. Control statements are much important
in business logic development.
For example someone want to withdraw money from his/her account so
in this process before withdraw money first check the available balance
must be greater or at least equal to the requested amount then only
money should be withdraw otherwise provide some insufficient balance
type messages.Another example is examination result generating system
in this process marks and some threshold values must be compare and
then produce results according to the applied conditions.
Types of control statements
1. if statement
2. if-else statement
3. switch-case statement
4. break statement
5. continue statement
6. goto-label statement
7. return statement

3.2 IF STATEMENT
33 C LANGUAGE

if is keyword followed by parenthesis, inside parenthesis write valid


constraints.

if(condition) if(condition)
Statement1; or {
Statement1;
}

In the above diagram if the given condition is true then only statement1
will be execute. If the given condition is false then statement1 will not
execute. We can write above if statement like
If we are not using curly brasses then only just first statement will be
inside if statement only.

If(condition) If(condition)
Statement1; {
Statement2; or Statement1;
Statement3; }
Statement2;
Statement3;

In above format only statement1 is control by if statement and


statement2 and statement3 are independent by if because both are
outside of if statement.

If we want to control over the multiple statements then we have to use


curly brasses in if statement.
34 C LANGUAGE

If(condition)
{
Statement1;
Statement2;
Statement3;
}
In the above if statement all three statements are inside if, all three will
be execute when condition will true otherwise all three statements will
not execute.
Example1:-program for result generation.
#include<stdio.h>
#include<conio.h>
void main()
{
int sub;
printf(“Enter your marks for a subject”);
scanf(“%d”,&sub);
if(sub>35)
printf(“you are pass in this sub”);
getch(); }
Run1:-
Enter your marks for a subject
40
You are pass in this sub
35 C LANGUAGE

Run2:-
Enter your marks for a subject
30
In this case our program not producing any message, to solve above
problem we have to check subject marks two times, it is a another
problem to verify same number twice it increase complexity of our
program ,program takes more time for execution.
Example2:-program for result generation.
#include<stdio.h>
#include<conio.h>
void main()
{
int sub;
printf(“Enter your marks for a subject”);
scanf(“%d”,&sub);
if(sub>35)
printf(“you are pass in this sub”);
if(sub<35)
printf(“you are fail in this sub”);
getch();
}
36 C LANGUAGE

Run1:-
Enter your marks for a subject
40
You are pass in this sub
Run2:-
Enter your marks for a subject
30
You are fail in this sub
3.3 IF-ELSE STATEMENT
Problem in if statement is solved by the if-else statement.
Syntax of if –else is:-
If(condition)
Statement1;
else
Statement2;
Or
If(condition){
Statement1;
}
else{
Statement2;
37 C LANGUAGE

}
Above syntax is best suitable for controlling single statement at a time.
In this syntax if the given condition is true then if block will execute and
else block will not execute. If the given condition is not true (false) then
if block will not execute and else block will execute.
If we want to control multiple statements then we have to use curly
brasses.
if(condition){
Statements;
}
else{
Statements;
}
Example1:-greater number between two number.
void main()
{
int a,b;
printf(“enter any two number”);
scanf(“%d%d”,&a,&b);
if(a>b){
printf(“\n greater number is %d”,a);
}
38 C LANGUAGE

else{
printf(“\n greater number is %d”,b);
}
getch();
}
Example2:-program for check the given number is negative or
positive.
void main()
{
int n;
printf(“enter a number”);
scanf(“%d”,&n);
if(n>0)
{
printf(“number %d is positive”,n);
}
else
{
printf(“number %d is negative”,n);
}
getch();
39 C LANGUAGE

}
Example3:-program for withdraw amount.
void main()
{
int bal=1000;
int w;
printf("Enter requested amont\n");
scanf("%d",&w);
if(bal>=w)
{
printf("\nplease take your amount\withdrawal success");
bal=bal-w;
printf("\nAvailable amount is %d",bal);
}
else
{
printf("\n Insufficient fund");
}
getch();
}
3.4 SWICH-CASE STATEMENT
40 C LANGUAGE

In if-else statement we have two options if block and else block but at a
time only one option executed either if either else. We user want to
execute multiple options then we have to go for switch-case statements.
Here both switch and case are keyword.
Syntax of switch-case:-
switch (n)
{
case 1:statement1;
case 2:statement2;
case 3:statement3;
default: default statement;
}
or
switch (n)
{
case 1:
{statements;}
case 2:
{statements;}
case 3:
{statements;}
default:
41 C LANGUAGE

{default statements;}
}
Or
switch (n)
{
case 2:statement2;
case 1:statement1;
default: default statement;
case 3:statement3;
}
Or
switch (n)
{
case 1:statement1;
case 2:statement2;
case 3:statement3;
}
Or
switch (n)
{
case „a‟:statement1;
42 C LANGUAGE

case „2‟:statement2;
case „G‟:statement3;
}

In above all syntaxes n may be char or int type and there is no rule for
order of cases, cases may be occur in any order.
How switch-case work it depends on value of n it matches n with cases
from top to down one by one, if any case match then execution start
from that case to below all cases.
Example1:-Demo of switch case.
void main()
{
int n;
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n) {
case 1:printf(“This is case 1”);
case 2:printf(“This is case 2”);
case 3:printf(“This is case 3”);
default:printf(“This is default case”);
}
getch();}
43 C LANGUAGE

Example2:-Demo of switch case.


void main()
{
int n;
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n)
{
case 1:printf(“This is case 1”);
printf(“you are in case 1”);
case 2:printf(“This is case 2”);
printf(“you are in case 2”);
case 3:printf(“This is case 3”);
printf(“you are in case 3”);
default:printf(“This is default case”);
printf(“you are in default case ”);
}
}
44 C LANGUAGE

We can write any number of statements in any cases.


If we want to execute only particular case which is matches. Then we
have use break statements in cases.
Example3:-Demo of switch case with break statement.
void main()
{
int n;
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n)
{
case 1:printf(“This is case 1”);
break;
case 2:printf(“This is case 2”);
break;
case 3:printf(“This is case 3”);
break;
default:printf(“This is default case”);
}
}
3.5 BREAK & CONTINUE STATEMENT
45 C LANGUAGE

break statement is used in switch-case and loops. Purpose of break


statement is used to stop the block execution. In the case of switch case
statement when control reached to break statement, break statement
switch control to outside of switch statement. Break statement can be
used at anywhere in cases.
continue statement:-continue statement is used only in loops we will
discuss at the time of loop explanation.
goto-label statement:- goto-label is a unconditional transfer jump
statement , less used in programming.
Syntax of goto label:- Statement1;
Statement2;
goto p;
Statement3;
p:
Statement4;
Example1:-Demo of goto statement.
void main{
printf(“main function begining”);
goto p;
printf(“inside goto ”);
p:
printf(“out side of goto”);
getch();}
46 C LANGUAGE

In this syntax statement1 and 2 will normally execute but when control
reached at goto p then control automatically transfer to p label and
statement3 will not execute and statement4 will normally executed.
We can write goto statement in another way like.
Statement1;
Statement2;
p:
Statement3;
goto p;
Statement4;
In the above format statement3 will executed infinite time.
return statement:-return statement is mainly used for function, we will
explain in user define function.

Chapter 4 LOOPS
47 C LANGUAGE

4.1 INTRODUCTION
Basic purpose of loops or iterative statement is to reduce length of
application or code and time of developer.

When to use loop, when same statements repeated sequentially then we


have to put that repeated code inside loops.

Types of loop:-
1. for loop
2. while loop
3. do-while loop
4.2 FOR LOOP
Syntax of for loop:-

for(initialization;condition;counter)
statement1;

OR

for(initialization;condition;counter)
{
statement1;
}
48 C LANGUAGE

for is a keyword followed by parentheses and inside parentheses part is


divided into three parts.
1. Initialization
2. Condition
3. Counter
First part initialization is a starting point of your result like if we
want to display 1 to 10, and then initialize variable to value 1.
Second part condition is play most important role, this part control
over the loop. For example our code not exceed to 10.
Third part is used for how to go from 1 to 10.
Like
1 2 3 4 5 ………10 maintain gap between elements or
1 3 5 ………….9 or
For loop for display table 1 to 10.

for(i=1;i<=10;i++)
printf(“%d\n”,i);

for(i=1;i<11;i++)
printf(“%d\n”,i);

for(i=1;i<=10;++i)
printf(“%d\n”,i);
49 C LANGUAGE

for(i=1;i<11;++i)
printf(“%d\n”,i);

for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
}

i=1;
for( ;i<=10;i++)
printf(“%d\n”,i);

for(i=1;i<=10; )
printf(“%d\n”,i++);

i=1 ;
for(;i<=10;)
{
printf(“%d\n”,i++);
}

i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
i++;
}
50 C LANGUAGE

i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
++i;
}

Complete program For for loop for display table 1 to 10.

void main(){
int i;
for(i=1;i<=10;i++)
printf(“%d\n”,i);
getch();
}

void main(){
int i;
for(i=1;i<11;i++)
printf(“%d\n”,i);
getch();
}
51 C LANGUAGE

void main(){
int i;
for(i=1;i<=10;++i)
printf(“%d\n”,i);
getch();
}

void main(){
int i;
for(i=1;i<11;++i)
printf(“%d\n”,i);
getch();
}

void main(){
int i;
for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
}
getch();
}

void main(){
int i;
i=1;
for( ;i<=10;i++)
printf(“%d\n”,i);
getch();
}
52 C LANGUAGE

void main(){
int i;
for(i=1;i<=10; )
printf(“%d\n”,i++);
getch();
}
void main(){
int i;
53 C LANGUAGE
i=1 ;
for(;i<=10;)
{
printf(“%d\n”,i++);
}
getch();
}
54 C LANGUAGE

void main(){
int i;
i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
i++;
}
getch();
}

void main(){
int i;
for(;i<=10;)
{
printf(“%d\n”,i);
++i;
}
getch();
}

Example1:-Program for total characters of C LANGUAGE.


void main()
{
int i,s=0;
for(i=0;i<=127;i++)
{
55 C LANGUAGE

printf("%c\t",i);
s++;
}
for(i=-1;i>=-128;i--)
{
printf("%c\t",i);
s++;
}
printf("\nC LANGUAGE SUPPORT TOTAL %d CHARACTERS",s);
getch();
}
Example2:-Program for finding ASCII value associated with
character.
#include<dos.h>
void main()
{
int i;
clrscr();
for(i=0;i<=127;i++)
{
printf("character %c Ascii value %d\t",i,i);
56 C LANGUAGE

delay(1000);
}
for(i=-1;i>=-128;i--)
{
printf("character %c Ascii value %d\t",i,i);
delay(1000);
}
getch();
}
4.3 WHILE LOOP
While loop work same as for loop both for and while are entry
controlled loop that means loop will execute if given condition satisfied.
While is a keyword followed by parentheses, inside parenthesis we have
to write valid condition.
Syntax of while loop:-
Initialization;
While(condition)
{
Statements;
}

In the above syntax first variable will initialize once then control goes to
check the given condition is true or not. If the given condition is true
then control goes to inside loop and execute statement then again control
57 C LANGUAGE

transfer for checking condition whenever condition is true then loop will
be executed, once condition will be false then loop will stop.
Example1:-Demo of while loop for print ten times Well-Come.
void main()
{
int i;
printf(“This is demo of while loop\n”);
i=1;
while(i<=10)
{
printf(“Well-Come”);
}
getch();
}
Example2:-print 1 to 10.
void main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
58 C LANGUAGE

i++;
}
getch();
}
Example3:-print 1 to 10.
void main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
++i;
}
getch();
}
Example4:-print 1 to 10.
void main()
{
int i=1;
while(i<=10){
printf(“%d\n”,i++);
59 C LANGUAGE

}
getch();}
4.4 DO-WHILE LOOP
do-while loop is an exit control loop, in this loop statements or body of
loop executed before satisfying conditions for first time that means do-
while loop executed at least once even condition is false. All three loops
are used for same purpose.
Syntax of do-while loop:-
Initialization;
do{
Statements;
}while(condition);

In the above syntax control first goes to body of do after execution of do


block condition will checked , if condition is satisfy then control again
goes to do block this process repeated whenever condition satisfied.
Example1:-Demo of while loop for print ten times Well-Come.
void main()
{
int i;
printf(“This is demo of while loop\n”);
i=1;
do{
printf(“Well-Come”);
60 C LANGUAGE

i++;
}while(i<=5);
getch();}
Example2:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i);
i++;
}while(i<=10);
getch();
}
Example3:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i);
61 C LANGUAGE

++i;
}while(i<=10);
getch();}
Example4:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i++);
}while(i<=10);
getch();
}

4.5 BREAK & CONTINUE


Both statement are used in loop, inside loop when control reached at
break statement, control transfer to the end of loop (loop finish) and
whenever control reached at continue statement inside loop, control will
transfer to the beginning of loop.
Example1:-Demo of break statement.
void main()
{
62 C LANGUAGE

int i;
for(i=1;i<=10;i++)
{
if(i==6)
{
break;
}
printf(“%d\n”,i);
}
getch();
}
Example2:-Demo of break statement.
void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==6)
{
continue;
}
63 C LANGUAGE

printf(“%d\n”,i);
}
getch();
}
4.6 NESTING
Nesting is a control switching process in which some thing happening
under something.
Types of nesting:-nesting is categorized into two categories.
1. Statement nesting
2. Loop nesting
Statement nesting:-in statement nesting, statement run under
another statement, both statement may be same or may be
different.
Example1:-
if(condition) ----------outer statement
{
if(condition)-----------inner statement
{
}
}
Example2:-
if(condition)------------outer statement
{
switch(exp)-------------inner statement
{
case 1:
case 2:
64 C LANGUAGE

………
}
}
Nesting of loop:-loop inside loop called nesting (any loop inside
any loop)

Example1:-Demo of nesting of loop


void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d\t%d\n”,i,j)
}
}
getch();
}
Example2:-Demo of nesting of loop
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,j);
}
65 C LANGUAGE

printf(“\n”);
}
getch();
}

Example3:-Demo of nesting of loop


void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,i);
}
printf(“\n”);
}
getch();
}
Example4:-Demo of nesting of loop
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(“*\t”);
66 C LANGUAGE

}
printf(“\n”);
}
getch();
}

Chapter 5 ARRAY

5.1 1-D ARRAY

array is a special type of variable which can store more than one
value into a single variable at a time but all the values must be
similar. Array elements stored sequentially in memory. Array is
also called indexed variable, array index start from 0.
That means whole memory assign to array variable is divide into
array indexed.

Syntax for array declaration:-


Data type arrayname[size];

For example:- int arr[5];

Here size means number of elements size must be integer or


character type.
int arr[5];
67 C LANGUAGE

In the above variable declaration arr is a array variable can store


maximum five elements and all five elements will be int type. It is
not possible to store three int and two float type values in any array
of size five. All the values must be similar.
Memory allocation for int arr[5] is:-

value1 value2 value3 value4 value5


arr[0] arr[1] arr[2] arr[3] arr[4]
Here [] symbol is called subscript so array is also known as
subscripted variable. If arrayname followed by single subscript
than array will be one dimensional variable (1D array or linear
array).
Why array concept come because ordinary variables can store at a
time only single value.

Example1:-Why need array.


void main()
{
int a;
a=100;
a=200;
a=300;
printf(“a=%d\n”,a);
printf(“a=%d\n”,a);
printf(“a=%d\n”,a);
getch();
}
Output:-
a=300
68 C LANGUAGE

a=300
a=300
Example2:-Why need array.
void main()
{
int a;
a=100;
printf(“a=%d\n”,a);
a=200;

printf(“a=%d\n”,a);
a=300;
printf(“a=%d\n”,a);
getch();
}

Output:-
a=100
a=200
a=300

Initialization of array:-
int arr[5]={10,20,30,40,50};
Or
int arr[5]={10,20,30,40};
Or
int arr[]={10,20,30,40,50,60,70};
Or
int arr[5]={};
69 C LANGUAGE

Example1:-Demo of array.
void main()
{
int arr[5]={10,20,30,40,50},i;
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
getch();
}
Example2:-Demo of array.
void main()
{
int arr[5]={50,40,30,20,10},i;
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
getch();
}
Example3:-Demo of array.
void main()
{
int arr[5]={10,20,30,40,50},i;
printf(“array elements are:-\n”);
for(i=4;i>=0;i--)
{
printf(“%d\n”,arr[i]);
70 C LANGUAGE

}
getch();
}
Example4:-Demo of array(input from keyboard).
void main()
{
int arr[5],i;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
scanf(“%d”,&arr[i]);
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
printf(“%d\n”,arr[i]);
getch();
}
Example5:-Demo of array(input from keyboard).
void main()
{
int arr[5],i;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
71 C LANGUAGE

getch();
}
Example6:-program for sum of all array elements.
void main()
{
int arr[5],i,s=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}

Example7:-program for sum of all even elements of array .


void main()
{
int arr[5],i,s=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
72 C LANGUAGE

for(i=0;i<=4;i++)
{
if(arr[i]%2==0)
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}
Example8:-program for sum of all odd elements of array.
void main()
{
int arr[5],i,s=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
if(arr[i]%2!=0)
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}
Example9:-program for sum of all even and all odd elements of
array.
void main()
{
73 C LANGUAGE

int arr[5],i,even=0,odd=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
if(arr[i]%2==0)
{
even=even+arr[i];
}
else
{
odd=odd+arr[i];
}
}
printf(“\nSum of even elements=%d”,even);
printf(“\n Sum of odd elements=%d”,odd);
getch();
}
Example10:-program for search an element into array.
void main()
{
int arr[5],i,s=0,n;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
74 C LANGUAGE

}
printf(“\n Enter search element\n”);
scanf(“%d”,&n);
printf(“Searching is going on:::::-\n”);
for(i=0;i<=4;i++){
if(arr[i]==n)
{
s++;
break;
}
}
if(s==0)
printf(“\n element not found\n”);
else
printf(“\n element found at position %d”,i);
getch();}
Example11:-program for check duplicate data occurrence.
void main()
{
int a[5],n,i,s=0;
printf("Enter array Elements\n");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
printf("Enter a search element\n");
scanf("%d",&n);
printf("Searching started...........\n");
for(i=0;i<=4;i++)
{
75 C LANGUAGE

if(a[i]==n)
{
s++;
}
}
if(s==0)
printf("Element not found");
else
printf("Element found at %d times",s);
getch();
}

Example12:-program for reverse a array.


#define size 5
void main()
{
int a[size],i,j,p;
clrscr();
printf("Enter array Elements\n");
for(i=0;i<=size-1;i++)
{
scanf("%d",&a[i]);
}
printf("Array elements before reverse\n");
for(i=0;i<=size-1;i++)
{
76 C LANGUAGE

printf("%d\n",a[i]);
}
printf("Revrsing process start....\nArray elements are ::::-\n");
j=4;
for(i=0;i<size/2;i++)
{
p=a[i];
a[i]=a[j];
a[j]=p;
j--;
}
for(i=0;i<=size-1;i++)
{
printf("%d\n",a[i]);
}
getch();}

5.2 2D ARRAYS

Two dimensional array is a special type of variable which can store


data into row and column form or store data into two dimension.
2D array is used to create matrices.

Syntax of declaration:-
Datatype arrayname[size][size];
Here first subscript represents number of row and second subscript
represents number of columns.

For example:-
int arr[2][2]; here arr contains four values that will
77 C LANGUAGE

be arrange in row and column form.


Like

arr[0][0] arr[0][1]
arr[1][0] arr[1][1]
Here arr[0][0],means first element(zero row ,zero column)
arr[0][1],means second element(zero row ,one column)
arr[1][0],means third element(one row ,zero column)
arr[1][1],means fourth element(one row ,one column)

Array initialization:-
int[2][2]={{11,22},{33,44}};
Or
int[2][2]={11,22,33,44};

int[][2]={11,22,33,44};

Example1:- program for matrix hard coded value.


void main()
{
int arr[2][2]={{11,22},{33,44}},i,j;
clrscr();
printf("matrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
78 C LANGUAGE

}
printf("\n");
}
getch();
}
Example2:- program for matrix hard coded value.
void main(){
int arr[2][2]={11,22,33,44},i,j;
clrscr();
printf("matrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();}
Example3:-program for matrix hard coded value.
void main()
{
int arr[][2]={11,22,33,44},i,j;
clrscr();
printf("matrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
79 C LANGUAGE

}
printf("\n");
}
getch();
}
Example4:-program for matrix input from keyboard.
void main()
{
int arr[2][2],i,j;
clrscr();
printf("Enter matrix elements:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nmatrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
Example5:-program for addition of two matrix
80 C LANGUAGE

void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
81 C LANGUAGE

for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix addition is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

Example6:-program for subtraction of two matrix


void main()
82 C LANGUAGE

{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
for(i=0;i<=1;i++)
83 C LANGUAGE

{
for(j=0;j<=1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix addition is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Example6:-program for matrix multiplication.
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,k,s=0;
84 C LANGUAGE

clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
85 C LANGUAGE

{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix multiplication is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
for(k=0;k<=1;k++)
{
s=s+a[i][k]*b[k][j];
}
c[i][j]=s;
}
s=0;
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
5.3 POINTER
86 C LANGUAGE

Pointer is a special type of variable which contains address of


another variable but same data type. Every pointer variable
occupied two byte memory. Problem in ordinary variable is that,
they can store value of another variable, but cannot store address of
another variable.

Need of pointer:-some times we have to access value from the


address that time we have to use pointer variable. Using the pointer
we can access real value of that variable.

Syntax of pointer declaration:-


Datatype *variablename;
For example:-
int *p;
Here p can store address of another variable.
*p mean value at p address.

Example1:-
void main()
{
int a=10,b;
b=a;
printf(“original value of a=%d\n”,a);
printf(“mirror value of a=%d”,b);
getch();
}
87 C LANGUAGE

Example2:-
void main()
{
int a=10,b;
b=&a; //Error
getch();
}
Example3:-
void main()
{
int a=10,*b;
b=&a;
printf(“original value of a=%d\n”,a);
printf(“original value of a=%d\n”,*b);
printf(“original value of a=%d\n”,*(&a));
printf(“address of a=%u”,&a);
printf(“\n address of a=%u”,p);
getch();
}

Example4:-withdraw from account.


void main()
{
int acc=1000;
int w=500;
printf(“before withdraw account=%d”,acc);
if(acc>=w)
{
printf(“\n withdra success”);
88 C LANGUAGE

acc=acc-w;
}
printf(“\n after withdra account=%d”,acc);
getch();
}

Example5:-withdraw from account using pointer.


void main()
{
int acc=1000;
int *p;
p=&acc;
printf(“before withdraw account=%d”,acc);
*p=500;
printf(“\n after withdra account=%d”,acc);
getch();
}
89 C LANGUAGE

Chapter 6 FUNCTION
6.1 INTRODUCTION
function is a set of instructions which perform some unique task.
Function is a self contained block of statements, which perform
some task.

How function works: - anything which are using in program must


be having some relation in the program. Like if we used a variable
in our program , that variable must be declared , if used any
function like printf or any other library function in our program
for that we must have to include header file of that function
because in that header fine function has declared and define.

Types of functions:-functions are divided into two parts.


1. Library or inbuilt or standard function (own functions of
language)
2. User defines function.
90 C LANGUAGE

Library functions:-library functions are those functions which


comes together with system (like c and cpp are inbuilt in
TC,java functions are inbuilt in jdk or in our mobile having
phone,contact,message etc are inbuilt).
Properties of Library functions:-
1. We cannot change function name (name is fixed).
2. We cannot change definition or working of function (cannot
write definition or definition is fixed).
3. Cannot change return type (return type is fixed).
4. Cannot not change number of function parameters and its
types.

Properties of User define functions:-


1. We can write any name (except library functions and
keywords) or change function name (name is not fixed).
2. We can change definition or working of function or user
provide definition (definition is not fixed).
3. Can change return type (return type is not fixed).
4. User decides number of parameters and its types.
For use of user define function:-
1. Function prototype or function declaration.
2. Function call.
3. Function definition.

Syntax of function prototype:-


Return type functionname(type,type,……);
For example
91 C LANGUAGE

int sum(int,int);

Function call:-
Functionname(variable,variable,……);
For example:-
sum(a,b);

Function called or function definition:-


Return type functionname(type v1,type v2,…..)
{
Instructions;
}
For example:-
int sum(int p,int q)
{
return(p+q);
}

Example1:-
void fun1();
void fun2();
void main()
{
clrscr();
printf("This is main function");
fun1();
printf("\nControl come back in main function\n");
fun2();
printf("Control come back in main function\n");
92 C LANGUAGE

getch();
}
void fun1()
{
printf("\n This is function1");
}
void fun2()
{
printf("\n This is function2");
}

Example2:-
void fun1();
void fun2();
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
93 C LANGUAGE

printf("Control come back in main function\n");


fun2();
printf("Control come back in main function\n");
getch();
}
Example3:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example4:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
94 C LANGUAGE

{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun2();
fun1();
printf("Control come back in main function\n");
fun2();
fun1();
printf("Control come back in main function\n");
getch();}
Example5:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
fun1();
printf("Control come back in main function\n");
95 C LANGUAGE

fun1();
printf("Control come back in main function\n");
getch();
}
Example6:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun2();
fun2();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example7:-
void fun2();
void fun1()
{
fun2();
printf(" This is function1\n");
96 C LANGUAGE

}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();}
Example8:-
void fun2();
void fun1()
{
fun2();
printf(" This is function1\n");
fun2();
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
97 C LANGUAGE

fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example9:-
void fun2(void);
void fun1(void)
{
fun2();
printf(" This is function1\n");
fun2();
}
void fun2(void)
{
printf(" This is function2\n");
}
void main(void)
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example10:-
void fun2();
98 C LANGUAGE

void fun3();
void fun1()
{
fun2();
printf(" This is function1\n");

}
void fun2()
{
fun3();
printf(" This is function2\n");
}
void fun3()
{
printf(" This is function3\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in end of main function\n");
getch();
}
Example11:-
void fun2();
void fun3();
void fun1()
{
printf(" This is function1\n");
99 C LANGUAGE

}
void fun2()
{
printf(" This is function2\n");
fun1();
}
void fun3()
{
printf(" This is function3\n");
fun2();
}
void main()
{
clrscr();
printf("This is main function\n");
fun3();
printf("Control come back in end of main function\n");
getch();
}

6.2 RECURSION
Recursion is a process by which function call itself again and again
infinitely, recursion must required a terminating condition for
avoiding infinite calls.
Example1:-
int rec(int);
void main()
{
int n,f;
100 C LANGUAGE

printf("Enter a number for factorial\n");


scanf("%d",&n);
f=rec(n);
printf("factorial of %d is %d",n,f);
getch();
}
int rec(int n)
{
if(n==1)
{
return 1;
}
else
{
return(n*rec(n-1));
}}
Example2:-
int count;
int rec(int);
void main()
{
int n,f;
clrscr();
printf("Enter a number for factorial\n");
scanf("%d",&n);
count=0;
f=rec(n);
printf("factorial of %d is %d",n,f);
getch();
}
101 C LANGUAGE

int rec(int n)
{
count++;
if(n==1)
{
printf("function call itself %d time\n",count);
printf("Terminating condition Hit\n");
return 1;
}
else
{
printf("function call itself %d time\n",count);
return(n*rec(n-1));
}
}

Example3:-
int count;
void rec();
void main()
{
int n;
clrscr();
printf("\Function call start\n");
rec();
getch();
}
void rec()
{
count++;
102 C LANGUAGE

if(count==10)
{
printf("function call itself %d time\n",count);
printf("Terminating condition Hit\n");
}
else
{
printf("function call itself %d time\n",count);
rec();
}
}

6.3 GLOBAL VARIABLE

A variable which is declared outside function called global variable.


Global variable is available in full program where it is declared.
Example1:-program for global variable.
int a=100;
void main()
{
int b;
printf("Enter a number");
scanf("%d",&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
getch();
103 C LANGUAGE

}
Example2:-program for global variable.
int a=100;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
getch();
}
Example3:-program for global variable.
int a;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
getch();
}
Example4:-program for global variable.
int a;
void main()
{
int a;
104 C LANGUAGE

printf("Enter two number");


scanf("%d%d",&a,&a);
printf("Global variable a=%d\n",a);
printf("Local variable a=%d\n",a);
printf("Sum of Global and Local=%d",a+a);
getch();
}
Example5:-program for global variable.
int a;
void main()
{
int a;
printf("Enter two number");
scanf("%d%d",&::a,&a);
printf("Global variable a=%d\n",::a);
printf("Local variable a=%d\n",a);
printf("Sum of Global and Local=%d",::a+a);
getch();
}
Example6:-program for global variable.
void disp();
int a;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
105 C LANGUAGE

disp();
getch();
}
void disp()
{
printf("outside main global variable a=%d\n",a);
printf("outside main(declared in main) local variable=%d\n",b);
}
Output:-Compiling GL6.C:
Error GL6.C 17: Undefined symbol 'b'

Example7:-program for global variable.


void disp();
int a;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
disp();
getch();
}
106 C LANGUAGE

void disp()
{
printf("outside main global variable a=%d\n",a);
}

Example8:-program for global variable.


void disp();
int a;
void main()
{
int b;
clrscr();
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable in main function b=%d\n",b);
printf("Sum of Global and Local in main=%d\n",a+b);
disp();
getch();
}
void disp()
{
int a=10;
printf("Global variable a=%d\nLocal variable in disp
function=%d\n",::a,a);
printf("Sum of Global and Local in disp=%d",::a+a);
}

6.4 STRING
107 C LANGUAGE

String is a sequence of character, in real life applications string is


an important type of entity. In real life name of any place or person
may change or any updating required then we have to do according
to the requirement so have some string handling functions to do
needful.

Example1:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
scanf("%s",name);
printf("\n Well-come %s",name);
getch();
}
Example2:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
getch();
}
Example3:-
#include<string.h>
void main()
{
108 C LANGUAGE

char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
printf("\n Number of character in your name are:-
%d",strlen(name));
getch();
}
Example4:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
printf("\n After passing from strupr:-%s",strupr(name));
getch();
}

Example5:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
109 C LANGUAGE

gets(name);
printf("\n Well-come %s",name);
printf("\n After passing from strupr:-%s",strupr(name));
printf("\n After passing from strrev:-%s",strrev(name));
printf("\n After passing from strlpr:-%s",strlwr(name));
printf("\n After passing from strrev:-%s",strrev(name));
getch();
}
Example6:-
#include<string.h>
void main(){
char n[50],m[50];
printf("Enter your name in first array\n");
gets(n);
printf("Enter your surname in second array\n");
gets(m);
printf("\n Well-come name is %s\t surname is %s\n",n,m);
strcpy(n,m);
printf("\n After passing from strcpy: name is %s\t surname is
%s\n",n,m);
strcat(n,m);
printf("\n After passing from strcat: name is %s\t surname is
%s",n,m);
getch();}
Example7:-
#include<string.h>
void main()
{
char n[50],m[50];
int l;
110 C LANGUAGE

printf("Enter first string\n");


gets(n);
printf("Enter second string\n");
gets(m);
printf("\n first string is %s\t second string is %s\n",n,m);
l=strcmp(n,m);
if(l==0)
printf("\n both strings are equal\n");
else
printf("strings are not equal");
strupr(n);
printf("\n first string is %s\t second string is %s\n",n,m);
l=strcmp(n,m);
if(l==0)
printf("\n both strings are equal\n");
else
printf("strings are not equal");
printf("\n Demo of strcmpi()\n");
l=strcmpi(n,m);
if(l==0)
printf("\n both strings are equal\n");
else
printf("strings are not equal");
getch();}
111 C LANGUAGE

Chapter 7 STRUCTURE & UNION

7.1 INTRODUCTION

Structure is a special type of variable which has been solved


the limitation of the arrays ,major issues with array is that
array can store multiple values but all the values must be
same type or similar values into a single variable. But
sometimes in real world required variety type of multiple
values for this purpose array is not suitable variable but here
structure can contains different types of values or multiple
values into a single variable. structure is a very powerful
variable which can contains normal or ordinary values , can
contains multiple arrays or different types of multiple arrays
and can contains address of different variables. Different
elements into a structure called member variables of structure
and nobody can direct access these members, for access these
member variables need a structure variable and using dot
operator member can access. In a structure every member
occupied individual memory.
112 C LANGUAGE

For access structure: - variable.member

Variable declaration:-
struct structure name
{
Memeber1;
Member2;
.
.
.
Member n;
}variables;

Example:-
struct book
{
char name[50];
int pages;
float price;
}b;

Initialization:-
struct book
{
char name[50];
int pages;
float price;
};
struct book b={“C language”,200,250.0};

Example1:-program for structure (initialization).


void main()
113 C LANGUAGE

{
struct book
{
char name[50];
int pages;
float price;
};
struct book b={"C Language",200,250.0};
printf("Name of book is %s\n",b.name);
printf("pages=%d\n price=%f",b.pages,b.price);
getch();
}

Example2:-program for structure (input from keyboard).


void main()
{
struct book
{
char name[50];
int pages;
float price;
}b;
clrscr();
printf("Enter book name\n");
gets(b.name);
printf("Enter pages\n");
scanf("%d",&b.pages);
printf("Enter price\n");
scanf("%f",&b.price);
printf("\n===========Finally book Record is==========\n");
114 C LANGUAGE

printf("Name of book is %s\n",b.name);


printf("pages=%d\nprice=%f",b.pages,b.price);
getch();
}
Example3:-program of structure using pointer.
void main()
{
struct book
{
char name[50];
int pages;
float price;
}*p;
struct book b={"C Language",200,250.0};
p=&b;
printf("Name of book is %s\n",p->name);
printf("pages=%d\n price=%f",p->pages,p->price);
getch();
}
Example4:-program of structure using pointer.
void main()
{
struct book
{
char name[50];
int pages;
float price;
}b;
struct book *p;
clrscr();
115 C LANGUAGE

printf("Enter book name\n");


gets(b.name);
printf("Enter pages\n");
scanf("%d",&b.pages);
printf("Enter price\n");
scanf("%f",&b.price);
p=&b;
printf("\n===========Finaly book Record is==========\n");
printf("Name of book is %s\n",p->name);
printf("pages=%d\nprice=%f",p->pages,p->price);
getch();
}

7.2 UNION

Union is very much similar to structure with little difference


that is memory allocation process for the members, structure
occupied memory for individual where as union share the
highest memory of member and other member share same
allocated memory. In above all program use union in place of
struct keyword.
Difference can be observe by the below program.
Example1:-program for difference between structure and
union.
void main()
{
struct book
{
char name[50];
int pages;
116 C LANGUAGE

float price;
}s;
union book1
{
char name[50];
int pages;
float price;
}u;
printf("Memory taken by structure %d bytes\n",sizeof(s));
printf("Memory taken by union %d bytes",sizeof(u));
getch();
}

7.3 FILE HANDLING

FILE <STDIO.H>

File control structure for streams.

typedef struct {
short level;
unsigned flags;
char fd;
unsigned char hold;
short bsize;
unsigned char *buffer, *curp;
unsigned istemp;
short token;
117 C LANGUAGE

} FILE;
Here FILE is declared as a structure type variable in <stdio.h>
header file. Using the keyword typedef we can create our own type
of variable. Like we can create now FILE type variables, FILE
*fp.
In file handling we can create new file, update file, delete file and
many operation can perform on file.
For performing any type of operations file must be open first, for
opening a file we have to use library function fopen().
Syntax of fopen function:-

fopen(“filename”,”filemode”);

fopen function return address of given filename, if given file not


exist in the current directory then return NULL pointer.
In fopen function having two parameters first is file name or we
can use full path of file where file is saved or have to save, second
parameter is file mode that means what type of operation we want
to perform of file.
File modes:-
1. Read mode(r)
2. Write mode(w)
3. Append mode(a)
In read mode we can perform only read operation, in this mode
if file not exist then function will not create new file, function
return NULL.

In write mode we can perform read and write both operation, in


this mode if file not exist then file will be created automatically.
118 C LANGUAGE

In append mode we can perform read, write and append


operation, in this mode if file not exist then file will be created
automatically.
Example1:-program for read mod.
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("a123.c","r");
if(fp==NULL)
{
printf("file not exist\n");
}
else
{
printf("file exist");
}
getch();
}
Example2:-program for write mod.
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("a123.c","w");
if(fp==NULL)
{
printf("file not exist\n");
}
else
119 C LANGUAGE

{
printf("file exist");
}
getch();
}
Example3:-program in append mod.
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("a123.c","a");
if(fp==NULL)
{
printf("file not exist\n");
}
else
{
printf("file exist");
}
getch();
}
Example4:-program for writing data into file.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50];
fp=fopen("a123.c","w");
printf("Enter your name\n");
gets(name);
120 C LANGUAGE

fputs(name,fp);
printf("Data inserted successfully");
getch();
}
Example5:-program for reading from a file and writing into
file.
#include<stdio.h>
void main()
{
FILE *fp,*fw;
char name[50],ch;
fp=fopen("a123.c","r");
fw=fopen("a12.c","w");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
fputc(ch,fw);
ch=fgetc(fp);
}
printf("Data inserted successfully");
getch();
}
Example6:-program for counting number of lines.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50],ch;
int s=0;
121 C LANGUAGE

fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==‟;‟)
{
s++;
}
ch=fgetc(fp);
}
printf("Total lines %d",s-1);
getch();
}

Example7:-program for counting number of spaces.


#include<stdio.h>
void main()
{
FILE *fp;
char name[50],ch;
int s=0;
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==‟ ‟)
{
s++;
122 C LANGUAGE

}
ch=fgetc(fp);
}
printf("Total spaces %d",s-1);
getch();
}
Example8:-program for counting particular character.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50],ch;
int s=0;
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==‟Z ‟)
{
s++;
}
ch=fgetc(fp);
}
printf("Total character %d",s-1);
getch();
}
Example9:-program for counting total character into file.
#include<stdio.h>
void main()
123 C LANGUAGE

{
FILE *fp;
char name[50],ch;
int s=0;
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
s++;
ch=fgetc(fp);
}
printf("Total character into file %d",s-1);
getch();
}

Potrebbero piacerti anche