Académique Documents
Professionnel Documents
Culture Documents
c
/*
C LANGUAGE
INTRODUCTION TO C:
~~~~~~~~~~~~~~~~~~~
C was originally developed in 1970's by Dennis Ritche at Bell
Telephone laborataries (INC). For the purpose of to implementing
the unix operating system. It is an out growth two earlier
language called BCPL & B.
CURRENT USES OF C:
~~~~~~~~~~~~~~~~~~
The C language is used for developing following application,
1.Database system
2.Graphics packages
3.Spreadsheets
4.CAD/CAM application
5.Word processor
6.Office automation
7.Scientiffic & Engineering application
C is an uncontrollable language.It has,
*Graphical package(games)
*Stock information
*Spreadsheet(Excel-Information maintanance)
*CNC Proogram (C language)
We can maintain the information also a file.
Office organisation software computerised,
Scientiffic engineering algorithm programs.
Foxpro--> Database Management System.
Three levls:
1.Low level(0 & 1 binary)
2.High level(keyword as keyword)
3.Medium level(0 & 1 and keyword)
C is medium level language.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
C NOTES
Size
====
Integer
%d
2 bytes
Long integer
%ld
4 bytes
Char
%c
1 byte
Float
%f
4 bytes
Double
%lf
8 bytes
String
%s
DATA TYPE:
~~~~~~~~~~
string(more than alphabets)
character(single alphabet)
integer(number)
long integer(crore values)
float(decimal values)
double(long decimal values)
FORMAT OF C PROGRAM:
~~~~~~~~~~~~~~~~~~~~
//HEADER FILE DECLARATION PART.
#include<stdio.h>
#include<conio.h>
//----------MAIN PROGRAM STARTS-----------void main() \\ function which the operating system will start
{
clrscr(); \\ clear screen
//----------DECLARATION SECTION-----------int a,b,c;
C NOTES
//-----------ASSIGNMENT SECTION-----------printf("Enter the value of a and b:");
scanf("%d%d",&a,&b);
//-----------COMPALICATION SECTION--------c=a+b;
printf("Addtion of two numbers is :%d",c);
//-----------END OF THE PROGRAM----------getch();
}
Meaning:
<stdio.h>--->standard input output header file
This header file contains with printf and scanf functions.
<conio.h>--->console input output header file
This header file contains with clrscr and getch functions.
void main()->This is the function which the operating system will
start
execution each and every program must have main function.
clrscr()---->clear screen
clears the current text window and places the curser in the upper
left hand corner.(at position 1,1)
VARIABLE DECLARATION:
~~~~~~~~~~~~~~~~~~~~~
All variables used in C language must declare. C variable
declarations includes with name of the variable and its data type.
Variables are used to fold given input.
PRINTF FUNCTION:
~~~~~~~~~~~~~~~~
The printf function print
stringsall output screen.
any
set
characters
or
numerics
or
C NOTES
SCANF FUNCTION:
~~~~~~~~~~~~~~~
The scanf function reads values from output buffer and stored it
at supplied address.
&---->address of int a,b,c
a=10
b=10
GETCH() FUNCTION:
~~~~~~~~~~~~~~~~~
This function used to get a single character from the output
screen.
ARITHMETICAL OPERATORS:
~~~~~~~~~~~~~~~~~~~~~~~~
Operator
purpose
+
addtion
*
multiplication
subtraction
divison
module
C NOTES
getch();
}
2
// EXAMPLE FOR MODULE FUNCTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Module means remaining values.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int wd,rd,mon,dy;
printf("Enter the number of days:");
scanf("%d",&dy);
wd=dy/7;
mon=dy/30;
rd=dy%30;
printf("WEEKS:%d \n",wd);
printf("MONTHS:%d \n",mon);
printf("REMAINING DAYS: %d \n",rd);
getch();
}
3
// EXAMPLE PROGRAM FOR AREA OF TRIANGLE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int b,h,area;
printf("Enter the breath:");
scanf("%d",&b);
printf("Enter the height:");
scanf("%d",&h);
area=0.5*b*h;
printf("Area of the triangle:%d",area);
getch();
}
4.EXAMPLE PROGRAM TO KNOW SIZE OF THE DATATYPE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
C NOTES
#include<conio.h>
void main()
{
clrscr();
printf("INTEGER : %d BYTES \n",sizeof(int));
printf("FLOAT : %d BYTES \n",sizeof(float));
printf("DOUBLE : %d BYTES \n",sizeof(double));
printf("CHARACTER : %d BYTES \n",sizeof(char));
printf("CHARACTER : %d BYTES \n",sizeof(char[20]));
getch();
}
CONTROL STRUCTURES:
~~~~~~~~~~~~~~~~~~~
The control structures are used to redirect the sequential flow of
control through the program to desired locations.
IF ELSE CONDITION:
~~~~~~~~~~~~~~~~~~~
syntax for if and else:
~~~~~~~~~~~~~~~~~~~~~~~~
if(condition)
statment 1;
else
statment 2;
In the above structure staement 1 will be executed
conditionis true otherwise statement 2 will be executed.
if
the
C NOTES
scanf("%d%d",&a,&b);
if(a>b)
{
printf("A is greater:%d",a);
}
else
{
printf("B is greater:%d",b);
}
getch();
}
6 // EXAMPLE PROGRAM FOR IF ELSE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[20];
int age,wt;
printf("Enter the name:");
scanf("%s",name);
printf("Enter the age :");
scanf("%d",&age);
if(age>18)
{
printf("%s you are eligible to vote",name);
}
else
{
wt=18-age;
printf("%s you just wait for %d years",name,wt);
}
getch();
}
7
//EXAMPLE PROGRAM FOR IF ELSEIF ELSE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
C NOTES
int a,b,c;
printf("Enter the value for a,b and c:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("A is greater %d",a);
}
else if(b>c && b>a)
{
printf("B is greater %d",b);
}
else
{
printf("C is greater %d",c);
}
getch();
}
8 //EXAMPLE PROGRAM FOR IF ELSEIF ELSE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
else
{
printf("Result:FAIL \n");
}
if(avg>40 && avg<=55) #include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int rn,m1,m2,m3,tot;
float avg;
char name;
printf("Enter your
scanf("%d",&rn);
printf("Enter your
scanf("%s",name);
printf("Enter your
scanf("%d",&m1);
printf("Enter your
scanf("%d",&m2);
printf("Enter your
scanf("%d",&m3);
tot=m1+m2+m3;
avg=tot/3;
C NOTES
printf("Roll number:%d \n",rn);
printf("Name:%s \n",name);
printf("Total:%d\n",tot);
printf("Average:%f\n",avg);
if(m1>=40 && m2>=40 && m3>=40)
{
printf("Result:PASS \n
{
printf("Grade:D");
}
else if(avg>55 && avg<=65)
{
printf("Grade :C");
}
else if(avg>65 && avg<=70)
{
printf("Grade:B");
}
else
{
printf("Grade:A");
}
getch();
}
C NOTES
{
clrscr();
int num;
csc:
printf("Enter any positive no:");
scanf("%d",&num);
if(num>0)
{
printf("Given no is positive:%d",num);
}
else
{
goto csc;
}
getch();
}
10
// EXAMPLE PROGRAM FOR GOTO STATEMENT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num;
char ch;
csc:
printf("Enter any number: \n");
scanf("%d",&num);
if(num>0)
{
printf("The given no is positive \n");
}
else if(num<0)
{
printf("The given no is negative \n");
}
else
{
printf("The given no is zero \n");
}
printf("Do you want to continue [y/n]: \n");
ch=getche();
if(ch=='y'||ch=='Y')
{
goto csc;
10
C NOTES
}
getch();
}
11
C NOTES
switch(op)
{
case'+':
printf("Addition of two nos:%d",a+b);
break;
case'*':
printf("Multiplication of two nos:%d",a*b);
break;
case'-':
printf("Subtraction of two nos:%d",a-b);
break;
case'/':
printf("Divison of two nos:%f",a/b);
break;
default:
printf("Invalid operator");
}}
getch();
12
// EXAMPLE PROGRAM FOR SWITCH CASE STATEMENT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BANKING SYSTEM:
~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int dep,bal=0,wd;
float ci;
char ch;
printf("\t\t\t
STATE BANK OF INDIA\n ");
printf("\t\t\t
~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t\t D----->Deposit \n");
printf("\t\t\t W----->Withdraw \n");
printf("\t\t\t G----->Get balance\n");
printf("\t\t\t C----->Compound interest\n");
printf("\t\t\t Q----->Quit");
top:
printf("\n Enter your option:");
ch=getche();
switch(ch)
{
case'D':
printf("Enter your deposit amount:");
scanf("%d",&dep);
12
C NOTES
bal=bal+dep;
break;
case'W':
printf("Enter your withdraw amount:");
scanf("%d",&wd);
bal=bal-wd;
break;
case'G':
printf("Balance is:%d",bal);
break;
case'C':
ci=float(bal*4/100);
bal=bal+ci;
printf("Compound interest is:%f",ci);
break;
case'Q':
goto end;
default:
printf("Invalid option");
}
if(ch!='Q')
{
goto top;
}
end:
getch();
}
EXAMPLE FOR PRE INCREMENT OPERATOR
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIS TYPE OF OPERATORS INCREMENT (OR) DECREMENT WILL EXECUTE IN
PRESENT COMPILATION.
13. EX :
~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
printf("i = %d\n",i);
printf("i = %d\n",++i);
printf("i = %d\n",i);
getch();
}
13
C NOTES
POST INCREMENT
~~~~~~~~~~~~~~
THIS TYPE OF OPERATORS INCREMENT (OR) DECREMENT WILL NOT EXECUTE
IN
PRESENT COMPILATION.
14.EX:
~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
printf("i = %d\n",i);
printf("i = %d\n",i++);
printf("i = %d\n",i);
getch();
}
FOR LOOP STATEMENTS
~~~~~~~~~~~~~~~~~~~
This control structure is used to repeat a set of instruction
for a finite number till a condition becomes false.
SYNTAX:
~~~~~~~
for(<loop counter>=<start value>;<final condition>;<increment
expression>)
15 // EXAMPLE PROGRAM FOR FOR LOOPING STATEMENTS:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SUM 1 TO N NUMBERS:(1+2+3+..........+N)
~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int s=0,n;
printf("Enter the value for n:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
s=s+i;
}
14
C NOTES
printf("Sum of given numbers is :%d",s);
getch();
}
LOOPING STATEMENTS:
~~~~~~~~~~~~~~~~~~~
The looping statements are used to perform any operation
repeatedly.
WHILE LOOP:
~~~~~~~~~~~
SYNTAX:
~~~~~~~
while<condition>
statement;
DO.........WHILE LOOP:
~~~~~~~~~~~~~~~~~~~~~~~
SYNTAX:
~~~~~~~
do
statement;
while(condition);
16
// EXAMPLE PROGRAM FOR USING WHILE STATEMENT
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
C NOTES
TO FIND FACTORIAL VALUES:
~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int s=1,n,i=1;
clrscr();
printf("Enter the value for n:");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
}
printf("Factorial of given no is :%d",s);
getch();
}
17.BREAK STATEMENT WITH IN A LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int x=1;
clrscr();
while(x<=10)
{
printf("X =%d \n",x);
if(x==5)
break;
x++;
}
getch();
}
18
// EXAMPLE PROGRAM OF USING WHILE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MULTIPLICATION TABLE:
~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int tn,nr,i=1;
16
C NOTES
clrscr();
printf("Enter the table name:");
scanf("%d",&tn);
printf("Enter the number of rows:");
scanf("%d",&nr);
while(i<=nr)
{
printf("%d*%d=%d \n",i,tn,i*tn);
i++;
}
getch();
}
19 // EXAMPLE PROGRAM TO FIND ODD OR EVEN NUMBERS USING WHILE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int num,tmp=0,i=1;
clrscr();
printf("Enter any no:");
scanf("%d",&num);
while(i<=num)
{
if(num%i==0)
{
tmp=tmp+1;
}
i++;
}
if(tmp==2)
{
printf("Given number is odd");
}
else
{
printf("Given number is even");
}
getch();
}
20
// TO FIND THE POWER VALUE USING WHILE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
17
C NOTES
{
int bv,pv,i=1,tmp=1;
clrscr();
printf("Enter the base value:");
scanf("%d",&bv);
printf("Enter the power value:");
scanf("%d",&pv);
while(i<=pv)
{
tmp=tmp*bv;
i++;
}
printf("Power value:%d",tmp);
getch();
}
21 // EXAMPLE PROGRAM FOR USING FOR LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NESTED FOR LOOP:
~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("\t %d",i);
}
printf("\n");
}
getch();
}
22 // EXAMPLE PROGRAM FOR USING FOR LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NESTED FOR LOOP:
~~~~~~~~~~~~~~~~
#include<stdio.h>
18
C NOTES
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("\t %d",j);
}
printf("\n");
}
getch();
}
23 // PROGRAM FOR STUDENTS MARK LIST USING FOR LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3,tot,rn,ns,i;
float avg;
char name;
clrscr();
printf("Enter the number of students:");
scanf("%d",&ns);
for(i=1;i<=ns;i++)
{
printf("Enter the student roll number:");
scanf("%d",&rn);
printf("Enter the student name:");
scanf("%s",name);
printf("Enter the mark n1:");
scanf("%d",&n1);
printf("Enter the mark n2:");
scanf("%d",&n2);
printf("Enter the mark n3:");
scanf("%d",&n3);
tot=n1+n2+n3;
avg=tot/3;
printf("Total mark is:%d \n",tot);
printf("Average mark is:%f \n",avg);
}
19
C NOTES
getch();
}
24 // EXAMPLE PROGRAM FOR DO......WHILE LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i=5;
clrscr();
printf("Enter the value for a:");
scanf("%d",&a);
do
printf("A value is :%d",a);
while(i<=a);
getch();
}
ARRAYS
~~~~~~
An array is a collection of data elements of similar type.
By the usage of arrays many elements can be accessed with a single
name.
In the memory of the array elements are stored in consequent
memory
locations.
The individual elements of the array can be accessed using the
position of the element in the array.This position is called as
subscript or index.
WE HAVE TWO TYPES ARRAYS,
=========================
1.SINGLE DIMENSION ARRAY.
2.MULTI DIMENSION ARRAY.
DECLARATION OF AN ARRAY:
~~~~~~~~~~~~~~~~~~~~~~~~
20
C NOTES
data type <variable name>[size 1][size 2]............[size n]>
EXAMPLE:
~~~~~~~~~~
int mark[10];
int mark[10][5];
int mark[10][7][9];
The subscripts of the elements ranges 0 to -1.
We can access the individual elements as
mark[5];
mark[6][20];
mark[20][30][50];
For example mark[5]
72------->mark[0]
52------->mark[1]
65------->mark[2]
85------->mark[3]
97------->mark[4]
25 // EXAMPLE PROGRAM FOR SINGLE DIMENSION ARRAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],sum=0,n;
printf("Enter number of elements:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
sum=sum+a[i];
}
printf("Sum of given number is :%d",sum);
getch();
}
26 // PROGRAM TO FIND MAXIMUM NUMBER FROM GIVEN ELEMENTS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
21
C NOTES
{
clrscr();
int a[5],n,sum,max=0;
printf("Enter the number of elements:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("Maximum number is :%d",max);
getch();
}
27 // PROGRAM TO FIND THE PARTICULAR ELEMENT IN THE ARRAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],sum,n,cnt=0;
printf("Enter the number of elements:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to find:");
scanf("%d",&sum);
for(i=1;i<=n;i++)
{
if(a[i]==sum)
cnt=cnt+1;
}
printf("%d presented array %d times",sum,cnt);
getch();
}
22
C NOTES
28 // PROGRAM TO FIND EVEN NUMBERS IN THE ARRAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],sum,n,cnt=0;
printf("Enter the number of elements:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
if(a[i]%2==0)
cnt=cnt+1;
}
printf("%d even numbers presented in array",cnt);
getch();
}
29 //
PROGRAM TO USING TWO DIMENSION ARRAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num;
char
day[7]
[15]={"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY",
"SATURDAY"};
printf("Enter the week day:");
scanf("%d",&num);
printf("Week day is:%s",day[num-1]);
getch();
}
30.EXAMPLE PROGRAM FOR MULTIDIMENSION ARRAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
23
C NOTES
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],row,col,i,j;
printf("Enter no of columns :\n");
scanf("%d",&col);
printf("Enter Number Of Rows :\n");
scanf("%d",&row);
printf("Enter The First Matrix :\n");
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
scanf("%d",&a[i][j]);
printf("Enter Thw Second Matrix :\n");
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
scanf("%d",&b[i][j]);
printf("Addition is :\n");
for(i=1;i<=row;i++)
for(j=1;j<=col;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
DEFINE FUNCTION: (Directive)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Defines a macro.
SYNTAX:
~~~~~~~
#define<id 1>[(<id2>............)]
Macros provide a mechanism for token replacement with or without
a set of formal,functionline parameters.
31 // EXAMPLE PROGRAM FOR #DEFINE FUNCTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
24
C NOTES
#include<conio.h>
#define pf printf
#define sf scanf
void main()
{
clrscr();
char name[20];
pf("Enter your name:");
sf("%s",name);
pf("Hai %s Have a nice day",name);
getch();
}
#include(directive)
~~~~~~~~~~~~~~~~~~~~
Treats text in the file specified file name as if it is appeared
in
the current file.
SYNTAX:
~~~~~~~
#include"file name"
32 // PROGRAM FOR #include"file name" FUNCTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
[save this file. ex:JAM.C]
[open new file]
[type the following]
#include"jam.c"
printf("Hello! How are you?");
getch();
}
STRING FUNCTIONS
~~~~~~~~~~~~~~~~~
1.strlen:(string length)
~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the number of characters in string not counting the
null character.
For string programs we have add the header file
25
C NOTES
#include<string.h>
33 // PROGRAM FOR STRLEN FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[15];
printf("Enter your name:");
scanf("%s",name);
ln=strlen(name);
printf("Length of given string is:%d",ln);
getch();
}
2.strcpy functions: (string copy)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copies the string src to dest stopping after the terminating
null character has been moved.
34 // PROGRAM FOR STRCPY FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[15],name1[15];
printf("Enter your name:");
scanf("%s",name);
strcpy(name1,name);
printf("Given string is :%s",name1);
getch();
}
3.strcmp function: (string comparison)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strcmp performs an unsigned comparison of string1 to string2
26
C NOTES
strcmp is the for version.
35 // PROGRAM FOR STRCMP FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[15],name1[15];
printf("Enter the first name:");
scanf("%s",name);
printf("Enter the second name:");
scanf("%s",name1);
if (strcmp(name,name1)==0)
{
printf("Both names are same");
}
else
{
printf("Both names are different");
}
getch();
}
4.strrev functions:(string reverse)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Strrev function changes all characters in a string to reverse
order except the terminating null character.
36 // PROGRAM FOR STRREV FUNCTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[20];
printf("Enter your name:");
scanf("%s",name);
strrev(name);
27
C NOTES
printf("Reverse string is:%s",name);
getch();
}
5.gets function[GET STRING WITH SPACE]:
~~~~~~~~~~~~~~~~~
Gets collects a string of characters terminated by a new line
from the standard input stream stdin.
37 //PROGRAM FOR GETS FUNCTION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[20];
printf("Enter the name:");
gets(name);
printf("Name=%s",name);
getch();
}
6.strcat functions: (string concardination)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strcat appends a copy of src to the end of the dest.The length
of the resulting string is strlen(dest)+str(src).
38 // PROGRAM FOR STRCAT FUNCTIONS:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[20];
printf("Enter your name:");
gets(name);
strcat(name," COMPUTER");
printf("name=%s",name);
getch();
}
28
C NOTES
39 // PROGRAM FOR PALINDROME USING STRCPY,STRREV,STRCMP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int ln;
char name[20],name1[20];
printf("Enter the name:");
scanf("%s",name);
strcpy(name1,name);
strrev(name);
if(strcmp(name,name1)==0)
{
printf("It is palindrome");
}
else
{
printf("It is not a palindrome");
}
getch();
}
PRINTING ASCII.
~~~~~~~~~~~~~~~
EX:40
~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch='a';
printf("%c ascii %d",ch,ch);
ch++;
printf("\n%c ascii %d",ch,ch);
getch();
}
EX:41
~~~~~
A TO Z ASCII.
29
C NOTES
~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
char ch='a';
for(int i=1;i<=26;i++)
{
printf("\n%c ascii %d",ch,ch);
ch++;
if (i==22)
{
delay(4000);//getch();
}
}
getch();
}
FUNCTIONS
~~~~~~~~~
A function is self contained program units which performs a
speciffic task.If the task has to be a performed repeatedly
many number of times of we need not repeat the same code many
number of times.
We can write the code inside a function and we can call the
function from the main program any number of times with different
input values based on the input different results will be
returned by the function to the main program.
For example cosider the formula,
ncr=n!/r!*(n-r)!
Here the calculation for the factorial of a number should be
performed three times with different values.Instead of repeating
the code we can use a function to find factorial and we can call
the function three times with different inputs.
The input given to function is called a parametre or arguement.
30
C NOTES
There can be any number of function in a program.
The program starts its run time from the main function.
There are four types of functions we have,
1.
2.
3.
4.
function
function
function
function
31
C NOTES
printf("Enter the value of a and b:");
scanf("%d%d",&a,&b);
action(a,b);
getch();
}
void action(int k,int l)
{
int c;
c=k+l;
printf("Addition is:%d",c);
getch();
}
MEANING FOR VOID:
~~~~~~~~~~~~~~~~~
[When used as a function return type void means that the function
does not return a value.]
44 // 3.FUNCTION WITH ARGUEMENT AND WITH RETURN VALUE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
action(int a,int b);
void main()
{
clrscr();
int a,b,c;
printf("Enter the value for a and b:");
scanf("%d%d",&a,&b);
c=action(a,b);
printf("Addition is:%d",c);
getch();
}
action(int a,int b)
{
int d;
d=a+b;
return(d);
}
45 // 4.FUNCTION WITHOUT ARGUEMENT AND WITH RETURN VALUE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
32
C NOTES
action();
void main()
{
clrscr();
int c;
c=action();
printf("Addition is %d",c);
getch();
}
action()
{
int a,b,d;
printf("Enter the value for a and b:");
scanf("%d%d",&a,&b);
d=a+b;
return(d);
}
STORAGE CLASSES
~~~~~~~~~~~~~~~
1. EXTERNAL
2. STATIC
3. REGISTER
1. EXTERNAL VARIABLES
~~~~~~~~~~~~~~~~~~~~~
THESE VARIABLES ARE DECLARED OUTSIDE A FUNCTION. THE SCOPE OF
THESE
VARIABLES IS IN ALL THE FUNCTIONS WHICH ARE AVAILABLE IN THE
PROGRAM.
THESE VARIABLES ARE DECLARED WITH THE KEYWORD "extern".
46.EX
~~~~~~
#include<stdio.h>
#include<conio.h>
void disp();
void main()
{
extern int a;
clrscr();
a=10;
disp();
getch();
}
33
C NOTES
int a;
void disp()
{
printf("The Value of A = %d\n",a);
//"a" IS GLOBAL AND CAN BE ACCESSED FROM ALL FUNCTIONS
}
2. STATIC VARIABLES
~~~~~~~~~~~~~~~~~~~
THE SCOPE OF THESE VARIABLES ALSO LOSTS WITHIN THE FUNCTION WITHIN
WHICH THEY ARE DECLARED.
THE STATIC
ITSELF.
VARIABLES
ARE
INITIALISED
TO
WHILE
THE
DECLARATION
VALUE
WHICH
34
IS
C NOTES
3. REGISTER VARIABLES
~~~~~~~~~~~~~~~~~~~~~
THESE VARIABLES ARE ALSO SIMILAR TO AUTOMATIC VARIABLES.
THEY ARE DECLARED WITH THE STORAGE CLASS "register".
NORMALLY IF WE DECLARE A VARIABLE IT WILL BE STORED IN THE RAM
AREA OF
MEMORY. FOR PERFORMING ANY OPERATION THE CPU CANNOT OPERATE
DIRECTLY
ON THESE MEMORY LOCATIONS.
THE CPUHAS INTERNAL MEMORY LOCATIONS CALLED AS REGISTERS.
ANY OPERATION CAN BE PERFORMED ONLY WITH THE VALUES IN THE
REGISTERS.
SO FOR OPERATING A VARIABLE IN MEMORY IT HAS TO BE MOVED TO THE
CPU REGISTER AND THEN IT HAS TO BE OPERATED AND THEN THE
CALCULATED
RESULTS MUST BE STORED AGAIN TO MEMORY.
THE TIME TAKEN FOR THESE MEMORY TO REGISTER TRANSACTION MAKES
DELAY IN
PROGRAM RUNNING SPEED.
IF WE DECLARE A VARIABLE WITH "register" STORAGE CLASS THEN
THOSE VARIABLES WILL BE DIRECTLY STORED IN THE REGISTERS. SO TIME
TAKEN FOR
TRANSACTION IS AVOIDED AND PROGRAM RUNNING SPEED
INCREASES.
48.EX
~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
register int a;
clrscr();
a=10;
getch();
printf("A value = %d\n",a);
getch();
}
STRUCTURE
~~~~~~~~~
A structure is a collection of related variables which may be of
35
C NOTES
different data types.
Structure is a user defined data type.
Structures help to organize complicated data,particularly in
large programs becaus they permit group of related variables to be
treated as a unit instead to seprate entities.
DECLARATION OF DATA TYPES:
~~~~~~~~~~~~~~~~~~~~~~~~~~
struct struct name
{
<data type 1> <variable 1>;
<data type 2> <variable 2>;
..........................
..........................
..........................
<data type n> <variable n>;
};
DECLARING STRUCTURE VARIABLE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct <tag name> <variable name>;
struct emp
{
int emp no;
char name[25];
float sal;
}
worker 1;
struct emp worker 2;
ACCESSING STRUCTURE ELEMENTS:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<structure variable name> <member name>
worker 1 emp no=10;
worker 1 sal=5000.55;
worker 2 emp no=11;
worker 2 sal=2555.40;
DIFFERENCE BETWEEN ARRAYS AND STRUCTURES:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ARRAYS:
STRUCTURES:
~~~~~~~
~~~~~~~~~~~
int a[5];
struct s[5]
36
C NOTES
36---------->a[1]
25---------->a[2]
565--------->a[3]
7987-------->a[4]
1210-------->a[5]
h----------->s[1]
52---------->s[2]
5465358----->s[3]
latha------->s[4]
454.5425---->s[5]
37
C NOTES
storage area.
the size of a union is the size of the largest pf the elements
contain
in it.a union is defined as
union union type
{
member_type1 membername1;
member_type1 membername2;
member_type1 membername3;
};
50.EX:
~~~~~~
#include<stdio.h>
#include<conio.h>
union stud
{
int id;
double height;
long age;
};
void main()
{
union stud st;
clrscr();
printf("Enter Id No : ");
scanf("%d",&st.id);
printf("Enter Height :");
scanf("%lf",&st.height);
printf("\nEnter Age: ");
scanf("%d",&st.age);
printf(" Id Number: %d",st.id);
printf("\n Height= %lf\n",st.height);
printf("\n Age = %ld\n",st.age);
getch();
}
51.PASSING STRUCTURES TO FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
struct record
{
38
C NOTES
char *name;
int acct_no;
char acct_type;
float balance;
};
struct record adjust(struct record);
void main()
{
clrscr();
struct record customer={"sanjay",3333,'c',33.33};
printf("%s
%d
%c
%.2f\n",customer.name,customer.acct_no,customer.acct_type,customer
.balance);
customer=adjust(customer);
printf("%s
%d
%c
%.2f\n",customer.name,customer.acct_no,customer.acct_type,customer
.balance);
getch();
}
struct record adjust(struct record cust)
{
cust.name="jones";
cust.acct_no=9999;
cust.acct_type='r';
cust.balance=99.99;
return(cust);
}
52. EXAMPLE PROGRAM TO ACCESS SYSTEM TIME AND DATE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<time.h>
#include<dos.h>
void main()
{
clrscr();
struct time t;
struct date d;
gettime(&t);
getdate(&d);
printf("\nSystem time :%d : %d %d",t.ti_hour,t.ti_min,t.ti_sec);
Printf("\nSystem Date :%d : %d :%d",d.da_day,d.da_mon,d.da_year);
getch();
39
C NOTES
}
FILE HANDLING (TO SAVE THE OUTPUT RESULTS)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So far in all the sample programs we have considered the data was
return
to the standard output and data was read from the standard
input.As long
as only small amount of data are being accessed in the form of
simple
variables and characters strings this type of input and output is
sufficient however with large amount of data.
The information has to be written to or read from auxillary
storage
device. Such information is stored on the devices in the form of a
data
files.
There are two different catagories of data files.
1. Stream oriented data files.
2. Binary files.
40
C NOTES
This allows access to a file and establishes the position offset
in the file.
CLOSE:
~~~~~~
This ends access to the file when access to a file completes it
should be closed. The number of files that a running program can
have at any time is limited by closing file properly these limited
facilities can be used more intelligently.
READ:
~~~~~
This gets information from the file either in the form of
characters,
strings
or
integer
the
form
of
data.
( combined integer, floating, point numbers and structures)
WRITE:
~~~~~~
This adds information to the file or replace information already
in the file.
FILE OPENING (MODES):
Modes-------->for specification
~~~~~~~~~~~~~~~~~~~~~
r ------------------------------------>opened for reading
Position at the begining of the file.
w ------------------------------------>opened for writing
The assumption is that the file is to be created. In the file does
not exist. It is truncated position at the beginning at the file.
a------------------------------------>opened for appending
Same rules apply as for mode except that initial positioning is at
the
end of the file.
r+----------------------------------->opened for update
File is available for reading and writing but it
truncated.The
initial positioning is at the start of the file.
is
not
41
C NOTES
a+----------------------------------->opened for update
File is available for reading and writing is not truncated and
initial
positioning is at the end of the file.
53
// EXAMPLE PROGRAM FOR WRITING MODE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;
char *name;
int age,sal;
fp=fopen("Quize.txt","w");
printf("Enter the name:");
scanf("%s",name);
printf("Enter the age:");
scanf("%d",&age);
printf("Enter the salary:");
scanf("%d",&sal);
fprintf(fp,"%s %d %d",name,age,sal);
printf("Successfully stored");
getch();
}
54
// EXAMPLE PROGRAM FOR APPEND MODE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;
char *name;
int age,sal;
fp=fopen("Quize.txt","a");
printf("Enter the name:");
scanf("%s",&name);
printf("Enter the age:");
scanf("%d",&age);
printf("Enter the salary:");
scanf("%d",&sal);
42
C NOTES
fprintf(fp,"%s %d %d",name,age,sal);
printf("Successfully stored");
getch();
}
55
// EXAMPLE PROGRAM FOR READ MODE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;
char *name;
int age,sal;
fp=fopen("Quize.txt","r");
printf("Name Age Salary \n");
while(fscanf(fp,"%s %d %d",name,&age,&sal)!=EOF)
{
printf("%s %d %d",name,age,sal);
printf("\n");
}
getch();
}
56
// EXAMPLE PROGRAM FOR ERROR HANDLING IN READ MODE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;
char *name;
int age,sal;
fp=fopen("Quize.txt","r");
if(fp==NULL)
{
printf("Invalid file name");
}
else
{
printf("Name Age Salary \n");
while(fscanf(fp,"%s %d %d",name,&age,&sal)!=EOF)
{
43
C NOTES
printf("%s %d %d",name,age,sal);
printf("\n");
}
}
getch();
}
57 // EXAMPLE FOR BINARY FILE HANDALING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void add();
struct staff
{
int id;
char name[25];
int age;
long int sal;
}sf;
void main()
{
clrscr();
add();
getch();
}
void add()
{
int id;
char ch,c;
FILE *fp;
top:
fp=fopen("staff.txt","ab+");
printf("Enter the staff id number:");
scanf("%d",&sf.id);
printf("Enter the staff name:");
scanf("%s",sf.name);
printf("Enter the staff age:");
scanf("%d",&sf.age);
printf("Enter the staff salary:");
scanf("%ld",&sf.sal);
fwrite(&sf,sizeof(sf),1,fp);
printf("Successfully stored");
fclose(fp);
printf("\nDo you want to continue [y/n]:");
ch=getche();
if(ch=='y'||ch=='Y')
44
C NOTES
goto top;
}
58 // EXAMPLE PROGRAM IN READ MODE(a+) TO READ RECORD WISE
..~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void disp();
struct staff
{
int id;
char name[25];
int age;
long int sal;
}sf;
void main()
{
clrscr();
disp();
getch();
}
void disp()
{
FILE *fp;
int id1,c=0;
fp=fopen("staff.txt","ab+");
printf("Enter the staff id number:");
scanf("%d",&id1);
while(fread(&sf,sizeof(sf),1,fp)==1)
{
if(id1==sf.id)
{
printf("\n Staff name:%s",sf.name);
printf("\n Staff age:%d",sf.age);
printf("\n Staff salary:%ld",sf.sal);
c=1;
}
}
if(c==0)
{
printf("Record is not found");
}
}
45
C NOTES
59 // EXAMPLE PROGRAM IN READ MODE(r+) TO DELETE RECORDWISE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<conio.h>
void del();
struct staff
{
int id;
char name[25];
int age;
long int sal;
}sf;
void main()
{
clrscr();
del();
getch();
}
void del()
{
FILE *fp,*ft;
int id2,c=0;
char ch;
printf("Enter the id number to delete:");
scanf("%d",&id2);
fp=fopen("staff.txt","rb+");
ft=fopen("Temp.txt","ab+");
rewind(fp);
// [rewind keyword is point the
position]
while(fread(&sf,sizeof(sf),1,fp)==1)
{
if(sf.id!=id2)
{
fwrite(&sf,sizeof(sf),1,ft);
}
else
{
c++;
}
}
fclose(fp);
fclose(ft);
remove("staff.txt");
rename("Temp.txt","staff.txt");
printf("Successfully deleted");
}
46
C NOTES
----------------------------
End
--------------------
47