0% found this document useful (0 votes)
19 views56 pages

C - Notes FULL

The document provides an overview of the C programming language, detailing its history, features, structure, and fundamental concepts such as tokens, keywords, identifiers, variables, constants, data types, and operators. It explains the significance of C as a middle-level language and its applications in system programming, along with the syntax and rules for constructing C programs. Additionally, it covers the various types of operators and their functions within C, emphasizing the importance of understanding these elements for effective programming.

Uploaded by

suraj9873219771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views56 pages

C - Notes FULL

The document provides an overview of the C programming language, detailing its history, features, structure, and fundamental concepts such as tokens, keywords, identifiers, variables, constants, data types, and operators. It explains the significance of C as a middle-level language and its applications in system programming, along with the syntax and rules for constructing C programs. Additionally, it covers the various types of operators and their functions within C, emphasizing the importance of understanding these elements for effective programming.

Uploaded by

suraj9873219771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

PCE Programming for Problem Solving 1FY3-06

History of C
C was developed by a system programmer Dennis Ritchie in 1972, at American Telegraph
& Telecommunication (AT & T) Bell Laboratories in New Jersey USA. The C language is
often referred as middle level language because we can write high level as well as low
level programs through C.

Features of C
• C is a general purpose programming language. You can generate games, business
software, utilities, mathematical models, word processors, spreadsheets and other
kinds of software.
• C is a structured programming language. It uses structured statements such as
while, for loops in place of goto statement which cause bugs (error) in the program.
• System programming: C is used for system programming i.e. writing operating
system. The UNIX operating system is also rewritten from C. Major parts of
Windows, Linux, UNIX are still written in C because speed of execution is very fast
in C compare to other languages.

Structure of C program:
Documentation Section
Link Section (#include section)
Definition Section
Global Declaration Section
Main Function Section
{
Declaration Part
Execution Part
}
Subprogram Section (User Defined Function)
Function 1
Function 2
….
Function n

Preprocessor
A preprocessor is a program that processes our program before it is passed to the
compiler. Preprocessing is the first step of the language processing system. A
preprocessor mainly performs three tasks on the High Level Language code:
1. Removing comments
2. File inclusion
3. Macro expansion

‘C’ Character Set


Character set of a language is set of all the symbols used to write a program in that
language. The characters in C are grouped into four categories:
1. Letters : A-Z or a-z
2. Digits : 0-9
3. Special symbols : ~, ‘! @#%^&*()_-+=\|{}[]:;” .?/
4. White spaces : blank space, tab space, carriage return, new-line, form-feed

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06

C Tokens

Tokens are the smallest individual unit of a program. Each and every punctuation and
word that you come across in a C program is token. C programs are written using these
tokens and the syntax of the language. C has six types of tokens:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators

Keywords or Reserved Words


Keywords are predefined, reserved words used in programming that have special
meanings to the compiler. All keywords have fixed meanings and these meanings cannot
be changed. Keywords serve as basic building blocks for program statements. All
keywords must be written in lowercase. A list of keywords employed in C language:
auto double int struct break else long switch
case enum register typedef char extern return union
const float short unsigned continue for signed void
default goto sizeof volatile do if static while

Identifiers
Identifiers are the names given to variables, arrays, functions, pointers and structures.
These are user-defined names and consist of a sequence of letters and digits, with a letter
as a first character. Both uppercase and lowercase letters are permitted.

Variables
A variable is the name of a memory location that we use for storing data. We can change
the value of a variable and we can also reuse it multiple times. Each variable has a name,
data-type, size and the value it stores.
Rules for constructing variable name in C language are listed below:
• Variable name may be a combination of alphabets, digits or underscores.
• First character must be an alphabet or underscore.
• No commas or blank spaces are allowed in a variable name.
• No word, having a reserved meaning in C can be used for variable name.
• Lower and upper case letters are significant.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06

Constants
A constant in C refer to fixed values that does not change during the execution of a
program. There are four basic types of constants in C:
 Character Constant
 String Constant
 Integer Constant
 Real Constant

Character Constants: A character constant consist of a single character, single digit or


a single special symbol enclosed within a pair of single inverted commas. All escape
sequences are also considered as character constant.
Example:
const char ch = ‘a’;

Backslash Character Constant (Escape Sequence): Certain non-printable characters,


which are used in the printf function are called as escape sequences. They always begin
with backslash (\).

Escape sequence Meaning


\n new line
\t Horizontal tab
\b Back space
\r Carriage return
\a bell alert
\” to print double quotation
\0 null (point end of the string)

String Constants: A string constant is a sequence of one or more characters enclosed


within a pair of double quotes (“ ”). If a single character is enclosed within a pair of double
quotes, it will also be interpreted as a string constant and not a character constant.
Example:
“Bhagirath Chauhan”

Integer Constant: An integer constant refers to a sequence of digits and has a numeric
value. There are three types of integers in C: decimal, octal and hexadecimal.
Decimal integers : 2, 45, -4 etc.
Octal integers : 020, -04 etc.
Hexadecimal integers : 0x4, -0x2B etc.

Real Constants: A number with a decimal point (fractional part) and an optional
preceding sign represents a real constant. A floating point number can also be
represented as exponential or scientific notation. For e.g. 0.0000987 can be written as
9.87*10-5 or 9.87e-05. Thus the general form is:
mantissa e exponent
So 9.87 is called mantissa and -05 is called as exponent.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06

Difference between Variable and Constant

S.No. Variables Constants


Stores data type value in a It is similar to a variable but cannot be
1
program. changed during program execution.
It is a fixed variable that cannot be
It can be changed after defining the
2 changed after defining the variable in
variable in a program.
a program.

Typically, it uses int, float, char, It can be express in two ways:


3 double, etc. data types in a #define preprocessor and the const
program. keyword.
Example: cons tint size = 10; #define
4 Example: int a = 5; float pi = 3.14;
PI 3.14

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06

Data Types

The way a value stored in a variable interpreted is known as its data type. In other words,
data type determines the type and size of data associated with variables. Every computer
language has its own set of data types it supports. A data type signifies two important
aspects:
1. Amount of space (size) to be reserved in the memory to store the data and
2. Nature (type) of data to be stored.

Data Types

Primitive/Primary/Basic Data Types Derived Data Types User Defined Data Types
- int - array - structure
- char - pointers - union
- float - typedef
- double - enum
- void

There are five primary data types in C language:

int data type


Integers are whole numbers with a range of values supported by a particular machine.
The range of an int data type is compiler dependent (2 or 4 bytes). The highest bit (MSB)
of an integer is used to store the sign of the integer. Format specifier for int data type is
%d.
In order to provide some control over the range of numbers and storage space, C has
three classes of integer storage, namely short int, int, and long int, in both signed and
unsigned forms.

short and long (modifiers)


The short and long integers would usually occupy two and four bytes respectively. Format
specifier for short int is %hd and long int is %ld.

signed and unsigned (modifiers)

Sometimes the program requires only positive values. In such cases the data type can be
made unsigned. Thus the range of unsigned short int become 0 to 65535. Format specifier
for unsigned is %u. (unsigned short - %hu).

char data type


A single character can be defined as a character (char) type data. A char data type
occupy one byte memory. The qualifier signed or unsigned may be explicitly applied to
char. A signed char has range from -128 to +127. An unsigned char has a range from 0 to
255. Format specifier for char data type is %c.
Formula to calculate the range of signed data types is 2n-1, and for unsigned data type is
2n where n is the number of bits occupied by the data type.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06

float, double and long double


A float occupies four bytes and can store from -3.4e38 to +3.4e38. If this is insufficient
then C offers a double data type that occupies 8 bytes in memory and has a range from -
1.7e308 to +1.7e308. If this is also insufficient then there is a long double that occupies 10
(12 in VS Code, 16 in Linux gcc) bytes and has a range from -1.7e4932 to +1.7e4932.
Format specifier for float is %f, for double %lf and for long double is %Lf.

void type
The void type has no values. This is usually used to specify the type of functions. It can
also play the role of a generic type, meaning that it can represent any of the other
standard types.

typedef
C supports a feature known as ‘type definition’ that allows users to define an identifier that
would represent an existing data type. The user-defined data type identifier can later be
used to declare variables. It takes the general form:
typedef type identifier;
where type refers to an existing data type and ‘identifier’ refers to the ‘new’ name given to
the data type.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6


PCE PPS  Operators 1FY3-06

Operators
An operator is a symbol that operates on a value or a variable. An operator tells the
computer to perform certain mathematical or logical manipulations. For example: + is an
operator to perform addition.
Operators

Unary Operators Binary Operators Ternary Operator


(Conditional Operator)
Expression
An expression is a sequence of operands and operators that reduce to a single value.
When both the operands in a single arithmetic expression are integers, the expression is
called an integer expression.
Integer arithmetic always yields an integer value. An arithmetic operation involving only
real operands is called real arithmetic expression.
When one of the operands is real and the other is integer, the expression is called a
mixed-mode arithmetic expression and the result is always a real number.
C operators can be classified into a number of categories. They include:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

Arithmetic operators
C provides all the basic arithmetic operators. The operators +, -, *, and / all work the same
way as they do in other languages. The % operator is known as modulus operator. It
produces the remainder after the division of two operands.
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division

Relational Operators
Relational operators is used to compare two operands. While the operands can be
variables, constants or expressions, the result is always either true or false.
Operator Meaning
== is equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE PPS  Operators 1FY3-06

Logical Operators
More than one relational expression can be combined to form a single compound
relational expression using logical operators. A compound relation behaves identically
producing either true or false. C provide three logical operators:
Logical AND (&&) Logical OR (||) Logical NOT (!)
ex1 ex2 ex1&&ex2 ex1 ex2 ex1||ex2 exp !exp
0 0 0 0 0 0 0 1
1 0 0 1 0 1 1 0
0 1 0 0 1 1
1 1 1 1 1 1

Assignment Operators
Assignment operators are used to store the result of an expression to a variable. Note that
data type of both the sides should be either the same or compatible to each other.
Short Hand Assignment operator
x += 10; is equivalent to x = x + 10;
x -= 10; is equivalent to x = x - 10;
x *= 10; is equivalent to x = x * 10;
x /= 10; is equivalent to x = x / 10;
x %= 10; is equivalent to x = x % 10;

Increment and Decrement Operators


These are unary operators as they require only one operand. When they are used before
the operand, it is termed as prefix, while when used after the operand, they are termed as
postfix operators. In expressions, postfix operator uses the current value and then
increments/decrements while in the prefix form the value is incremented/ decremented
first and then used in the expression.
For example,
b = a++; is equivalent to
b = a;
a = a + 1;
and b = -- a;
is equivalent to
a = a - 1;
b = a;

Conditional Operator
This is the only ternary type operator in C. This operator is a set of two operators (? and :)
which work together. Conditional operator always works with relational operators.
Syntax:
condition ? exp1 : exp2;
If condition is true then value returned will be exp1, otherwise the value returned will be
exp2.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE PPS  Operators 1FY3-06

Bitwise Operators
Bitwise operators are used for manipulation of data at bit level. These operators are used
for testing the bits, or shifting them right or left. Bitwise operators may not be applied to
float or double. A list of different bitwise operators are:
1. Bitwise Logical Operators
(i) Bitwise Logical AND (&)
(ii) Bitwise Logical OR (|)
(iii) Bitwise Logical XOR (^)
2. Bitwise Shift Operators
(i) Bitwise Left shift (<<)
(ii) Bitwise Right shift (>>)
6. Bitwise One’s complement (~)

Bitwise AND Operator


The & operator operates on two operands. While operating upon these two operands they
are compared on a bit-by-bit basis.
op1 op2 op1&op2
0 0 0
1 0 0
0 1 0
1 1 1

Bitwise OR Operator
The truth table for the OR operator is:
op1 op2 op1|op2
0 0 0
1 0 1
0 1 1
1 1 1

Bitwise XOR Operator


The truth table for the XOR operator is:
op1 op2 op1^op2
0 0 0
1 0 1
1 0 1
1 1 0

Bitwise Right Shift Operators


It shifts each bit in its left operand to the right. Thus, ch>>2 would shift all bits in ch two
places to the right. Similarly, ch>>3 would shift all bits in ch 3 places to the right. The
blanks created on the left are always filled with zeros.
Example:
int n = 16;
n = n>>2; //now n will be 4

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE PPS  Operators 1FY3-06

Bitwise Left Shift Operator


In left shift the bits are shifted to the left, and for each bit shifted, a 0 is added to the right
of the number.
Example:
int n = 4;
n = n<<2; //now n will be 16

One’s Complement Operator


Also referred as the complementation operator. It is a unary operator that inverts the bits
of the operand, i.e. all 0 become 1 and all 1 become o. The operand for the operator must
always be an integer value.

Special Operators
C language also provides number of special operators which have no counter parts in
other languages.
1. comma operator
2. sizeof operator
3. pointer operators (& and *)
4. member selection operators (. and ->)

Comma Operator
This operator is generally used in the for loop. The expression separated by comma
operator are solved from left to right.

sizeof operator
The sizeof operator works on variables, constants and even on all the data types. It
returns the number of bytes the operand occupies in the memory.

Type Casting (Data Type Conversion)


Typecasting is basically a process in C in which we change a variable belonging to one
data type to another one. The process of type casting can be performed in two major
types in a C program. These are:
1. Implicit Type conversion
2. Explicit Type Conversion
C automatically converts any intermediate values to the proper type so that the expression
can be evaluated without loosing any significance. This automatic conversion is known as
implicit type conversion. During evaluation, if the operands are of different types, the
‘lower’ type is automatically converted to the ‘higher’ type before the operation proceeds.
There are instances when we want to force a type conversion in a way that is different
from the automatic conversion. The process of such a local conversion is known as
explicit conversion or casting a value. Example:
int x = 10, y = 4;
float z;
z = x/y;
z = (float) x / (float) y;

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE PPS  Operators 1FY3-06

Operator Precedence and Associativity


Operator precedence determines which operator is performed first in an expression with
more than one operators with different precedence. Precedence defines the sequence in
which operators are to be applied on the operands, while evaluating the expressions
involving more than one operators. The operators at the higher level of precedence are
evaluated first. Operators of same precedence are evaluated from left to right or right to
left, depending upon the level. This is known as associativity property of an operator.

Rules of Precedence and Associativity


 Precedence rules decides the order in which different operators are applied.
 Associativity rule decides the order in which multiple occurrences of the same level
operator are applied.

Rank Operator Description Associativity


() Function Call
1 Left to Right
[] Array Element Reference
+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Logical negation
2 Right to Left
~ Once complement
* Pointer reference (indirection)
& Address
sizeof Size of an object
(type) Type cast (conversion)
* Multiplication
3 / Division Left to Right
% Modulus
+ Addition
4 Left to Right
– Subtraction
<< Left shift
5 Left to Right
>> Right shift
< Less than
<= Less than or equal to
6 Left to Right
> Greater than
>= Greater than or equal to
== Equality
7 Left to Right
!= Inequality
8 & Bitwise AND Left to Right
9 ^ Bitwise XOR Left to Right
10 | Bitwise OR Left to Right
11 && Logical AND Left to Right
12 || Logical OR Left to Right
13 ?: Conditional expression Right to Left
=
14 *= /= %= += -= &= ^= |= Assignment operators Right to Left
<<= >>=
15 , Comma operator Left to Right

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 2FY3-06

Input / Output Functions

Input means to provide the program with some data to be used in the program and output
means to display data on the screen or write the data to a printer or a file.
Reading, processing, and writing of data are the three essential functions of a computer
program. There are two methods of providing data to the program variable. One method is
to assign values to variables through the assignment statements, and another method is
to use the input function which can read data from a keyboard. C programming language
provides standard library functions to read any given input and display data on the
console.

Reading / Writing a Character


Reading a single character can be done by using the getchar function. This can also be
done with the help of the scanf function.
Var_name = getchar();
Like getchar, putchar is used for writing characters one at a time to the terminal. It takes
the form as shown below:
putchar(Var_name);

Formatted Input Function (scanf Function)


Formatted input refers to an input data that has been arranged in a particular format. This
is possible in C using the scanf function. The general form of scanf is:
int scanf ( “control string” , arg1, arg2, …. argn );
Control string (also known as format string) contains field specifications, which direct the
interpretation of input data. It may include conversion character %, a format specifier, field
width, blanks, tabs, or backslash character.

Formatted Output Function (printf Function)


For outputting results we have used extensively the function printf which sends results out
to a terminal. The printf function provides certain features that can be effectively exploited
to control the alignment and spacing of print-outs on the terminals. The general form of
printf statement is:
int printf ( “control string” , arg1, arg2, …., argn );
control string consists of following there types of items:
 Characters that will be printed on the screen as they appear.
 Format specifications that define the output format for display of each item.
 Escape sequence characters such as \n, \t, \b etc.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 2FY3-06

Decision Making and Branching / Control Statements


Control Statements enable us to specify the flow of program control; i.e., the order in
which the instructions in a program must be executed. Using control statements we can
control the flow of program in such a way so that it executes certain statements based on
the outcome of a condition (i.e. true or false). In C, decision control statements are:
1. if statement
2. switch statement
3. conditional operator statement
4. goto statement
These statements are popularly known as decision-making statements. Since these
statements ‘control’ the flow of execution, they are also known as control statements.

if Statement
The if statement in C is used to perform the operations based on some specific condition.
The operations specified in if block are executed if and only if the given condition is true.
if ( expression )
{
//code to be executed
}
It allows the computer to evaluate the expression first and then, depending on whether the
value of the expression is true or false.

Entry

True
Condition

False

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 ladder

Simple if statement
The syntax of a simple if statement is
if ( expression )
{
Statements;
}
Stmt;

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 2FY3-06

Entry

True
Condition

Statements

Stmt

The ‘Statements’ may be a single statement or a group of statements. If the test


expression is true, the statements will be executed, otherwise execution will jump to the
‘Stmt’.

The if…else Statement


The if…else statement is an extension of the simple if statement which, we can perform
two different operations, i.e., one is for the true of that condition, and the other is for the
false of the condition. The syntax of the if-else statement is:
if ( expression )
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}

Entry

False True
Condition

Statements2 Statements1

Stmt

Example: Check whether a number is even or odd.


#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2 == 0)

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 2FY3-06

{
printf("%d is even number", number);
}
else
{
printf("%d is odd number", number);
}
return 0;
}
Output:
enter a number:4
4 is even number
enter a number:5
5 is odd number

Nested if…else Statement

When a series of decisions are involved, we may have to use more than one if…else
statement in nested form as shown below:
if ( expression 1 )
{
if ( expression 2 )
{
Statement 1;
}
else
{
Statement 2;
}
}
else
{
Statement 3;
}
Stmt;
If the expression 1 is false, the statement 3 will be executed, otherwise it continues to
perform the second test. If the expression 2 is true, the statement 1 will be evaluated,
otherwise the statement 2 will be evaluated.
Example: Write a program to determine whether the input year is a leap year or not.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year % 100 == 0)
{
if(year % 400 == 0)
printf("\nLeap Year");
else

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 2FY3-06

printf("\nNot a leap year");


}
else
{
if( year % 4 == 0)
printf("\nLeap year");
else
printf("\nNot a leap year");
}
return 0;
}
Output:
Enter a year: 2020
Leap year

The else if ladder


There is another way of putting ifs together when multipath decision are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an
if. It takes the following general form:
if ( expression-1 )
statement-1;
else if (expression-2 )
statement-2;
else if ( expression-3 )
statement-3;
else if ( expression-4 )
statement-4;
else
statement-5;
statement-n;
This construct is known as the else if ladder. The conditions are evaluated from the top,
downwards. As soon as a true condition is found, the statement associated with it is
executed and the control is transferred to the statement-n. When all the n condition
become false, then the final else containing statement-5 will be executed.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 2FY3-06

The switch Statement


C has a built-in multiway decision statement known as a switch. The switch statement
tests the value of a given variable (or expression) against a list of case values and when a
match is found, a block of statements associated with that case is executed. The general
form of the switch statement is as shown below:
switch ( expression ) {
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
…….
default: //optional
default-stmts;
}

Rules for switch statement


 The switch expression must be of an integer or character type.
 The case value must be an integer or character constant and are known as case
labels which ends with a colon (:).
 Duplicate case value are not allowed.
 The case value can be used only inside the switch statement.
 The break statement at the end of each block signals the end of a particular case
and causes an exit from the switch statement. It is optional.
 The default is an optional case. When present, it will be executed if the value of the
expression does not match with any of the case values.
 You can have switch statements inside another switch statement (Nested switch).
The main reason for using a switch include improving clarity, by reducing otherwise
repetitive coding, and also offering the potential for faster execution through easier
compiler optimization in many cases.

Limitation of switch statement


 It is good for equality comparisons but not for range comparisons.
 It works well for int type data only and not good for other data types.
 It works well with constants but not with variables (as case labels).
Example: Write a program to perform simple calculation (+, -, *, / and %).
void main() {
char operator;
float n1, n2;
printf("Enter an operator (+, -, *, /) : ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f",&n1, &n2);
switch(operator) {
case '+': printf("%.2f + %.2f = %.2f", n1, n2, n1+n2);
break;
case '-': printf("%.2f - %.2f = %.2f", n1, n2, n1-n2);
Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6
PCE Programming for Problem Solving 2FY3-06

break;
case '*': printf("%.2f * %.2f = %.2f",n1, n2, n1*n2);
break;
case '/': printf("%.2f / %.2f = %.2f",n1, n2, n1/n2);
break;
default: printf("Sorry!! Operator is not matched");
}
}

Difference between if-else and switch

Title if – else switch case


If statement is used to select The switch statement is used to select
Definition among two alternatives. among multiple alternatives.
It contains either logical or It contains a single expression which can
Expression equality expression. be either a character or integer variable.
It evaluates all types of data, It evaluates either an integer, or
Evaluation such as integer, floating-point, character.
character or Boolean.
If the condition is not true, If the value does not match with any
Default then by default, else block will case, then by default, default statement
execution be executed. is executed.
Editing is not easy in the if- Cases in a switch statement are easy to
else statement. maintain and modify. Therefore, we can
Editing say that the removal or editing of any
case will not interrupt the execution of
other cases.
If there are multiple choices If we have multiple choices then the
implemented then the speed switch statement is the best option as
Speed of the execution will be slow. the speed of the execution will be much
higher than if-else.

Conditional Operator ( ? : )
The C language has an unusual operator, useful for making two-way decisions. This
operator is a combination of ? and :, and takes three operands. This operator is known as
the conditional operator. The syntax of conditional operator is:
exp1 ? exp2 : exp3 ;
The exp1 is evaluated first. If the result is true, exp2 is evaluated otherwise exp3 is
evaluated and its value is returned. For example, the segment
if ( a > b )
max = a;
else
max = b;
can be written as
max = ( a > b ) ? a : b ;

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 7


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Iteration / Looping and Decision Making

Loops or iterations are used when we want to execute a statement or set of statement
many times. A program loop therefore consists of two segments, one known as the body
of the loop and the other known as the control statement (test condition). The control
statement tests certain conditions and then directs the repeated execution of the
statements contained in the body of the loop.
Depending on the position of the control statement in the loop, a control structure may be
classified either as the entry-controlled loop or as the exit-controlled loop.

Entry Entry

False True Loop


Condition Body

Loop
Body
True
Condition

False

Entry Controlled Loop Exit Controlled Loop

The C language provides for three constructs for performing loop operations. They are:
1. The while statement
2. The do statement
3. The for statement

The while statement


The while is an entry-controlled loop statement. The condition is evaluated and if the
condition is true, then the body of the loop is executed. This process of repeated
execution of the body continues until the condition finally becomes false and the control is
transferred out of the loop. The basic format of the while statement is:
while ( condition )
{
//code to be executed;
}

The do statement
The do is an exit controlled loop. On some occasions it might be necessary to execute the
body of the loop before the test is performed. Such situations can be handled with the help
of the do statement. This takes the form:
do
{
//code to be executed;
} while (condition );

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

On reaching the do statement, the program proceeds to evaluate the body of the loop
first. At the end of the loop, the condition in the while statement is evaluated. If the
condition is true, the program continues to evaluate the body of the loop again. This
process continues as long as the condition is true.

Difference between while and do while loop

S.No. while loop do-while loop


1 while loop is an entry controlled loop. do-while is en exit controlled loop.
Condition is checked first then Statement(s) is executed atleast once,
2
statement(s) is executed. thereafter condition is checked.
The while loop terminates when the As long as the condition is true, the
3
condition becomes false. compiler keeps executing the loop.
It might occur statement(s) is executed At least once the statement(s) is
4
zero times, if condition is false. executed.
5 No semicolon at the end of while. Semicolon at the end of while.
If there is a single statement, brackets
6 Brackets are always required.
are not required.
Variable in condition is initialized before Variable may be initialized before or
7
the execution of loop. within the loop.
while loop is not used for creating It is mostly used for creating menu-
8
menu-driven programs. driven programs.
Syntax of while loop: Syntax of do-while loop
while (condition) do
9 { {
Statement block; Statement block;
} } while (condition);

The for statement

The for loop is another entry-controlled loop that provides a more concise loop control
structure. The general form of the for loop is:
for ( initialization ; condition ; increment/decrement/update )
{
//Loop Body
}
The execution of the for statement is as follows:
1. Initialization of the control variables is done first, using assignment statements.
2. The value of the control variable is tested using the condition. If the condition is
true, the loop body is executed, otherwise the loop is terminated.
3. After executing loop body, the control variable is incremented or decremented, and
new value of the control variable is again tested.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Jumping Out of a Loop (Unconditional branching)


Jump statement makes the control jump to another section of the program
unconditionally when encountered. It is usually used to terminate the loop or switch case
instantly. It is also used to escape the execution of a section of the program. There are
following jump statements offered by C language:
1. break
2. continue
3. goto
4. return

break: When a break statement is encountered inside a loop, the loop is immediately
exited and the program continues with the statement immediately following the loop.
When the loop are nested, the break would only exit from the loop containing it.
Example: Add only positive numbers, if negative value is entered then jump out from loop.
#include <stdio.h>
int main()
{
int i, n, sum = 0;
for ( i = 1; i <= 5; i++)
{
printf(“\nEnter number: ”);
scanf(“%d”, &n);
if ( n < 0)
break;
sum += n;
}
printf(“\nSum = %d”, sum);
return 0;
}
continue: The continue causes the loop to be continued with the next iteration after
skipping any statements in between.
Example: Add only positive numbers out of 5 inputted numbers.
#include <stdio.h>
int main()
{
int i, n, sum = 0;
for ( i = 1; i <= 5; i++)
{
printf(“\nEnter number: ”);
scanf(“%d”, &n);
if ( n < 0)
continue;
sum += n;
}
printf(“\nSum = %d”, sum);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

The goto Statement

C supports the goto statement to branch unconditionally from one point to another in the
program. The goto requires a label in order to identify the place where the branch is to be
made. A label is any valid variable name, and must be followed by a colon. The label is
placed immediately before the statement where the control is to be transferred.

goto label;
….
label: statement;

return Statement

Return jump statement is usually used at the end of a function to end or terminate it with
or without a value. It takes the control from the calling function back to the main function
(main function itself can also have a return).

Nesting of Loops
Nesting of loops, that is, one loop statement within another loop statement, is allowed in
C. For example, two loops can be nested as follows:

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


{
while ( condition )
{
Statement(s);
}
}
The nesting may continue up to any desired level. (ANSI C allows up to 15 levels of
nesting).

Example: Print the factorial of a given number.

#include <stdio.h>
int main()
{
int i, num, fact = 1;
printf(“Enter a number: “);
scanf(“%d”, &num);
for(i = 2; i <=num; i++)
{
fact = fact * i;
}
printf(“\nFactorial of %d = %d”,num, fact);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Example: Print the sum of digits of a number.


#include <stdio.h>
int main()
{
int num, temp, remainder, sum = 0;
printf(“Enter a number: ”);
scanf(“%d”, &num);
temp = num;
while(temp != 0)
{
remainder = temp % 10;
sum = sum + remainder;
temp = temp / 10;
}
printf(“\nSum of digits of %d = %d”, num, sum);
return 0;
}

Example: Print the reverse of a number.


#include <stdio.h>
int main()
{
int num, temp, reverse = 0, remainder;
printf(“Enter a number: ”);
scanf(“%d”, &num);
temp = num;
while(temp != 0)
{
remainder = temp % 10;
reverse = (reverse * 10) + remainder;
temp = temp / 10;
}
printf(“\nReverse of %d = %d”, num, reverse);
return 0;
}

Example: Calculate the sum of first n natural numbers.


#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf(“Enter a positive number: “);
scanf(“%d”,&num);
for( i = 1; i <= num; i++)
{
sum = sum + i;
}
printf(“\nSum = %d”, sum);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Example: Print the following pattern.

1
22
333
4444

#include <stdio.h>
int main()
{
int line, i , j;
printf(“\nEnter number of lines: “);
scanf(“%d”, &line);
for( i = 1; i <= line; i++)
{
printf(“\n”);
for( j = 1; j <= i ; j++)
{
printf(“%d “, i );
}
}
return 0;
}

Example: Program for check prime number.


#include <stdio.h>
int main()
{
int i , num, flag = 1;
printf(“\nEnter a number: “);
scanf(“%d”,&num);
for(i = 2; i <= num / 2 ; i++)
{
If(num % i == 0)
{
flag = 0;
break;
}
}
If ( flag == 1)
printf(“\nNumber is Prime”);
else
printf(“\nNumber is NOT Prime”);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Array

An array is defined as the collection of similar type of data items stored at contiguous
memory locations. It is simply a grouping of like-type data. Arrays are the derived data type
in C language which can store the primitive type of data such as int, float, char, double etc.
In its simplest form, an array can be used to represent a list of numbers, or a list of names.

Properties of Array
 Each element of an array is of same data type and carries the same size.
 Array elements are accessed by using an integer index. Array index starts with 0 and
goes till size of array minus 1.
 Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location (base address).
 Name of the array is also a constant pointer to the first element of the array.
 Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of the data
element.

Advantages of Array
 Code Optimization: Less code to the access the data.
 Ease of traversing: By using the loop, we can retrieve the elements of an array
easily.
 Ease of sorting: To sort the elements of the array, we need a few lines of code only.
 Random Access: We can access any element randomly using the array.

Types of Array

We can use arrays to represent not only simple lists of values but also tables of data in two,
or more dimensions. Following are the types of an array:
1. One-dimensional arrays
2. Two-dimensional arrays
3. Multidimensional arrays

One-Dimensional Arrays

A list in which the elements are accessible by the variable name assigned to the list and its
subscript is known as a one-dimensional array.

Declaration of one-dimensional arrays

Like any other variable, arrays must be declared before they are used so that the compiler
can allocate space for them in memory. The general form of array declaration is:
Type array_name [ size ];
The Type specifies the type of element that will be contained in the array and size indicates
the maximum number of elements that can be stored inside the array. For example:
int list [10] ;

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

declares the list as an array to contain a maximum of 10 integer.


The C language treats character string simply as arrays of characters. For example:
char name [15];
declares the name as a character array (string) variable that can hold a maximum of 15
characters.

Initialization of One-Dimensional Arrays

We can initialize the elements of arrays in the same way as the ordinary variables when
they are declared. For example:
int list [10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; or
int list [ ] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
char name [15] = {‘B’, ‘h’, ‘a’, ‘g’, ‘I’, ‘r’, ‘a’, ‘t’, ‘h’, ‘\0’ }; or
char name [ ] = “Bhagirath”;
The values to the array elements can also be assigned as follows:
list[ 0] = 10;
list [1] = 20;
list [2] = 30;
The values to the array elements can also be initialized at run time as follows:
for( i = 0; i < 5; i++)
{
scanf(%d”, &list[i] );
}

Example:
#include <stdio.h>
int main()
{
int arr[10], i;
printf(“Enter 10 numbers: “);
for( i = 0; i < 9; i++)
scanf(“%d”, &arr[i]);
printf(“\nElements of an Array are: \n”);
for( i = 0; i < 9; i++)
printf(“%d\t”, arr[i]);
return 0;
}

Example: Search an element of the array (linear search method).


#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, flat = 0, item, i;
printf(“Enter the size of an array: “);
scanf(“%d”,&n);
printf(“\nEnter %d elements: ”, n);
for( i = 0; i < n; i++)

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

scanf(“%d”,&arr[i]);
printf(“\nEnter item to be search: ”);
scanf(“%d”,&item);
for( i = 0 ; i < n; i++)
{
if( arr[i] == item)
{
flag = 1;
break;
}
}
if( flag == 1)
printf(“\nitem is present at %d location”, i);
else
printf(“\nitem is not present in the array”);
return 0;
}

Example: Write a program to implement bubble sort in C language.


#include <stdio.h>
int main()
{
int i, j, temp;
int arr[5] = {10, 32, 30, 12, 28};
printf("\nBefore sorting array elements are:\n");
for (i = 0; i < 5; i++)
printf("%4d", arr[i]);
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for (i = 0; i < 5; i++)
printf("%4d", arr[i]);
return 0;
}
Output:
Before sorting array elements are:
10 32 30 12 28
After sorting array elements are:
10 12 28 30 32

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Two-Dimensional Array

The 2D array can be defined as an array of arrays. The two-dimensional (2-D) array is also
called a matrix. C allows to define table (matrix) of items by using two-dimensional arrays.
Each dimension of the array is indexed from zero to its maximum size minus one. Two-
dimensional arrays are declared as follows:
Type arr_name [rows] [columns];
Example: int table[2][3];
We think of this table as a matrix consisting of two rows and three columns.

Initialization of Two-Dimensional Arrays

As like the one-dimensional array, two-dimensional arrays may be initialized by following


their declaration with a list of initial values enclosed in braces. For example:
int table[2][3] = { 2, 4, 6, 8, 10, 12}; or
int table[2][3] = { {2, 4, 6}, {8, 10, 12} };

Example:
#include <stdio.h>
int main()
{
int i, j;
int arr[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
printf(“\nArray elements are:\n”);
for ( i =0; i < 3; i++)
{
printf(“\n”);
for (j = 0; j < 3; j++)
{
printf(“%4d”, arr[i][j]);
}//end of inner loop
}//end of outer loop
return 0;
}

Memory Layout

The subscripts in the definition of a two-dimensional array represent rows and columns.
This format maps the way that data elements are laid out in the memory. The elements of
all arrays are stored contiguously in increasing memory location, essentially in a single
list. If we consider the memory as a row by bytes, with the lowest address on the left and
the highest address on the right, a simple array will be stored in memory with the first
element at the left end and the last element at the right end. Similarly, a two-dimensional
array is stored “row-wise”, starting from the first row and ending with the last row, treating
each row like a simple array.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

Multi-Dimensional Arrays

C allows arrays of three or more dimensions. The exact limit is determined by the compiler.
The general form of a multi-dimensional array is:
Type arr_name[s1][s2][s3];
Where s1, s2 and s3 are the size of the dimension. For example:
int report[2][2][3];

Static and Dynamic Arrays

An array created at compile time by specifying size in the source code has a fixed size and
cannot be modified at run time. The process of allocating memory at compile time is known
as static memory allocation and the arrays that receive static memory allocation are called
static arrays.
In C it is possible to allocate memory to arrays at run time. This feature is known as
dynamic memory allocation and the array created at run time are called dynamic arrays.
Dynamic arrays are created using pointer variables and memory management functions
malloc, calloc and realloc.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06 / 2FY3-06

String-Handling Functions

The C library supports a large number of string-handling functions that can be used to carry
out many of the string manipulations. Following are the most commonly used string-
handling functions:

strlen() Function
This function counts and returns the number of characters in a string. It takes the form:
len = strlen(str);
Where len is an integer variable, which receives the value of the length of the string str.
The argument may be a string constant. For example:
printf( “Total characters = %d”, strlen(“Bhagirath”) );
will print “Total characters = 9”.

strcpy() Function
The strcpy function works almost like a string-assignment operator. It takes the following
form:
strcpy( str1, str2);
and assigns the contents of str2 to str1. str2 may be a character array variable or a string
constant. For example, the statement
strcpy(name, “Bhagirath”);
will assign the string “Bhagirath” to the string variable name.

strcat() Function
The strcat function joins two strings together. It takes the following form:
strcat (str1, str2);
str1 and str2 are character arrays. When the function strcat is executed, str2 is appended
to str1. The string at str2 remains unchanged. For example:
strcpy(name, “Bhagirath”);
strcat(name, “Singh”);

strcmp() Function
The strcmp function compares two strings identified by the arguments and has a value 0 if
they are equal. It they are not, it has the numeric difference between the first nonmatching
characters in the strings. It takes the form:
strcmp (str1, str2 );
str1 and str2 may be string variables or string constants. Examples are:
strcmp(name1, name2);
strcmp(name, “Bhagirath”);

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6


PCE Programming for Problem Solving 1FY3-06/2FY3-06

Pointers

A pointer is a variable which stores the address of another variable. A pointer is a derived
data type in C. It is built from one of the fundamental data types available in C. Pointers
contain memory addresses as their values. The purpose of pointer is to save memory
space and achieve faster execution time.
Pointers are used frequently in C, as they offer a number of benefits to the programmers.
They include:
 Pointers can be used to return multiple values from a function via function
arguments.
 Pointers permit references to functions and thereby facilitating passing of functions
as arguments to other functions.
 The use of pointer arrays to character strings results in saving of data storage space
in memory.
 Pointers allow C to support dynamic memory management.
 Pointers provide an efficient tool for manipulating dynamic data structures such as
structures, linked lists, queues, stacks and trees.

Declaring Pointer Variables


Since pointer variables contain addresses that belong to a separate data type, they must
be declared as pointer before we use them. The declaration of a pointer variable takes the
following form:
Data_type * p_name;
This tells the compiler three things about the variable p_name.
1. The asterisk (*) tells that the variable p_name is a pointer variable.
2. p_name needs a memory location.
3. p_name points to a variable of type Data_type.
For example:
int *ptr; //pointer to an integer
float *p; //pointer to float
declares the variable ptr as a pointer variable that points to an integer data type and p as a
pointer variable that points to a float data type.
The data type of pointer and the variable must match, an int pointer can hold the address
of int variable, and similarly a pointer declared with float data type can hold the address of
a float variable.

Initialization and Accessing


Once a pointer has been assigned the address of a variable, we can access the value of
the variable using the operator * (asterisk), usually known as the indirection operator.
Another name for the indirection operator is the dereferencing operator.
int num, *ptr, val;
num = 25;
ptr = &num;
val = *ptr;

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06/2FY3-06

Example:
#include <stdio.h>
void main()
{
int n = 10;
int *ptr;
ptr = &n;
printf(“Address of n = %p”, &n); //using address of operator of variable n
printf(“\nAddress of n = %p”, ptr); //using pointer
printf(“\nValue of n = %d”, *ptr); //using dereference operator
}

Pointer Arithmetic
There are only a few operations that are allowed to perform on pointers in C language.
The operations are slightly different from the ones that we generally use for mathematical
calculations. The operations are:
 Increment/Decrement of a pointer (++ or --)
 Addition of integer to a pointer ( + or +=)
 Subtraction of integer to a pointer (- or -=)
 Subtracting two pointers of the same type
 Comparison of two pointers. (p == ptr or p == NULL)
 Pointer arithmetic is meaningless unless performed on an array.

Void (Generic) Pointer

Void pointer (void *) is a pointer that points to some data location in storage, which doesn’t
have any specific type. Void refers to the type. Basically the type of data that is points to is
can be any. If we assign address of char data type to void pointer it will become char
pointer, if int data type then int pointer and so on. Any pointer type is convertible to a void
pointer hence it can point to any value.
Example:
#include <stdio.h>
void main()
{
int inum = 10;
float fnum = 2.2;
void* ptr; //void/generic pointer
ptr = &inum;
// (int*)ptr – does type casting of void
//*((int*)ptr) – dereferences the typecasted void pointer variable
printf("\nValue of inum = %d",*((int*)ptr));
ptr = &fnum; //void pointer is now float
printf("\nValue of fnum = %.2f",*((float*)ptr));
}
Output:
Value of inum = 10
Value of fnum = 2.20

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06/2FY3-06

Pointer to Pointer (Chain of Pointers)


It is possible to make a pointer to point to another pointer. A variable that is a pointer to a
pointer must be declared using additional indirection operator symbols in front the name.
For example:
int *p, **ptr, num;
This declaration tells the compiler that p is a pointer and ptr is a pointer to a pointer of int
type.
p = &num;
ptr = &p;
**p = 15;

Pointers to an Array

When an array is declared, the compiler allocates a base address and sufficient amount
of storage to contain all the elements of the array in contiguous memory locations.
Suppose we declare an array num as follows:
int num[50] = { 10, 20, 30, 40, 50 };
if we declare ptr as an integer pointer, then we can make the pointer ptr to point to the
array num by the following assignment:
ptr = num; or ptr = &num[0];
now we can access every value of num using ptr++ to move from one element to
another.
Pointers can be used to manipulate two-dimensional array as well. For example:
int v[2][3]; is a two-dimensional array,
int *ptr;
if we declare ptr as an int pointer with the initial address of &v[0][0], then
v[i][j] is equivalent to *(ptr+4 * i + j );

Pointers and Character Strings


C supports a method to create strings using pointer variable of type char. Example
char *name = “Bhagirath”;
This creates a string for the literal and then stores its address in the pointer variable
name. Like in one-dimensional arrays, we can use a pointer to access the individual
characters in a string.

Array of Pointers
An array of pointers would be a collection of addresses. The addresses present in it can
be addresses of isolated variables or addresses of array elements or any other
addresses. For example:
int *ptr[3]; //array of integer pointers
int a = 10, b = 20, c = 30;
Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3
PCE Programming for Problem Solving 1FY3-06/2FY3-06

ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
One important use of pointers is in handling of a table of strings. Consider the following
array of strings:
char names[3][20];
This says that the name is a table containing three names, each with a maximum length
of 20 characters. We know that rarely the individual strings will be of equal length.
Therefore, instead of making each row a fixed number of characters, we can make it a
pointer to a string of varying length. For example,
char *names[3] = { “Bhagirath”, “Vedant”, “Suraj” };
declares names to be an array of three pointers to characters, each pointer pointing to a
particular name.

Pointers and Structures


We know that the name of an array stands for the address of its zeroth element. The
same thing is true of the names of arrays of structure variables. Suppose students is an
array variable of struct type. The name students represents the address of its zeroth
element.
struct record
{
int rollno;
char name[30];
float per;
} students[5], *ptr;
ptr = students;
The pointer ptr will now point to students[0]. Its members can be accessed using the
following notation:
ptr -> rollno;
ptr ->name;
ptr -> per;
The symbol -> is called the arrow operator (also known as member selection operator).
We could also use the notation:
(*ptr).rollno;

Disadvantages of Pointers
 Pointers are a little complex to understand.
 Pointers can lead to various errors such as segmentation faults or can access a
memory location which is not required at all.
 If an incorrect value is provided to a pointer, it may cause memory corruption.
 Pointers are also responsible for memory leakage.
 Pointers are comparatively slower than that of the variables.
 Programmers find it very difficult to work with the pointers; therefore it is
programmer’s responsibility to manipulate a pointer carefully.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06/2FY3-06

Dynamic Memory Allocation

The process of allocating memory during program execution is called dynamic memory
allocation. Dynamic Memory Allocation is manual allocation and freeing of memory
according to our programming needs.
C provides some functions to achieve these tasks. There are 4 library functions provided
by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C
programming. They are:
1. malloc()
2. calloc()
3. free()
4. realloc()

malloc() Function
 malloc or ‘memory allocation’ function is used to allocate space in memory during
theexecution of the program.
 malloc function does not initialize the memory allocated during execution. It carries
garbage value.
 malloc function returns null pointer if it couldn't able to allocaterequested amount of
memory.
Syntax:
ptr = (castType*) malloc (size in bytes);
Example: Create a Dynamic array of n elements and print the sum of elements.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int size, i. *ptr, sum = 0;
printf(“\nEnter the size of an array: ”);
scanf(“%d”,&size);
ptr = (int*) malloc (size * sizeof(int) );
if(ptr == NULL)
{
printf(“\nNo memory available”);
exit(0);
}
printf(“\nEnter %d elements:”, size);
for(i =0; i < size ; i++)
{
scanf(“%d”, ptr+i);
sum = sum + *(ptr+i);
}
printf(“\nSum = %d”,sum);
free(ptr);
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06/2FY3-06

calloc() Function

“calloc” or “contiguous allocation” method in C is used to dynamically allocate the


specified number of blocks of memory of the specified type. it is very much similar to
malloc() but has two different points and these are:

1. It initializes each block with a default value ‘0’.


2. It has two parameters or arguments as compare to malloc().
Syntax:
ptr = (castType*) calloc (n, size in bytes);

realloc() Function
 realloc function modifies the allocated memory size by malloc and calloc functions to
new size.
 If enough space doesn't exist in the memory of the current block to extend, a new
block is allocated for the full size of reallocation, then copies the existing data to the
new block and then frees the old block.

free() Function

 Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own.
 free() function frees the allocated memory by malloc(), calloc(), realloc() functions.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6


Poornima College of Engineering B.Tech-I Programming for Problem Solving

User-Defined Functions

A function is a self-contained block of statements that performs a specific task. If a


program is divided into functional parts, then each part may be independently coded and
later combined into a single unit. These independently coded programs are called
subprograms that are much easier to understand, debug, and test. In C, such
subprograms are referred to as ‘functions’. This ‘division’ approach clearly results in a
number of advantages.
 It improve the readability of code.
 The length of a source program can be reduced by using functions.
 It improves the reusability of the code, same function can be used in any program
rather than writing the same code from scratch.
 Debugging of the code would be easier, as errors are easy to be traced.
 A function may be used by many other programs.
 It facilitates top-down modular programming.
There are two types of function in C language:

Function

Library Function User-Defined Function

Library Functions: The standard library functions are built-in functions in C programming.
Library functions are the inbuilt function in C that are grouped and placed at a common
place called the library. Such functions are used to perform some specific operations. For
example, printf() is a library function used to print on the console. The library functions are
created by the designers of compilers. All C standard library functions are defined inside
the different header files saved with the extension .h. We need to include these header
files in our program to make use of the library functions defined in such header files.

User-Defined Functions: A User-defined functions on the other hand, are those functions
which are defined by the user at the time of writing program. These functions are made for
code reusability and for saving time and space.

Modular Programming

Modular programming is a strategy applied to the design and development of software


systems. It is defined as organizing a large program into small, independent program
segments called modules (functions) that are separately named and individually callable
program units. These modules are carefully integrated to become a software system that
satisfies the system requirements. It is basically a “divide-and-conquer” approach to
problem solving. In C, each module refers to a function that is responsible for a single
task. Some characteristics of modular programming are as follows:
 Each module should do only one thing.
 Communication between modules is allowed only by a calling module.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


Poornima College of Engineering B.Tech-I Programming for Problem Solving

 A module can be called by one and only one higher module.


 No communication can take place directly between modules that do not have
calling-called relationship.
 All modules are designed as single-entry, single-exit systems using control
structures.

Elements of User-Defined Functions

In order to make use of a user-defined function, we need to establish three elements that
are related to functions.

1. Function declaration: A function must be declared in a program to tell the


compiler about the function name, function arguments, and return type.
2. Function definition: It contains the actual statements which are to be executed. It
is the most important aspect to which the control comes when the function is called.
3. Function call: Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration.

The function definition is an independent program module that is specially written to


implement the requirements of the function. In order to use this function we need to invoke
it at a required place in the program. This is known as the function call. The program that
calls the function is referred to as the calling program or calling function. The calling
program should declare any function that is to be used later in the program. This is known
as the function declaration or function prototype. Syntax of a function is:
Return_type function_name ( argument list )
{
//block of statements
}

Definition of Functions

A function definition, also known as function implementation shall include the following
elements:
 Function name
 Return type
 List of parameters
 Local variable declarations
 Function statements
 Return statement
List of parameters contains variables name along with their data types. These arguments
are kind of inputs for the function.
Return type can be of any data type such as char, int, short, float, double etc. A C function
may or may not return a value from the function. If you don’t have to return any value from
the function, use void for the return type.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


Poornima College of Engineering B.Tech-I Programming for Problem Solving

Category of Functions

A function, depending on whether arguments are present or not and whether a value is
returned or not, may belong to one of the following categories:
1. Functions with NO arguments and NO return values.
2. Functions WITH arguments and NO return values.
3. Functions NO arguments and WITH return values.
4. Functions WITH arguments and WITH return values.
5. Functions that return multiple values.

Parameters (Arguments) Passing

The parameters specified in the function call are known as actual parameters and those
specified in the function declaration are known as formal parameters. The scope of
formal parameters is limited to its function only.
Parameter passing is a mechanism for communication of data and information between
the calling function (caller) and the called function (callee). It can be achieved either by
passing the value or address of the variable. C supports the following two types of
parameter passing schemes:
1. Pass By Value or Call by Value
2. Pass By Address/Pointer/Reference or Call by Reference

Pass By Value / Call by Value

 The value of the actual parameters is copied into the formal parameters.
 We cannot modify the value of the actual parameter by the formal parameter.
 In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.
 The actual parameter is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
Pass-by-value mechanism does not change the contents of the argument variable in the
calling function, even if they are changed in the called function.
Example: Swap integer values by pass by value.

void swap (int x, int y) int main()


{ {
int t; int a, b;
t = x; printf(“Enter two numbers: ”);
x = y; scanf((%d%d”,&a, &b);
y = t; swap ( a, b );
printf(“After swaping in swap fun”); printf(“\nAfter calling swap function”);
printf(“\nx = %d y = %d”, x, y); printf(“\n a = %d b = %d”, a, b);
} }

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06

Pass By Address / Call by reference


 In call by reference, the address of the variable is passed into the function call as
the actual parameter.
 The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
 In call by reference, the memory allocation is similar for both formal parameters and
actual parameters. All the operations in the function are performed on the value
stored at the address of the actual parameters, and the modified value gets stored at
the same address.
In Pass-by-Address mechanism, instead of passing the value, the address of the variable is
passed in the function. The de-referencing operator ( * ) is used to access the variable in
the called function.
Example: Swap integer values by pass by address.
void swap (int * x, int * y)
{
int t;
t = * x;
x = * y;
y = * t;
printf(“After swaping in swap fun”);
printf(“\nx = %d y = %d”, * x, * y);
}
int main()
{
int a, b;
printf(“Enter two numbers: ”);
scanf((%d%d”,&a, &b);
swap ( &a, &b );
printf(“\nAfter calling swap function”);
printf(“\n a = %d b = %d”, a, b);
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06

Recursion

Any function which calls itself is called recursive function, and such function calls are called
recursive calls. When a called function in turn calls another function a process of ‘chaining’
occurs. Recursion is a special case of this process, where a function calls itself. For
example:
main()
{
printf( “main() is called recursively” )
main();
}
Recursive functions can be effectively used to solve problems where solution is expressed
in terms of successively applying the same solution to subsets of the problem. For example,
following the C function to generate the nth term of Fibonacci series:
int fibo( int n )
{
if ( n <=0 )
{ printf (“Series cannot be generated”);
return -111; //error state
}
else if ( n == 1 || n == 2)
return 1;
else
return ( fibo(n – 1 ) + fibo( n – 2 ) );
}

Example: Program to calculate the factorial of a number using recursion.


#include <stdio.h>
int fact(int);
int main()
{
int num, factorial;
printf(“\nEnter a number: ”);
scanf(“%d”,&num);
factorial = fact(num);
printf(“\nFactorial of %d = %d”, num, factorial);
return 0;
}
int fact(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * fact(n – 1);
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06

Storage Classes
(The Scope, Visibility, and Lifetime of Variables)

In C all variables have a data type and also have a storage class. A variable’s storage class
tells us the following things about the variable:
 Where would the variable be stored?
 What would be the default initial value of the variable?
 What would be the scope of the variable, i.e., to which statements the value of the
variable would be available?
 What would be the life of the variable, i.e., how long would the variable exist.
There are four storage classes in C:
1. Automatic storage class
2. Register storage class
3. Static storage class
4. External storage class

Automatic Storage Class


 The visibility of the automatic variables is limited to the block in which they are
defined.
 The scope of the automatic variables is limited to the block in which they are
defined.
 The automatic variables are initialized to garbage by default.
 The memory assigned to automatic variables gets freed upon exiting from the
block.
 The keyword used for defining automatic variables is auto.
 Every local variable is automatic in C by default.

Register Storage Class


 The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
 We cannot dereference the register variables, i.e., we cannot use & operator for the
register variable.
 The access time of the register variables is faster than the automaticvariables.
 The initial default value of the register local variables is 0.
 The register keyword is used for the variable which should be stored in the CPU
register. However, it is compiler’s choice whether or not; the variables can be stored
in the register.

Static Storage Class


 The variables defined as static specifier can hold their value between the multiple
function calls.
 Static local variables are visible only to the function or the block in which they are
defined.
 Default initial value of the static integral variable is 0 otherwise null.
Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6
PCE Programming for Problem Solving 1FY3-06

 The visibility of the static global variable is limited to the file in which it has
declared.
 The keyword used to define static variable is static.
Example:
#include <stdio.h>
void print(void);
int main()
{
int i;
for( i = 1; i <= 5; i++)
print();
return 0;
}
void print(void)
{
static int n = 1;
printf(“%4d”,n);
n++;
}

External Storage Class


 The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
 The variables declared as extern are not allocated any memory. It is only declaration
and intended to specify that the variable is declared elsewhere in the program.
 The default initial value of external integral type is 0 otherwise null.
 We can only initialize the extern variable globally, i.e., we cannot initialize the
external variable within any block or method.

Storage Default
Storage Scope Life
Class Value
Local to the block in Till the control remains
auto Memory Garbage which variable is within the block in which the
defined. variable is defined.
Local to the block in Till the control remains
register CPU Register Garbage which variable is within the block in which the
defined. variable is defined.
Local to the block in Value of the variable
static Memory Zero which variable is persists between different
defined. function calls.
As long as the program’s
extern Memory Zero Global execution doesn’t come to
an end.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 7


PCE Programming for Problem Solving 1FY3-06

Structures and Unions


C supports a constructed data type known as structure, a mechanism for packing data of
different types. Structure is a user-defined data type in C language which allows us to
combine data of different types together. A structure is a convenient tool for handling a
group of logically related data items. Structure help to organize complex data in a more
meaningful way.
Syntax:
struct <structure_name>
{
DataType member_1;
DataType member_2;
……
DataType member_n;
};

Defining a Structure
Unlike arrays, structures must be defined first for their format that may be used later to
declare structure variables. For example:
struct record
{
int rollno;
char name[20];
float per;
};
struct record student1, student2;
The keyword struct declares a structure to hold the details of three data fields, namely
rollno, name and per. These fields are called structure elements or members. Each
member may belong to a different type of data. record is the name of the structure and is
called the structure tag.
Student1 and student2 are the variable of type struct record. 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.

Accessing structure members


The link between a member and a variable is established using the member operator ‘.’
Which is also known as ‘dot operator’. For example:
student1.roll = 201;
strcpy(student1.name, “Bhagirath”);
We can also use scanf function to give the values through the keyboard. To access pointer
variable, we use the -> (arrow operator).

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06

Structure Initialization

Like any other data type, a structure variable can be initialized at compile time like:
struct record
{
int rollno;
char name[20];
float per;
};
struct record student1 = { 201, “Bhagirath”, 88.0 };

Arrays of Structures

We use structures to describe the format of a number of related variables. We may declare
an array of structures, each element of the array representing a structure variable.
struct record student[50];
student[0].rollno = 101;
strcpy ( student[0].name, “Bhagirath”);
defines an array called student, that consists of 50 elements. Each element is defined to
be of the type struct record and assign values to the members.
Example:
#include <stdio.h>
struct student
{
int rollno;
char name[20];
};
int main()
{
struct student stu[5];
int i;
for( i = 0; i < 5; i++)
{
printf(“\nEnter roll Number: “);
scanf(“%d”,&stu[i].rollno);
printf(“\nEnter name: “);
gets(stu[i].name);
}
for( i = 0; i < 5; i++)
{
printf(“\nRoll Number: %d”, stu[i].rollno);
printf(“\nName: %s“, stu[i].name);
}
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06

Structures within Structures

Structure within a structure means nesting of structures. Nesting of structures is permitted


in C. For example:
struct record
{
int rollno;
char name;
struct
{
int phy;
int che;
int math;
} marks;
} student;
The record structure contains a member named marks, which itself is a structure with three
members.
An inner-most member in a nested structure can be accessed by chaining all the concerned
structure variable with the member using dot operator. For example:
student.marks.phy = 83;
Example:
#include <stdio.h>
struct student
{
int rollno;
char name[30];
struct marks
{
int eng, math;
float per;
} score;
};
int main()
{
struct student obj = {101, "Bhagirath", 87, 76};
obj.score.per = (obj.score.eng + obj.score.math) / 2;
printf("\nRecord is: \n");
printf("\nRoll Number: %d", obj.rollno);
printf("\nName : %s", obj.name);
printf("\nMarks in eng: %d", obj.score.eng);
printf("\nMarks in math: %d", obj.score.math);
printf("\nPercentage: %.2f", obj.score.per);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06

Passing Structure to a Function


There are three methods by which the values of a structure can be transferred from one
function to another:
1. The first method is to pass each member of the structure as an actual argument.
2. The second method involves passing of a copy of the entire structure.
3. The third method employs passing address of a structure to a function.
The general format of sending a copy of a structure to the called function is:
Function_name ( structure_variable );
When a structure is used as an argument to a function, the entire structure is passed using
the call by value method. When using a structure as a parameter, remember that the type of
the argument must match the type of the parameter.
Example: Passing structure to a function.
#include <stdio.h>
struct student
{
int rollno;
char name[20];
};
void display(sturct student);
int main() {
sturct student obj = {101, “Bhagirath”};
display(obj);
return 0;
}
void display(struct student ob)
{
printf(“\nName = %s”,ob.name);
printf(“\nRoll Number = %d”,ob.rollno);
}

Self Referential Structures


structure which contain a member field that point to the same structure type are called
self referential structures. Each structure consist of two fields, one containing the item,
and the other containing the address of the next item (a pointer to the next item). For
example:
struct node
{
int code;
struct node *next;
};
Such a structure is also called dynamic data structure. Using self-referential structure, a
list can grow or shrink in size during the execution of a program. This list does not waste
memory space. It uses the memory that is just needed for the list at any point of time.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06

Structure Padding

Structure padding is a concept in C that adds the one or more empty bytes between the
memory addresses to align the data in memory. For example:
struct student
{
char ch1; //1 byte
char ch2; //1 byte
int num; //4 byte
} obj;
int main()
{
printf(“\nThe size of obj = %d bytes”, sizeof(obj) );
return 0;
}
Output:
The size of obj = 8 bytes //due to concept of structure padding.

Structure Packing (to avoid the structure padding)

Example:
#include <stdio.h>
#pragma pack(1)
struct number
{
int num;
char ch;
double d;
};
int main()
{
struct number obj;
printf("\nSize of obj = %d bytes",sizeof(obj));
return 0;
}
Output:
Size of obj = 13 bytes

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06

Union

Like structures, union is a user defined data type. In union, all members share the same
memory location. This implies that, although a union may contain many members of
different types, it can handle only one member at a time. Like structures, a union can be
declared using the keyword union as follows:
union item
{
char c;
int m;
float x;
} code;
This declares a variable code of type union item. The union contains three members,
each with a different data type. However, we can use only one of them at a time. The
compiler allocates a piece of storage that is large enough to hold the largest variable type
in the union.
Example: To illustrate difference between structure and union.
#include <stdio.h>
struct snumber
{
int num;
char ch;
double d;
}sobj;
union unumber
{
int num;
char ch;
double d;
}uobj;
int main()
{
printf("\nSize of sobj = %d bytes",sizeof(sobj));
printf("\nSize of uobj = %d bytes",sizeof(uobj));
return 0;
}
Output:
Size of sobj = 16 bytes
Size of uobj = 8 bytes

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6


PCE Programming for Problem Solving 1FY3-06

Difference between Structure and Union

Key
Structure Union
Difference
The keyword struct is used to The keyword union is used to
keyword
define a structure. define a union.
When a variable is When a variable is associated
associated with a structure, with a union, the compiler
the compiler allocates the allocates the memory by
Size memory for each member. considering the size of the largest
The size of structure is memory. So, size of union is
greater than or equal to the equal to the size of largest
sum of sizes of its members. member.
Each member within a
Memory allocated is shared by
Memory structure is assigned unique
individual members of union.
storage area of location.
Altering the value of a Altering the value of any of the
Value
member will not affect other member will alter other member
Altering
members of the structure. values.
Accessing Individual member can be Only one member can be
Members accessed at a time. accessed at a time.
Initialization Several members of a Only the first member of a union
of Members structure can initialize once. can be initialized.

Enumerated Data Type

The enumerated data type gives us an opportunity to invent our own data type and define
what values the variable of this data type can take. This can help in making the program
listings more readable.
enum dept
{
mca = 21, mba = 11, btech = 10, bca = 30
};
enum dept s1,s2,s3;
Now we can give values to these variables.
s1 = mca;
s2 = bca;
Internally, the compiler treats the enumerator as integers. Each value on the list of
permissible values corresponds to an integer, starting with 0. This way of assigning
numbers can be overridden by the programmer by initializing the enumerators to different
integer values.

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 7


PCE Programming for Problem Solving 1FY3-06

File Management in C
A computer system stores programs and data in secondary storage in the form of files.
Storing programs and data permanently in main memory is not preferred due to the
following reasons:
 Main memory is usually too small to permanently store all the needed programs
and data.
 Main memory is a volatile storage device, which loses its contents when power is
turned off.
It is therefore necessary to have a more flexible approach where data can be stored on
the disks and read whenever necessary, without destroying the data. This method
employs the concept of files to store data. A file is a place on the disk where a group of
related data is stored.
File handling 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:
 Creation of the new file
 Opening an existing file
 Reading from the file
 Writing to the file
 Deleting the file
To perform file operations in C, the important file handling functions that are available in
the C library are:

Function Name Operation


fopen() Creates a new file or Open an existing file for use.
fclose() Closes a file which has been opened for use.
getc() or fgetc() Reads a character from a file.
putc() or fputc() Writes a character to a file.
getw() or fgetw() Reads an integer from a file.
putw() or fputw() Writes an integer to a file.
fprintf() Writes a set of data values to a file.
fscanf() Reads a set of data values from a file.
fseek() Sets the position to a desired point in the file.
Gives the current position in the file (in terms of bytes from
ftell()
the start).
rewind() Sets the position to the beginning of the file.

Defining and Opening a File

Data structure of a file is defined as FILE in the library of standard I/O function definitions.
When we open a file, we must specify what we want to do with the file. For example, we
may write data to the file or read the already existing data. Following is the general format
for declaring and opening a file:

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 1


PCE Programming for Problem Solving 1FY3-06

FILE *fptr;
fptr = fopen (“filename”, “mode”);
The first statement declares the variable fptr as a ‘pointer to the data type FILE’. The
second statement specifies the purpose of opening this file. Mode can be one the
following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding) data to it.

Closing a File
A file must be closed as soon as all operations on it have been completed. This ensures
that all outstanding information associated with the file is flushed out from the buffers and
all links to the file are broken. It also prevents any accidental misuse of the file. The I/O
library supports a function to do this. It takes the following form:
fclose (file_pointer);
This would close the file associated with the FILE pointer file_pointer.

Input/Output Operations on Files

The getc and putc functions

The simplest file I/O function are getc and putc. These functions handle one character at
a time.
putc ( ch, fptr );
writes the character contained in the character variable ch to the file associated with FILE
pointer fptr.
ch = getc ( fptr );
would read a character from the file whose file pointer is fptr.

The getw and putw Functions

The getw and putw are integer-oriented function. They are used to read and write
integer values. The general forms of getw and putw are as follows:
putw ( int_num , fptr );
int_num = getw ( fptr );

The fprintf and fscanf Functions

The functions fprintf and fscanf perform I/O operations that are identical to the familiar
printf anf scanf functions, except that they work on files. The first argument of these
functions is a file pointer which specifies the file to be used. General form of fprintf is:
fprintf ( fptr, “control string” , list);
where fptr is a file pointer associated with a file that has been opened for writing. The
control string contains output specifications for the items in the list.
The general format of fscanf is:

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 2


PCE Programming for Problem Solving 1FY3-06

fscanf ( fptr, “control string”, list);


This statement would cause the reading of the items in the list from the file specified by
fptr, according to the specifications contained in the control string.

Error Handling During I/O Operations

It is possible that an error may occur during I/O operations on a file. Typical error
situations include the following:
 Trying to read beyond the end-of-file mark.
 Device overflow.
 Trying to use a file that has not been opened.
 Trying to perform an operation on a file, when the file is opened for another type of
operation.
 Opening a file with an invalid filename.
 Attempting to write to a write-protected file.
We have two status-inquiry library function, feof and ferror that can help us detect I/O
errors in the files.
The feof function can be used to test for an end of file condition. It takes a FILE pointer
as its only argument and returns a nonzero integer value if all of the data from the
specified file has been read, and returns zero otherwise.
if ( feof ( fptr ) )
printf(“End of file”);
The ferror function reports the status of the file indicated. It also takes a FILE pointer as
its argument and returns a nonzero integer if an error has been detected up to that point,
during processing. It returns zero otherwise.
if ( ferror( fptr ) != 0 )
printf (“Error occurred”);
Whenever a file is opened using fopen function, a file pointer is returned. If the file
cannot be opened for some reason, then the function returns a NULL pointer. This facility
can be used to test whether a file has been opened or not.
if ( fptr == NULL )
printf (“File opening Error”);
Example: A program to print the file contents in uppercase.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("file.txt","r");
if(fp == NULL)
{
puts("File opening error.");

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 3


PCE Programming for Problem Solving 1FY3-06

exit(1);
}
do
{
ch = fgetc(fp);
putchar(toupper(ch));
} while(ch != EOF);
return 0;
}
Example: A program to copy the contents of a file and write it in another file. (file
copy program).
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("file.txt", "r");
if (fp1 == NULL)
{
puts("File opening error.");
exit(1);
}
fp2 = fopen("output.txt", "w");
if (fp2 == NULL)
{
puts("File opening error.");
exit(1);
}
do
{
ch = fgetc(fp1);
fputc(ch, fp2);
} while (ch != EOF);
_fcloseall();
printf("\nThe contents of output.txt:\n");
fp1 = fopen("output.txt", "r");
do
{
ch = fgetc(fp1);
putchar(ch);
} while (ch != EOF);
fclose(fp1);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 4


PCE Programming for Problem Solving 1FY3-06

Random Access to Files

There are occasions, however, when we are interested in accessing only a particular part
of a file and not in reading the other parts. This can be achieved with the help of the
functions fseek, ftell, and rewind available in the I/O library.
ftell takes a file pointer and return a number of type long, that corresponds to the current
position. It takes the following form:
pos = ftell ( fptr );
rewind takes a file pointer and resets the position to the start of the file.
rewind ( fptr );
fseek function is used to move the file position to a desired location within the file. It
takes the following form:
fseek ( fptr, offset, position );
Here fptr is a pointer to the file concerned, offset specifies the number of positions
(bytes) to be moved from the location specified by position. The position can take one
of the following three values:
0 for beginning of file
1 for current position
2 for end of file
Example:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("file3.c","r+");
fseek(fp,-1,2);
printf("\nTotal Characters in File: %d",ftell(fp));
printf("\nLast character is: %c",fgetc(fp));
fseek(fp,0,0);
printf("\nFirst character is: %c",fgetc(fp));
fseek(fp,4,1);
printf("\nSixth character is: %c",fgetc(fp));
rewind(fp);
printf("\nAgain! First character is: %c",fgetc(fp));
fclose (fp);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5


PCE Programming for Problem Solving 1FY3-06

Command Line Arguments

The arguments passed from command line are called command line arguments. These
arguments are handled by main() function. Command line argument is a parameter
supplied to a program when the program is invoked. This parameter can be of any type.
In fact main can take two arguments called argc and argv and the information contained
in the command line is passed on to the program through these arguments, when main is
called up by the system.
The variable argc is an argument counter that counts the number of arguments on the
command line. The argv is an argument vector and represents an array of character
pointers that point to the command line arguments. In order to access the command line
arguments, we must declare the main function and its parameters as follows:
main ( int argc, char *argv[ ] )
{
…..
…..
}
The first parameter in the command line is always the program name and therefore
argv[0] always represents the program name.
Example:
#include <stdio.h>
void main(int argc, char *args[]) {
int i;
for(i = 0; i < argc; i++)
{
printf("\nArgument args[%d] is: %s",i,args[i]);
}
}
Example: Add two numbers using command line arguments.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char*args[])
{
int n1, n2, sum;
if(argc != 3)
{
printf("Arguments must be: \"programName value1 value2\"");
exit(1);
}
n1 = atoi(args[1]);
n2 = atoi(args[2]);
sum = n1 + n2;
printf("\nSum = %d",sum);
return 0;
}

Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 6

You might also like