C Notes
C Notes
Assembly language
What is C?
Features of C
1. Portability or machine independent
2. Sound and versatile language
3. Fast program execution.
4. An extendible language.
5. Tends to be a structured language.
Historical developments of C
Year Language Developed by Remarks
1960 ALGOL International committee Too general, too
abstract
1963 CPL Cambridge University Hard to learn,
difficult to
implement
1967 BCPL Martin Richards at Could deal with
Cambridge university only specific
problems
1970 B Ken Thompson at AT & Could deal with
T only specific
problems
1972 C Dennis Ritche at AT & T Lost generality of
BCPL and B
restored
Data types
Output Functions:-
printf( ): This function is used to display the information on the
screen. It displays the variable values and the string values.
This function belongs to stdio.h.
Syntax:- printf(“control string” ,variable list);
float pi=3.14;
printf(“\n The value of pi is %f”, pi);
char ch=’y’;
printf(“The option is %c”,ch);
char name[20]=”India” ;
printf(“\n The name is %s”,name);
Input functions:
Scanf( ):- It is an input function, that is used to accept the values
into variables at the time of executing a program. This function
belongs to stdio.h .
float b;
scanf(“%f”, &b);
char ch;
scanf(“%c”, &ch);
char name[20];
scanf(“%s”,name);
int a,b;
scanf(“%d%d”,&a,&b);
int a;
float b;
scanf(“%d%f”,&a,&b);
int a;
float b;
char ch,name[20];
scanf(“%d%f%c%s”, &a, &b, &ch, name);
Structure of a C program
main( ) :
The compilation and the running process is done with the
help of main( ). A ‘C’ program definitely requires main( ).
Declaration par``t:
This area is where we declare all the variables which are
used in the program.
Initialization part:
Here, we give values to the declared values.
Executable part:
The output is seen from the executable part. By using
output functions.
Programming rules:
A ‘C’ program should be written in lower case. Every
statement should end with; . The pair of { } should be matched.
Compilation and Execution process:
After writing the program we should compile by pressing
on the Compile option of Compile menu. This converts the text
format that is source code into Binary format which is called as
object code. After that by pressing Run option the output can be
seen.
Sample programs
#include<stdio.h>
#include<conio.h>
main( )
{
clrscr( );
printf(“Welcome to C”);
printf(“ Course name \t : \t ‘C’ lang \n”);
getch( ) ;
return 0; g1c`c3 gn
}
#include<stdio.h>
#include<conio.h>
main ( )
{
int a, b, c;
a=20;
b=30;
c=a+b;
printf(“C = %d”, c);
getch ( );
}
3. Write a program to calculate area and perimeter of a
rectangle.
#include<stdio.h>
#include<conio.h>
main ( )
{
float l, b, area, perimeter;
clrscr ( );
l=4.5;
b=3.5;
area=l*b;
perimeter=2*(l+b);
printf(“\n The area of the rectangle is %f sq units”, area);
printf(“\n The perimeter of the rectangle is %f units”,
perimeter);
getch ( );
}
#include <stdio.h>
#include <conio.h>
main ( )
{
float side, area, perimeter;
clrscr( );
printf(“\n Enter side of the square”);
scanf(“%f ”, &side);
area=side*side;
perimeter=4*side;
printf(“\n The area of a square is %f squints”, area);
printf(“\n The perimeter of a square is %f units”,
perimeter);
getch( );
}
5. Write a program to calculate the radius and area of the
circle.
#include<stdio.h>
#include<conio.h>
main ( )
{
float radius, area;
float pie =3.14;
clrscr ( );
printf(“\n Enter radius of the circle”);
scanf(“%f ”, &radius);
area=pie*radius*radius;
printf(“\n The area of the circle is %f sq units”, area);
getch( );
}
# include <stdio.h>
# include <conio.h>
void main( )
{
int eno;
float basic_salary,hra,da,gross;
char name[20];
clrscr( );
printf("\n Enter eno, name , basic salary ");
scanf("%d %s %f", &eno, name, &basic_salary);
hra=basic_salary*15/100;
da=basic_salary*12/100;
gross=basic_salary+hra+da;
printf("\n Employ number is %d",eno);
printf("\n Employ name is %s",name);
printf("\n Basic is %f",basic_salary);
printf("\n Hra is %f",hra);
printf("\n Da is %f",da);
printf("\n Gross is %f",gross);
getch( );
}
Operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment or decrement operators
6. Conditional operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic
operations like addition, subtraction, multiplication, division and
remainder.
Relational operators:-
These operators are used to check the relation between the
values.
Note:- When we used relational operators the output is seen in
the form of 0 or 1. If the relation is true the output will be in 1.
If the relation is false the output will be 0.
Ex:-a=10;
b=3;
Operator Name Exampl Result
e
< Less than a<b False or 0
> Greater than a>b True or 1
<= Less than or a<=b False or 0
equal to
>= Greater than or a>=b True or 1
equal to
== Equal to A= =b False or 0
!= Not equal to a!=b True or 1
name
Operator
&& And
|| Or
! Not
i. && : (And)
Statement:- When all arguments are true then only the
total condition is true. Any one of the arguments is false the
total condition is false.
Truth Table
(a>=b&&a>=c) a=6,b=5,c=2
Expression1 && Result
Expression2
T T T
T F F
F T F
F F F
ii. || : (Or)
Statement:- When one of the argument is true the total
condition is true. If all the arguments are false then only the
condition is false.
Truth Table
(a>=b||a>=c) a=3,b=5,c=2
Expression1 || Result
Expression2
T T T
T F T
F T T
F F F
iii. ! (Not):
Statement:- This is the negative operator, we can place the
‘not’ operator before any one of the condition. It returns the
opposite result of the condition. i.e. It converts true to false
and false to true.
Truth Table
!(a<=b) a=3,b=2
! result
expression
T F
F T
1) ++ increment operator
2) -- decrement operator
Increment operator (+ +)
Increment operator is used to increase the value by 1.
Pre Increment( ++a)
First the value will be incremented, and then the new
value will be printed.
Ex:- printf(“%d”,++a);
Post Increment(a++)
First the value will be printed and then the value will be
incremented.
Ex:- printf(“%d”,a++);
Decrement operator ( - - )
Decrement operator is used to decrease the value by 1.
#include<stdio.h>
#include<conio.h>
main( )
{
int a, b;
clrscr( );
printf(“\n Enter a and b values \t:\t”);
scanf(“%d %d”, &a, &b);
printf(“\n The sum of %d & %d is %d”, a,b, a+b);
printf(“\n The difference of %d & %d is %d”, a,b, a-b);
printf(“\n The product of %d & %d id %d”, a,b, a*b);
printf(“\n The division of %d & %d is %d”, a,b, a/b);
printf(“\n The reminder of %d & %d is %d”, a,b, a%b);
getch( );
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
int a, b, c;
clrscr( );
printf(“\n Enter a and b values”);
scanf(“%d %d”, &a,&b);
c=a>b;
printf(“%d”,c);
getch( );
}
3. Write a program by using logical operators.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
int m, p, c, d, e;
clrscr( );
printf(“\n Enter the subject marks”);
scanf(“ %d %d %d”, &m, &p, &c);
d=(m>=35 && p>=35 && c>=35);
e=(m>=35 || p>=35 || c>=35);
printf(“\n The output using ‘and’ operator is %d”, d);
printf(“\n The output using ‘or’ operator is %d”, e);
getch( );
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
int a,b,c,d,e;
clrscr( );
printf(“\n Enter the value of ‘a’”);
scanf(“%d”, &a);
b=a++;
c=++a;
d=a--;
e=--a;
printf(“\n The value of b=%d”,b);
printf(“\n The value of c=%d”,c);
printf(“\n The value of d=%d”,d);
printf(“\n The value of e=%d”,e);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“\n Enter any value”);
scanf(“%d”,&n);
(n%2= =0) ? printf(“\n The number is Even”) : (“\n The
number is Odd”);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int s1, s2, s3;
clrscr( );
printf(“\n Enter 3 subjects marks \t:\t”);
scanf(“%d %d %d”, &s1, &s2, &s3);
(s1>=35&&s2>=35&&s3>=35) ? printf(“\n The student
is passed”) : printf(“\n The student is failed”);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int rollno, s1, s2, s3, s4, s5, s6, total;
float avg;
char name[20];
clrscr( );
printf(“\n Enter rollno”);
scanf(“ %d”, &rollno);
printf(“\n Enter name of the student”);
scanf(“ %s”, &name);
printf(“\n Enter subject marks”);
scanf(“ %d %d %d %d %d %d”, &s1, &2, &s3, &s4,
&s5, &s6);
total=s1+s2+s3+s4+s5+s6;
avg=total*100/600;
printf(“\n Rollno=%d”, rollno);
printf(“\n Name=%s”, name);
printf(“\n Total=%d”, total);
printf(“\n Avg=%f”, avg);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int nc;
float const rate=2.50;
float bill;
clrscr( );
printf(“\n Enter nc per month \t:\t”);
scanf(“%d”, &nc);
bill=nc*rate;
printf(“\n The bill of the month for %d is Rs. %f”, bill);
getch( );
}
Conditional statements
If:-
Statement: This statement is used to perform conditional
operations.
Syntax1:
if(condition)
statement1;
If the condition is true then statement1 will be executed.
If the condition is false then it will not execute statement1.
Syntax 2:
if(condition)
{
statement1;
statement2;
}
If the condition is true then statement1 and statement2 will be
executed.
If-else:-
Syntax 1:
if(condition)
statement1;
else
statement2;
#include<stdio.h>
#include<conio.h>
main( )
{
int age;
clrscr( );
printf(“\n Enter the age of the citizen”);
scanf(“%d”, &age);
if(age>18)
{
printf(“\n Eligible for voting”);
}
else
{
printf(“\n Not eligible for voting”);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int year;
clrscr( );
printf(“\n Enter the year”);
scanf(“%d”, &year);
if(year%4= =0);
{
printf(“\n Leap year”);
}
else
{
printf(“\n Non leap year”);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,c;
clrscr( );
printf(“\n Enter any 3 values”);
scanf(“%d %d %d”, &a,&b,&c);
if(a<b&&a<c)
{
printf(“\n %d is the least value”, a);
}
else
if(b<c)
{
printf(“\n %d is the least value”, b);
}
else
{
printf(“\n %d is the least value”, c);
}
getch( );
}
selection
Switch statement
It is used to execute one of the options from no. of
options. It is also called as multi branching statement.
Switch (expression)
{
case value:
statements;
case value:
statements;
default:
statements;
}
Break statement:
It is used to exit from a looping statement. We can use
this in for, while , do while, and switch statement.
Ex:- break;
Switch case
Syntax: switch(expression)
{
case 1:
break;
case 2:
break;
default:
}
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
clrscr( );
printf(“\n Enter any number”);
scanf(“ %d”, &n);
switch(n)
{
case 1:
printf(“\n The name of the day is Sunday”);
break;
case 2:
printf(“\n The name of the day is Monday”);
break;
case 3:
printf(“\n The name of the day is Tuesday”);
break;
case 4:
printf(“\n The name of the day is Wednesday”);
break;
case 5:
printf(“\n The name of the day is Thursday”);
break;
case 6:
printf(“\n The name of the day is Friday”);
break;
case 7:
printf(“\n The name of the day is Saturday”);
break;
default:
printf(“\n Invalid number”);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int a, b;
char op;
printf(“\n enter the operator symbol”);
scanf(“%s”, &op);
printf(“\n enter a & b values”);
scanf(%d %d”, &a,&b);
switch(op)
{
case ‘+’:
printf(“\n The sum of %d and %d is %d”, a,b, a+b);
break;
case ‘-‘:
printf(“\n The difference of %d and %d is %d”, a,b, a-b);
break;
case ‘*’:
printf(“\n The product of %d and %d is %d”, a,b, a*b);
break;
default:
printf(“\n Invalid operator”);
}
getch( );
}
Go to statement
#include<stdio.h>
#include<conio.h>
main( )
{
int i=1;
clrscr( );
orbit:
printf(“%d”, i);
i++;
if(i<=10)
goto orbit;
getch( );
}
Continue statement
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( );
for(i=1;i<=100;i++)
{
if(i%10= =0)
continue;
printf(“%d”,i);
}
getch( );
}
LOOPS
LOOPING:- The process of executing a block of code
repeatedly is called as looping.
There are 3 types of loops in ‘C’ language.
1) For loop
2) While loop
3) Do while loop
All the three loops are used for same process but there syntax is
different.
All the three loops have 3 steps in common.
1) Initialization:- It is the starting point of the loop process.
2) Condition:- In this step logical test is applied. It is true the
block of code is executed. Till the condition fails.
3) Increment / Decrement:- In order to proceed or stop the
loop increment or decrement is required.
For loop
for(;i<=10;i++)
y
for(; i<=10 ; )
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( );
for (i=1;i<=10;i++)
{
printf(“\n ORBIT”);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( );
for(i=100;i<=200;i++)
{
printf(“\n %d”, i);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( );
for(i=10;i<=50;i++)
{
if(i%2= =0)
{
printf(“\n %d”, i);
}
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i;
clrscr( );
for(i=100;i<=200;i++)
{
if(i%2!=0)
{
printf(“%d”, i);
}
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int n,i,r;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &n);
for(i=1;i<=10;i++)
{
r =n*i;
printf(“%d*%d=%d”,n, i, r);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i, fact=1, n;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“\n Factorial of %d=%d”, n, fact);
getch( );
}
While loop
While statement:
It is used to execute a set of statements repeatedly as long
as the given condition is true.
Syntax:
while (condition)
{
statements; //body of while loop
------------;
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i=50;
clrscr( );
while(i<=100)
{
if(i%5 = =0)
printf(“%d”, i);
i++;
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int i, n, rem, sum=0;
clrscr( );
printf(“\n Enter any 3 digit number”);
scanf(“%d”, &n);
while(n>=1)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf(“\n The sum of digits of given number is %d”,
sum);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int n, sum, rev=o, rem;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &n);
while(n>=1)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“\n The reverse of the given number is %d”, rev);
getch( );
}
4) Write a program to test for given number is
Palindrome.
#include<stdio.h>
#include<conio.h>
main( )
{
int n, rem, rev=0, temp;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &n);
temp=n;
while(n>=1)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“\n The reverse of the number is %d”, rev);
if(rev= =temp)
{
printf(“\n The number is a Palindrome number”);
else
{
printf(“\n The number is not a Palindrome number”);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
int n, rem, sum=0, temp;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &n);
temp=n;
while(n>=1)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(sum= =temp)
{
printf(“\n The number is Armstrong number”);
}
else
{
printf(“\n The number is not a Armstrong number”);
}
getch( );
}
Do While Loop
This is similar to while loop, but the difference is this loop will
execute the statements at least once.
#include<stdio.h>
#include<conio.h.
main( )
{
int s1, s2, s3, tot;
char c;
do
{
printf(“\n Enter subject marks”);
sanf(“%d %d %d”, &s1, &s2, &s3);
tot=s1+s2+s3;
printf(“\n Total=%d”, tot);
printf(“\n Do you want to try again”);
scanf(“%c”, &c);
}
while(c= =’y’);
getch( );
}
# include <stdio.h>
# include <conio.h>
void main()
{
int n,i;
clrscr();
printf("\n Enter a number ");
scanf("%d",&n);
i=1;
do
{
printf("\t%d",i);
i=i+1;
}
while(i<=n);
getch();
}
NESTED FOR LOOP
# include <stdio.h>
123
1234
12345
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%3d",j);
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%3d",j);
printf("\n");
}
getch();
}
ARRAYS
An array is contiguous area in the memory referred by a
common name. The array allows the storage of a number of
data values in one variable. Each array element i.e. each
individual data item is referred by specifying the array name
followed by one or more subscripts. Each subscript is enclosed
with square brackets. Each subscript indicates the position of
the element in the array. Arrays are divided into 2 types.
1) Single dimensional arrays. or One dimensional arrays
2) Multi dimensional arrays
#include<stdio.h>
#include<conio.h>
main( )
{
char string[6];
int i;
clrscr( );
printf(“\n Enter any string”);
gets (string);
printf(“\n Reverse of a string”);
for(i=6;i>=0;i--)
{
printf(“%c”, string[i]);
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
char string[10], f;
int i, c=0;
clrscr( );
printf(“\n Enter the string”);
gets(string);
printf(“\n Enter a letter to find in the string”);
scanf(“%c”, &f);
for(i=0;i<=9;i++)
{
if(string[i]= =f)
{
c++;
}
}
printf(“\n The letter %c appears %d times in the %s”, f,
c, string);
getch( );
}
#include<stdio.h>
#include<conio.h>
main( )
{
char city[5]={‘N’,’A’,’N’’D’,’E’,’D’};
int pin[6]={5,0,0,0,2,8};
int i;
crscr( );
for(i=0;i<=5;i++)
{
printf(“%c”, city[i]);
}
printf(“-“);
for(i=0;i<=5;i++)
{
printf(“%d”, pin[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
main( )
{
int month[12]={1,3,5,7,8,10,12,4,6,9,11,2};
int i, mn;
clrscr( );
printf(“\n Enter number of month”);
scanf(“%d”, &mn);
for(i=0;i<=11;i++)
{
if(mn= =month[i])
goto compare;
}
printf(“\n Invalid month”);
exit (1);
compare:
if(“month (%d) contains 28 days”, month[i]);
if(i+1<8)
printf(“month(%d)contains 31 days”, month[i]);
if(i+1>7&&i+1!=12)
printf(“month (%d) contains 30 days”, month[i]);
getch( );
}
Single character
# include <stdio.h>
# include <conio.h>
# include "stringfn.c"
void main( )
{
char name[20],name1[20];
int ch=0;
clrscr( );
while(ch<5)
{
printf("\n 1.STRLEN ");
printf("\n 2.STRCPY ");
printf("\n 3.STRCMP ");
printf("\n 4.STRCAT ");
printf("\n 5.EXIT ");
printf("\n Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter your name ");
scanf("%s",name);
nstrlen(name);
break;
case 2:
printf("\n Enter Source string ");
scanf("%s",&name);
nstrcpy(name,name1);
printf("\n The copied string is %s",name1);
break;
case 3:
printf("\n Enter your first string ");
scanf("%s",name);
printf("\n Enter your second string ");
scanf("%s",name1);
nstrcmp(name,name1);
break;
case 4:
printf("\n Enter your first name ");
scanf("%s",name);
printf("\n Enter your last name ");
scanf("%s",name1);
nstrcat(name,name1);
printf("\n Your full name is %s",name);
}
getch( );
}
}
#include<stdio.h>
#include<conio.h>
#include<string.g>
main( )
{
int length;
char string[20];
clrscr( );
printf(“\n Enter any string”);
gets(string);
length=strlen(string);
printf(“\n The length of the string=%d”, length);
getch( );
}
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main( )
{
char name[20],name1[20];
clrscr( );
printf("\n Enter your string ");
scanf("%s",name);
strcpy(name1,name);
printf("\n The given string is %s",name);
printf("\n The new string is %s",name1);
getch( );
}
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main( )
{
char name[20],name1[20];
clrscr( );
printf("\n Enter your first string ");
scanf("%s",name);
printf("\n Enter your second string ");
scanf("%s",name1);
if(strcmp(name,name1)= =0)
printf("\n Two strings are equal ");
else
printf("\n Two strings are not equal");
getch( );
}
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main( )
{
char name[20];
clrscr( );
printf("\n Enter your first string ");
scanf("%s",name);
printf("\n The given string is %s",name);
strrev(name);
printf("\n The reverse string is %s",name);
getch( );
}
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main( )
{
char name[20];
clrscr( );
printf("\n Enter your string ");
scanf("%s",name);
printf("\n The given string is %s",name);
strupr(name);
printf("\n the string in upper case is %s",name);
strlwr(name);
printf("\n The string in lower case is %s",name);
getch( );
}
7) Write a program to convert uppercase string to lower
case string by using
Ascii code.
#include<stdio.h>
#include<conio.h>
#include<string.g>
main( )
{
char str[30];
int i=0;
clrscr( );
printf(“\n Enter any string”);
gets(str);
while(str[i]!=’\0’)
{
if(str[i]>=’A’&&str[i]<=’Z’)
str[i]=str[i]+32;
i++;
}
printf(“\n Converted given string from uppercase to
lowercase is %S”, str);
getch( );
}
while(name[i]!='\0')
{
printf("%c",toupper(name[i]));
i++;
}
getch( );
}
Functions
Functions
Function definition
Function prototype
Invoking a function
Function definition:-
a) Function heading
i. Return type:- Return is called as data type mention
according to the type of value returned from the
function.
Note:- When there is no return statement we should
mention void as a default return type.
ii. Function name:- Here we mention the name of a
function. It is just like a variable name
iii. Specification of parameters: - specifying the number
of values or variables taken by the called function from
main program.
Note:-The parameters mentioned in the main program
for calling a function are called as actual parameters.
The parameters mentioned in the function which are
received from the main program are called formal
parameters.
Function Prototype
It is declared in the main program about the function.
Note:-Function prototype is optional if the function is
defined before the main. It is compulsory if the function is
defined after the main.
Invoking a Function
From here we call the function. A function as no life when
it is not Invoked.
Uses of functions:
a. Dividing the task into sub tasks.
b. If a particular task is repeated number of times in a
program we can create a function and call that function
wherever we want.
c. Whenever a function is called in a statement the control
will be transferred to that called function. After
executing all statements it returns back to the calling
function.
d. The length of the source program will be reduced by
using functions at appropriate places.
e. A function can be used in other programs also.
Syntax of a function
Return_type function_name (argument list)
{
declarations;
statements;
-------------
return value;
}
Return statement:
It returns a value of the expression to the calling function.
Syntax:- return(expression)
Ex:- return(0);
return(a);
return(a+b);
return a;
return;
#include<stdio.h>
#include<conio.h>
void table (int n)
{
int i,r;
for (i=1;i<=10;i++)
{
r=n*i;
printf(“%d * %d = %d”, n,i,r);
}
}
void main( )
{
int no;
clrscr( );
printf(“\n Enter any number”);
scanf(“ %d”, &no);
table (no);
getch( );
}
#include<stdio.h>
#include<conio.h>
int compare(int a, int b, int c)
{
if(a>b&&a>c)
{
return a;
}
else
if(b>c)
{
return b;
}
else
{
return c;
}
}
void main( )
{
int x, y, z;
int compare(int, int, int);
clrscr ( );
printf (“\n Enter any 3 values”);
scanf (“%d %d %d”, &x, &y, &z);
printf (“\n Highest value is %d”, compare(x,y,z);
getch ( );
}
#include<stdio.h>
#include<conio.h>
void reverse(int n)
{
int rev=0, rem;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“Reverse of the given number = %d”, rev);
{
void main( )
{
int no;
clrscr( );
printf(“\n Enter any number”);
scanf(“%d”, &no);
reverse(no);
getch( );
}
#include<stdio.h>
#include<conio.h>
void display(int b [] )
{
int i;
for(i=0;i<10;i++)
{
printf(“\n %d”, b[i] );
}
}
void main( )
{
int a[10], i;
clrscr( );
printf(“\n Enter 10 values”);
for(i=0;i<10;i++)
{
printf(“\n Enter %d value”, i);
scanf(“%d”, &a[i]);
}
display(a);
getch( );
}
#include<stdio.h>
#include<conio.h>
int total(int s1, in s2, int s3)
int tot;
tot=s1+s2+s3;
return tot;
}
void main( )
{
int i, sno, sub1, sub2, sub3;
char name[20];
clrscr( );
for(i=0;i<=4;i++)
{
printf(“\n Enter student no”);
scanf(“%d”, &sno);
printf(“\n Enter student name”);
scanf(“%s”, &name);
printf(“\n Enter 3 subject marks”);
scanf(“%d %d %d”, &sub1, &sub2, &sub3);
printf(“total = %d”, total(sub1, sub2, sub3));
}
getch( );
}
Recursive Function
POINTERS
Memory map:-
i
10
65524
Call by value
Call by address
struct book
{
int pages;
char author[20];
float price;
}a,b,c;
# include <stdio.h>
# include <conio.h>
void main( )
{
struct student
{
int sno;
char name[20];
int tot;
};
struct student s={1,"Kranthi",500};
clrscr( );
printf("\n the struct values are ");
printf("\n %d %s %d ",s.sno,s.name,s.tot);
getch( );
}
Note:- In the above program student is the name of the
structure. sno,name,tot are the members of the structure. 's'
is the structure variable.
2) Write a program to take the values of structure
elements and display
them back.
#include<stdio.h>
#include<conio.h>
main( )
{
struct employee
{
int emp_id;
char emp_name[20];
float emp_sal;
};
clrscr( );
struct employee e1;
printf(“\n Enter employee id no, employee name &
employee salary”);
scanf(“%d %s %f”, &e1.emp_id, e1.emp_name,
&e1.emp_sal);
printf(“\n Employee Id no = %d”, e1.emp_id);
printf(“\n Employee name = %s”, e1.emp_name);
printf(“\n Employee salary = %f”, e1.emp_sal);
getch( );
}
# include <stdio.h>
# include <conio.h>
void main( )
{
struct item
{
char name[10];
int qty;
int rate;
int amount;
};
struct item a;
clrscr( );
printf("\n Enter item name, qty, rate ");
scanf("%s%d%d",a.name,&a.qty,&a.rate);
a.amount=a.qty*a.rate;
printf("\n The values are");
printf("\n %s %d %d %d",a.name,a.qty,a.rate,a.amount);
getch( );
}
Array of Structures
#include<stdio.h>
#include<conio.h>
Void main( )
{
int i;
struct student
{
int sno;
char sname[20];
};
struct student s[5];
for(i=0;i<5;i++)
{
printf(“Enter student number”);
scanf(“%d”, &s[i].sno);
printf(”Enter student name”);
scanf(“%s”, s[i].sname);
}
clrscr( );
for(i=0;i<5;i++)
{
printf(“Student number is %d \n”, s[i].sno);
printf(“Student name is %s \n”, s[i].sname);
}
getch( );
}
Nested structures
STORAGE CLASSES
# include <stdio.h>
# include <conio.h>
void main( )
{
auto int j, i=10;
clrscr( );
{
auto int i=5;
printf("\n The value of i is %d",i);
}
printf("\n The value of i is %d",i);
printf("\n The value of j is %d",j);
getch( );
}
4)
# include <stdio.h>
# include <conio.h>
int n;
void display( )
{
printf("\n In display function the value of n=%d", n);
}
void main( )
{
clrscr( );
printf("\n The value of n before assignment is %d
",n);
display( );
n=100;
printf("\n The value of n after assignment is %d", n);
display( );
getch( );
}
FILES
A file is a collection of information on a storage device with a
particular file name.
Uses of a file:-
1) Stores the data in the form of a file and we can retrieve it
whenever we require.
2) Using the data from the file in different programs.
File functions:-
1) getc( )
This function is used to read a character from a file.
Syntax :- getc(file pointer);
Ex:- getc(fp);
2) putc( )
This function is used to write a character into a file.
Syntax:- putc(character, file pointer);
Ex:- putc(ch,fp);
The command line arguments are typed by the user and are
delimited by a space. The first argument is always the file name
and contains the program to be executed. The main( ) function
which we have been using up to now without any arguments can
take two arguments as shown below.
void main(int argc, char *argv[ ])
5) fread( ):- This function is used to read the data from a binary
file.
Syntax:- fread(address of struct variable, size of struct
variable , no. of blocks, file pointer);
Ex:- fread(&b,sizeof(b),1,fp);
Graphs
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
# include <dos.h>
void mai0n( )
{
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
while(!kbhit())
{
for(i=10;i<=200;i++)
{
setcolor(random(16));
circle(320,240,i);
delay(50);
}
for(i=200;i>=10;i--)
{
setcolor(random(16));
circle(320,240,i);
delay(50);
}
}
closegraph( );
}
2)
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
#include<dos.h>
void main( )
{
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(4);
settextstyle(7,0,3);
outtextxy(100,200,"HELLO");
bar(200,200,400,400);
floodfill(50,50,RED);
setcolor(RED);
while(!kbhit( ))
{
putpixel(random(640),random(480),random(15));
setcolor(random(16));
arc(random(640),random(480),10,100,150);
delay(100);
}
getch( );
closegraph( );
}
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
# include <dos.h>
void main( )
{
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(BLUE);
settextstyle(6,0,5);
setfillstyle(1,WHITE);
while(!kbhit( ))
{
for(i=640;i>=-450;i-=10)
{
bar(0,390,640,470);
outtextxy(i,400,"Sample flash news");
delay(100);
}
}
closegraph( );
}
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main( )
{
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
ellipse(400,200,0,360,180,70);
setfillstyle(1,BLUE);
fillellipse(100,100,50,30);
getch( );
closegraph( );
}
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
# include <dos.h>
void main( )
{
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(GREEN);
for(i=0;i<=10;i++)
{
cleardevice( );
settextstyle(i,0,10);
outtextxy(50,150,"SISI CMTES");
delay(1000);
}
getch( );
closegraph( );
}
6) Example program for bar3d and fill style.
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
# include <dos.h>
void main( )
{
int gd,gm,i=0,x,y;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<13;i++)
{
setcolor(WHITE);
setfillstyle(i,GREEN);
bar3d(220,140,420,340,50,1);
delay(1000);
}
getch( );
closegraph( );
}
8)
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <stdlib.h>
void main( )
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setfillstyle(1,BLUE);
bar(100,100,105,300);
setfillstyle(1,BROWN);
bar(106,100,180,120);
setfillstyle(1,LIGHTGRAY);
bar(106,121,180,141);
setcolor(BLUE);
circle(144,131,10);
setfillstyle(1,GREEN);
bar(106,142,180,162);
setfillstyle(1,LIGHTBLUE);
bar(90,300,115,320);
setfillstyle(1,CYAN);
bar(80,321,125,341);
setfillstyle(1,BROWN);
bar(70,341,135,361);
getch( );
closegraph( );
}