C Language
C Language
C
Developed
by
Dennis Ritchie
Directly
Interact with the
Hardware devices
Developed
in 1972
American
Telephone &
Bell laboratories Telegraph
Of AT & T
simple
Machine Independent or Portable Mid-level Programming Language
Rich Library C
C
Recursion
Memory Management
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
C Installation
void main()
{
printf(“FortuneCloud”);
}
Statement
int a; int 2;
int _ab; int a b;
int a30; int long;
Local Global
Static
Automatic External
void display()
{
int a=10; //local variable
}
void display()
{ Declared with the
int a=10; //local variable static keyword
static int b=10; //static variable
a=a+1;
b=b+1;
printf("\n%d,%d",a,b);
} Output:
Output:
void main()
{ All variables in C that are
11,11
int a=10; //local variable (also automatic) declared inside the
11,12
block, are automatic
11,13
auto int b=20; // automatic variable variables by default.
}
#include<stdio.h>
#include<conio.h> Also known as
Output:global variable
extern int e=50; // external variable
11,11
Defined outside
void main() 11,12
the function
{ 11,13
printf(“%d”,e);
} They are
everywhere in
the program
void
array enum
int
pointer
char
structure
float
union
double
int
char
e stands for Exponent
float
double
1 byte 8 byte
2.225073858507201
−128 to 127 4 E – 308 to
1.797693134862315
8 E + 308
Control
Statements
for
Loops while
do-while
statement or statements to be
executed if the condition is
determined to be true
for
while
do-while
if(expression)
{
//code to be executed check some given
} condition and
perform some
operations
depending upon the
if(expression) correctness of that
{ condition
//code to be executed
}
else
{
//code to be executed if condition is false
}
int num=10;
if(num%2==0)
{
printf(“Even Number”);
}
else
{
printf(“Odd Number”);
}
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true Multiple
} cases to be
else if(condition3) performed
{ for different
//code to be executed if condition3 is true conditions
}
Else
{
//code to be executed if all the conditions are false
}
int day=1;
if(day==1)
{
printf(“Sunday”);
}
else if(day==2)
{
printf(“Monday”);
}
…….
else
{
printf(“Day not found”);
}
switch(expression)
{
case value1:
//code to be executed;
break;
Alternate to if-else-if
case value2: statement which
//code to be executed; allows us to execute
break; multiple operations
......
default:
code to be executed if all cases are not matched;
}
int choice=1;
switch(choice)
{
case 1:
printf(“Case 1 executed”);
break;
case 2:
printf(“Case 2 executed”);
break;
default:
printf(“Wrong choice”);
}
int i;
for(i=1;i<=10;i++)
{
printf(“%d”,i);
}
Mostly used
while(condition) in the case
where the
{
number of
//code to be executed iterations is
} not known in
advance.
int i=1;
while(i<=10)
{
printf(“%d”,i);
i++;
}
int i=1;
do
{
printf(“%d”,i);
i++;
} while(i<=10);
do-while loop is
executed for first time
While loop is executed irrespective of the
only when given condition. After
condition is true. executing while loop
for first time, then
Condition is checked first condition is checked.
then statement(s) is
executed. Statement(s) is
executed atleast once,
while loop is entry thereafter condition is
controlled loop. checked.
Advantages
We can track a large C program easily when it is divided into multiple functions.
Function
Library Function
User-defined
printf(), scanf(), etc. Function
void main()
{
printf(“%d”,addition(10,20));
}
return 0;
}
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
Call by reference
return 0;
}
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
Arrays
store the primitive type of data such as int, char, double, float, etc.
each data element can be randomly accessed by using its index number.
The lowest address corresponds to the first element and the highest address to the last
element.
Multi-dimensional array
int num[3];
num[0]=10;//initialization of array
num[1]=20;
num[2]=30;
printf(“%d”,num[0]);
printf(“%d”,num[1]);
printf(“%d”,num[2]);
OR
int i;
for(i=0;i<3;i++)
{
printf("%d ",num[i]);
}
int num[2][2]={{1,2},{3,4}};
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",num[i][j]);
}
}
int num[2][2][2];
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf("%d ",num[i][j][k]);
}
}
}
int num[5];
int i,sum=0;
for(i=0;i<5;i++)
{
scanf(“%d”,&num[i]);
}
for(i=0;i<5;i++)
{
sum=sum+num[i];
}
printf(“Sum=%d”,sum);
The character array or the string is used to manipulate text such as word or sentences.
strlen(ch1) strrev(ch1)
strlen strrev
strcpy(ch3,ch1) strlwr(ch1)
strcpy strlwr
char str[100];
int i=0, count=0;
printf(“Enter a string\n");
gets(str);
struct employee
{ int id; struct employee
char name[20]; { int id;
float salary; char name[20];
}; float salary;
}e1,e2;
struct employee
{ int id;
char name[20];
}e1;
int main( )
{
e1.id=101;
strcpy(e1.name, “abc");
int main()
{
struct student s[5]; printf("\nStudent Information\n");
int i;
for(i=0;i<5;i++){
for(i=0;i<5;i++){ printf("\nId:%d, Name:%s",s[i].id,s[i].name);
printf("\nEnter Id:"); }
scanf("%d",&s[i].id); return 0;
printf("\nEnter Name:"); }
scanf("%s",&s[i].name);
}
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
Union
It occupies less memory because it occupies the size of the largest member only.
union employee
{ int id;
char name[20];
}e1; id gets garbage
value because
void main() name has large
{ memory size
e1.id=101;
strcpy(e1.name, “abcd");
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.
Example
Returns the
address of a
variable
int num=10;
we need to use
%u to display the
address of a
variable.
A pointer that is not assigned any value but NULL is known as the NULL pointer.
int *ptr=NULL;
if(ptr!=NULL)
{
printf("value of ptr is : %d",*ptr);
}
else
{
printf(“Null pointer");
}
int a=10,b=20,c;
int *p,*q;
p=&a;
q=&b;
c=*p+*q;
printf(“addition: %d",c);
int a=10;
It determines the
printf(“%d”,sizeof(a)); size of the
expression or the
//sizeof(int); data type
//sizeof(char);
//sizeof(float);
int main()
{
int a,b;
int (*p)(int,int);
int add(int a, int b)
{
int result;
int c;
printf("Enter a and b : ");
c=a+b;
scanf("%d%d",&a,&b);
return c;
}
p=add;
result=(*p)(a,b);
printf(“Addition: %d",result);
return 0;
}
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
File Handling
File handling in C enables us to create, update, read, and delete the files stored on the
local file system through our C program.
Operations
w Opens a text file for writing. If it does not exist, then a new file is created.
a
Opens a text file for writing in appending mode. If it does not exist, then a new file is
created.
Opens a text file for both reading and writing. It first truncates the file to zero length
W+
if it exists, otherwise creates a file if it does not exist.
Opens a text file for both reading and writing. It creates the file if it does not exist.
a+
The reading will start from the beginning but writing can only be appended.
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
fopen rewind
Functions for
fprintf ftell
file handling
fscanf fgetw
fputc fputw
fgetc fseek
fclose
FILE *fp;
fp = fopen("file.txt", "w");//opening file used to
write set of
fprintf(fp, "Hello file by fprintf...\n");//writing data into file characters
into file
fclose(fp);//closing file
FILE *fp;
char buff[255];//creating char array to store data of file
used to
fp = fopen("file.txt", "r");
read set of
characters while(fscanf(fp, "%s", buff)!=EOF)
from file {
printf("%s ", buff );
}
fclose(fp);
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
fputc() and fgetc()
FILE *fp;
fp = fopen("file.txt", "w");//opening file used to write
a single
fputc('a',fp);//writing single character into file character
into file
fclose(fp);//closing file
FILE *fp;
char c;
returns a
single fp=fopen("file.txt","r");
character
from the file while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
©2021, FORTUNE CLOUD TECHNOLOGIES GROUP
fputs() and fgets()
FILE *fp;
FILE *fp;
char text[300];
reads a line of
characters fp=fopen("myfile.txt","r");
from file printf("%s",fgets(text,200,fp));
fclose(fp);
FILE *fp;
fp=fopen("file.txt","r");
sets the file
pointer at the while((c=fgetc(fp))!=EOF){
beginning of printf("%c",c);
the stream }
fclose(fp);
FILE *fp;
int length; returns the
current file
fp = fopen("file.txt", "r"); position of the
fseek(fp, 0, SEEK_END); specified
stream
length = ftell(fp);
get the total
fclose(fp); size of a file
after moving
printf("Size of file: %d bytes", length); file pointer at
the end of file.