Vous êtes sur la page 1sur 67

Programming in C

By

Er. Sourav Kumar Giri

(Department of Computer Science)

Srinix College Of Engineering

Balasore.

1 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


CONTENTS

Sl Chapter Page

1 Introduction to C 3

2 Conditional Statements 14

3 Loop 20

4 Function and Pointers

5 Array

6 String

7 Structure

8 Data types, enums, Storage class

9 File Handling

2 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 1

Introduction

What is C?

 C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972.


 It was designed and written by a man named Dennis Ritchie.

General Communication system

Scenario 1

Both individual agree upon a common language and communicate with each other.

English English
Direct communication is possible

Scenario 2

Both individual do not agree upon a common language and direct communication is not possible.

Hindi English
Direct communication is not possible
possible

Scenario 3

Both individual do not agree upon a common language and communication is possible through a
mediator.

3 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


English English, Hindi Hindi

Computer System

C Compiler
Programme

C Language Machine Language

Steps to learn English and C language

English Alphabets Words Sentences Paragraphs


1. Alphabets
2. Constants
Digits
C Variables 3. Instructions 4. Program
Special
Keywords
symbols

Variables

 An entity that may vary during program execution is called a variable.


 Variable names are names given to locations in memory.
 These locations can contain integer, real or character constants.

Rules for Constructing Variable Names

 A variable name is any combination of 1 to 31 alphabets, digits or underscores.


 The first character in the variable name must be an alphabet or underscore.
 No commas or blanks are allowed within a variable name.

4 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


 No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
 A variable cannot be a keyword.
 Example: si_int, m_hra, pop_e_89

Constant

 A constant refers to some value whose value do not changes.


 There are four types of constant: integer, real, character and string constant.

Constant Description Examples


Integer Do not contain decimal point 56, 89, 908, -34 etc
Real Contain a decimal point 6.7, -9.0, 899.987 etc
Character A single character enclosed within single quotes ‘A’, ‘5’, ‘x’ etc
String A set of characters enclosed within double quotes “Srinix”, “5” etc

Keywords

 These are the special words whose meaning is known to the compiler.
 There are 32 keywords available in C.
 int, char, for, if, else, break, continue etc are the examples of keyword.

Data types and format specifiers

Data type Keyword Format specifier


Integer int %d
Character char %c
Real float %f
String - %s

More data types will be discussed vividly later on.

Declaration/ Initialization

In declaration of a variable, the type of the variable is specified.


int x; means x is a variable of integer type. char p; means p is a variable of character type. int x, y, z;
means x, y and z are 3 variables of integer type.
In initialization, we assign some value to a variable.
int x=5; means x is a integer variable with initial value 5.

5 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Input/ output statement

Input statements are used to take input from the keyboard. scanf() is an example of input statement.
Syntax of scanf():

scanf(“Format specifiers”, &v1, & v2, &v3……..);

Output statements are used to print the output/result. printf() is an example of output statement.
Syntax of printf():

printf(“Format specifiers”, v1, v2, v3……..);

scanf(“%d”, &x); means computer will take the value of x which is an integer.
scanf(“%d %c”, &a, &b); means computer will take the value of a and b where a is an integer and b is a
character.
printf(“%d”, x); means computer will print the value of x which is an integer.
printf(“%d %c”, a, b); means computer will print the value of a and b where a is an integer and b is a
character.
printf(“Srinix”); prints srinix on the computer screen.

Escape Character/Sequence

Symbol Meaning
\n New line
\t Tab
\r Carriage return
\b Back space
\a Alert

Mathematical expression VS C expression

Mathematical Expression C Expression


axb–cxd a*b-c*d
(m + n) (a + b) (m+n)*(a+b)
3x2 + 2x + 5 3*x^2- 2*x + 5
x4 x^4 or x*x*x*x
√x x^(1/2)

6 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Hierarchy of Operations

Priority Operators Description


1st */% multiplication, division, modular division
2nd +- addition, subtraction
3rd = assignment

Example 1.1 Determine the hierarchy of operations and evaluate the following expression:

i=2*3/4+4/4+8-2+5/8
i=2*3/4+4/4+8-2+5/8
i=6/4+4/4+8-2+5/8 operation: *
i=1+4/4+8-2+5/8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i=1+1+8-2+0 operation: /
i=2+8-2+0 operation: +
i = 10 - 2 + 0 operation: +
i=8+0 operation : -
i=8 operation: +

Common steps to write a Program

1. Header file
2. void main()
3. Opening Brace( { )
4. Necessary declaration or initialization
5. Take input from keyboard
6. Necessary calculation to get result
7. Print result
8. Closing Brace( } )

Program 1.1 WAP to find sum of two integers.

Solution:
#include<stdio.h>
void main()
{
int x, y, r;
printf(“Enter two numbers”);
scanf(“%d%d”, &x, &y);
r=x+y;
printf(“%d”, r);

7 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


}

Output:
Enter two numbers
3 4
7

Program 1.2 WAP to find area and perimeter of a rectangle with its length and breadth given
from the keyboard.

Solution:
#include<stdio.h>
void main()
{
int l, b, p, a;
printf(“Enter length and breadth”);
scanf(“%d%d”, &l, &b);
a=l*b;
p=2*(l+b);
printf(“Area=%d and perimeter=%d”, a, p);
}

Output:
Enter length and breadth
2 5
Area=10 and perimeter=20

Program 1.3 WAP to find square and cube of an integer given from the keyboard.

Solution:
#include<stdio.h>
void main()
{
int n, c, s;
printf(“Enter a number”);
scanf(“%d”, &n);
c=n*n*n;
s=n*n;
printf(“Cube is %d and Square is %d”, c,s);
}

Output:
Enter a number
3
Cube is 27 and Square is 9

8 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Program 1.4 Write a program to calculate simple interest for given value of principal, rate of
interest and time period.

Solution:
#include<stdio.h>
void main( )
{
int p, n ;
float r, si ;
printf(“Enter value of P, R and T\n”);
scanf(“%d%d%d”, &p, &r, &t);

si = p * n * r / 100 ;
printf ( "\n%f" , si ) ;
}

Output:
Enter value of P, R and T
1000 5 2
100.

C Operators:

1. Arithmetic Operators (+, -, *, /, %)


The five arithmetical operations supported by the C language are:

Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulo (%)

Operations of addition, subtraction, multiplication and division literally correspond with their respective
mathematical operators.
Division Rule

Integer/integer=integer
Integer/float=float
Float/integer=float
Float /float=float

9 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Modular division
a>=b a%b =remainder when a is divided by b
a<b a

Program 1.5

Solution:
#include <stdio.h>
void main ()
{
int a, b, p, q, r, s;
a = 10;
b=4;
p= a/b;
q= a*b;
r= a%b;
s= b%a;
printf(“%d%d%d%d”,p, q, r, s);
}

Out put:
2 40 2 4

2. Assignment Operator (=)


The assignment operator assigns a value to a variable.
a = 5;
This statement assigns the integer value 5 to the variable a. The part at the left of the assignment
operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue
has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or
any combination of these. The most important rule when assigning is the right-to-left rule: The
assignment operation always takes place from right to left,
and never the other way:
a = b;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value
that was stored until this moment in a is not considered at all in this operation, and in fact that value is
lost.

Shorthand assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
When we want to modify the value of a variable by performing an operation on the value currently
stored in that variable we can use compound assignment operators:
value += increase; is equivalent to value = value + increase;
a -= 5; is equivalent to a = a - 5;
a /= b; is equivalent to a = a / b;

10 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


price *= units + 1; is equivalent to price = price * (units + 1); and the same for all other operators.

3. Relational and equality operators (==, !=, >, <, >=, <= )
In order to evaluate a comparison between two expressions we can use the relational and equality
operators. The result of a relational operation is a Boolean value that can only be true or false,
according to its Boolean result. We may want to compare two expressions, for example, to know if they
are equal or if one is greater than the other is. Here is a list of the relational and equality operators that
can be used in C:

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Here there are some examples:


(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Of course, instead of using only numeric constants, we can use any valid expression, including variables.
Suppose that a=2, b=3 and c=6,
(a == 5) // evaluates to false since a is not equal to 5.
(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.

Important Tips………
Be careful! The operator = (one equal sign) is not the same as the operator == (two equal signs), the first
one is an assignment operator (assigns the value at its right to the variable at its left) and the other one
(==) is the equality operator that compares whether both expressions in the two sides of it are equal to
each other. Thus, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we
compared it to a, that also stores the value 2, so the result of the operation is true.

11 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


4. Logical operators ( !, &&, || )
The Operator ! is the C operator to perform the Boolean operation NOT, it has only one operand,
located at its right, and the only thing that it does is to inverse the value of it, producing false if its
operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of
evaluating its operand.
For example: !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) //
evaluates to true because (6 <= 4) would be false. !true // evaluates to false. !false // evaluates to true.
The logical operators && and || are used when evaluating two expressions to obtain a single relational
result. The operator && corresponds with Boolean logical operation AND. This operation results true if
both its two operands are true, and false otherwise. The following panel shows the result of operator
&& evaluating the expression a && b:

a b a && b
True True True
True False False
False True False
False False False

The operator || corresponds with Boolean logical operation OR. This operation results true if either one
of its two operands is true, thus being false only when both operands are false themselves. Here are the
possible results of a || b:

a b a || b
True True True
True False True
False True True
False False False

For example:
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

5. Increment and Decrement Operator (++, --)


The increment operator (++) and the decrement operator (--) increase or reduce by one the value stored
in a variable. They are equivalent to +=1 and to - =1, respectively. Thus:
c++;
c+=1;
c=c+1;
are all equivalent in its functionality: the three of them increases the value of c by one.

12 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it
can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions
like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the
increase or decrease operation is evaluated as a value in an outer expression they may have an
important difference in their meaning: In the case that the increase operator is used as a prefix (++a) the
value is increased before the result of the expression is evaluated and therefore the increased value is
considered in the outer expression; in case that it is used as a suffix (a++) the value stored in a is
increased after being evaluated and therefore the value stored before the increase operation is
evaluated in the outer expression.

Pre First increment/decrement then assignment.


Post First assignment then increment/decrement.

Notice the difference:

Example 1.2 Find the value of A and B.

B=3;
A=++B;
Ans: A contains 4, B contains 4

Example 1.3 Find the value of A and B.

B=3;
A=B++;
Ans: A contains 3, B contains 4

In Example 7, B is increased before its value is assigned to A. While in Example 8, the value of B is
assigned to A and then B is increased.

6. Conditional operator ( ? : )
The conditional operator evaluates an expression returning a value if that expression is true and a
different one if the expression is evaluated as false. Its format is: condition? result1: result2. If condition
is true the expression will return result1, if it is not it will return result2.

7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.


7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.

13 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Program 1.6 WAP to find the greatest of two nos.

solution:
#include <stdio.h>
void main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
printf(“%d”,c);
}

Output:
7

In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true, thus the first
value specified after the question mark was discarded in favor of the second value (the one after the
colon) which was b, with a value of 7.

7. Comma operator ( , )
The comma operator (,) is used to separate two or more expressions that are included where only one
expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost
expression is considered.
For example, the following code:
a = (b=3, b+2); would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end,
variable a would contain the value 5 while variable b would contain value 3.

8. Explicit type casting operator


Type casting operators allow you to convert a datum of a given type to another. There are several ways
to do this in C++. The simplest one, which has been inherited from the C language, is to precede the
expression to be converted by the new type enclosed between parentheses (()):
int i;
float f = 3.14;
i = (int) f;
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here,
the typecasting operator was (int). Another way to do the same thing in C++ is using the functional
notation: preceding the expression to be converted by the type and enclosing the expression between
parentheses:
i = int ( f );
Both ways of type casting are valid in C.

14 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Assignment-1
1. Give a C language preprocessor statement that will create a constant named MAX with
value 100.
2. Explain the difference between following:

 ++I and i++


 Variable and Keyword with examples

3. What is a keyword? How many keywords are there in C? Give 5 examples of keyword?
4. Find out the output of the following:
a).#include <stdio.h>
void main()
{
intx=4,y,z;
Y=--x;
Z=x--;
Printf(“%d%d%d”,x,y,z);
}
b).#include<stdio.h>
void main()
{
Int x=0,y=3;
While(x!=y){
Printf(“%d”,y);
If(y==2)
Break;
Y--;
}

c).#include<stdio.h>
void main()
{
Int i=-2;
If(--i)
Printf(“NON-ZERO”);
else
printf(“ZERO”);
}

d).void main()
{
Int i=8;
Printf(“%d%d%d”,i++,++I,i);
}
5. What is a variable? How are keywords different from them?

15 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


6 .What is the precedence of comma operator compared with other C operators?
7. Differentiate between compiler and interpreter.
8. Who invented c?
9. What are different escape sequences in C?
10. What is an operator? Describe different types of operators that are available in C language.

Chapter 2
Decision Control Structure

 By default the instructions in a program are executed sequentially.


 Many a times, we want a set of instructions to be executed in one situation, and an entirely
different set of instructions to be executed in another situation.
 This kind of situation is dealt in C programs using a decision control instruction/ conditional
statement.

if statement

Syntax of different if statements are given below:

if if else if
if ( condition ) if ( condition )
{ {
// This block works if condition is true /*This block works if condition is true*/
} }
else
{
/*This block works if condition is not
true*/
}
if else if nested if else
if ( condition1 ) if ( condition1 )
{ {
/*This block works if conditio1 is true*/ if ( condition2 )
} {
else if(condition2) /*This block works if both conditio1 and
{ condition 2 are true*/
/*This block works if conditio2 is true */ }
} }
else if ( condition3 ) else
{ {
/*This block works if conditio3 is true*/ if ( condition3 )

16 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


} {
else /*This block works if conditio1 is false and
{ condition3 is true*/
/*This block works if none of the above }
condition is true*/ else
} {
/*This block works if both conditio1 and
condition 3 are false*/
}
}

 Here the condition can be any valid expression including a relational expression.
 We can even use arithmetic expressions in if statement.
 In C a non-zero value is considered to be true, whereas a 0 is considered to be false.

For example all the following if statements are valid:

if ( 3 + 2 % 5 )
printf ( "This works" ) ;

if ( a = 10 )
printf ( "Even this works" ) ;

if ( -5 )
printf ( "Surprisingly even this works" ) ;

Program 2.1 The current year and the year in which the employee joined the organization are
entered through the keyboard. If the number of years for which the employee has served the
organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of
service are not greater than 3, then the program should do nothing.

Solution:
#include<stdio.h>
void main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and joining year " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "\n %d", bonus ) ;
}

17 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


}

Output:
Enter current year and year of joining
2010 2002
2500

Program 2.2 Write a program to check whether a number is even or odd.

Solution:
#include<stdio.h>
void main( )
{
int n ;
printf ( "Enter a number " ) ;
scanf ( "%d ", &n) ;
if ( n%2==0 )
{
printf(“\n Even”);
}
else
{
printf(“\n Odd”);
}
}

Output:
Enter a number
9
Odd

Program 2.3 The marks obtained by a student in 5 different subjects are input through the
keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 – Fail. Write a program to calculate the division obtained by the student.

Solution:
#include<stdio.h>
void main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )

18 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


{
printf ( "First division" ) ;
}
else if ( ( per >= 50 ) && ( per < 60 ) )
{
printf ( "Second division" ) ;
}
else if ( ( per >= 40 ) && ( per < 50 ) )
{
printf ( "Third division" ) ;
}
else
{
printf ( "Fail" ) ;
}
}

Output:

Enter marks in five subjects


34 26 35 35 70
Third division

Program 2.4 Write a program to find the roots of a quadratic equation ax2+bx+c=0, where the
values of coefficient a, b and c are given from the keyboard.

Solution:
#include<stdio.h>
void main()
{
int a, b, c, d;
float r1, r2, x, y;
printf(“Enter the coefficients”);
scanf(“%d%d%d”, &a, &b, &c);
d=(b*b) - (4*a*c);
if(d>0)
{
r1=(-b+d^(1/2))/2*a;
r2=(-b-d^(1/2))/2*a;
printf(“%f%f”, r1, r2);
}
else if(d==0)
{
r1=-b/2*a;
printf(“%f”, r1);
}
else

19 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


{
x=-b/2*a;
y=(-d)^(1/2)/2*a;
printf(“%f +i %f”, x, y);
printf(“%f - i %f”, x, y);
}
}

Output:
Enter the coefficients
1 -3 2
1.0 2.0

Program 2.5 If the three sides of a triangle are entered through the keyboard, write a
program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.

Solution:
#include<stdio.h>
void main()
{
int a, b, c;
printf(“Enter 3 sides”);
scanf(“%d%d%d”,&a,&b,&c);
if(a+b>c && b+c>a && c+a>b)
{
if((a*a+b*b)==c*c || (b*b+c*c)==a*a || (c*c+a*a)==b*b)
{
printf(“Right Angled”);
}
else if( (a==b &&b!=c) || (b==c&&c!=a) || (c==a&&a!=b))
{
printf(“Isosceles”);
}
else if(a==b && b==c)
{
printf(“Equilateral”);
}
else
{
printf(“Any Valid Triangle”);
}
}
else
{
printf(“Cannot form a valid triangle”);
}
}

20 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Output:
Enter 3 sides
3 45
Right Angled
Assignment-2

1. List different types of decision control statements. Explain the types of if statement & give
one example for each.
2. Write a C program to solve a quadratic equation.
3. What is the purpose of goto statement? How the associated target statement is identified?
4. Write a program to find the largest of three numbers given from the keyboard. Also draw
the flow chart.

Chapter 3
Loop

The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves
repeating some portion of the program either a specified number of times or until a particular condition
is being satisfied. This repetitive operation is done through a loop control instruction.
There are three methods by way of which we can repeat a part of a program. They are:

(a) Using a for statement


(b) Using a while statement
(c) Using a do-while statement

The for loop:

It is the most popular looping instruction. The for allows us to specify three things about a loop in a
single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of repetitions
desired.
(c) Increasing the value of loop counter each time the program segment within the loop has been
executed.

Syntax of for loop:

for(initialization; condition; increment/decrement)


{

// Body of the loop

21 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


}

Syntax of while loop:

initialization

while(condition)
{

// Body of the loop

increment/decrement

Syntax of do while loop:

initialization
do
{
//Body of the loop
Increment/decrement

}while(condition);

Difference between while and do while loop…………….


As condition is checked at the end of body of loop, the do while loop is executed at least once(no
matter, whether the condition is true or false).

Program 3.1 WAP to print your name 5 times.


(i)Using for loop

Solution:
#include<stdio.h>

22 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


void main()
{
int i;
for(i=1; i<=5; i++)
{
printf(“Ram\n”);
}
}
Output:
Ram
Ram
Ram
Ram
Ram

(ii) Using while loop

Solution:
#include<stdio.h>
void main()
{
int i=1;
while(i<=5)
{
printf(“Ram\n”);
i++;
}
}
Output:
Ram
Ram
Ram
Ram
Ram

(iii) Using do while loop

Solution:
#include<stdio.h>
void main()
{
int i=1;
do {
printf(“Ram\n”);
i++;
} while(i<=5);
}
23 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com
Output:
Ram
Ram
Ram
Ram
Ram
Explanation:
 When the for statement is executed for the first time, the value of i is set to an initial value 1.
 Now the condition i <=5 is tested. Since i is 1 the condition is satisfied and the body of the loop
is executed for the first time.
 Upon reaching the closing brace of for, control is sent back to the for statement, where the
value of i gets incremented by 1.
 Again the test is performed to check whether the new value of i exceeds 5.
 If the value of i is still within the range 1 to 5, the statements within the braces of for are
executed again.
 The body of the for loop continues to get executed till i doesn’t exceed the final value 5.
 When i reach the value 6 the control exits from the loop and is transferred to the statement (if
any) immediately after the body of for.

Program 3.2 WAP to print all the even integers from 1 to 100.

Solution:
#include<stdio.h>
void main()
{
int i;
for(i=0; i<100 && i%2==0; i++)
{
printf(“%d\n”, i);
}
}

Output:
2
4
6
.
.
.98

Program 3.3 WAP to compute 1+2+3+4+………………….+n for a given value of n.

Solution:
#include<stdio.h>
void main()

24 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


{
int i, n, s=0;
printf(“Enter the value of n”);
scanf(“%d”, &n);

for(i=1; i<n; i++)


{
s=s+i;
}
printf(“%d”, s);
}
Output:

Enter the value of n 5

15

Program 3.4 WAP to compute factorial of a number.

Solution:
#include<stdio.h>
void main()
{
int i, n, f=1;
printf(“Enter the value of n”);
scanf(“%d”, &n);

for(i=1; i<n; i++)


{
f=f*i;
}
printf(“%d”,f);
}
Output:
Enter the value of n 5

120

Program 3.5 WAP to find sum of digits of a number.

Solution:
#include<stdio.h>
void main()
{
int a, n, s=0;
printf(“Enter a number”);

25 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


scanf(“%d”, &n);
while(n!=0)
{
a=n%10;
s=s+a;
n=n/10;
}
printf(“%d”,s);
}

Output:
Enter a number 435
12

Explanation:
Initially n=435(≠0)
So a=435%10=5, s=0+5=5, n=435/10=43

Now n=43(≠0)
So a=3, s=5+3=8, n=4

Now n=4(≠0)
So a=4, s=8+4=12, n=0

Now n=0(=0), So loop will stop.

Program 3.6 WAP to reverse a number.

Solution:
#include<stdio.h>
void main()
{
int a, n, rn=0;
printf(“Enter a number”);
scanf(“%d”, &n);

while(n!=0)
{
a=n%10;
rn=rn*10+a;
n=n/10;
}

printf(“%d”,rn);
}

Output:

26 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Enter a number 435

534
Explanation:
Initially n=435(≠0)
So a=435%10=5, s=0*10+5=5, n=435/10=43

Now n=43(≠0)
So a=3, s=5*10+3=53, n=4

Now n=4(≠0)
So a=4, s=53*10+4=534, n=0

Now n=0(=0), So loop will stop.

Example 3.1 WAP to check whether a no is palindrome number or not. (A number is said to be
palindrome number if it is equal its reverse

Solution:

#include<stdio.h>
void main()
{
int a, n, rn=0, b;
printf(“Enter a number”);
scanf(“%d”, &n);
b=n;
while(n!=0)
{
a=n%10;
rn=rn*10+a;
n=n/10;
}

if(b==rn)
{
printf(“palindrome”);
}
else
{
printf(“not palindrome”);
}
}

Output:
Enter a number 121
palindrome

27 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 3.2WAP to check whether a no is armstrong number or not. (A number is said to be
armstrong number if sum of cube of its digit is equal to that number).

Solution:
#include<stdio.h>
void main()
{
int a, n,s=0, b;
printf(“Enter a number”);
scanf(“%d”, &n);
b=n;

while(n!=0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}

if(b==rn)
{
printf(“Armstrong”);
}
else
{
printf(“Not Armstrong”);
}
}

Output:
Enter a number 121

palindrome

Break statement
 A break statement takes the control out of the loop.
 When break is encountered inside any loop, control automatically passes to the first statement
after the loop.
 A break is usually associated with an if.

28 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


 The keyword break allows us to do this.

Example 3.3 WAP to determine whether a number is prime or not. A prime number is one,
which is divisible only by 1 or itself.

Solution:
#include<stdio.h>
void main( )
{
int n, i ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;

for(i=2; i<=n-1; i++)


{
if ( n% i == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
}
if ( i == n )
printf ( "Prime number" ) ;
}

Output:
Enter a number
11

Prime number

Example 3.4 Write a program to print all the prime numbers from 1 to 500

Solution:
. #include<stdio.h>
void main( )
{
int n, i ;
for(n=2; i<=n; n++)
{
for(i=2; i<=n-1; i++)
{
if ( n% i == 0 )
{
break ;

29 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


}
}
if ( i == n )
{
printf ( "%d\n", n ) ;
}
}
}

Output:
2
3
5
7
11
13
17
19
23
29
…………..

continue statement
 continue statement take the control to the beginning of the loop, bypassing the statements
inside the loop, which have not yet been executed.
 The keyword continue allows us to do this.

Flow chart
Flow chart is the structural representation of a program or algorithm.

Shape Meaning

Instruction

Input/Output

Condition

Start/Stop

30 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 3.5 Draw a flow chart to check whether a no is even or odd.

Solution:

START

Declare n as integer

Input n

YES

n%2=
Even
=0?

NO

Odd

STOP

31 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 3.6 Draw a flow chart to print all integers from 1 to 1000.

Solution:

START

Declare i as integer

i=0 i=i+1

YES YES
i <==
Print i
1000
NO

STOP

Assignment-3

1. Write a C program to find the square and cube of 2 digits odd numbers.
2. Write a C program using do while loop, to calculate the sum of all even integers beginning with
i=2 and values of i that are less than 100.
3. Write a C program that will generate a table of values for the equation.
F(x ,y)=2e^x^3+(23+y)^x
Where 1<=x<=5 with an increment 0.5 and 1<=y<=5 with an increment 0.25.
4. Which statement is used to take the control to the beginning of the loop?
5. Explain the difference between while and do while loop.

32 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 4
Function and Pointer

A function is a self-contained block of statements that perform a coherent task of some kind. Every C
program can be thought of as a collection of these functions.

Why functions ???

1. Writing functions avoids rewriting the same code over and over. Suppose you have a section of
code in your program that calculates area of a triangle. If later in the program you want to
calculate the area of a different triangle, you won’t like it if you are required to write the same
instructions all over again. Instead, you would prefer to jump to a ‘section of code’ that
calculates area and then jump back to the place from where you left off. This section of code is
nothing but a function.
2. Using functions it becomes easier to write programs and keep track of what they are doing. If
the operation of a program can be divided into separate activities, and each activity placed in a
different function, then each could be written and checked more or less independently.
Separating the code into modular functions also makes the program easier to design and
understand.

Basically a function use consists of three parts:


(i) Function prototype: Function prototype is the skeleton of a function.

Syntax is: return_type function_name(type of arguments);

Example: int add(int, int); void display(void);

(ii) Function Calling: A function gets called when the function name is followed by a
semicolon.
Syntax is : function_name(list of arguments);

Example: sum(x, y); search(mark);

(iii) Function Definition: A function is defined when function name is followed by a pair of
braces in which one or more statements may be present.
Syntax is:
return_type function_name(list of arguments with their types)
{
//Function Body
}

33 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 4.1 WAP to find addition of two integer using function:
(i) Return type should be void
(ii) Return type should be int
Solution:

Return type is void:

#include<stdio.h>
void add(int, int);

void main()
{
int x, y;
printf(“enter two nos:”);
scanf(“%d%d”, &x, &y);
add(x, y);
}
void add(int a, int b)
{
int c;
c=a+b;
printf(“%d”, c);
}

Return type is int:

#include<stdio.h>
int add(int, int);

void main()
{
int x, y, r;
printf(“enter two nos:”);
scanf(“%d%d”, &x, &y);
r =add(x, y);
printf(“%d”, r);
}
int add(int a, int b)
{
int c=a+b;
return(c);
}

34 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 4.2 WAP to find factorial of a number using function.

Solution:

#include<stdio.h>
int factorial(int);
void main( )
{
int a, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &a ) ;
fact = factorial ( a ) ;
printf ( "Factorial value = %d", fact ) ;
}
int factorial ( int x )
{
int f = 1, i ;
for ( i = 1 ; i<=x ; i++ )
{
f=f*i;
}
return ( f ) ;
}

Actual arguments and formal arguments


The arguments which are present in function calling are called as actual argument and the arguments
which are present in function definition are called as formal argument. Formal arguments are the
photocopy of actual arguments. In Example 1, variables x and y are called actual argument and the
variables a and b are called formal arguments. Therefore value of x will be copied into a and the value of
y will be copied into b.

Important Facts about function


1. C program is a collection of one or more functions.
2. A function gets called when the function name is followed by a semicolon. For example:
main( )
{
argentina( ) ;
}
3. A function is defined when function name is followed by a pair of braces in which one or more
statements may be present. For example,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
35 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com
}
4. Any function can be called from any other function. Even main( ) can be called from other
functions.For example,
main( )
{
message( ) ;
}
message( )
{
printf ( "\nCan't imagine life without C" ) ;
main( ) ;
}
5. A function can be called any number of times. For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "\nJewel Thief!!" ) ;
}
6. The order in which the functions are defined in a program and the order in which they get called
need not necessarily be same. For example,

main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf ( "\nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "\nMary bought some butter" ) ;
}

7. A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect of C
functions later in this chapter.

36 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


8. A function can be called from other function, but a function cannot be defined in another function.
Thus, the following program code would be wrong, since argentina ( ) is being defined inside
another function, main ( ).
main( )
{
printf ( "\nI am in main" ) ;
argentina( )
{
printf ( "\nI am in argentina" ) ;
}
}

9. There are basically two types of functions:


Library functions Ex. printf( ), scanf( ) etc. (definition is known to compiler)
User-defined functions Ex. argentina( ), brazil( ) etc. (definition is given by the user)

Return statement
If return type of a function is not void, then the function must return some value to the calling function.
This is done using return statement.
1) There is no restriction on the number of return statements that may be present in a function.
Also, the return statement need not always be present at the end of the called function. The
following program illustrates these facts.
fun( )
{
char ch ;
printf ( "\nEnter any alphabet " ) ;
scanf ( "%c", &ch ) ;
if ( ch >= 65 && ch <= 90 )
return ( ch ) ;
else
return ( ch + 32 ) ;
}
In this function different return statements will be executed depending on whether ch is capital
or not.
2) Whenever the control returns from a function some value is definitely returned. If a meaningful
value is returned then it should be accepted in the calling program by equating the called
function to some variable. For example,
sum = calsum ( a, b, c ) ;
3) All the following are valid return statements.
return ( a ) ;
return ( 23 ) ;
return ( 12.34 ) ;
return ;

37 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


In the last statement a garbage value is returned to the calling function since we are not
returning any specific value. Note that in this case the parentheses after return are dropped.
4) If we want that a called function should not return any value, in that case, we must mention so
by using the keyword void as shown below.
void display( )
{
printf ( "\nHeads I win..." ) ;
printf ( "\nTails you lose" ) ;
}
5) A function can return only one value at a time. Thus, the following statements are invalid.
return ( a, b ) ;
return ( x, 12 ) ;
There is a way to get around this limitation, which would be discussed later in this chapter when
we learn pointers.
6) If the value of a formal argument is changed in the called function, the corresponding change
does not take place in the calling function. For example,
main( )
{
int a = 30 ;
fun ( a ) ;
printf ( "\n%d", a ) ;
}
fun ( int b )
{
b = 60 ;
printf ( "\n%d", b ) ;
}
The output of the above program would be:
60
30

Calling Convention
Calling convention indicates the order in which arguments are passed to a function when a function call
is encountered. There are two possibilities here:
 Arguments might be passed from left to right.
 Arguments might be passed from right to left.

C language follows the second order.


Consider the following function call:
fun (a, b, c, d ) ;
In this call it doesn’t matter whether the arguments are passed from left to right or from right to left.
However, in some function call the order of passing arguments becomes an important consideration.
For example:

38 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


int a = 1 ;
printf ( "%d %d %d", a, ++a, a++ ) ;
It appears that this printf( ) would output 1 2 3.
This however is not the case. Surprisingly, it outputs 3 3 1. This is because C’s calling convention is from
right to left. That is, firstly 1 is passed through the expression a++ and then a is incremented to 2. Then
result of ++a is passed. That is, a is incremented to 3 and then passed. Finally, latest value of a, i.e. 3, is
passed. Thus in right to left order 1, 3, 3 get passed. Once printf( ) collects them it prints them in the
order in which we have asked it to get them printed (and not the order in which they were passed). Thus
3 3 1 gets printed.

Recursion
A function is said to be recursive if it calls itself.

Example 4.2 Write program to find factorial of a number using recursive function.

Solution:
#include<stdio.h>
int factorial(int);
void main( )
{
int a, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &a ) ;
fact = factorial ( a ) ;
printf ( "Factorial value = %d", fact ) ;
}
int factorial ( int x )
{
int f ;
if ( x == 1 )
return ( 1 ) ;
else
f = x * factorial ( x - 1 ) ;
return ( f ) ;
}

Example 4.3 Write program to print fibonacii series up to n terms using recursion.

Solution:
#include<stdio.h>
void fibo(int, int, int);

39 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


void main()
{
int a=0, b=1, n;
printf(“enter no of terms:”);
scanf(“%d”, &n);
printf(“%d\t%d\t”, a, b);
fibo(a, b, n-2);
}
void fibo (int x, int y, int p)
{
int z=x+y;
printf(“%d \t”, z);
if (p!=0)
{
fibo(y, z, p-1);
}
}

Pointers
Pointer is variable which holds the address of another variable.
Symbol Meaning
& Address of
* Value at address of

Consider the declaration,


int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.

int *p; means p is a pointer variable which will hold the address of an integer variable.
float *q; means q is a pointer variable which will hold the address of a float variable.
Consider the following example:
#include<stdio.h>
void main( )
{
int i = 3, *p;
p=&i;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *p) ;
}

40 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Output:
Address of i = 65524
Value of i = 3
Value of i = 3
Value of i = 3
Here in the above example, &i means address of I, *(&i) means value at address of i.

Call by value and call by Reference


In call by value, values of the arguments are passed during function calling. In call by reference,
addresses of the arguments are passed during function calling.

Call by value Call by Reference


#include<stdio.h> #include<stdio.h>
void swap(int, int); void swap(int *, int *);
void main( ) void main( )
{ {
int x = 10, y = 20 ; int x = 10, y = 20 ;
swap ( x, y ) ; swap ( &x, &y ) ;
printf ( "\nx = %d y = %d", x, y ) ; printf ( "\nx = %d y = %d", x, y ) ;
} }
void swap ( int a, int b ) void swap ( int *a, int *b )
{ {
int t ; int t ;
t=a; t = *a ;
a=b; *a = *b ;
b=t; *b = t ;
printf ( "\na = %d b= %d", a, b) ; printf ( "\na = %d b= %d", *a, *b) ;
} }
Output: Output:
a=20 b=10 a=20 b=10
x=10 y=20 x=20 y=10

Conclusions :

From the above programs that we discussed here we can draw the following conclusions:
 If we want that the value of an actual argument should not get changed in the function being
called, pass the actual argument by value.
 If we want that the value of an actual argument should get changed in the function being called,
pass the actual argument by reference.

41 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Can a function return more than one vale???
Using a call by reference intelligently we can make a function return more than one value at a time,
which is not possible ordinarily. This is shown in the program given below:
Example 4.4 WAP to find the radius and perimeter of a circle.

Solution:
#include<stdio.h>
void areaperi(int, float *, float *);
void main( )
{
int radius ;
float area, perimeter ;
printf ( "\nEnter radius of a circle " ) ;
scanf ( "%d", &radius ) ;
areaperi ( radius, &area, &perimeter ) ;
printf ( "Area = %f", area ) ;
printf ( "\nPerimeter = %f", perimeter ) ;
}
void areaperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

Output:

Enter radius of a circle 5


Area = 78.500000
Perimeter = 31.400000

Important…………..

If a function is to be made to return more than one value at a time then return these values indirectly by
using a call by reference.

42 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Assignment-4

1. Define pointer in C language. How the declarations are made for pointer variables? What is the
difference between the function to pointer and pointer to function? What is far pointer?
2. What is a function? What is meant by function call? Define and differentiate between formal and
actual argument.
3. Write a function in c that takes a string as the single parameter and return the integer 1 if the
string is a palindrome, otherwise zero should be returned.
4. Explain the difference between call by value and call by reference with suitable example.
5. Can a function return more than one value? If yes, explain.
6. Write a function power (a, b) to calculate the value of a raised to b both recursively and non
recursively.
7. Any year is entered through the keyboard. Write a function to determine whether the year is a
leap year or not.
8. Write a recursive function to obtain the running sum of first 25 natural numbers.
9. Write a function to find the binary equivalent of a given decimal integer and display it.
10. Write a program to calculate GCD of two integers using recursion.

43 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 5
Array
Array is collection of similar elements stored in contiguous memory location.
For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending
order. In such a case we have two options to store these marks in memory:
 Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each
variable containing one student’s marks.
 Construct one variable (called array or subscripted variable) capable of storing or holding all the
hundred values.
int a[5]; means a is an array which can hold 5 integer values, where the 1st element will be stored in a[0]
and last element will be stored in a[4].

1st element a[0]


2nd element a[1]
3rd element a[2]
4th element a[3]
5th element a[4]

 The first element in the array is numbered 0, so the last element is 1 less than the size of the
array.
 An array is also known as a subscripted variable.
 Before using an array its type and dimension must be declared.

Example 5.1 Write a program to find average marks obtained by a class of 30 students in a
test.
Solution:
#include<stdio.h>
void main( )
{
int i, sum = 0, marks[30];
float avg;
for ( i = 0 ; i <30 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}
for ( i = 0 ; i <30; i++ )
{
sum = sum + marks[i] ;
}
avg = sum / 30.0 ;
printf ( "\nAverage marks = %d", avg ) ;
}

44 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Array Initialization
So far we have used arrays that did not have any values in them to begin with. We managed to store
values in them during program execution. Let us now see how to initialize an array while declaring it.
Following are a few examples that demonstrate this.
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
Note the following points carefully:
(a) Till the array elements are not given any specific values, they are supposed to contain garbage
values.
(b) If the array is initialized where it is declared, mentioning the dimension of the array is optional
nd
as in the 2 example above.

int num[ ] = { 24, 34, 12, 44, 56, 17 } ;


We also know that on mentioning the name of the array we get its base address. Thus, by saying *num
we would be able to refer to the zeroth element of the array, that is, 24. One can easily see that *num
and *( num + 0 ) both refer to 24. Similarly, by saying *( num + 1 ) we can refer the first element of the
array, that is, 34. In fact, this is what the C compiler does internally. When we say, num[i], the C
compiler internally converts it to *( num + i ).
This means that all the following notations are same:
num[i]
*( num + i )
*( i + num )
i[num]
And here is a program to prove:
void main( )
{
int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "%d %d", num[i], *( num + i ) ) ;
printf ( "%d %d", *( i + num ), i[num] ) ;
}
}
Output:
24 24 24 24 34 34 34 34 12 12 12 12 44 44 44 44 56 56 56 56 17 17 17 17

45 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Assignment-5

1. Write an algorithm to multiply two matrix of size m*n and also write a c program.
2. Write a c program to find out the largest number of matrix of size m*n.
3. Write a c program to perform insertion, deletion in a one dimensional array.
4. There are two arrays A & B. A contains 25 elements, whereas B contains 30 elements.
Write a function to create an array c that contains only those elements that are
common to A and B.
5. Write a program to subtract two sparse matrices implemented as an array.
6. Write a program to build a sparse matrix as an array. Write functions to check if the
sparse matrix is a square, diagonal, lower triangular or upper triangular matrix.
7. What is a subscript? What range of values is permitted for the subscript of a one
dimensional n element array?
8. What is an array? How initialization of an array occurs?
9. Write a program that interchanges the odd and even components of an array.
10. Write a program to find if a square matrix is symmetric.

46 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 6
String
A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For example,
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;
Each character in the array occupies one byte of memory and the last character is always ‘\0’. What
character is this? It looks like two characters, but it is actually only one character, with the \ indicating
that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same.
ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48. Figure 9.1 shows the way a character array is
stored in memory. Note that the elements of the character array are stored in contiguous memory
locations.
The above string can be also initialized as
char name[ ] = "HAESLER" ;

The %s used in printf( ) is a format specification for printing out a string. The same specification can be
used to receive a string from the keyboard, as shown below.
main( )
{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s!", name ) ;
}
Output:
Enter your name Debashish
Hello Debashish!

While entering the string using scanf( ) we must be cautious about two things:

 The length of the string should not exceed the dimension of the character array. This is because
the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly
exceed the bounds there is always a danger of overwriting something important, and in that
event, you would have nobody to blame but yourselves.

 scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Debashish Roy’
would be unacceptable. The way to get around this limitation is by using the function gets().
The usage of functions gets( ) and its counterpart puts( ) is shown below.

main( )
{
char name[25] ;
printf ( "Enter your full name " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ;

47 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


}
Output:
Enter your name Debashish Roy
Hello!
Debashish Roy

String operations (string.h)


C library supports a large number of string handling functions that can be used to array out many o f
the string manipulations such as:

 Length (number of characters in the string).


 Concatenation (adding two are more strings)
 Comparing two strings.
 Reverse a String(Reverse a given string)
 Copy(copies one string over another)

To do all the operations described here it is essential to include string.h library header file in the
program.

strlen() function
This function counts and returns the number of characters in a string. The length does not include a
nullcharacter.
Syntax: n=strlen(string);
Where n is integer variable, which receives the value of length of the string.
Example:
length =strlen(“Hollywood”);
The function will assign number of characters 9 in the string to a integer variable length.

Example 6.1 WAP to find the length of the string using strlen() function

Solution:

#include < stdio.h >


include < string.h >
void main()
{
char name[100];
int length;
printf(“Enter the string”);
gets(name);
length=strlen(name);
printf(“\nNumber of characters in the string is=%d”,length);
}

48 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


strcat() function
When you combine two strings, you add the characters of one string to the end of other string. This
process is called concatenation. The strcat() function joins 2 strings together. It takes the following
form:
strcat(string1,string2)

string1 & string2 are character arrays. When the function strcat is executed string2 is appended to
string1. the string at string2 remains unchanged.
Example:
strcpy(string1,”sri”);
strcpy(string2,”Bhagavan”);
printf(“%s”,strcat(string1,string2);
From the above program segment the value of string1 becomes sribhagavan. The string at str2 remains
unchanged as bhagawan.

strcmp() function
In c you cannot directly compare the value of 2 strings in a condition like if(string1==string2)
Most libraries however contain the strcmp() function, which returns a zero if 2 strings are equal, or a
non zero number if the strings are not the same. The syntax of strcmp() is given below:
strcmp(string1,string2);
String1 & string2 may be string variables or string constants. String1, & string2 may be string variables
or string constants some computers return a negative if the string1 is alphabetically less than the
second and a positive number if the string is greater than the second.
Example:
strcmp(“Newyork”,”Newyork”) will return zero because 2 strings are equal.
strcmp(“their”,”there”) will return a 9 which is the numeric difference between ASCII ‘i’ and ASCII ’r’.
strcmp(“The”, “the”) will return 32 which is the numeric difference between ASCII “T” & ASCII “t”.
strcmpi() function:
This function is same as strcmp() which compares 2 strings but not case sensitive.

Example
strcmpi(“THE”,”the”); will return 0.

49 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


strcpy() function
C does not allow you to assign the characters to a string directly as in the statement name=”Robert”;
Instead use the strcpy(0 function found in most compilers the syntax of the function is illustrated below.
strcpy(string1,string2);
Strcpy function assigns the contents of string2 to string1. string2 may be a character array variable or a
string constant.
strcpy(Name,”Robert”);
In the above example Robert is assigned to the string called Name.

Assignment-6

1) What would be the output of the following programs:


(a) main( ) {
char c[2] = "A" ;
printf ( "\n%c", c[0] ) ;
printf ( "\n%s", c ) ;
}
(b) main( )
{
char s[ ] = "Get organised! learn C!!" ;
printf ( "\n%s", &s[2] ) ;
printf ( "\n%s", s ) ;
printf ( "\n%s", &s ) ;
printf ( "\n%c", s[2] ) ;
}

(c) main( )
{
char s[ ] = "No two viruses work similarly" ;
int i = 0 ;
while ( s[i] != 0 )
{
printf ( "\n%c %c", s[i], *( s + i ) ) ;
printf ( "\n%c %c", i[s], *( i + s ) ) ;
i++ ;
}
}

(d) main( )
{
char s[ ] = "Churchgate: no church no gate" ;
char t[25] ;
char *ss, *tt ;

50 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


ss = s ;
while ( *ss != '\0' )
*ss++ = *tt++ ;

2.Fill in the blanks:

(1). "A" is a ___________ while ’A’ is a ____________.

(2). A string is terminated by a ______ character, which is written as ______.

(3). The array char name[10] can consist of a maximum of ______ characters.

(4). The array elements are always stored in _________ memory locations.

3. Write a program that converts all lowercase characters in a given string to its equivalent
uppercase character.

4. Write a program to sort a set of names stored in an array in alphabetical order.

5. Write a program to delete all vowels from a sentence. Assume that the sentence is not more
than 80 characters long.

51 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 7
Structure and Union
A structure is a collection of dissimilar data types.
Syntax of structure definition:

struct structure_name
{

Structure members

};

The variables which are declared inside structure definition are called as structure member.
Example:
struct book
{
char name[20];
char auth[25];
int page;
float price;
};

struct book b1, b2; here b1 and b2 are two structure variables of type book.

Name of b1 is b1.nm
Author of b1 is b1.auth
Page of b1 is b1.page
Price of b1 is b1.price

Similarly,
Name of b2 is b2.nm
Author of b2 is b2.auth
Page of b2 is b2.page
Price of b2 is b2.price
So access the structure member by using a structure variable through dot (.) operator.

Initialization of structure variable


For the above structure we can write,
b1={“Let Us C”, ”Y. Kanethkar”, 450, 500.0};
means
b1.nm=Let Us C
b1.auth=Y.Kanethkar

52 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


b1.page=450
b1.price=500.0

Example 7.1 WAP to accept name, roll, age and marks in 3 different subjects of 80 students
and display the names of the student having percentage greater than 60.

Solution:
#include<stdio.h>
void main()
{
int i;
struct stud
{
char name[20];
char roll[10];
int age, m1, m2, m3;
float avg;
};
struct stud s[80];

for (i=0; i<80; i++)


{
printf(“enter name, roll no, age and marks in 3 different subject”);
scanf(“%s%s%d%d%d%d”, &s[i].name, &s[i].roll, &s[i].age, &s[i].m1, &s[i].m2, &s[i].m3);
}

for (i=0; i<80; i++)


{
s[i].avg=(s[i].m1+s[i].m2+s[i].m3)/3.0;
}

for (i=0; i<80; i++)


{
if(s[i].avg>60)
{
printf(“%s”, s[i].name);
}
}
}

53 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 7.2 Define a structure Complex which contains real and imaginary part of a complex
number. WAP to find addition of two complex numbers.

Solution:
#include<stdio.h>
void main()
{
struct Complex
{
int real;
int img;
};
struct Complex c1, c2, c3;

printf(“enter real and imaginary part of complex number 1”);


scanf %d%d”, &c1.real, &c1.img);

printf(“enter real and imaginary part of complex number 2”);


scanf %d%d”, &c2.real, &c2.img);

c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;

printf(“%d+i%d”, c3.real, c3.img);


}

Self Referential Structure


A structure which contains a pointer to itself is known as self referential structure.
Example:
struct link
{
int data;
struct link *ptr;
};

54 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Union
Union is a collection of dissimilar data types.

union union_name
{

Union members

};

Structure Union
1) Syntax: 1. Syntax:
struct structure_name union union_name
{ {
//Data types //Data types
} }

2) All the members of the structure can 2. In a union only one member can be used at a
be accessed at once. time.

3) Structure allocates memory to each 3. Union allocates the memory equal to the
structure member. maximum memory required by the member of
the union.
struct example{
int x; union example{
float y; int x;
} float y;
Here memory allocated is size }
of(x)+sizeof(y). Here memory allocated is sizeof(y), because
size of float is more than size of integer.
4) Takes more memory. 4. Takes less memory.

55 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Assignment-7

1. What is a structure? What is a structure member? What is the relationship between a


structure and a structure member?
2. WAP to accept name ,age and address of five persons and display the name of eldest
person.
3. Explain the difference between union and structure. How the data members of a structure
are accessed?
4. WAP to enter roll no ,name and marks in three subject for each student. Find the average
marks of each student and also calculate the average marks in each subject in a class of 20
students.
5. What do you mean by self Referential structure . what is the size of a structure? What is
array of structure .

56 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 8
Data types, Storage Class, Enums, Macros and Dynamic memory Management

Data Type Range Bytes Format


signed char -128 to + 127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to +3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf

Storage Class
To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’. In other
words, not only do all variables have a data type, they also have a ‘storage class’. From C compiler’s
point of view, a variable name identifies some physical location within the computer where the string of
bits representing the variable’s value is stored. There are basically two kinds of locations in a computer
where such a value may be kept— Memory and CPU registers. It is the variable’s storage class that
determines in which of these two locations the value is stored.

Moreover, a variable’s storage class tells us:

 Where the variable would be stored.


 What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the
default initial value).
 What is the scope of the variable; i.e. in which functions the value of the variable would be
available.
 What is the life of the variable; i.e. how long would the variable exist.

There are four storage classes in C:

(a) Automatic storage class


(b) Register storage class
(c) Static storage class
(d) External storage class

57 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Let us examine these storage classes one by one.

Automatic Storage Class


The features of a variable defined to have an automatic storage class are as under:

Storage Memory.

Default initial value An unpredictable value, which


is often called a garbage value.

Scope Local to the block in which the


variable is defined.

Life Till the control remains within


the block in which the variable
is defined.

Following program shows how an automatic storage class variable is declared, and the fact that if the
variable is not initialized it contains a garbage value.
main( )
{
auto int i, j ;
printf ( "\n%d %d", i, j ) ;
}
The output of the above program could be...
1211 221
where, 1211 and 221 are garbage values of i and j. When you run this program you may get different
values, since garbage values are unpredictable. So always make it a point that you initialize the
automatic variables properly, otherwise you are likely to get unexpected results. Note that the keyword
for this storage class is auto, and not automatic.

Register Storage Class


The features of a variable defined to be of register storage class are as under:

Storage CPU registers.

Default initial value Garbage value.

Scope Local to the block in which the


variable is defined.

58 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Life Till the control remains within
the block in which the variable
is defined.

A value stored in a CPU register can always be accessed faster than the one that is stored in memory.
Therefore, if a variable is used at many places in a program it is better to declare its storage class as
register. A good example of frequently used variables is loop counters. We can name their storage class
as register.
main( )
{
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
}

Static Storage Class


The features of a variable defined to have a static storage class are as under:

Storage Memory
Default initial value Zero
Scope Local to the block in which the
variable is defined.
Life Value of the variable persists
between different function calls.

External Storage Class


The features of a variable whose storage class has been defined as external are as follows:

Storage Memory.
Default initial value Zero.
Scope Global.
Life As long as the program’s
execution doesn’t come to an
end.

int x = 21 ;
main( )
{

59 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


extern int y ;
printf ( "\n%d %d", x, y ) ;
}

int y = 31 ;
Here, x and y both are global variables. Since both of them have been defined outside all the functions
both enjoy external storage class. Note the difference between the following:
extern int y ;
int y = 31 ;
Here the first statement is a declaration, whereas the second is the definition. When we declare a
variable no space is reserved for it, whereas, when we define it space gets reserved for it in memory. We
had to declare y since it is being used in printf( ) before it’s definition is encountered. There was no
need to declare x since its definition is done before its usage. Also remember that a variable can be
declared several times but can be defined only once.

Another small issue—what will be the output of the following program?

int x = 10 ;
main( )
{
int x = 20 ;
printf ( "\n%d", x ) ;
display( ) ;
}
display( )
{
printf ( "\n%d", x ) ;
}
Here x is defined at two places, once outside main ( ) and once inside it. When the control reaches the
printf( ) in main( ) which x gets printed? Whenever such a conflict arises, it’s the local variable that gets
preference over the global variable. Hence the printf( ) outputs 20. When display ( ) is called and
control reaches the printf ( ) there is no such conflict. Hence this time the value of the global x, i.e. 10
gets printed.

Dennis Ritchie has made available to the C programmer a number of storage classes with varying
features, believing that the programmer is in a best position to decide which one of these storage
classes is to be used when. We can make a few ground rules for usage of different storage classes in
different programming situations with a view to:

(a) Economize the memory space consumed by the variables

(b) Improve the speed of execution of the program

60 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


The rules are as under:

o Use static storage class only if you want the value of a variable to persist between
different function calls.

o Use register storage class for only those variables that are being used very often in a
program. Reason is, there are very few CPU registers at our disposal and many of them
might be busy doing something else. Make careful utilization of the scarce resources. A
typical application of register storage class is loop counters, which get used a number
of times in a program.

o Use extern storage class for only those variables that are being used by almost all the
functions in the program. This would avoid unnecessary passing of these variables as
arguments when making a function call. Declaring all the variables as extern would
amount to a lot of wastage of memory space because these variables would remain
active throughout the life of the program.

o If you don’t have any of the express needs mentioned above, then use the auto storage
class. In fact most of the times we end up using the auto variables, because often it so
happens that once we have used the variables in a function we don’t mind loosing
them.

Preprocessor
The preprocessor offers several features called preprocessor directives. Each of these preprocessor
directives begins with a # symbol. The directives can be placed anywhere in a program but are most
often placed at the beginning of a program, before the first function definition.
Have a look at the following program.

#define UPPER 25
void main( )
{
int i ;
for ( i = 1 ; i <= UPPER ; i++ )
printf ( "\n%d", i ) ;
}
In this program instead of writing 25 in the for loop we are writing it in the form of UPPER, which has
already been defined before main( ) through the statement,
#define UPPER 25
This statement is called ‘macro definition’ or more commonly, just a ‘macro’. What purpose does it
serve? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with
25.
Here is another example of macro definition.

61 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


#define PI 3.1415
void main( )
{
float r = 6.25 ;
float area ;
area = PI * r * r ;
printf ( "\nArea of circle = %f", area ) ;
}

UPPER and PI in the above programs are often called ‘macro templates’, whereas, 25 and 3.1415 are
called their corresponding ‘macro expansions’.

Macros with Arguments


The macros that we have used so far are called simple macros. Macros can have arguments, just as
functions can.
Here is an example that illustrates this fact.

#define AREA(x) ( 3.14 * x * x )


void main( )
{
float r1 = 6.25, r2 = 2.5, a ;
a = AREA ( r1 ) ;
printf ( "\nArea of circle = %f", a ) ;
a = AREA ( r2 ) ;
printf ( "\nArea of circle = %f", a ) ;
}

Here’s the output of the program...

Area of circle = 122.656250


Area of circle = 19.625000
In this program wherever the preprocessor finds the phrase AREA(x) it expands it into the statement (
3.14 * x * x ). However, that’s not all that it does. The x in the macro template AREA(x) is an argument
that matches the x in the macro expansion ( 3.14 * x * x ). The statement AREA(r1) in the program
causes the variable r1 to be substituted for x. Thus the statement AREA(r1) is equivalent to:
( 3.14 * r1 * r1 )

#define SQUARE(n) n * n
main( )
{
int j ;
j = 64 / SQUARE ( 4 ) ;
printf ( "j = %d", j ) ;
}

62 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


The output of the above program would be:
j = 64
Whereas, what we expected was j = 4.

Enumeration
An enumeration consists of a set of named integer constants.
enum rainbowcolors { red, orange, yellow, green, blue, indigo, violet };

Now internally, the compiler will use an int to hold these and if no values are supplied, red will be 0,
orange is 1 etc.

enum name {x, y, z=8, w};

Now internally, the compiler will use an int to hold these and x=6, y=7, z=8 and w=9.

Dynamic memory allocation


The process of allocating memory at run time is known as dynamic memory allocation. Although c does
not inherently have this facility there are four library routines which allow this function.
Many languages permit a programmer to specify an array size at run time. Such languages have the
ability to calculate and assign during executions, the memory space required by the variables in the
program. But c inherently does not have this facility but supports with memory management functions,
which can be used to allocate and free memory during the program execution. The following functions
are used in c for purpose of memory management.

Function Task
malloc Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated
space
calloc Allocates space for an array of elements initializes them to zero and returns a pointer to the
memory
free Frees previously allocated space
realloc Modifies the size of previously allocated space.

Allocating a block of memory:


A block mf memory may be allocated using the function malloc. The malloc function reserves a block of
memory of specified size and returns a pointer of type void. This means that we can assign it to any
type of pointer. It takes the following form:

ptr=(cast-type*)malloc(byte-size);
ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with
size byte-size.
Example:
x=(int*)malloc(100*sizeof(int));

63 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


On successful execution of this statement a memory equivalent to 100 times the area of int bytes is
reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int
Allocating multiple blocks of memory:
Calloc is another memory allocation function that is normally used to request multiple blocks of storage
each of the same size and then sets all bytes to zero. The general form of calloc is:
ptr=(cast-type*) calloc(n,elem-size);
The above statement allocates contiguous space for n blocks each size of elements size bytes. All bytes
are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not
enough space a null pointer is returned.
Releasing the used space:
Compile time storage of a variable is allocated and released by the system in accordance with its
storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is
not required. The release of storage space becomes important when the storage is limited. When we no
longer need the data we stored in a block of memory and we do not intend to use that block for storing
any other information, we may release that block of memory for future use, using the free function.
free(ptr);
ptr is a pointer that has been created by using malloc or calloc.

To alter the size of allocated memory:


The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the
situations we can change the memory size already allocated with the help of the function realloc. This
process is called reallocation of memory. The general statement of reallocation of memory is :
ptr=realloc(ptr,newsize);

This function allocates new memory space of size newsize to the pointer variable ptr ans returns a
pointer to the first byte of the memory block. The allocated new block may be or may not be at the
same region.

Assignment-8

1. Explain different types of storage class.


2. What do you mean by dynamic memory allocation explain.
3. Define Enumeration.
4. Write notes on the following:
 Preprocessor
 Macros with Arguments

64 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Chapter 9
File Handling
File Operations
There are different operations that can be carried out on a file. These are:

 Creation of a new file


 Opening an existing file
 Reading from a file
 Writing to a file
 Moving to a specific location in a file (seeking)
 Closing a file

The file <stdio.h> contains declarations for the Standard I/O library and should always be included at the very
beginning of C programs using files.

Constants such as FILE, EOF and NULL are defined in <stdio.h>.

The function fopen is one of the Standard Library functions and returns a file pointer which you use to refer to
the file you have opened e.g.

fp = fopen( “prog.c”, “r”) ;

The above statement opens a file called prog.c in read mode.

Example 9.1 Write a program to read a file and display its contents on the screen.

Solution:

# include <stdio.h>
main( )
{
FILE *fp ;
char ch ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
{
break ;
}
printf ( "%c", ch ) ;
}
fclose ( fp ) ;
}

65 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


Example 9.2 Write a program to read a file and Count no of characters, spaces, tabs and
newlines in the file.

Solution:
# include <stdio.h>
void main( )
{
FILE *fp ;
char ch ;
int nol = 0, not = 0, nob = 0, noc = 0 ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
noc++ ;
if ( ch == ' ' )
nob++ ;
if ( ch == '\n' )
nol++ ;
if ( ch == '\t' )
not++ ;
}
fclose ( fp ) ;
printf ( "\nNumber of characters = %d", noc ) ;
printf ( "\nNumber of blanks = %d", nob ) ;
printf ( "\nNumber of tabs = %d", not ) ;
printf ( "\nNumber of lines = %d", nol ) ;
}

Example 9.3 WAP to copy content of one file to another.

Solution:
#include <stdio.h>
void main( )
{
FILE *fs, *ft ;
int ch ;
fs = fopen ( "pr1.exe", "rb" ) ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( ) ;
}

66 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com


ft = fopen ( "newpr1.exe", "wb" ) ;
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
fclose ( fs ) ;
exit( ) ;
}
while ( 1 )
{
ch = fgetc ( fs ) ;
if ( ch == EOF )
break ;
else
fputc ( ch, ft ) ;
}
fclose ( fs ) ;
fclose ( ft ) ;
}

Assignment-9

1. What is file? Explain different mode of operation on a file.


2. Write a program to read a text file WRONG.DAT and copy to another file RIGHT.DAT.

67 Programming in C by Er. Sourav Kumar Giri, SCE Balasore. Email: sourav.giri4@gmail.com

Vous aimerez peut-être aussi