0% found this document useful (0 votes)
45 views

Final C Language

Uploaded by

mrqader16181
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Final C Language

Uploaded by

mrqader16181
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

www.prsoftwares.

com
Contact:9030202723

C Language Digital Content


Class-1:-

 What Is C And History Of C Language?


C is a general-purpose, high-level language that was originally
developed by Dennis M. Ritchie to develop the UNIX operating system at
Bell Labs in 1972.

−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

A. Include Header FiIe Section


C program depends upon some header files for function definition that are
used in program. Each header file by default is extended with .h. The
header file should be included using #include directive as given here.

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.

F. User Defined Function


The functions defined by the user are called user-defined functions. These
functions are generally defined after the main () function.

 DELIMITERS

Delimiters Use

: Colon Useful for label

; Semicolon Terminates the statement

( ) Parenthesis Used in expression and


function

[ ] Square Bracket Used for array declaration

{ } Curly Brace Scope of the statement

# hash Preprocessor directive

, Comma Variable separator

 Basic programs

 Write a C program to print your message on screen?

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.

 Write a C program to print a multiple messages on the screen?


#include<stdio.h>
#include<conio.h>
void main()
{
Printf(“first step towards your dreams\n”);
Printf(“to become a successful developer\n”);
Printf(“learn new concepts with smart techniques”);
getch();
}
Output: - first step towards your dreams
To become a successful developer
Learn new concepts with smart techniques

 Steps To Compile and Execute C Program?


Following are the simple steps −

 Open a text editor and add the above-mentioned code.

 Save the file as filename.c

 Compile the program with ctrl+f9 or f9.

 If you find any error rectify it and compile the program again.

 If no error execute by using f10.

 You will see the output printed on the screen.

 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

Auto double Int struct

Break else Long switch

Case enum Register typedef

Const extern Return union

Char float Short unsigned

Continue for Signed volatile

Default goto Sizeof void

Do if Static while

 Character Set
In C language characters are grouped into the following
categories,

1. Letters(all alphabets a to z & A to Z).


2. Digits (all digits 0 to 9).
3. Special characters, ( such as colon :, semicolon ;, period ., underscore _,
ampersand &etc).
4. White spaces.

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.

 Rules for an Identifier

1. An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 )


and underscore( _ ).
2. The first character of an identifier can only contain alphabet( a-z , A-Z
) or underscore ( _ ).
3. Identifiers are also case sensitive in C. For
example name and Name are two different identifier in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters, such as semicolon, period, whitespaces, slash or
comma are permitted to be used in or as Identifier.

 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.

C operators can be classified into following types,

 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

Class-3 & 4:-

 Data Types in C Language


Data types specify how we enter data into our programs and
what type of data we enter. C language has some predefined set of data
types to handle various kinds of data that we use in our program. These data
types have different storage capacities.

1. Integer Type

Integers are used to store whole numbers.

Size and range of Integer type on 16-bit machine

TYPE SIZE RANGE FORMAT

SPECIFIER

int or signed 2 -32,76 %d


int to32767

Unsigned int 2 0 to 65535 %u

Short int or 1 -128 to 127 %i or %d


signed short
int

Unsigned 1 0 to 255 %u
short int

By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723

Long int or 4 - %li or %ld


signed long 2,147,483,648
int to

2,147,483,647

Unsigned long 4 0 to %lu


int
4,294,967,295

2. Floating Type

Floating types are used to store real numbers.

Size and range of Integer type on 16-bit machine

TYPE SIZE RANGE FORMAT

SPECIFIER

Float 4 3.4E-38
%f
to 3.4E+38

Double 8 1.7E-308
%lf
to 1.7E+308

long double 10 3.4E-4932


%lf
to 1.1E+4932

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

TYPE SIZE RANGE FORMAT

SPECIFIER

char or signed 1 -128 to 127


char %c

unsigned char 1 0 to 255


%c

4. Void Type
void type means no value. This is usually used to specify the
type of functions.

 Write a C program to read the number on the screen.

#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

 Write a C program to read the two numbers and print it on the


screen.
#include<stdio.h
#include<conio.h>
Void main()
{
int a,b;
printf(“enter the two numbers”);
scanf(“%d %d”,&a,&b);
printf(“a=%d b=%d”,a,b);
}
Output:-enter the two numbers 8,22
a=8, b=22.

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

 Rules To Define Variable Name

1. Variable name must not start with a digit or underscore(_).


2. Variable name can consist of alphabets, digits and special symbols like
underscore _.
3. Blank or spaces are not allowed in variable name.
4. Keywords are not allowed as variable name.

Class-5 & 6:-

 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

 Decision Making With If Statement


The if statement may be implemented in different forms
depending on the complexity of conditions to be tested. The different forms
are,

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

The general form of a simple if statement is,


if( expression )

statement inside;

statement outside;

if...else statement

The general form of a simple if...else statement is,


if( expression )

statement block1;

else

statement block2;

Nested if....else statement

The general form of a nested if...else statement is,


if( expression )

if( expression1 )

statement block1;

else

statement block2;

else

statement block3;

else-if ladder

By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723

The general form of else-if ladder is,


if(expression1)

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

 Rules for Using Switch

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.

 Variable initialization.( e.gint x=0; )


 condition( e.g while( x<=10) )
 Variable increment or decrement ( x++ or x-- or x=x+2 )

Syntax:
variable initialization ;

while (condition)

statements ;

variable increment or decrement ;

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)

Practice same example program as while loop.

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,

for(initialization; condition ; increment/decrement)


{

statement-block;

The for loop is executed as follows:

1. It first evaluates the initialization code.


2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluates the increment/decrement condition and again follows
from step 2.
5. When the condition expression becomes false, it exits the loop.

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)

for(initialization; condition; increment/decrement)

statement ;

Example Programs:-

 Write a C program for following pattern when n=3.

By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723

Class-10:-

1) Break Statement

When break statement is encountered inside a loop, the loop is


immediately exited and the program continues with the statement
immediately following the loop.

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.

2) Ease of traversing: By using the for loop, we can retrieve the


elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few


lines of code only.

4) Random Access: We can access any element randomly using the


array.

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.

 One Dimensional Array


We can declare an array in the c language in the following way.

1. data type array name[array size];

Now, let us see the example to declare the array.

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;

 C Array: Declaration with Initialization


We can initialize the c array at the time of declaration. Let's see the code.

1. int marks[5]={20,30,40,50,60};

By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723

2. In such case, there is no requirement to define the size. So it may


also be written as the following code.
3. int marks[]={20,30,40,50,60};

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.

 Two Dimensional Array in C


The two-dimensional array can be defined as an array of
arrays. The 2D array is organized as matrices which can be represented
as the collection of rows and columns.

 Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

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}};

 C language passing an array to function example

#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

min=minarray(numbers,6);//passing array with size


printf("minimum number is %d \n",min);
return 0;
}

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

 C Double Pointer (Pointer to Pointer)


As we know that, a pointer is used to store the address
of a variable in C. Pointer reduces the access time of a variable.
However, In C, we can also define a pointer to store the address of
another pointer. Such pointer is known as a double pointer (pointer to
pointer). The first pointer is used to store the address of a variable
whereas the second pointer is used to store the address of the first
pointer. Let's understand it by the diagram given below.

The syntax of declaring a double pointer is given below.

1. int **p; // pointer to a pointer which is pointing to an integer.

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

Dynamic memory allocation in C


The concept of dynamic memory allocation in c language enables
the C programmer to allocate memory at runtime. Dynamic memory allocation in
c language is possible by 4 functions of stdlib.h header file.

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.

static memory allocation dynamic memory allocation

memory is allocated at compile Memory is allocated at run time.


time.

memory can't be increased while memory can be increased while


executing program. executing program.

used in array. used in linked list.

malloc() Allocates single block of requested


memory.

calloc() Allocates multiple block of requested


memory.

realloc() Reallocates the memory occupied by


malloc() or calloc() functions.

free() Frees the dynamically allocated memory.

By Mr.Prem Agarwal.
www.prsoftwares.com
Contact:9030202723

 malloc() function in C
The malloc() function allocates single block of requested memory.

It doesn't initialize memory at execution time, so it has garbage value initially.

It returns NULL if memory is not sufficient.

The syntax of malloc() function is given below:

1. ptr=(cast-type*)malloc(byte-size)

Let's see the example of malloc() function.

#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.

It initially initialize all bytes to zero.

It returns NULL if memory is not sufficient.

The syntax of calloc() function is given below:

1. ptr=(cast-type*)calloc(number, byte-size)

Let's see the example of calloc() function.

#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

Let's see the syntax of realloc() function.

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.

Let's see the syntax of free() function.

1. free(ptr)

Class-20:-

 Why use structure?


In C, there are cases where we need to store multiple attributes
of an entity. It is not necessary that an entity has all the information of one type
only. It can have different attributes of different data types. For example, an
entity Student may have its name (string), roll number (int), marks (float). To
store such type of information regarding an entity student, we have the
following approaches:

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;
};

 Declaring structure variable


We can declare a variable for the structure so that we can access
the member of the structure easily. There are two ways to declare structure
variable:

1. By struct keyword within main() function


2. By declaring a variable at the time of defining the structure.

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;
};

Now write given code inside the main() function.

1. struct employee e1, e2;

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;

Which approach is good

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.

 Accessing members of the structure


There are two ways to access structure members:

1. By . (member or dot operator)


2. By -> (structure pointer operator)

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.

 Advantage of union over structure

It occupies less memory because it occupies the size of the largest


member only.

 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

Let's see the example to define union for an employee in c.

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.

No. Function Description

1) ceil(number) rounds up the given number. It returns the


integer value which is greater than or equal to
given number.

2) floor(number) rounds down the given number. It returns the


integer value which is less than or equal to given
number.

3) sqrt(number) returns the square root of given number.

4) pow(base, returns the power of given number.


exponent)

5) abs(number) returns the absolute value of given number.

#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;
}

 Use of fflush() in C language

fflush() is typically used for output stream only. Its purpose is to


clear (or flush) the output buffer and move the buffered data to
console (in case of stdout) or disk (in case of file output stream).

 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.

o Creation of the new file


o Opening an existing file
o Reading from the file
o Writing to the file
o Deleting the 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.

You might also like