100% found this document useful (1 vote)
239 views

C Language Notes

The document provides an introduction to the C programming language. It discusses that C is a high-level, procedural language developed in the 1970s. The document then covers the history and development of C from earlier languages like ALGOL and BCPL. It describes the key characteristics of C like being simple, reliable, and able to access low-level hardware. The rest of the document defines various C language concepts like data types, variables, constants, operators, and control structures at a high level.

Uploaded by

gmr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
239 views

C Language Notes

The document provides an introduction to the C programming language. It discusses that C is a high-level, procedural language developed in the 1970s. The document then covers the history and development of C from earlier languages like ALGOL and BCPL. It describes the key characteristics of C like being simple, reliable, and able to access low-level hardware. The rest of the document defines various C language concepts like data types, variables, constants, operators, and control structures at a high level.

Uploaded by

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

KAMAL’S

C - LANGUAGE NOTES

Prepared by
KAMALA KAR VANKALA
M.C.A.,
CHEEPURUPALLI
VIZIANAGARAM(DIST)
E-mail: [email protected]
OUR OTHER MATERIALS: COBOL, FOXPRO,C++,DATASTRUCTURES
Introduction of C language

Computer Programming languages


Naturally a language is the source of communication between two
persons, and also between person to machine like computer. The languages we
can use to communicate with the computer are known as Computer
programming languages.

Generally there are two major types of languages are available are as follows:

1. Low level languages

The set of commands available in low level is complex and not easy to
understandable. In this category " Assembly " and " machine codes " are
available. Assembly programs are faster than other high-level language
programs.

2. High level languages

The set of commands available in high level language is very simple and
easy to understandable.

High level languages are further divided into two major categories.

1. Procedure Oriented language

In this category we are able to create our project or programs using


procedural approach means in this type we can able to divide our big
project/program into small subroutines or procedures. After making
procedures we can able to call a ‘procedure’ one or more places.

The lists of procedural languages are as follows:

C language
C++ (Object Oriented)
Java (Objected Oriented)
Smalltalk (Objected Oriented)
Pascal language

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 2


2. Non-Procedural Languages: This category also known as ‘Problem Oriented
languages’. In this type of languages we can able to make program only at
specific range like database. The followings are the examples of Non
procedural languages

1. SQL (Structured Query Language)


2. SNOBOL (String processor)

HISTORY OF C LANGUAGE

• In 1960,an international committed was set up to develop a general-


purpose language.
• This committee developed ALGOL 60 in the year 1960.this language was
very abstract.
• Combined programming language(CPL) was developed in 1963.But CPL was
very big and had many features.It was difficult to learn and use.
• BCPL was developed in 1967 by Martin Richards. It was not very powerful.
• Ken Thompson simplified BCPL to make B language in 1970. B represents
the first letter of BCPL. This Language was very small.
• Dennis Richie by using B,BCPL & some additional feature developed C
language in 1972. Letter C is taken from the second letter of BCPL.

ALGOL 60 CPL BCPL B C


(1960) (1963) (1967) (1970) (1972)

Characteristics of C language
C language was developed by Dennis Richie at AT & T Bell Laboratories in
1972. It is a general purpose, flexible ,powerful and structured programming
languge. C is used for many application. It has many inbuilt functions. It is
simple to use and easy to learn friendly,reliable and compact language. It is
case sensitive language. The variable in upper case & lower case letters have
different meaning. It has many library functions. The user can add new
functions. It is high-level language but it is called middle level language. It has
characteristics of both high-level language & low-level.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 3


Characteristics

• It is simple to use & easy to learn.


• C is reliable language.
• The keywords & library functions in C are like English words.
• C program can be executed on different computers.
• C compiler is very fast so execution is very fast.
• C compiler is easily available. It require small disk space for storage. It is easy
to load.
• Errors are display on screen. They can be easily removed.
• Program can be divided into small modules by using functions. Program is easy
to understand.
• Can be used for recursive programming.
• Can be used for graphic programming.
• Format free language.
• Pointer can be used.
• More data types and more operators than any other language.
• It has many version such as Turbo C,ANSI C.
• It has the characteristics of both low level & high level language.
• Bitwise manipulate is possible.
• It can be used for structured programming.
• Coding of C program is compact as compared to other languages.
• It can be used for system programming.
• C language has only 32 keywords. They are easy to learn & use.

CHARACTER SET
The characters that can be used to form words, numbers and expressions
depend upon the computer which the program is run. The characters in C are
grouped into the following categories

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 4


1. Letters
A-Z,a-z.
2. Digits
0-9
3. Special characters

( . , ; : ‘ “ { [ ! > * / @ & ^ $ # % ? ) } ] etc

4. White spaces

These can be used to separate two words.


Eg: hello world

C TOKENS

In a passage of text, Individual words and punctuations marks are called


tokens, Similarly in a C program the smallest individual units are known as tokens.
C has six types of tokens as shown below
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
Keywords

Keywords are predefined words whose meaning is already explained to the


complier. It cannot be used as variable name. Keywords are always written in
lowercase. They are called reserved words. C language has 40 keywords.32 are
used for high level programming. These are called standard keywords. Remaining
8 are used for low level programming. These are called optional keywords.
auto break case char constant continue default Do
double else enum extern float foe goto If
int long register return short signed size Static
struct switch typedef union unsigned void volatile While

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 5


Identifiers

These are the names of variables, functions & arrays. It consists of letters, digits &
underscore characters (_).
Rules for identifier:-
 It must be from the character set.
 First character must be a letter.
 Special character cannot be used.
 Avoid single character identifiers. Use combination of letters, digits &
underscore character.
 Length must not be more than 8 characters. Some compilers allow 32
character identifiers.
 Keyword cannot be used in user defined identifier.
Eg: si, sum, SUM,a5_4.

CONSTANTS
Constants in C refers to fixed values that do not change during the
execution of a program

C constants can be divided into two major categories:


1. Numeric Constants
2. Character Constants

1)Numeric Constants

Numeric constants can be declared as the following

a. Integer Constant
b. Real Constant

a) Integer constants
An integer constant refers to a sequence of digits.
Ex.: 426
+782
-8000
-7605

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 6


Rules for Constructing Integer Constants
• An integer constant must have at least one digit.
• It must not have a decimal point.
• It can be either positive or negative.
• If no sign precedes an integer constant it is assumed to be positive.
• No commas or blanks are allowed within an integer constant.
• The allowable range for integer constants is -32768 to 32767.

b) Real constants

The Constants which contains the fractional part is known as real


constants.

Ex.: +325.34
426.0
-32.76
-48.5792

Rules for Constructing Real Constants

• A real constant must have at least one digit.


• It must have a decimal point.
• It could be either positive or negative.
• Default sign is positive.
• No commas or blanks are allowed within a real constant.

2) character constants

character constants can be classified into two categories


• Single character constants
A single character constant contains a single character enclosed
within a pair of single quote marks

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 7


Eg: ‘A’ ‘X’ ‘5’
The maximum length of a character constant can be 1 character.
• string character constants
A string character constant is a sequence of characters enclosed
Within a double quote marks
Eg: ”Hello world ”

Symbolic contants:
Symbolic constants are the constants which are assigned to an
identifier, the identifier can represents that particular constant in the entire
program. Symbolic names are sometimes called constant identifiers. Since
the symbolic names are constants, they do not appear in the declarations.

Syntax:
#define symbolic-name value of constant
Eg:
#define STRENGTH 100
#define PASS_MARK 50
#define PI 3.14159
The following rules apply to a #define statement which define a symbolic
constant.

1. Symbolic names have the same form as variable names. (Symbol


names shuld be in capital letters)
2. No blank space between the pound sign ‘#’ and the word “define” is
permitted.
3. # must be the first character in the line.
4. A blank space is required between #define and symbolic name.
5. #define statement must not end with the semicolon.
6. After definition the symbolic name should not be assigned any other
value within the program by using assignment statement. For
example STRENGTH=200 is illegal
7. Symbolic names are not declared for data types.
8. #define statements may appear any where in the program but before
it is referenced in the program.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 8


Strings
String is nothing but a collection of characters enclosed within a double
quote marks as shown below
Eg: “hello world, welcome to C”
The maximum length of a string can be any number of
characters.

Operators

An operator is a symbol that tells the computer to perform a certain


mathematical or logical manipulation.
The various operators in c are as follow
1. Arithmetic operators
2. Assignment operators
3. Increment/ decrement operators
4. Relational operators
5. Logical operators
6. Conditional operators
7. Special operators
8. Bitwise operators

Variable in a C language
A variable is a quantity, which can change during program execution. It is
the name of the location in the memory of computer in which constant can be
stored. These locations can store integer, float or character constant. Variable
must be declare before it can be used in a program. Variable declaration tells the
compiler about the name of the variable and type of data which it can hold. When
a variable is declared, compiler creates space in memory & gives that name to the
space. General form of variable declaration is:
Data type list of variable separated by comma.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 9


E.g: Int a,b;
Float x,y;
Char a,b;
Here a and b are the variables which can hold integer data. X and Y are variable to
hold float data.we can also assign value to the variable at the time of declaration.
E.g. Int a=26;
Here a is the name of variable and 26 is a value which is stored in this variable.

Following rules are followed to construct variable name:


• A variable name is a combination of a 1 to 8 alphabets, digits or
underscores.
• The first character in the variable name must be alphabet or underscore
character.
• Comma and blank space are not allowed in a variable name.
• No special symbol other than underscore is allowed in variable name.

DATA TYPES IN C LANGUAGE

The type of data which a variable can store is called its data type. C language
supports following data types

1. Primary data types


2. Derived data types/secondary data types/structured data types
3. User defined data types
1) Primary data types
They are further divided into integer, float, double, char & void data types.

a) Integer data type


It is used to store integer value. It requires 2 byte memory space. Integers are
declared by the keyword int. Range is from -32768 to 32767. some qualifiers
Such as short, long, unsigned are used with integer data type can be used as
follows:
Syntax : datatype variable-name;
Eg: Int a,b,c;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 10


Short int x,y,z;
Long int x,y;
Unsigned int length;
b) Float data type
It is used to store floating point constants. It requires 4 byte memory space.
Number of digits after decimal are 6. It is declared by keyword float. Range is b/w
3.4 e -38 and 3.4 e 38.
Syntax : data type variable-name;
Eg: float p,r,t;
C) double data type
it is used to represent real number in exponential form. It requires 8 byte memory
space. Range is from 1.7 e -308 to 1.7 e 308. It is declared by using the keyword
double. Long is used as qualifier.
Syntax : data type variable-name;
Eg: double x,y;
d) char data type
It is used to store single character or a group of characters. Characters can be
either signed or unsigned. It takes one byte for storage. Range is -128 to 127.
Syntax: data type variable-name;
Eg: Char name;
e) void data type
It stores nothing. It takes 0 byte. Keyword void is used.
Keyword Description Low High Bytes Format
string
Char Single -128 127 1 %c
character
Int Integer -32768 32767 2 %d
Long int Long int -2147483648 2147483848 4 %ld
Float Floating 3.4 e-38 3.4 e 38 4 %f
Double Double 1.7 e -308 1.7 e 308 8 %lf
floating
Long double Long double 3.4 e -4932 1.1 e 4932 10 %Lf
floating
Unsigned Char with no 0 255 1 %c
char sign
Unsign int Int with no 0 65535 2 %u
sign
Unsign long Long int no 0 4294967295 4 %lu
int sign

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 11


2) Derived data types

These data types are derived from primary data types. These are also called
structured data types or secondary data types. They can be further divided into
arrays, function and pointers.

a) Array

An array is a collection of related data items, which have similar data type
& have a common name. It is used to represent a group of related data items. The
data items can be integer, float or character. All the data items must have same
data type & storage class.

Syntax: data type array-name[size];

Eg: char city[20];

b) Function

A function is a collection of statements, which do some useful work. It may


contains any number of statements. The C program contains atleast one
function. If any C program contains only one function, that must be main function.

Syntax: function prototype function name()


{
Block of statements;
}
Eg: void add()
{
Int a,b,c;
Printf(“Enter two values”);
Scanf(“%d%d”,&a,&b);
c=a+b;
printf(“sum of a&b is %d”,c);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 12


c) Pointer
Pointer is a special variable, pointer variables are used to for to indicate the
address of another variable. Simple variable always points the value of memory
location. Whereas, pointer variable always points the address of the memory
location. Declaration can be done using the following syntax.

Syntax: data type *variable-name;

Eg: int *a;

Here *a represents the pointer variable which can be used to store the address of
another variable.
Eg: int i=5; // address of i is 1001
Int *j;
j=&i;
*j=(&i) //Asterisk(*)
=(1001)
=5

3) User defined data types

The data types which are defined by user is known as user defined data types.
Structure, union & enum are user defined data types.

a) Structures

Structure is a collection of related data items of different data types. A structure


may contain integer elements, floating point elements & character elements.
These elements are called member of the structure. Structure is similar to a
record.
Syntax: struct structure-name
{
Block of statements;
};
Struct structure-name structure variables;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 13


Eg:
struct emp
{
char name[25];
int code;
char dept[20];
float salary;
}struct emp e1,e2;

Here struct is a keyword.emp is the name of the structure, name, code,


dept. & salary are the elements of the structure.e1 & e2 are structure variables.
Member of structure will occupy memory only if structure variables are created.

b) union

Union is a data type. It is similar to structure data type. Like structure, union can
also contain members of different data types. However at a time, union can hold
one member. The difference b/w structure and union is that only one member of
the union can contain a value at a given time. Whereas a structure provide
storage for each member a union provide storage for one member. The space
allotted to a union is dependent on the largest member of union.

Syntax: union union-name;


{
Block of statements;
}Union-name union variable;
Eg:
union student
{
char name[20];
int roll-no;
};
student s1;
The variable s1 can represent either name or roll-no at a time.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 14


c) Enumeration
Enum declaration is same as structure declaration, difference is enumeration
variables contains default values starting with zero. This can be declared by using
the keyword.
Eg:- Enum day = {Sunday, Monday, Tuesday, Wednesday, Thursday};
Here enum is keyword, day is the name of type.
The following integer values are assigned
Sunday 0
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
STORAGE CLASSES IN C
Storage class of a variable gives the following information.-
• Place where variable will be stored. The variable can be stored either in
memory or CPU registers.
• The default initial value of the variable.
• Scope of the variable.
• Lifetime of the variable.
Storage classes are four types.

1) Automatic storage class


The variable with automatic storage class has following properties:
Storage Memory
Default initial value Unpredicatable value
Scope Local to the block in which the
variable is defined.
Lifetime Till the control remain with the block
in which variable is defined.

Example:
main()
{
auto int i,j;
printf(“%d%d”,i,j);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 15


Output : 1221 220
Where is 1221 220 garbage value.

2) External storage class:-

The variable with exteranal storage class has following properties:


Storage Memory
Default initial value Zero
Scope Global
Lifetime Throughout the program
Example:
main()
{
extern x;
printf(“%d’,x);
}
3) static storage class
The variable with static storage class has following properties:
Storage Memory
Default initial value Zero
Scope Local to the block in which variable
is defined
Lifetime Value of the variable persist b/w
difference function calls.

Example:
Main()
{
Increment();
Increment();
Increment();
}
Increment()
{
static int i=1;
Printf(“%d”,i);
I=i+1;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 16


}
Output: 1 2 3

4) Register storage class


The variable with register storage class has following properties:
Storage CPU registers
Default initial value Unpredictable
Scope Local to the block in with variable is
defined
Lifetime Till the control remain with the block
in which variable is defined

for example:
main()
{
register int i;
for(i=0;i<=10;i++)
printf(“%d”,i);
}

Assignment statement:
Values can be assigned to variables using the assignment operator = as
follows
Syntax: data-type variable=constant;
Eg: int a=1000;

OPERATORS
An operators is a symbol that tells the computer to performed certain
mathematical or logical manipulation.
The various operatore in c are as follow
1. Arithmetic operators
2. Assignment operators
3. increment/ decrement operators
4. Relational operators

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 17


5. Logical operators
6. conditional operators
7. Special operators
8. Bitwise operators

1) Arithmetic operators:-Arithmetic operators are the simplest & basic


operators that do arithmetic operations. These operators require two
variable & are called binary operation.
Operator Meaning Associative
+ Addition L->R
- Subtract L->R
* Multiplication L->R
/ Divide L->R
% Module L->R

2) Assignment Operator:-Assignment Operators are used to assign the result of


an expression to a variable.
The most commonly used assignment operator is = assignment expression that
make use of these operator are written in the form.
Syntax: Identifier=expression
Operators Meaning
= Assign value on Right to Left
+= LHS to RHS
-= RHS to LHS
*= LHS to RHS
/= LHS to RHS
%= LHS to RHS

Program :-
main()
{
int x=5;
x+=10;
printf(“%d”,x);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 18


3) Increment/decrement Operators
These operators are c have two useful operators. These are increment(++) &
decrement(--) operators.
e.g;-x=4;
y=++x;
4) Relational Operators
These operators are used for comparing two values & depending on their relation
take certain decision.e.g:- 15>25 True
45<15 False
Operators Meaning
< Less than
> Greater than
<= Less than equal
>= Greater than equal
== Equal to
!= Not equal

5) Logical Operators
These operators are refered to as Boolean operators. These Operators act upon
Operators that are themselves logical expression.
After testing the value of condition it gives result,which is either true or false.
Operators Meaning
&& AND
|| OR
!! NOT

6)Conditional operators
The conditional operators are also called as “Terinary operators”. The
conditional operators are ?,:
It executes value one if the condition is true and executes value two if the
condition is false.

Syntax: expression1? Expression2: expression3

Expression1 is evaluated first. If is true i.e.value is non zero.then expression2 is


evaluated & becomes value of conditional expression.
Eg: z=(x>y)?x:y;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 19


The value of x will be assign to z if the condition is true otherwise the value of y
will be assigned to z
//Program find the greatest of two no.x & y using conditional operators.
#include<stdio.h>
#include<conio.h>
Void main()
{
int x,y,z;
clrscr();
printf(“\n enter the value of x & y=”);
scanf(“%d%d”,&x,&y);
z=(x>y)? x:y;
printf(“enter the no. %d is greater=”,z);
getch();
}

7)Special operators:-these operators are divided into two categories.


• Sizeof operator:- the size of operator returns the number of bytes required
to represent a data type or variable.
Syntax:-
Sizeof(datatype)
Sizeof(variable)
The data type can be standard or user defined.
e.g:-sizeof(int)
return value 2
sizeof(float)
return value4
• The comma operators;- a set of expressions is seprated by commas in c. it
links related expressions together. Expression
linked using comma operator are evaluated from left to right & the
value of the right most expression is the result.
e.g:- x=(y=5,y+6);
the expressions is evaluated from left to right.
X=11
The value of the entire comma seprated expression is the value of right
most expression.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 20


8)Bitwise operator:-A bitwise operator c has a difference of supporting,several
operator known as bitwise operator for manipulation of data at bit level.
operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Bitwise shift left
>> Bitwise shift right

Type cast operator:


Changing of one data type into another data type is known as “Type
casting”.
Eg: If we write the statement as follows
Int n=5.5/2;
When you compile this code, the compiler gives the following message
“incomparable declaration”.
Explicit cast is required to convert float point.
Void main()
{
Int n=int(5.5)/2;
}
C permits explicit conversation of variables or expressions by using the type
cast operator.

OPERATOR HIERARCHY/PRECEDENCE OF OPERATORS

C enables us to add multiple meanings for the operators, Their association


and precedency remains the same. For example, multiplication operator(*) will
continue having higher precedency than add(+) operator. The symbols +,-,* and
& are used as both unary and binary operators. The unary operators assume
higher precedency.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 21


RANK OPERATOR ASSOCIATIVITY

1 :: Left-Right

2 (),[],++,--(post fix) Left-Right

3 ++,--(pre fix) Right-Left

4 new,delete,size of Right-Left

5 *,%,/ Left-Right

6 +,- Left-Right

7 <<, >> Left-Right

8 ==,!= Left-Right

9 & Left-Right

10 ^ Left-Right

11 ! Left-Right

12 !! Left-Right
13 ? ,: Right-Left

INPUT-OUTPUT FUNCTION USED IN C LANGUAGE

Input-output function are used to transfer data b/w peripheral devices and
memory. C language has no provision for reciving data from input device. It
cannot send data to the output devices. So input output operations are done by
using standard input output library functions. These functions are kept in special
library files. When a particular input output function is required in a program, the

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 22


corresponding library files are include in the program. Standard input output
library function are divided into three type:

a)Console input output functions


b)Disk input output functions
c)Port input output functions

a)Console input output functions


These function receive input from the keyword and display output on monitor.
Following console is input output function.

Formatted function Unformatted function


Type input output Type input output
Char scanf() printf() char getch() putch()
Int scanf() printf() int getche() putchar()
Float scanf() printf() float getchar()
String scanf() printf() string gets() puts()

FORMATTED FUNCTION

printf()

printf() is built-in function we can display with printf() any message, variable

value on screen/file/printer.

Syntax: printf(“control strings”,arg1,arg2,…argn);

Here control string specifies the field of format in which the data is to be entered
and arg1,arg2,…,argn specifies the name of the variables where the data is
stored. Control string and arguments are separated by commas.

Eg1: printf(“%d%d”,a,b);

Here %d is the control string for printing an integer values. a,b specifies the
locations of the variables a and b respectively.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 23


Eg2: printf(“%f%f”,a,b);

Here %d is the control string for printing real values. a,b specifies the locations of
the variables a and b respectively.

Eg3: scanf(“%c%c”,a,b);

Here %d is the control string for printing character values. a,b specifies the
locations of the variables a and b respectively.

In printf() we can use many escape sequences and format specifies.

Escape sequences are special notations through which we can display our data
Variety of ways:

Some escape sequences and their functions are as follows:

Escape Description Example


Sequence
\n Perform line feed & Carriage return printf("A\nB");
operation
\t Prints a tab sequence on screen printf ("A\tb");
\’ Prints a single quote character on printf ("\’a\’");
screen
\" Prints a double quote character on printf ("\"a\"");
Screen
\r Perform carriage return operation printf ("a\rb")
\b Remove one character from left printf ("a\bHi!" );

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 24


Scanf()

This is a way to giving values to variables is to input data through keyboard


using the scanf(). The general form of scanf() is as fallows

Scanf(“control string”,arg1,arg2…argn);
Here control string specifies the field of format in which the data is to be entered
and arg1,arg2,…,argn specifies the address of the location where the data is
stored. Control string and arguments are separated by commas.

Eg1: scanf(“%d%d”,&a,&b);

Here %d is the control string for reading an integer. &a,&b specifies the address
locations of the variables a and b respectively.

Eg2: scanf(“%f%f”,&a,&b);

Here %f is the control string for reading a real number. &a,&b specifies the
address locations of the variables a and b respectively.
Eg3: scanf(“%c%c”,&a,&b);

Here %c is the control string for reading a character. &a,&b specifies the address
locations of the variables a and b respectively.

//write a prog. Sum with 2 variable.


main()
{
int a,b,sum,diff;
printf(“enter the value of a & b”);
scanf(“%d%d”,&a,&b);
sum=a+b;
diff=a-b;
print(“sum=%d\ndiff=%d”,sum,diff);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 25


output
enter the value of a & b= 20 10
sum=30
diff=10

UNFORMATTED CONSOLE INPUT-OUTPUT FUNCTION

1)getch()
It is used to read a single character from keyboard. It is non buffered function.
Data is directly assigned to variable without pressing enter key. It also maintains
the output on screen until we press enter key.
Syntax: C=getch();
Consider the following example program.

Void main()
{
Char ch;
Printf(“enter any character”);
Ch=getch();/*read a character from keyword*/
Printf(“type any charcter”);
Ch=getche();
Printf(“u have entered=”);
Putchar (ch);
Getch();/*to maintain output on screen*/
}

2)getche()
It is used to read a single character from keyword. It is non buffered function.
Data is directly assigned to variable without pressing enter key. when you enter
character from keyword , it will be seen on screen. It is an echoed function. It also
maintains the output on screen until we press enter key.
Syntax:-
C =getche();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 26


For e.g;-
Main()
{
Char ch;
Printf(“enter the any charcter”);
Ch=getche();
Printf(“type any charcter”);
Ch=getche();
Printf(“u have entered=”);
Putchar(ch);
Getche();

}
3)getchar()

It is used to read a single charcter from keyboard.it is buffered function. Data is


stored in memory until u press enter key. After pressing enter key,data is assigned
to the variable.

syntax: c=getchar();
for e.g:-
main()
{
char ch;
printf(“type any charcter”);
ch=getchar();
printf(“u have entered”);
putchar(ch);
getch();
}
4)putchar() and putch()
Putch() and putchar() print a charcter on screen. They can output only one
charcter at a time.
Syntax:-putch(ch);
For e.g.-
Main()
{
Char ch=’x’;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 27


Putch(ch);
Putchar(ch);
}
5)puts() & gets()
The function gets() receive a string from the keyboard. It is a buffered function. It
can i/p multiword.
Strings also.

Syntax:-gets(string);
Puts() is an o\p function. It is used to display a string on screen. It automatically
appends a new line charcter to the o\p.
Syntax:-puts(ch);
For eg.
Main()
{
Char ch[24];
Puts(“enter data”);
Gets(ch);
Puts(“Banglore”);
Puts(ch);
}

b)Disk input-output function


these function are used to do input & output operations on floppy disk or hard
disk. These functions are performed on files.there are many standard library
functions to performed disk or file input-output. These function can be divided
into two categories:high level file i\p function & low level i\p function.
c)Port input & output functions
these function are used to do input output operation on variable ports.

STRUCTURE OF C PROGRAM

C program consist one or more functions. Each function performs a specific


task. A function is a collection statements that are executed together.

Following is a simple C program that printing a message.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 28


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
Printf(“welcome to C”);
getch();
}

Here # is called as preprocessor directory.

#include<stdio.h> tells the compiler to read the file stdio.h and include its
contents in this file. stdio.h is one of the header files, contain the information
about input and output functions. stdio.h means Standard Input Output Header
file.

#include<conio.h> tells the compiler to read the file conio.h and include its
contents in this file. stdio.h is one of the header files, contain the information
about clrscr() and getch() functions. conio.h means Consoled Input Output
Header file.

Void main()
The word main followed by a pair of parenthesis(),which indicates that main
is also a function. Left brace ‘{‘ represents the beginning of the program. When
ever we found the right brace ‘}’ that indicates the ending of the program.

Clrscr () tells the compiler to clear the screen and kept the cursor at left side
corner.
Getch() is reads the single character directly from keyboard without printing on
the screen.

Comment lines: comments in the program are optional and may appear at any
where in the program. It indicates the message to the user. The comment lines
can be represented as follows

Eg: /* hi this is a C program

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 29


Wish you good look */
Or
// hi this is a C program.

Boady of function is enclosed with in a pair of braces. This section contains


two parts
a)Declaration part
b)Executable part

Rules to write C program


1. All C statemants must end with semi colon.
2. C is case sensitive. That is, upper case and lower case characters are
different. Generally the statements are typed in lower case.
3. A C statement can be written in one line or it can be split into multiple
lines.
4. Braces must always match upon pairs, i.e., every opening brace { must
have a matching close brace }.
5. Every C program starts with void main() function.
6. Comments cannot be nested. For Eg /* welcome to
‘C’,/*programming*/
7. A comment can be split into more than one line.
Execution of C program
Steps to followed in writing and running a C program.
a)Creation of source program
Create a C program file in various C compilers are available under MS-DOS,
Turbo C Editor etc.
b)Compilation of a program
Turbo C compiler is user friendly and provides integrated program
development environment. Thus, selecting the key combination can do
compilation that means Alt+F9.

c)Program Execution
The turbo C environment, the RUN option will do the compilation and
execution of a program. Press Ctrl+F9 for execution of the program.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 30


CONTROL STRUCTURES
Generally, the statements in a program are executed in top down fashion. But, in
some situations the flow of execution may have to change from linear
order(sequential order) to some other part of the program. To perform such
transfers control structures are used. There are two types of control structures
I. Conditional Control structures
II. Loop control structures

I. Conditional control Structures

1. if statement
the if statement is used to control the flow of execution of the statemants.

Syntax: if(condition)
Statement;

Here , if the condition is true then the statement will be executed otherwise
the controller will comes out.

Eg: if(a>b)
Printf(“welcome to C”);

The if statement may contains more than one statement

Syntax: if(condition)
{
Statement1;
Statement2;
.
.
}
Here, if the condition is true then all the statements will be executed in the
sequential order.

Consider the following example program to find out the greatest number.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 31


#include<stdio.h>
void main()
{

int a,b;
printf(“Enter two values”); // input: 20,10.
scanf(“%d%d”,&a,&b);
if(a>b)
{
Printf(“a is the greatest number”);
}

//output : a is the greatest number

2) if-else statement:-
It has a condition inside if bracket. If the condition is true, the statement following
if statement is executed. If the condition is false, the statement which following
else, is executed.

Syntax If(condition)
statement1;
else
statement2;
here if the condition is true the statement1 will be executed otherwise te
statement2 will be executed.

Consider the following example program for If-else


#include<stdio.h>
#include<conio.h>
Void main()
{
int marks;
clrscr();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 32


printf(“enter marks=”); //input:35
scanf(“%d”,&marks);
if(marks>=40)
printf(“u r pass”);
else
printf(“sorry! u r fail”);
getch();
}

Output:
enter marks=35
sorry! u r fail

3) Nested if-else statement:-


In this statement,if-else statement is placed inside if statement or else
statement or both.
Syntax: If(condition)
{
If(condition)
Statement;
Else
Statement;
}
Else
{
If(condition)
Statement;
Else
Statement;
}

Consider the following example program for nested If-else


#include<stdio.h>
#include<conio.h>
Void main();
{
int a,b,c;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 33


clrscr();
printf(“enter a,b,c=”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“largest=%d”,a);
else
printf(“largest=%d”,c);
}
else
{
if(b>c)
printf(“largest=%d”,b);
else
printf(“largest=%d”,c);
}
getch();
}
output
enter a,b,c=10 9 8
largest=10
enter a,b,c= 9 10 8
largest=10
enter a,b,c=8 9 10
largest=10

4) else-if ladder:
It is decision control statement.

Syntax:
If(condition)
Statement;
Else

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 34


If(condition)
Statement;
Else
If(condition)
Statement;
Else
Statement;
Consider the following example program for else-if ladder
#include<stdio.h>
#include<conio.h>
Void main()
{
int marks;
clrscr();
printf(“enter marks=”);
scanf(“%d”,&marks);
if(marks>=90)
printf(“A grade”);
else
if(marks>=80)
printf(“B grade”);
else
if(marks>=70)
printf(“C grade”);
else
if(marks>=60)
printf(“D grade”);
else
printf(“E grade”);
getch();
}
output
enter marks=95
A grade

4) Switch case statement

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 35


This statement is also known as “multidirectional conditional control structure”.
It is used when an expression value which is to be checked can be
arithmetic,logical or bitwise.
The syntax of the switch statement is
Switch(expression)
{
Case value 1: Block of statements;
Break;
Case value 2: Block of statements;
Break;
Case value 3: Block of statements;
Break;
.
.
.
.
Case value n: Block of statements;
Break;
Default : statement;
}

During the execution of the program, the expression value is checked against each
of the specified alternatives and when a match is occurs, the block of statements
of that first match is executed.
The break statement must be included at the end of each case. Incase, if it
is omitted, the control after executing a block of statements and also proceeds
into the next subsequent case, even though match has already taken place.

Consider the following example program for switch case statement


#include<stdio.h>
#include<conio.h>
Void main()
{
int day;
Clrscr();
printf(“enter day=”);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 36


scanf(“%d”,&day);
switch(day)
{
case1:printf(“monday”);
break;
case2:printf(“tuesday”);
break;
case3:printf(“wednesday”);
break:
case4:printf(“thursday”);
break;
case5:printf(“friday”);
break;
case6:printf(“saturday”);
break;
case7:printf(“sunday”);
break;
default;printf(“enter correct day”);
}
getch();
}

output
enter day=1
monday
enter day=8
enter correct day

The exit() function


The exit() is a function in the standard library of C. This function causes immediate
termination of the program and execution control return to operating system. In
general, the termination to exit() function is 0 to indicate that termination is
normal. Other arguments may be used to indicate some sort of an error.
Eg:
#include<stdio.h>
#include<conio.h>

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 37


Void main()
{
int day;
Clrscr();
printf(“enter day=”);
scanf(“%d”,&day);
switch(day)
{
case1:printf(“monday”);
break;
case2:printf(“tuesday”);
break;
case3:printf(“wednesday”);
break:
case4:printf(“thursday”);
break;
case5:printf(“friday”);
break;
case6:printf(“saturday”);
break;
case7:printf(“sunday”);
break;
default;exit(0);
}
getch();
}

LOOP CONTROL STRUCTURES

The concept of repeating the execution of a particular block of statements against


a condition is called as looping.
Phases of a Loop
Looping construct have following three phases:
1. Initialization phase
2. Condition phase
3. Increment/Decrement phase

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 38


Three types of Loops
1. for loop
2. while loop
3. do-while loop

1. for loop: It is a looping statement which repeat again & again till some
condition is satisfied.It contains three expressions in a single line. First
expression indicates initialization, the second expression indicates
condition and the third expressin indicates
incrementation/Decrementation.

The syntax of for loop is as follows

for(exp1;exp2;exp3)
{
Block of statements;
}
The expression1,expression2,expression3 may not present in a loop

For( ; ; )
{
Block of statemens;
}

//Initialize counter the used to initialize the variable. Test counter test the
condition. Increment counter increment the value of the variable.

Consider the following example program for for-loop

#include<stdio.h>
#include<conio.h>
Void main()
{
int i;
clrscr();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 39


for(i=10;i>=1;i--)
printf(“%d”,i);
getch();
}
Output
10 9 8 7 6 5 4 3 2 1

Nesting loops
Loops can be nested one inside the other.
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j;
Clrscr();
for(j=1;j<=9;j++)
{
printf(“\n”);
for(i=1;i<=j;i++)
{
printf(“%d\t”,i);
}
}
getch();
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 40


2. while loop: It is an entry condition loop. In this loop,first the condition is
checked,if it is true then the statements will be executed. That particuler
block of statements executes repeatedly untill the condition fails. The
general form of while is as shown below.
Syntax
While(condition)
{
Statement;
Statement;
Increment/Decrement;
}
Consider the following example program for while-loop
#include<stdio.h>
#include<conio.h>
Void main()
{
int=i;
Clrscr();
while(i<=10)
{
printf(“%d\t”,i);
i=i+1;
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10

3. Do-while loop:It is an exit control loop. First the statements are executed &
then the condition is checked. The statements are executed once even if the
condition fails at the beginning.
Syntax:
Do
{
Statement1;
Statement2;
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 41


While(condition);
Consider the following example program for do-while loop
#include<stdio.h>
#include<conio.h>
Void main()
{
Clrscr();
Do
{
Printf(“best of luck”);
}
While(4<1);
Getch();
}
Output: Best of luck

Difference b/w while loop & do-while loop


While Do-while

Entry –controlled loop Exit –controlled loop

Test condition is checked before Test condition is checked after


execution of the statements execution of the statements
If test condition fails, body of the
Body of the loop is executed once
loop is not executed. even if the test condition fails at
first run.
Semicolon is not used after while Semicolon is used after while.
statemen.
Syntax: Syntax:

While(test counter) Do
{ {
Statement; Statement 1;
Statement; Statement 2;
Increment/decrement counter; }
} While(comdition);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 42


GO TO STATEMENT

Goto statement is used to change the normal sequence of program. It takes the
control to some other part of the program.
syntax:
goto label;
label;
statement;

we can transfer the control either forward or backwards.

Example program for goto statement

#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b,sum;
clrscr();
goto forward;
sum=a+b;
printf(“sum=%d”,sum);
forward:
printf(“enter a,b=”);
scanf(“%d%d”,&a,&b);
getch();
}

Break statement
Break statement is used to exit from a loop. It can be used with while loop,do-
while,if statement & switch statement.

Eg: while(condition)
{
Statement1;
Statement2;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 43


Break;
Statement3;
Statement4;
}

Consider the following Example program for Break statement

#include<stdio.h>
#include<conio.h>
Void main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
printf(“%d\t”,i);
if(i==8)
break;
}
getch();
}
Output
1 2 3 4 5 6 7 8

Continue statement
A Continue statement returns the control to the beginning of the repeating loop.
Eg: while(condition)
{
........;
........;
If(n==0) continue;
. . . . . . . .;
. . . . . . . .;
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 44


Consider the following Example program for Break statement

#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j;
clrscr();
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
printf(“%d\t%d”,i,j);
}
}
getch();
}

Output
1 2
2 1

Structured Programming:

Structured programming is an approach to design and development of the


programs. It is a discipline of making a program’s logic easy to understand by
using only the basic three control structures:
• Sequence (straight line) structure
• Selection (branching) structure
• Repetition (looping) structure
While sequence and looping structures are sufficient to meet all the requirements
of programming, the selection structure provides to be more convenient in some
situations.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 45


The use of structured programming technique s helps ensure well-designed
programs are easier to write ,read, debug and maintain compared to those that
are unstructured.
Structured programming discourages the implementation of unco9nditional
branching using jump statements such as goto,braek and continue. In its purest
form, structured programming is synonymous. With “goto programming”.
Do not goto goto statement!

ARRAYS
An array is a collection of similar data items,which have similar data type and
have a common name is used to represent a group of related data items. The data
items can be integer,float or character. All data items must have same data type
and storage class. The first element in the array is numbered 0 and last element is
one less than the size of array. Array is also known as subscripted variable.
Array are of two types

1. Single dimensional array/linear array

2. Double dimensional array/non-linear array

3. Multi dimensional array

1) Linear Array /Single dimensional Array


One dimentional array is called as a linear array. They are represented by using
single sub script.

Syntax: data type variable-name[size];

E.g: char city [100];

City is the name of an array. It can stored 100 char data type values.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 46


Initialization of linear Array

Array can be initialized by giving initial values. These values must be enclosed in
braces and seprated by a comma.

Syntax: data type variable-name[size]={value1,value2,…value n};

Eg: int marks [5]={1,2,3,45};

Here marks is an array. It contains 5 values.

1 2 3 4 5
Marks[0] Marks[1] Marks[2] Marks[3] Marks[4]

2) Non-linear array/Double dimensional array


An Array having more than one sub script is called as a non-linear array. For
Two dimentional array is is a non-linear array. 2D array have two subscripts.

Syntax: data type variable-name[size][size];

Eg: int a[5][5];

These arrays are used to store the data in the form of row, column format.

Program with a 2D array.

#include<stdio.h>
#include<conio.h>
void main()
{
int student[4][2];

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 47


int,i,j;
printf(“enter the roll no & marks of four student\n”);
for(i=0;i<=3;i++)
{
for(j=0;j<2;j++)
{
scanf(“%d”,&student[i][j]);
}
}
printf(“this is what you enterd”);
for(i=0;i<=3;i++)
{
printf(“\n”);
for(j=0;j<2;j++)
{
printf(“%d\t”,student[i][j]);
}
}
getch();
}
Output
enter the roll no and marks
1 85 2 90 3 94 4 98
1 85
2 90
3 94
4 98
two-dimeentional array can be initilizad as given below
int student[4][2]={{1,85},{2,90},{3,94},{4,98}};

two-dimeentional array can be initilizad as given below


int student[4][2]={
{1,85},
{2,90},
{3,94},
{4,100}
};

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 48


An array can be traversed:-Traversing is to visit each element of array exactly
once. We can traverse an array with the help of loop control statements.

Consider the following Example program for printing an array elements

#include<stdio.h>
#include<conio.h>
Void main()
{
int marks[6]={10,20,30,40,50,60};
int i;
clrscr();
for(i=0;i<=5;i++)
printf(“%d\t”,marks[i]);
getch();
}
output:10 20 30 40 50 60

Write a program to enter & display element of array?


#include<stdio.h>
#include<conio.h>
void main()
{
int marks[6];
int i;
clrscr();
printf(“enter the elements of array=”);
for(i=0;i<=5;i++)
sacnf(“%d”,&marks[i]);
printf(“elements of array are \n”);
for(i=0;i<5;i++)
printf(“%d\t”,marks[i]);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 49


Output:
Enter the elements of array 10 20 30 40 50 60
Elements of array are
10 20 30 40 50 60

WAP to find the sum of elements of array?


#include<stdio.h>
#include<conio.h>
Void main()
{
int marks[6]={1,2,3,4,5,6};
int i,sum=0;
for(i=0;i<=5i++)
{
sum=sum+marks[i];
}
printf(“sum=%d”,sum);
getch();
}
output: sum=21

WAP to find the sum of elements of 2-D array?


#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][3]={1,2,3,4,5,6,7,8,9,5,7,3};
int i,j,sum=0;
clrscr();
printf(“the elements of array are\n”);
for(i=0;i<=3;i++)
{
printf(“\n”);
for(j=0;j<=2;j++)

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 50


{
printf(“%d\t”,a[i][j]);
sum=sum+a[i][j];
}
}
printf(“\nsum=%d”,sum);
getch();
}
output: sum=60

3) Multi dimensional array


C allows arrays of three or more dimensions. The exact limit is
determined by the compiler. If an array is said to be 3D array, if it has
three arguments to represent it’s size. The declaration of 3D arrays is as
follows.
Syntax: data type variable[index1 ][index2][index3];
Eg: int a[2][3][4];

Where index1 indicates the number of tables, index2 indicates the


number of rows, index1 indicates the number of columns in each table.

STRINGS
A string is nothing but a “group of characters ” or “character
array”. Each character occupies one byte of memory, and the last character is
always ‘\0’(null character). For example, the string SYSTEM would be stored as

‘S’ ‘Y’ ‘S’ ‘T’ ‘E’ ‘M’ ‘\0’


The length of the string is the number of characters it contains excluding null
character.

The general form of declaration of string variable is


Syntax: char string-name[size];

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 51


Eg: char name[30];

String variables can be initialized at the time of declaration

Eg: char name[30] = “Computers”


char designation[25] = “Education”

or

char name[30] = {‘C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’,’s’,’\0’};

The scanf() and printf() function used with %s with format specification to read
and print a string.

Example:
Char str[25];
Scanf(“%s”,str);
printf(“%s”,str);

In the case of reading strings, the ampersand(&) is not required before the
string variable name. As mentioned earlier, one of the limitations of the scanf() is
that it is not capable of holding multiword strings, even though it can read them.
To overcome this problem, we can use gets() function in place of scanf() function.
Similarly puts() function can be used in place of printf().

STRING HANDLING FUNCTIONS

1) Strlen()
2) Strlwr()
3) Strupr()
4) Strrev()
5) Strcpy()
6) Strcat()
7) Strcmp()
8) Stricmp()

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 52


1) Strlen(): This function counts the number of characters present in a string.

Syntax: strlen(string );

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
char s1[25];
printf(“Enter String:”);
gets(s1);
printf(“Total no.of characters are %d ”,strlen(s1));
}
In put: Enter string:Hello World
Out put : Total no.of characters are 11

2) Strlwr():This function converts a string into lower case.

Syntax: strlwr(string);

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
char s1[25];
printf(“Enter String:”);
gets(s1);
printf(“ %s ”,strlwr(s1));
}

In put: Enter string: HELLO WORLD

Out put: hello world

3)Strupr: The function converts a string into upper case.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 53


Syntax: strlupr(string);

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
char s1[25];
printf(“Enter String:”);
gets(s1);
Printf(“ %s ”,strupr(s1));
}

Input: Enter string: hello world

Output: HELLO WORLD

4)Strrev(): This function reverse the given string.

Syntax: strrev(string);

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
Char s1[24];
Printf(“Enter String:”);
gets(s1);
Printf(“ %s ”,strrev(s1));
}

Input: Enter string: hello world

Out put: dlrow olleh

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 54


5)strcat(): This function concatenates the source string at the end of the
target string. i.e., the combination of two strings forms a new string.

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
Char s1[25],s2[25];
Printf(“Enter two String:”);
gets(s1,s2);
Printf(“ %s ”,strcat(s1,s2));
}
In put: Enter two strings: Hello World
Welcome to C

Output: Hello World Welcome to C

6)strcpy(): This function copies the content of one string into another string.

Syntax: strcpy(string1,string2);

Here strcpy() is used to copy the character from string2 to string1. The function
returns the result string string1 the string2 remains unchanged. String2 may be a
character array or a string constant.

Eg: #include<stdio.h>
#include<string.h>
Void main()
{
char s1[25],s2[25];
Printf(“Enter String s1:”);
gets(s1);
Printf(“ The string s2 is : %s ”,strcpy(s2,s1));
}
In put: Enter string s1: Hi this is Kamal

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 55


Output: The string s2 is: Hi this is Kamal

7)strcmp(): This function compares two strings to determine whether the are
same or not. The two strings were compared character by character until there is
a match in string (or) end of the string. If two strings are equal, it returns a value
“0”. Otherwise it returns numeric difference between ASCII value of non matching
characters.

Syntax: strcmp(string1,string2);

In the above syntax, the function returns less than “0” if string1 is less than
string2, and greater than “0” if string1 is greater than string2.

Consider the following example program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[25],s2[25];
int c;
clrscr()
printf(“Enter String s1:”);
gets(s1);
printf(“Enter String s2:”);
gets(s2);
c=strcmp(s1,s2);
if(c>0)
printf(“string1>string2”);
else if(c<0)
printf(“string1<string2”);
else
printf(“both strings are equal”);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 56


Input:

Enter string s1:abc


Enter string2:ABC

Output:
String1>string2

FUNCTIONS
A function a self contained block of statements that perform a
specific task. Function are also called sub programs.

Dividing a program into functions is one of the major principles of top-down


structured programming. Using functions it is possible to reduce the size of
program by calling & using them at different places in a program. Thus a complex
problem may be decomposed into a smaller easily manageable parts or modules
called function.
Every C program must contains atleast one function. If the function contains only
one function that should be main().
The main Advantages of using functions are:
 Easy to write a correct & small function.
 Easy to reads ,write & debug a function.
 Easier to maintain or modify such a function.
 Small functions tend to be self documenting & highly readable.
 It can be called any number of times in any place with different
parameters.

SYNTAX:-
Return type functionname (datatype arg1,datatype arg2,…………)
{
body of function
………………….
………………….
………………….
return (expression);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 57


The first line of function defining (return type) refers to type of value it
would return to the calling portion of the program. Any datatype as int,float,char
etc.

E.g;- int functionname(…………………);


float functionname(…………………);
void functionname(…………………);

Features of functions:
a) Function Declaration and prototypes.
b) Calling functions by value or by reference
c) Recursion

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 with arguments and return values

Example program for Functions with no arguments and no return values


#include<stdio.h>
#include<conio.h>
void printline(); function declaration
void main()
{
clrscr();
printline();
printf(“This illustrates the use of C functions \n ”);
printline();
getch();
}
void printline()
{
int i;
for(i=1;i<=40;i++)

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 58


printf(“-”);
printf(“\n”);
}

Example program for Functions with arguments but no return values

#include<stdio.h>
#include<conio.h>
void swap(int, int); Function declaration
void main()
{
int a,b;
clrscr();
printf(“Enter 2 numbers :”);
scanf(“%d%d”,&a,&b);
swap(a,b); Function call
getch();
}
void swap(int x, int y) Called function
{
int z;
z = x;
x = y;
y = z;
printf(“\n After swapping : %d%d”, x,y);

Example program for Functions with arguments and return values

#include<stdio.h>
#include<conio.h>
int big(int, int);
void main()
{
int a,b,max;
clrscr();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 59


printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
max = big(a,b);
printf(“\n The biggest number is: %d”, max);
getch();
}
int big(int x, int y)
{
If(x>y)
return x;
else
return y;
}

Prototype: It is function declaration in which we specify the data type of the


arguments to be passed to the called function. It is also return type

For ex:float sample(int a,int b);

Difference b/w arguments & parameters

Arguments: These are variables, which are passed to the function at the time of
function call.

Parameters: These are the variables, which are present in the first line of function
definition.

Actual arguments: These arguments mentioned in function call are called as


“actual arguments”. For example in the function call statement swap(a,b), the
two parameters are a and b are known as Actual parameters.

Actual arguments: The arguments in the function declaration are called as


“formal arguments”. In the function definition swap(int x, int y), the parameters x
and y are called as formal parameters.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 60


Difference b/w formal & actual arguments

Formal arguments Actual arguments


These are local to function in which they They are global. Scope is in entire
are defined program.
They are written in first line of function They are written in function declaration.
defining.

WAP to find the factorial of a number


#include<stdio.h>
#include<conio.h>
long int factorial (int n);
{
int i;
long int prod=1;
if(n>1)
for(i=2;i<=n;i++)
prod*=i;
return(prod);
}
void main()
{
int n;
printf(“n=”);
scanf(“%d”,&n);
printf(“factorial is =”,%d factorial(n)); //calling the function
getch();
}
output: n=5
factorial=120

Function are of two types:


1. Library function:- These functions are predefined in the compiler. They are
kept in special library files. If a program needs a function , the corresponding
library file is attached to the program during processing. C language has a no. of
library function.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 61


 Library function that carry out standard input output operation. For e.g:
printf(), scanf(), getchar(),putchar(),fclose,fopen etc. they are include a
<stdio.h> file under.
 Library function which carry out of operation on characters. For e.g:-
tolower(c),toupper(c). they are grouped under a <ctype.h> file.
 Library function that a performed operation on strings. For e.g:-
strcmp(),strcpy(),strlen(),strcat(). They are grouped under a <string.h>file.
 Library function that carry out mathematical calculations. E.x:-sqrt(),
pow(),abs(). They are grouped under <math.h>file.

2.User defined functions;-A function, which is defined by the user & does some
useful work,is called user-defining function.
For ex:- akhil(); student();etc.

Use of function
 Large programs are difficult to understanding so they are divided into
functions.
 Function are easy to write,understand & debug.
 Different programs can use a single function.
 Library function can be attached to the program.
 Function can be reused in other programs.
 Library functions save time & space.
 Function are independent of system.
 Program design become easy and program can be executed fast.
 Coding becomes compact with the help of functions.
 Debugging of program becomes easy.
 Memory is efficiently used.
 Program complexity is reduced.
 Execution speed are increase.
 User defining function are more flexible.
 Testing becomes easy.
 Program reliability is high.
How a function can be declared?
Every function must be declared to the compiler before using it in the program.
<returntype><datatype><fuctionname>(arguments);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 62


How will you call a fumction?
The function can be called by giving its name followed by list of arguments.
Arguments are separated by commas & are enclosed in brackets. For ex:- calsum
(a,b);

Function can be defined;- in the function defining contents of are defined.


<returntype><datatype><fuctionname>(arguments);
Arguments declaration;
{
Function body;
}

#include<stdio.h>
#include<conio.h>
int calsum(int,int);
void main()
{
int a,b,sum;
clrscr();
printf(“enter the a,b=”);
scanf(“%d%d”,&a,&b);
sum=calsum(a,b);
getch();
}
int calsum(x,y);
int x,y;
{
int c;
c=x+y;
return(c);
}
What is the purpose of main() function?
The function main() invoke other functions in it. It is the first function to be called
when the program starts execution. Program execution always starts with the
function of main().

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 63


What is dummy function?
A function,which performs no function, is known as dummy function. It is a valid
function. They may be used as placeholders,which help to add new function later
on.
Defference b/w main() & user define function?
main() function user define function
It is compulsory function in a prog. It is not compulsory.
Program execution starts with main(). It is not true for user define function.
Main() function is called once. There can be many user defined function
in program.
It is informs compiler to begin compilation. It is used to perform a particular task.
It can be the only function in a program. It cannot be the only function in a
program.

Parameter passing Mechanism

The parameters can be generally passed to a function in the following 2 ways


• Call by value
• Call by reference
CALL BY VALUE
According to the call by reference mechanism, the value of each actual
parameters can be copied into corresponding formal parameters.

Any changes done to the formal parameters have no effect on the actual
parameters, because they have separate memory locations.

//TRY to Swap the value of two variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
swap v(a,b);
printf(“a=%d b=%d”,a,b);
}
Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 64
swap v(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
print(“x=%d y=%d”,x,y);
}
output;- x=20 y=10
a=10 b=20

CALL BY REFERENCE
According to this mechanism, the address of each actual parameters can be
copied into corresponding formal parameters. The formal parameters are pointer
variables and they are pointing to same memory location of the corresponding
actual parameters.
Any changes done to the formal parameters have to effect on the actual
parameters, because they have single memory locations

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
swap v(&a,&b);
printf(“a=%d b=%d”,a,b);
}
swap v(int *x,int *y);
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 65


output:

a=20,b=10
FUNCTIONS WITH ARRAYS

Like the values of simple variables, it is also possible to pass the values of an array
to a function. To pass an array to a called function, it is sufficient to list the name
of the array, without any subscripts, and the size of the array as arguments. For
example, the call

largest(a,n);
will pass all the elements contained in the array ‘a’ of size ‘n’. The called function
expecting this call must be appropriately defined. The largest function header
might look like:
int largest(array,size);
int array[];
int5 size;
Example Program for sorting of an array elements
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int marks[5];
clrscr();
printf(“Enter 5 sub marks:”);
for(i=0;i<=4;i++)
scanf(“%d”,&marks[i]);
printf(“\nMarks before sorting \n”);
for(i=0;i<=4;i++)
printf(“%d”,marks[i]);
sortmarks(marks,5);
printf(“\nMarks after sorting \n”);
for(i=0;i<=4;i++)
printf(“%d”,marks[i]);
getch();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 66


}
void sort(int x[],int n)
{
int I,j,t;
for(i=1;i<=n-1;i++)
for(j=1;j<=n-1;j++)
if(x[j-1] >= x[j])
{
t=x[j-1];
x[j-1]=x[j];
x[j]=t;
}
}

How the elements of array can be passed on to a function?


The element of array can be passed to a function by calling the function by
value or by reference. In call by value we pass the value of array elements to the
function. In call by reference we pass addresses of array elements to the function.

//passing array elements to a function using call by value.


main()
{
int i;
int num[]={10,20,30,40,50};
for(i=0;i<=5;i++)
display(int n);
printf(“%d”,n);
}
Output:-10 20 30 40 50

//passing array elements to a function using call by reference.


main()
{
int i;
int num[]={10,20,30,40,50};
for(i=0;i<=5;i++)
display(&num[i]);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 67


}
display(int*n)
{
printf(“%d”,*n);
}
output:- 10 20 30 40 50
RECURSION
The process in which a function calls itself again and again until some condition
is satisfied is called recursion. These functions are called recursive functions. We
can find factorial of a number by using recursion as follow.

5*fact(4) 4*fact(3) 3*fact(2) 2*fact(1)

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,fact;
printf(“enter any num=”);
scanf(“%d”,&a);
fact=rec(a);
printf(“factorial=%d”,fact);
}
int rec (int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 68


Output :
Enter any num=5
Factorial=120
Rec(5) return 5 times rec(4)
Rec(4) return 4 times rec(3)
Rec(3) return 3 times rec(4)
Rec(2) return 2 times rec(1)
Rec(1) return 1

Advantages of Recrsion

• with the help of recrsion, the program code is compact,each to write and
understand.
• Program code is compact.
• It can be used to solve mathematical problem.
• Useful in multiprocessing and multitasking.
• Compilation time of the program is less.
• Can be used to solve problem of data structures.
• It saves memory.

Disadvantage

• It takes more stored space.


• It is less efficient.
• It may take more time is some problems.
• It requires some stopping condition.

Local variable:- The variable, which are declared within the body of a function,
they are called local variables of that function. They can be used in that function
only.

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 69


clrscr();
a=20;
b=10;
sum=a+b;
printf(“sum=%d”,sum);
getch();
}

Output: sum=30

Global variable:
Local variables are declared within the body of a function, and can only be used
within that function. This is usually no problem, since when another function is
called, all required data is passed to it as arguments. Alternatively, a variable can
be declared globally so it is available to all functions. Modern programming
practice recommends against the excessive use of global variables. They can lead
to poor program structure, and tend to clog up the available name space.

A global variable declaration looks normal, but is located outside any of the
program's functions. This is usually done at the beginning of the program file, but
after preprocessor directives. The variable is not declared again in the body of the
functions which access it.

Example:
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
int sum;
clrscr();
a=20;
b=10;
sum=a+b;
printf(“sum=%d”,sum);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 70


Output:
sum=30
Static Variables
Another class of local variable is the static type. A static can only be accessed from
the function in which it was declared, like a local variable. The static variable is
not destroyed on exit from the function, instead its value is preserved, and
becomes available again when the function is next called. Static variables are
declared as local variables, but the declaration is preceeded by the word static.

static int counter;

Static variables can be initialised as normal, the initialisation is performed once


only, when the program starts up.

Difference b/w external variable & external declaration


External variable definition External variable declaration
It is not necessary to write keyword It is necessary to write keyword
extern. extern.
Storage space is allocated at a time Storage space is not allocated at a
of definition. time of definition.
It can include assignment of initial It cannot include assignment of
values. initial values.

For example:

#ionclude<stdio.h>
#include<conio.h>
int x=20; //external variable definition
void main()
{
extern int x; //external variable declaration
clrscr();
print(“%d”,x);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 71


STRUCTURE
Structure is a collection of related data items of different data
types. One structure may contain integer, floating point and character
elements. These elements are called member of the structure is similar
to a record, for example, For one student, we process data items such
as roll number, name, subject and marks.
Syntax:
Struct structure_name
{
Structure element 1;
Structure element 1;
Structure element 1;
.
.
};
Structure structure_name list of structure variables;
(or)
Struct structure_name
{
Structure element 1;
Structure element 1;
Structure element 1;
.
.
}list of structure variables;

List of structure variables should be separated with the comma.


Example:

struct student
{
char name[10];

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 72


float fees;
int roll no;
};
struct student s1,s2,s3;

Data can be entered into structure variable by using scanf ()


statement and dot operator. Data can be displayed by using printf () statement
and dot operator as shown:

Structure element can be referenced


The member function of the structure can be referred as shown:
Structure variable, member
The member is written after a dot following the structure variable. This dot is
called member operator.
Example:
S1.name;
S2.roll no;
S3.course

Example :
#include<stdio.h>
#include<conio.h>
struct personal
{
Char name[20];
Int day;
Char month[10];
Int year;
Float salary;
}
void main()
{
struct personal person;
clrscr();
printf(“Input values\n”);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 73


scanf(“%s%d%s%d%f”,person.name,
&person.day,person.month,&person.year,&person.salary);
printf(“%s%d%s%d%2f”,person.name,
&person.day,person.month,&person.year,&person.salary);
getch();
}
Input:
Ramu 23 December 1989 10000
Output:
Ramu 23 December 1989 10000 .00

STRUCTURE CONTAINING ARRAY/ ARRAY OF STRUCTURE


We can use array as structure member.
Example:
Struct student
{
int roll no;
char name[40];
int marks[12];
};

Array of structure
We can declared array of structures to declare many structure variable.
Example;
Struct example
{
Int roll no;
Char name[20];
Float marks[10];
};
Struct example student[100];

It define an array called student, which contain 100 students.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 74


Structure with in Structure
A structure can contain one or more structures as members. It is called
nesting of structure.

Example:

struct salary
{
char name[20];
char dept[10];
struct
{
int dearness;
int house_rent;
int city;
}allowance;
}employee;

In the above example , the salary structure contains a member named allowance
which it self is a structure with three members. The members contained in the
inner structure namely dearness, house_ rent, and city can be referred to as

employee.allowance.dearness
employee.allowance.house_rent
employee.allowance.city

Features of a structure
 It permits different data types.
 We can make an array of structures.
 Pointer to a structure can be made.
 Structures can be nested.
 Self referential structure can be defined.
 Union can be a member of structure.
 The member of a structure can be accessed and manipulated.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 75


Uses of structure
 Structures can be used for database management.
 Changing the size of cursor.
 Clearing the contents of screen.
 Placing the cursor at any position on the screen.
 To draw any graphics on screen .
 Receiving data from the keyboard.
 To check the memory size of the computer.
 To find out the llist of equipment attached to the computer.
 To format a floppy.
 Hiding a file from the directory.
 To display the directory of a disk.

Compare array and structures


Array Structures
It is a collection of data items of Collection of data items of different
same data type. data types.
It has declaration only. It has declaration and definition.
No keyword is used. Keyword is struct.
Array name represents address of It is a short hand description of the
starting elements. declaration.
Array cannot have bit fields. Structure may contain bit fields.

UNIONS
Unions are a concept which is similar to structures, therefore the syntax of the
unions are also same as structures. However, there is a major difference between
them in terms of storage. In structure each member has its own storage location,
where as all the members of a union use the same 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 key
word union as follows:

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 76


Syntax:
Union union-name
{
Declaration of variables ;
Block of statements;
}union-variable;

Eg:
Union item
{
int m;
float x;
char c;
} code;
The union contains three members, each with a different data type.
However, we can use only one of them at a time. This is due to the fact that only
one location is allocated for a union variable, irrespective of its size. The compiler
allocates a piece of storage that is large enough to hold the largest variable type
in the union. In the declaration above the member ‘x’ requires 4 bytes which is
the largest among the members.

Type def:
C provides the “typedef” key word that allows you to specify a new name
for a data type already provided in C programming language. That means, we can
use the type def keyword to specify an identifier for an existing data type.
Syntax:
typedef data-type new name;
Eg:
Typedef int num;
Here the new identifier “num” has been specified for “int” data type.

Example program for “typedef”:


#include<stdio.h>
#include<conio.h>
void main()
{

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 77


typedef int num;
num a,b,add;
a=10;
b=20;
add=a+b;
clrscr();
printf(“The sum of %d and %d is: %d”,a,b,add);
getch();
}

Size of structures:
We can use structures, unions and arrays to create variables of large sizes.
The actual size of these variables in terms of bytes may change from machine to
machine. We may use the unary operator “sizeof” to tell us the size of structure
(or any variable).

Syntax:
Sizeof(struct structure -name)
(Or)
Sizeof(structure variable)
Eg:
Sizeof(struct x)
(Or)
Sizeof(y)
Here y be the simple structure variable.

POINTER
Pointer is a memory address variable,which points to a location in memory. It
contains memory address of another variable or array element.
Example:
int i=5;
int *j;
j=&i;
here i is a variable which stored value 5 at some location in memory. Let
the address of this memory location is 1001. j which is a pointer variable will store

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 78


the address of i. the operator & is address operator. The statement j=&i; tells that
j contain the address of i. Pointer variables are declared with the help of
asterisk(*).
Asterisk (*) means value at address.
*j=*(&i)
=*(1001)
=5.
Uses of pointers
 Pointer can access elements easily.
 Pointer allows us to access a variable,which is defined outside the function.
 They reduce the length and complexity of program.
 They increase the speed of execution.
 Use of pointer array saves the data storage space in memory.
 They can be used to return multiple data items from a function.
 They can used to represent multidimentional arrays.
 Files can be handled efficiently..
 Pointers can be used for addressing by using hash techniques.
 Useful in data structure manipulate.
 Pointers can be used as scalar data type or derived data type.
 Array can be easily accessed by using pointer.
 Call by reference is achieved by using pointer.
 Complex declarations are possible in pointers.

Pointer variable can be declaration and initialized.


Pointer can be declares in same way as other variables. Pointer can be
declared as follow:
Type *variable name;
The asterisk(*) inform the compile that the variable is pointer variable. The
variable type refer to int char,float or doudle. Pointer variable inform the
compiler that it needs a memory location.

Example;
Int *ptr;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 79


Pointer can be used with array
Pointer are closely linked with arrays. Array name is pointer to the
first element of array. When we declare an array,compiler allots it sufficient
memory to stored the elements in contiguous memory locations. Example;
int a[5];
int *b;
b=a;
here a[5] is one dimentional array of size 5. first element of a declared as
a[0]. Address of a[0] can be stored in pointer variable b. other elements of array
can be represented.

a[1]=*(b+1);
a[2]=*(b+2);
a[3]=*(b+3);
a[4]=*(b+4);
let us explain this with a program.
void main()
{
int a[6]={5,10,15,20,25,30};
int i,n=6,*b;
clrscr();
b=a;
printf(“array a contain following data\n”);
for(i=0;i<6;i++)
printf(“%d\t”,*(b+1));
printf(“\n data in reserve order is”);
for(i=6;i<6;i--)
printf(“%d”,*(b+1));
getch();
}
OUTPUT: array a contain following data
5 10 15 20 25 30
Data is reserve order is
30 25 20 15 10 5

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 80


Pointer can be used in functions.
Pointer can be passed toa function as argument. This method of
passing argument to a function called call by reference. It can be explained with
following example;
main()
{
int a=10,b=20;
swap r(&a,&b);
printf(“a=%d b=%d”,a,b);
}
swap r(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
OUTPUT: a=20 b=10

WAP pointer can be used to find the length of string.


#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
char *ptr;
int i;
clrscr();
puts(“enter the string=”);
gets(str);
ptr=str;
while(*(ptr+i)!=’\0’)
i++;
printf(“length of string=%d”,i);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 81


OUTPUT
Enter the string =kamal
Length of string=5

DYNAMIC MEMORY ALLOCATION/ MEMORY MANAGEMENT OPERATORS

Many languages permit a programmer to specify an array’s size at run time. The
process of allocating memory at run time is known as dynamic memory
allocation.
Dynamic memory allocation can be done using the following functions.
1. Malloc()
2. Calloc()
3. Free()
4. Realloc()
5. Delete
6. New
The above functions are known as memory management functions.

Malloc()
The malloc() can be used to allocate the block of memory. If the allocation of
memory is done successfully, then starting address of the location returned to a
pointer. If the memory allocation cannot be done, then a “null pointer” will be
returned.
Suppose ‘p’ is a pointer variable, and if it should points to a block of ‘n’
memory locations then the ‘n’ memory locations can be used by the following
syntax
Syntax: ptr=(cast-type*)malloc(byte-size);

E.g: Int *p
p=(int *)malloc(n*size of(int));
If ‘p’ is null then the memory allocation is done, then starting address of
these ‘n’ locations will be returned to a “void pointer”. This void pointer is
converted into an integer pointer by using the concept “type casting”

Calloc()
It is similar to malloc() except that, it requires 2 parameters. The calloc() can be
declared using the following syntax.
Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 82
Syntax: ptr=(cast-type*)callo(n,elem-size);
E.g: Int *p
p=(int *)calloc(n,size of(int));
the major difference between malloc() and calloc() is the memory allocated
by malloc() contains all garbage values where as memory allocated by calloc()
contains all “zero” values.

Free()
This function is used to release the memory dynamic allocation functions. While
using any of the above functions a header file <alloc.h> must be included.
Syntax: free(ptr);
Eg: free(p);
Where ‘p’ is a pointer variable.

Realloc()
Modifies the size of previously allocated space. If the previously allocated
memory is not sufficient and we need additional space for more elements. It is
also possible that the memory allocated larger than necessary and we want to
reduce it. In both the cases, we can change the memory size already allocated
with help of the function realloc(). This process is called the reallocation of
memory. For example, if the original allocation is done by the statement
Ptr=malloc(size);
Then reallocation of space may be done by the statement
Ptr=realloc(ptr, newsize);
If the reallocation of memory is done successfully, it returns a pointer to the first
byte of the new memory block. Otherwise it returns a NULL pointer and original
block is freed(lost).

Delete
The delete operator is used to release the memory space for reuse. The general
form of declaration is
Syntax: delete pointer-variable;
Eg: delete p;
Where ‘p’ is a pointer variable.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 83


New
The new operator allocates memory to hold data object of a data type and
returns the address of the object. The data type may be valid data type.
The new operator can be used to create objects of any type. It takes the
following general form

Syntax: pointer variable=new data type;


Eg: *p=new int;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 84


FILE MANAGEMENT IN”C”
A file is nothing but a collection of related data. C supports a number of
functions that have the ability to perform basic file operations, which include:

 Naming a file
 Opening a file
 Reading data from a file
 Writing data to file, and
 Closing a file

HIGH LEVEL I/O FUNCTIONS

Function Name Operation

fopen() Creates a new file for use, Opens an existing file for
use
fclose() Closes a file which has been open for use
getc() Reads a character to a file
putc() Writes a character to afile
fprintf() Writes a set of data values to a file
fscanf() Reads a set of data values from a file
getw() Reads an integer from a file
putw() Writes an integer to file
fseek() Sets the position to a desired point in the file
ftell() Gives the current position in the file

rewind() Sets the position to the beginning of the fille.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 85


DEFINING AND OPENING A FILE

If we want to store data in a file in the secondary memory, we must specify


certain things about the file, to the operating system. They include:
1. Filename
2. Data structure
3. Pupose

Following is the general format for declaring and opening a file

FILE *fp;
fp=fopen(“filename”, “mode”);

The first statement declares the variable fp as a “pointer to the data type
FILE”. As stated earlier. File is a structure that is defined in the I/O library.
The second statement opens file named “filename” and assigns an
identifier to the FILE type pointer fp. This pointer contains all the
information about the file is subsequently used as communication link
between the system and the program.
The second statement also specifies the purpose of opening this file.
The mode does this job. Mode can be any one of the following.

Mode Purpose

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

r+ The existing file is opened to the beginning for


both reading and writing

w+ same as w except both for reading and writing

a+ same as a except both for reading and writing

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 86


Consider the following statements to open the two files “data” and “results”

FILE *p1,*p2;
p1 =fopen(“data”, “r”);
p2 =fopen(“results”, “w”);

CLOSING A FILE:

A file must be closed as soon as all operations on it have been completed.


To close a file we have to follow the following syntax.

Syntax:

fclose(file_pointer);
Eg:
……….
……….
FILE *p1,*p2;
p1 = fopen(“INPUT”, “w”);
p1 = fopen(“OUTPUT”, “r”);
………
………
fclose(p1);
fclose(p2);

INPUT / OUTPUT OPERATIONS ON FILES

Once a file is opened, reading out of or writing to it accomplished using the


standard I/O routines that are listed below.

• getc()
• putc()
• fprintf()
• fscanf()

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 87


The getc and putc functions:

The simplest file I/O functions are getc() and putc(). These are analoguos to
getchar() and putchar() and handle one character at atime. Assume that a file is
opened with mode ‘w’ and file pointer fp1. Then, the statement
putc(c,fp1);
Writes the character contained in the character variable c to the file associated
with FILE pointer fp1. Similarly, getc is used to read a character from a file that
has been opened in read mode. For example, the statement
C=getc(fp2)
Would read a character from the file whose file pointer is fp2.
The file pointer moves by one character position for every operation getc or
putc. The getc will return an end –of-file marker EOF, when end of the file has
been reached. Therefore, the reading should be terminated when EOF is
encountered.
Example program for writing to and reading from a file
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char c;
clrscr();
printf(“Data Input \n\n”);
f1=fopen(“INPUT”, “w”);
while(c=getchar() != EOF)
putc(c,f1);
fclose(f1);
printf(“\n data output \n\n”);
f1=fopen(“INPUT”, “r”);
while(c=getc(f1) != EOF)
printf(“%c”,c);
fclose(f1);
getch();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 88


}

The fprintf and fscanf functions:

fprintf():
The functions fprintf and fscanf perform I/O operations that are identical to
printf and scanf functions, except of course they work with files. The general form
of fprintf is shown below.

Syntax:
fprintf(fp, “control string”, list);

where fp is a file pointer associated with a file that has been opened string. The
control string contains output specifications for the items in the list. The list may
include variables, contains and strings.

Eg:
fprintf(f1, “%s%d%f”, name, age, 7.5);

fscanf():
The general format of fscanf is shown beliow
Syntax:
fscanf(fp, “control string”, list);
Eg:
fscanf(f2, “%s%d”, item, &quantity);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 89


PROGRAMS

THE FOR LOOP

Program for calculating the sum of 10 natural numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
sum=sum+I;
printf(“the sum of 10 natural numbers is %d”,sum);
getch();
}

Output: the sum of 10 natural numbers is:55

Program for calculating the sum even numbers below 10


#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=2;i<=10;i=i+2)
sum=sum+I;
printf(“the sum even numbers below 10 is %d”,sum);
getch();
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 90


Output: the sum even numbers below 10 is:30

Program for calculating the sum odd numbers below 10


#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i=i+2)
sum=sum+I;
printf(“the sum odd numbers below 10 is %d”,sum);
getch();
}
Output: the sum odd numbers below 10 is:25

Program for printing 1 to n numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf(“Enter n value:”);
scanf(“%d”,&n;)
for(i=1;i<=n;i++)
printf(“\n%d”,i);
getch();
}
Output:
Enter n value: 5
1
2
3
4
5

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 91


Program to caliculate a factorial of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
clrscr();
printf(“Enter n value:”);
scanf(“%d”,&n;)
for(i=1;i<=n;i++)
fact=fact*1;
printf(“Factorial is:%d”,fact);
getch();
}

Output:
Enter n value: 8
Factorial is: 40320

Program for printing the reverse of a number


#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,rev=0;
clrscr();
printf(“Enter a number :”);
scanf(“%d”, &n);
for ( ;n!=0;n=n/10)
{
rem = n%10;
rev=rev*10+rem;
}
printf(“\n the reverse of a number is %d”, rev);
getch();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 92


}
Output: Enter a number:123
the reverse of a number is 321
REVERSE OF A NUMBER
Program:-

#include<stdio.h>
#include<conio.h>
void main ( )
{
int n,rem,rev=0;
clrscr( );
printf(“enter n value”);
scanf(“%d”,&n);
while(n!=o)
{
rem =n%10;
rev=rev*10+rem;
n=n/10;
}
printf(“%d”,rev);
getch();
}
EXAMPLE:-
N rem=N%10 rev=rev*10+rem N=N/10

456 6 =0*10+6 45
= 0+6
=6

45 5 =6*10+5 4
=60+5
=65

4 4 =65*10+4 0
=650+4
=654

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 93


PALENDROM
PALENDROM:-
If the reverse of a number is equal to given number. Then the given number is called
as palendrom.

PROGRAM:-

#include<stdio.h>
#include<conio.h>
void main ( )
{
int n,rem,rev=0;
clrscr( );
printf(“enter n value”);
scanf(“%d”,&n);
while(n!=o)
{
rem =n%10;
rev=rev*10+rem;
n=n/10;
}
if(rev==n)
printf(“given number is a palendrom”);
else
printf(“given number is not a palendrom”);
getch( );
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 94


ARMSTRONG NUMBER
ARMSRTONG NUMBER:-
If the given number is equal to sum of the cubes of the digits of a
given number.

PROGRAM:-

#include<stdio.h>
#include<conio.h>
void main ( )
{
int n,rem,sum=0;
clrscr( );
printf(“enter n value”);
scanf(“%d”,&n);
while(n!=o)
{
rem =n%10;
rev=rev*10+rem;
n=n/10;
}
if(sum==n)
printf(“n is a armstrong number”);
else
printf(“n is not a armstrong number”);
getch( );
}

Program for printing the following formate

*
**
***
****
*****

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 95


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf(“* “);
}
printf(“\n”);
}
getch();
}

Program for printing the following format


1
12
123
1234
12345

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d“,j);
}
printf(“\n”);

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 96


}
getch();
}

Program for printing the following format


1
22
333
4444
55555

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d“,i);
}
printf(“\n”);
}
getch();
}

PROGRAMING WITH ARRAYS

PROGRAM FOR INITIALIZATION OF ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]= {10,20,30,40,50},i;
clrscr();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 97


printf(“enter the elements in an array :”);
for(i=0;i<=4;i++)
printf(“%d”,a[i]);
getch();
}

intput: enter elements in an array :10 20 30 40 50


output: 10 20 30 40 50

PROGRAM FOR INSERTING ELEMENTS INTO AN ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf(“enter 5 elements in an array :”);
for(i=0;i<=4;i++)
scanf(“%d”,&a[i]);
printf(“the 5 elements in an array are :”);
for(i=0;i<=4;i++)
printf(“%d”,a[i]);
getch();
}

intput: enter 5 elements into an array :10 20 30 40 50


output: the 5 elements in an array are :10 20 30 40 50

PROGRAM FOR LINEAR SEARCH


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,x;
clrscr();
printf(“enter 5 elements in an array :”);
for(i=0;i<=4;i++)

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 98


scanf(“%d”,&a[i]);
printf(“enter an element for searching:”);
scanf(“%d”,&x);
for(i=0;i<=4;i++)
{
If(a[i]==x)
{
printf(“the element is found”);
break;
}
Printf(“the element is not found”);
}
getch();
}

PROGRAM FOR BINARY SEARCH


#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i,x,l,u,mid,flag=0;
clrscr();
printf(“enter how many numbers”);
scanf(“%d”,&n);
printf(“enter elements in an array :”);
for(i=0;i<=n;i++)
scanf(“%d”,&a[i]);
printf(“enter an element for searching:”);
scanf(“%d”,&x);
l=0;
u=n-1;
while((l<=u)&&(flag==0))
{
mid=(l+u)/2;
if(x>a[mid])
l=mid+1;
else

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 99


if(x<a[mid])
u=mid-1;
else
if(x==a[mid])
flag=1;
break;
}
if(flag==1)
printf(“the search element is found”);
else
printf(“the search element is not found”);
}

Output:
enter how many numbers 5
enter n elements in an array: 10 20 30 40 50
enter an element for searching:30
the search element is found

PROGRAM FOR SORTING AN ARRAY ELEMENTS


#inclde<stdio.h>
#include<conio.h>
void main()
{
int i,a[5];
clrscr();
printf(“enter 5 numbers”);
for(i=0;i<=4;i++)
scanf(“%d”,&a[i]);
printf(“numbers before sorting”);
for(i=0;i<=4;i++)
scanf(“%d”,a[i]);
sort(a,5);
printf(“\n numbers after sorting \n”);
for(i=0;i<=4;i++)
scanf(“%d”,a[i]);
getch();

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 100


}
void sort(int x[], int n)
{
int i,j,t;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(x[j-1] >+ x[j])
{
t = x[j-1];
x[j-1] >= x[j];
x[j] = t;
}
}

PROGRAM FOR MATRIX MULTIPLICATION


#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,a[10][10],b[10][10],c[10][10],i,j,k;
clrscr();
printf(“enter number of rows”);
scanf(“%d”,&m);
printf(“enter number of columns”);
scanf(“%d”,&n);
printf(“enter elements of matrix a”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements of matrix b”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=0;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 101


for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf(“ the output matrix is:”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 102


ALGORITHM AND FLOW CHART
Algorithm/ Pseudocode:
Algorithm is a step by step representation of the program. Each step can be
called as instruction. An Algorithm is also called as Pseudocode.
Consider the following steps for making tea .
Begin
Step 1: Boil water.
Step 2: Put tea powder in the kettle.
Step 3: Pour boiled water in the kettle.
Step 4: Waite for three minutes.
Step 5: Boil milk.
Step 6: put boiled milk in a cup.
Step 7: Add sugar to the tea.
Step 8: Empty the Kettle in the Cup.
Step 9: Stir the cup with spoon.
End

Flow chart:
Flow chart is a pictorial representation of a program. In this representation
we have to use different types of symbols for both input and output statements.
The symbols are shown below.

Symbol Name Usage

Oval This symbol is used to


represent Start/Sop (or) Begin/End

Rectangle This symbol is used to


represents initializations

Decision box This is helpful to make decisions

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 103


Input/out put box It is used to represents Input/out
.put

Arrow It is used to Connect the Above


Symbols

Connector This will connects the parts of flow


chart

Programming in “C-Language”

Program for Sum of 2 numbers:


Algorithm:
Step 1: Start
Step 2: Initialize a,b,sum.
Step 3:Read a,b.
Step 4: Caliculate sum=a+b.
Step 5: Print sum.
Step 6:Stop.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“The sum is %d”,sum);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 104


Flow chart:
start

Int a,b,sum

Read
a,b

sum=a+b

Print sum

stop

Program for Subtraction of 2 numbers:


Algorithm:
Step 1: Start
Step 2: Initialize a,b,sub.
Step 3:Read a,b.
Step 4: Calculate sub=a-b.
Step 5: Print sub.
Step 6: Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 105


printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
sub=a-b;
printf(“The Result is is %d”,sub);
}

Flow chart:
start

Int a,b,sub

Read
a,b

sub=a-b

Print sub

stop

Program for multiplication of 2 numbers:


Algorithm:
Step 1: Start
Step 2: Initialize a,b,mul.
Step 3:Read a,b.
Step 4: Caliculate mul=a*b.
Step 5: Print mul.
Step 6:Stop.

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

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 106


{
int a,b,sum;
printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
mul=a*b;
printf(“The Result is is %d”,mul);
}

Flow chart:
start

Int a,b,mul

Read
a,b

mul=a*b

Print mul

stop

Program for Division of 2 numbers:


Algorithm:
Step 1: Start
Step 2: Initialize a,b,div.
Step 3:Read a,b.
Step 4: Caliculate div=a/b.
Step 5: Print div.
Step 6:Stop.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 107


Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,div;
printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
div=a/b;
printf(“The Result is is %d”,div);
}

Flow chart:
start

Int a,b,div

Read a,b

div=a/b

Print div

stop

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 108


Program to find remainder:
Algorithm:
Step 1: Start
Step 2: Initialize a,b,rim.
Step 3:Read a,b.
Step 4: Caliculate rim=a%b.
Step 5: Print rim.
Step 6:Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,rim;
printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
rim=a%b;
printf(“The Result is is %d”,rim);
}
Flow chart:
start

Int a,b,rim

Read
a,b

rim=a%b

Print rim

stop

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 109


Example programs for if statement:

Program to find out greatest of 2 numbers:


Algorithm:
Step 1: Start
Step 2: Initialize a,b.
Step 3:Read a,b.
Step 4: check the condition if(a>b).
Step 5: If the condition is true Print “a is big”
Step 6:If the condition is false print “b is big”.
Step 7:Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“Enter Values for a,b”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is big”);
else
printf(“b is big”);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 110


Flow chart:

start

Int a,b

Read
a,b

false
If(a>b)

True Print b big


Print a big

stop

Program to find out greatest of 3 numbers:

Algorithm:
Step 1: Start
Step 2: Initialize a,b,c.
Step 3:Read a,b,c.
Step 4: check if(a>b).
Step 5: If step 4 is true check if(a>c) .
Step 6:If step 5 is true print “a is big”.
Step 7:If step 4 is false print “b is big”.
Step 8:If step 4 is false check(b>c).
Step 9:If step 8 is true print “b is big”.
Step 10:If step 8 is false print “c is big”.
Step 11: Stop.

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 111


Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“Enter Values for a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);

if(a>b)
{
if(a>c)
printf(“a is big”);
else
printf(“c is big”);
}
else
If(b>c)
Printf(“b is big”);
else
Printf(“c is big”);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 112


Flow chart:

start

Int a,b

Read a,b

If(a>b)
false

true
If(b>c)
false
false
If(a>c)
true
Print c is big
true
Print c is big Print b big
Print a big

stop

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 113


Example program for while:

Program for printing Sum of N numbers


Algorithm:
Step 1: Start
Step 2: Initialize N, i =1,sum=0.
Step 3: Read N.
Step 4: check the condition while (i<=N).
Step 5: If Step 4 is true sum=sum+i;
Step 6: i++.
Step 7:Repeat the steps from 4 to 6 until step 4 became false
Step 8: Print sum.
Step 9: Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int N,sum=0,i=1;
printf (“Enter Number of Values to add”);
scanf (“%d”,&N);

while(i<=N)
{
Sum=sum+i;
i++;
}
Printf(“The sum is %d”,&sum);
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 114


Flow chart:
start

Int N,sum=0,i=1

Read N

While(i<=N) false

true
sum=sum+i

i=i+1

Print sum

stop

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 115


Program for printing N natural numbers
Algorithm:
Step 1: Start
Step 2: Initialize N, i =1.
Step 3: Read N.
Step 4: check the condition while (i<=N).
Step 5: If Step 4 is true Print i;
Step 6: increase I value by 1(i++).
Step 7:Repeat the steps from 4 to 6 until step 4 became false
Step 8: Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int N,i=1;
printf (“Enter Number of Values to print”);
scanf (“%d”,&N);
while(i<=N)
{
Printf(“%d”,i)
i++;
}
}

Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 116


Flow chart:
start

Int N,i=1

Read N

While(i<=N) false

true
Print i;

i=i+1

stop

“ It is a useful material to all learners of ‘C’ language “

“All the best”


Prepared by

KAMALAKAR VANKALA
M.C.A.,
Cheepurupalli,
Vizianagaram(Dist).
E-mail: [email protected]
Mobile: +91 9032986572
Kamalakar Vankala MCA., Cheepurupalli -9032986572 Page 117

You might also like