Final C Language
Final C Language
com
Contact:9030202723
−Why C?
Easy to learn
Structured language
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms
Why Use C?
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Structure of C language
B. GIobaI DecIaration
This section declares some variables that are used in more than one
function. These variables are known as global variables. This section must
be declared outside of all the functions.
C. Function part
Every program written in C language must contain main () function. The
function main() is a starting point of every C program.The execution of
the program always begins with the function main().
D. DecIaration Part
The declaration part declares the entire variables that are used in
executable part. The initialisations of variables are also done in this
section. Initialisation means providing initial value to the variables.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
E. ExecutabIe Part
This part contains the statements following the declaration of the
variables.This part conatins a set of statements or a single statement.
These statements are enclosed between the braces.
DELIMITERS
Delimiters Use
Basic programs
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
#include<stdio.h>
#include<conio.h>
void main()
{
Printf(“Welcome to Prsoftwares”);
getch();
}
Output: - welcome to prsoftwares.
If you find any error rectify it and compile the program again.
Keywords
Keywords are preserved words that have special meaning in There are
total 32 keywords in C language.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Do if Static while
Character Set
In C language characters are grouped into the following
categories,
Class-2:-
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Identifiers
In C language identifiers are the names given to variables,
constants, functions and user-define data. These identifier are defined
against a set of rules.
Operators in C Language
C language supports a rich set of built-in operators. An operator
is a symbol that tells the compiler to perform certain mathematical or logical
manipulations. Operators are used in program to manipulate data and
variables.
Arithmetic operators- { +, - , * , /, %}
Relation operators- {<,<=,>,>=,==,!=}
Logical operators- { &&, ||, !}
Bitwise operators-{ &,|,<<,>>,~,^}
Assignment operators-{ =,+=,-=,*=,/=,%=}
Conditional operators-{ ?: }
Special operators-{ Size of }
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
1. Integer Type
SPECIFIER
Unsigned 1 0 to 255 %u
short int
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
2,147,483,647
2. Floating Type
SPECIFIER
Float 4 3.4E-38
%f
to 3.4E+38
Double 8 1.7E-308
%lf
to 1.7E+308
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
3. Character Type
Character types are used to store characters value.
Size and range of Integer type on 16-bit machine
SPECIFIER
4. Void Type
void type means no value. This is usually used to specify the
type of functions.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a;
printf(“enter the number”);
scanf(“%d”,&a);
printf(“a=%d”,a);
}
Output:- enter the number 8
a=8.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Example Programs:-
Write a C program to read the three numbers and print it on the
screen.
Write a C program to read the three numbers and find out the
average of three numbers.
Write the C program to read the two numbers and swap the
numbers(with temporary variable and without temporary variables).
Variables in C Language
Variable is the name of memory location. Unlike constant,
variables are not changeable; we can change value of a variable during
execution of a program. A programmer can choose a meaningful variable
name. Example : num1,age,gst,etc..........
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Decision Making In C
Decision making is about deciding the order of execution of
statements based on certain conditions or repeat a group of statements until
certain specified conditions are met. C language handles decision-making by
supporting the following statements,
if statement
switch statement
conditional operator statement
goto statement
1. Simple if statement
2. If....else statement
3. Nested if....else statement
4. else if statement
Simple if statement
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
statement inside;
statement outside;
if...else statement
statement block1;
else
statement block2;
if( expression1 )
statement block1;
else
statement block2;
else
statement block3;
else-if ladder
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
statement block1;
else if(expression2)
statement block2;
else if(expression3 )
statement block3;
else
default statement;
The expression is tested from the top(of the ladder) downwards. As soon as
the true condition is found, the statement associated with it is executed.
Switch Statement
Switch statement is a control statement that allows us to choose
only one choice among the many given choices.
switch(expression)
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
1. The expression (after switch keyword) must yield an integer value i.e it
can be an integer or a variable or an expression that evaluates to an
integer.
2. The case label i.e. values must be unique.
3. The case label must end with a colon(:)
4. The following line, after case statement, can be any valid C statement.
Example Programs:-
Write a C program to read the number and check whether it is
greater than 10 or less than 10 by using if statement.
Write a C program to read the age of the student and check whether
the student is eligible to vote or not by using if else.
Write a C program to read the two numbers and find out which one is
big by using if else.
Write a C program to read the three numbers and findout which one is
big by using nested if and else if.
Write a C program for switch case statement.
Write a C program for application of calculator by using switch case.
Class-7:-
What is loop?
Loops are use for repetition purpose. There are 3 types of
loops in c language.
1. while loop
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
2. for loop
3. do-while loop
1. While loop
While loop can be addressed as an entry control loop. It is
completed in 3 steps.
Syntax:
variable initialization ;
while (condition)
statements ;
Example Programs:-
Write a C program to print the name 10 times.
Write a C program to print the numbers between 1 to n.
Write a C program to print the numbers between n to 1.
Write a C program to print sum of 1 to n numbers.
Write a C program to print even numbers between1 to n.
Write a C program to print odd numbers between1 to n.
2. Do While Loop
The body of the loop will be executed, even though the starting
condition inside while is initialized to false. General format of do-while loop
is,
do
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
....
.....
while(condition)
Class-8:-
3. For Loop
for loop is used to execute a set of statements repeatedly until a
particular condition is satisfied. we can say it an open ended loop. General
format is,
statement-block;
Example Programs:-
Write a C program to read the numbers and print its table.
Write a C program to print the sum of even numbers between 1-n.
Write a C program to print multiple of 3 & 7.
Write a C program to print multiple of 5 but not multiple of 10.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Class-9:-
4. Nested For Loop
We can also have nested for loops, i.e one for loop inside
another for loop. Basic syntax is,
for(initialization; condition; increment/decrement)
statement ;
Example Programs:-
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Class-10:-
1) Break Statement
2) Continue Statement
It causes the control to go directly to the test-condition and
then continue the loop process. On encountering continue, cursor leave the
current cycle of loop, and starts with the next cycle.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Example Programs:-
Write a C program to find out Fibonacci series.
Write a C program to read the number and print in reverse order.
Write a C program to check whether the given number is palindrome
or not.
Write a C program to find out the sum of individual number.
Class-11:- Test
Class-12:- Project
Class-13-15:-
Array
An array is defined as the collection of similar type of data
items stored at contiguous memory locations. Arrays are the derived data
type in C programming language which can store the primitive type of
data such as int, char, double, float, etc. It also has the capability to store
the collection of derived data types, such as pointers, structure, etc. The
array is the simplest data structure where each data element can be
randomly accessed by using its index number.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the
array, we can't exceed the limit. So, it doesn't grow the size dynamically
like Linked List which we will learn later.
int marks[5];
Initialization of C Array
The simplest way to initialize an array is by using the index of each
element. We can initialize each element of the array by using the index.
Consider the following example.
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;
1. int marks[5]={20,30,40,50,60};
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Example Programs:-
Write a C program to print 1D array.
Write a C program to print sum in 1D array.
Write a C program to find biggest value in 1D array.
1. data_type array_name[rows][columns];
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the
array if the declaration and initialization are being done simultaneously.
However, this will not work with 2D arrays. We will have to define at least
the second dimension of the array. The two-dimensional array can be
declared and defined in the following way.
1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function
int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
Output
minimum number is 3
Example Programs:-
Write a C program to print 2D array.
Write a C program to print sum of two matrices.
Write a C program to print subtraction of two matrices.
Write a C program to print multiplication of two matrices.
Class-16:-
C Pointers
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 an b y other pointer.
Example Programs:-
Write a C program for pointers.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
Example Programs:-
Write a C program for Double pointers.
Class-17:-
Example Programs:-
Write a C program for ATM Application?
Write a C program for toll plaza?
Write a C program for GST billing with Array?
Class-18:-(Test)
Class-19:-
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
1. malloc()
2. calloc()
3. realloc()
4. free()
Before learning above functions, let's understand the difference between static
memory allocation and dynamic memory allocation.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
malloc() function in C
The malloc() function allocates single block of requested memory.
1. ptr=(cast-type*)malloc(byte-size)
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
calloc() function in C
The calloc() function allocates multiple block of requested memory.
1. ptr=(cast-type*)calloc(number, byte-size)
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
realloc() function in C
If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
1. ptr=realloc(ptr, new-size)
free() function in C
The memory occupied by malloc() or calloc() functions must be released
by calling free() function. Otherwise, it will consume memory until program exit.
1. free(ptr)
Class-20:-
o Construct individual arrays for storing names, roll numbers, and marks.
o Use a special data structure to store the collection of different data types.
What is Structure
Structure in c is a user-defined data type that enables us to
store the collection of different data types. Each element of a structure is called
a member. Structures ca; simulate the use of classes and templates as it can
store various information
The ,struct keyword is used to define the structure. Let's see the syntax to
define the structure in c.
struct structure_name
{
data_type member1;
data_type member2;
.
.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
data_type memeberN;
};
1st way:
Let's see the example to declare the structure variable by struct keyword. It
should be declared within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
The variables e1 and e2 can be used to access the values stored in the structure.
Here, e1 and e2 can be treated in the same way as the objects in C++ and Java.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
If number of variables are not fixed, use the 1st approach. It provides you the
flexibility to declare the structure variable many times.
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
If no. of variables are fixed, use 2nd approach. It saves your code to declare a
variable in main() function.
Example Programs:-
Write a C program to create a Structure?
C Union
Like structure, Union in c language is a user-defined data type that
is used to store the different type of elements.
At once, only one member of the union can occupy the memory. In other words,
we can say that the size of the union in any instance is equal to the size of its
largest element.
Defining union
The union keyword is used to define the union. Let's see the syntax
to define union in c.
union union_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
union employee
{ int id;
char name[50];
float salary;
};
C Math Functions
There are various methods in math.h header file. The commonly used functions
of math.h header file are given below.
#include<stdio.h>
#include <math.h>
int main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
return 0;
}
File Handling in C
In programming, we may require some specific input data to be generated several
numbers of times. Sometimes, it is not enough to only display the data on the console.
The data to be displayed may be very large, and only a limited amount of data can be
displayed on the console, and since the memory is volatile, it is impossible to recover the
programmatically generated data again and again. However, if we need to do so, we
may store it onto the local file system which is volatile and can be accessed every time.
Here, comes the need of file handling in C.
File handling in C enables us to create, update, read, and delete the files stored on the
local file system through our C program. The following operations can be performed on a
file.
#include<stdio.h>
int main( )
{
FILE *fp ;
char s[80] ;
fp = fopen ( "java.txt", "w" ) ;
if ( fp == NULL )
By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723
{
puts ( "Cannot open file" ) ;
}
printf ( "\nEnter a few lines of text:\n" ) ;
while ( strlen ( gets ( s ) ) > 0 )
{
fputs ( s, fp ) ;
fputs ( "\n", fp ) ;
}
fclose ( fp ) ;
}
By Mr.Prem Agarwal.