C Language Notes
C Language Notes
NARASANNAPETA
Programming IN C
Unit- I:
Introduction to Algorithms and Programming Languages: Algorithm – Key features of
Algorithms – Some more Algorithms – Flow Charts. Introduction to C: Structure of C Program –
Writing the first C Program – File used in C Program – Compiling and Executing C Programs –
Using Comments – Keywords – Identifiers – Basic Data Types in C – Variables – Constants –
I/O Statements in C- Operators in CProgramming Examples – Type Conversion and Type
Casting
Unit-II:
Decision Control and Looping Statements: Introduction to Decision Control Statements –
Conditional Branching Statements – Iterative Statements – Nested Loops – Break and Continue
Statement – Go to Statement
Unit- III:
Functions: Introduction – using functions – Function declaration/ prototype – Function
definition – function call – return statement – Passing parameters – Scope of variables – Storage
Classes – Recursive function
Unit- IV:
Arrays: Introduction – Declaration of Arrays – Accessing elements of the Array – Storing
Values in Array – Calculating the length of the Array – Operations on Array – one dimensional
array for inter-function communication – Two dimensional Arrays –Operations on Two
Dimensional Arrays, Strings: Introduction String and Character functions
Unit-V:
Pointers: Understanding Computer Memory – Introduction to Pointers – declaring Pointer
Variables – - Passing Arguments to Functions using Pointer – Pointer and Arrays – Passing
Array to Function. Structure, Union, and Enumerated Data Types: Introduction – Nested
Structures – Arrays of Structures – Structures and Functions - Unions – Enumerated Data Types.
Page 1
Chapter-1
Introduction To Alagorithams And Programming Lanaguages
_________________________________________________________
Alagoritham: the typical menaing of an alagorotham is a formally defined procedure for
performing some calculation. If a procedure is formally defined, then it must be implemented
using some formal languages are known as programming languages. The alagoritham gives the
logoc of the program, that is step by step description of how to arrive at a solution.
In general terms, an alagoritham provides a blue print to writing a program to solve a
particular problem. It is considered to be an effective procedure for solving a problem in finate
number of steps.
Key features of algorithms:
An alagoritham has a finite number of steps and some steps may involve decision-making
and repetition. Boardly speaking, an alagoritham exhibits three key features, namely, sequence,
decision, and repetition
Sequence: sequence menas that each step of the alagoritham is in the specified order.
Repetition : repetition, which involves executing one or more steps for a number of times, can
be implemented using constructs such as the while, do-while, for loop. These loops execute one
or more steps until some condition is true.
Q: an alagoritham to print the first 10 natural numbers.
Page 2
Flow Charts
Flow Chart: a flow chart is a graphical symbolic representation of a process. It is basically used
to design and document virtually complex processors to help the viewers to visualize the logic of
the process. So that they can gain a better understanding of the process when designing a flow
chart , each step in the process is depicted by a diffrenet symbol and is associated with a short
decription
Start and end symbols: start and end symbols are also known as the terminal symbols and are
represented as a circules, ovals, or rounded rectangles .terminal symbols are always the first and
the last symbols in a flow chart.
Arrows: arrows depict the flow of control of the program. They illustrate the exact sequence in
which the instructions are executed.
Generic processing step: Generic processing step is also called as an activity is represented
using a rectangle .
Input and output symbols: Input and output symbols are represented using a parallelogram
and are used to get inputs from the users or display the results to them.
A conditional or decision symbol: A conditional or decision symbol is represendted using a
diamond. It is basically used to depict a Yes/No question.
Advantages:
They are very good communication tools to explain the logic of a system to all
concerned.they are help to analyze the problem in a more effective manner.
They are also used for program documentation .they are even more helpful in the case of
complex programs.
They can be used to debug programs that have error(s). they help the programmers to
easily detect ,locate ,and remove mistakes in the program in a systematic manner.
Page 3
Limitations:
Drawing flowcharts is a laborious and a time-consuming activity. Just imagine the effort
required to draw a flw chart of a program having 50,000 statemetns in it!
Many times , the flow chart of a complex program becomes complex asn clumsy.
At a times, a little bit of alteration in the solution may require complete re-drawing of the
flow chart.
1. machine language,
2. assembly language,
3. high level language ( also known as third generation or 3GL)
4. very high level language ( also known as fourth generation or 4GL)
and complers. Each high level language has many compilers, and there is one for each type of
computer
Fourth Generation: Very High Level Language
With each generation , programming languages have become easier to use and more
like natural languages. A typical example of a 4GL is the query language that allows a user to
request information from a database with precisely worded English like sentence. A query
language is used a databse from the user. For example, when working with structured query
language , the programmers jest need to remember a few rules of syntax and logic, but it is
much easier to learn than COBOL or C
Language processor
Language processor is special software it is used to convert the source code(high level)
into computer executable object code(machine language). Which is also known as interpreters
and compiler
Interpreters : interpreter is a special software which is used to convert soure code into object
Compiler: compiler is special software which is used to covert source code into object code at a
Page 5
Introduction to C
the programming languge C was deceloped by in the early 1970s by Dennis
Ritchie at Bell Laboratories to be used by the UNIX operating system. It was named „C‟
because many of its features were derived from an earlier language called „B‟
Like many other modern languages, C is derived from ALGOL (the first language
to use a block structure). Although ALGOL was not accepted widely in the united states but it
was widely used in Europe. ALGOL‟s introduction in the 1960‟s led the way for the
development of structured programming concepts.
Page 6
All functions are divided into two parts- the declaration section and the statement
section. The declaration section precedes the statements and is used to describe the data that will
be used in the function. Note that data declared within a function are known as local declaration
as that data will be visible only within that function. Stated in other terms, the life time of the
data will be only till the function ends. The statement section in a function contain the code that
manipulates the data to perform a specified task.
Page 7
Source code file: The source code file contain the source code of the program. The extension of
any C source code file is „.C‟ this file contains C source code that defines the main function and
may be other functions. The void main() is the starting point of execution when you successfully
compile and run the program. A C program in general may include even other source code files
Header files: when working with large projects, it is often desirable to separate out certain sub
routines from the void main() of the program. There also may be a case that the same subroutine
has to be used in different programs. In the latter case
Standared header files in all our programs that we had been writing till now, we were using
many functions that were not written by us. For example, strcmp() function which compares
Page 8
two strings. We do not know the details of how these functions work. Such functions that are
provided by all C compilers are included in standard header files
Object files: object files are generated by the compiler as a result of processing the source code
file. They contain compact binary code of the function definitions. Linker uses this object file to
produce an executable file (.exe file) by combining the object files together. Object files have a
„.obj‟ extension name
Executable file: The binary executable file is generated by the linker. The linker links the
various object files to produce a binary file that can be directly executed. On windows operating
system the excutable files have „.exe‟ extension.
Page 9
Comment lines: many a time the meaning or the purpose of the program code is not clear to
the reader. Therefore. It is a good programming practice to place some comments in the code to
help the reader understand the code clearly
“ Comments are physically presented logically absented ”
Page 10
Key words: like every computer language, C has a set of resevered words often known as
keywords that can‟t be used as an identifier. All key words are basically a seqauence of
characters that have a fixed meaning. By convention, keywords must be written in lower case
(small) letters. In C language has a 32 keywords.
Identifiers: identifiers, as the name suggests helps us to identify data and other objects in the
program. Identifiers are basically the names given to the program elements such as variables,arrays, and
functions. They may consist of an alphabet,digit,or an underscore _
Local variable: local variables are variables which are declared in its body. These life time and
scope is valid in it‟s block only.
Global variable: global variables are variables which are declared in out side body. These life
time and scope is valid in through out programm.
Page 11
Badic Data Types In C: Data types specify how we enter data into our programs and
what type of data we enter. C language has some predefined set of data types to
handle various kinds of data that we use in our program. These datatypes have different
storage capacities.
Derived data types are like arrays, functions, stuctures and pointers. These are
dicussed in detail later.
Page 12
Integer type
Floating type
Character type
Page 13
void type
void type means no value. This is usually used to specify the type of functions.
Page 14
Constants: constants are identifiers whose value does not change. Variables can change their
value at any time but constants can never change their value. Constants are used to define fixed
values. A constant is an explicit data value specified by the programmer. The compiler knows
1. Arithmetic operators
2. Relation operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Special operators
Page 15
Operator: operator is a special symbol which can perform an opearation on it‟s oprands
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic
arithmetic operators.
Operator Description
Relation operators
The following table shows all relation operators supported by C.
Operator Description
== Check if two operand are equal
> Check if operand on the left is greater than operand on the right
Page 16
Logical operators
C language supports following 3 logical operators. Suppose a=1 and b=0,
|| Logical OR (a || b) is true
Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bitsfrom right to left. Bitwise operators are not applied
to float or double.
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
The bitwise shift operators shifts the bit value. The left operand specifies the value to be
shifted and the right operand specifies the number of positions that the bits in the value
Example :
Page 17
a = 0001000
b= 2
a << b = 0100000
a >> b = 0000010
Assignment Operators
+= adds right operand to the left operand and assign a+=b is same as a=a+b
the result to left
-= subtracts right operand from the left operand and a-=b is same as a=a-b
assign the result to left operand
*= mutiply left operand with the right operand and a*=b is same as a=a*b
assign the result to left operand
/= divides left operand with the right operand and a/=b is same as a=a/b
assign the result to left operand
Conditional operator
Page 18
Special operator
sizeof Returns the size of an variable sizeof(x) return size of the variable x
& Returns the address of an variable &x ; return address of the variable x
Page 19
INSTRUCTIONS
Each line of a C program is a instruction.it is named like that why because instruct the compiler
to do some task. In C every instruction end with semi column (;) c instructions also called
statements.in C Instructions can be categorized into 4 types.
Ex: c=a+b;
a & b is operands
+ is a operator
Input and output instructions: In C language the input and out put instructions are used to
perform each and every work make easily and define good programming.
Input instruction: input instruction is an instruction which is used to given the input to the
program.
Syntax: scanf(“typespecifier”,&variablename);
Ex: scanf(“%d”,&a);
scanf(“%f”,&b);
scanf(“%c”,&c);
Output instruction: output instruction is an instruction which is used to given the output to the
programer.
Syntax: printf(“typespecifier”,variablename);
Ex: printf(“%d”,variablename);
printf(“ the result is %d “,c);
printf(“ welome to c language ” );
Page 20
Sample C-Programs
Q : Write a c-program to print your address ?
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“\n name: …………………..”);
printf(“\n fname: ………………….”);
printf(“\n street:……………………”);
printf(“\n village:………………….”);
printf(“\n state:…………………….”);
}
Page 21
Q : Write a c-program to initialize the values into a variables using input instruction?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n a= %d”,a);
printf(“\n b= %d”,b);
getch();
}
Page 22
Page 23
Implicit type conversion: - implicit type conversion is a type conversion, which works
automatically
Eg:
void main()
{
int a;
clrscr();
a=91;
printf(“\n A= %d”,a);
a=35.62;
printf(“\n A= %d”,a);
a=„b‟;
printf(“\n A= %d”,a);
getch();
}
Explicit type conversion :- explicit type conversion is a type conversion which done by the
user.
Eg:
void main()
{
int a,b;
float c;
clrscr();
printf(“\n enter a value „);
scanf(“%d”,&a);
printf(“\n enter b value „);
scanf(“%d”,&b);
c=(float)(a/b);
printf(“\n the division is %f”,c);
getch();
}
Page 24
scanf(“%d”,&m);
printf(“\n enter physical science marks ”);
scanf(“%d”,&ps);
printf(“\n enter social studies marks ”);
scanf(“%d”,&ss);
tot=t+e+h+m+ps+ss;
printf(“\n the total is %d ”,tot);
avg=tot/6;
printf(“\n the average is %f ”,avg);
getch();
}
Q : Write a c-program to convert lower case letter into UPPER CASE letter ?
#include<stdio.h>
#include<conio.h>
void main()
{
char g;
clrscr();
printf(“\n enter a character (lower case) ”);
scanf(“%s”,&g);
g=g-32;
printf(“\n the character is %c ”,g);
getch();
}
Q : Write a c-program to convert UPPER CASE letter into lower case letter ?
#include<stdio.h>
#include<conio.h>
void main()
{
char g;
clrscr();
printf(“\n enter a character (upper case) ”);
scanf(“%s”,&g);
Page 26
g=g+32;
printf(“\n the character is %c”,g);
getch();
}
Page 27
CONTROL INSTRUCTIONS
Decision Making And Branching : in this control will takes some decisions according to that
decision it take some branch. It is available in C in the following categories.
Simple IF
IF-Else
Nesting
IF-Else-IF
Switch Case
Simple If : If the condition is satisfied then the statement part will get executed otherwise it Not.
Syntax: if(condition)
{
statement part;
}
Eg: if(a>0)
{
printf(“\n positive ”);
}
Program:
Page 28
Page 29
If - Else: If the condition is satisfied then the statement part-1 will get executed otherwise
statement part -2 will get executed.
Syntax: if(condition)
{
statement part-1;
}
else
{
statement part-2;
}
Eg: if(a>0)
{
printf(“\n positive ”);
}
else
{
printf(“\n negative ”);
}
Program:
Q : Write a c-program to find given number is positive or Negative ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
printf(“\n positive ”);
}
else
{
printf(“\n negative ”);
}
getch();
}
Q : Write a c-program to find given number is Even or Odd ?
#Include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
Page 30
scanf(“%d”,&a);
if(a%2= =0)
{
printf(“\n even ”);
}
else
{
pritnf(“\n odd ”);
}
getch();
}
Nesting
The technique of placing one block into another block is known as Nesting.
Nested IF : Placing the one if block into another if block is called as Nested IF.
--------- If the condition-1 and condition-2 is satisfied then the statement part will get
Executed.
Syntax: if(condition-1)
{
if(Condition-2)
{
Statement part-2;
}
}
Eg: if(a>0)
{
if(a%2= =0)
{
printf(“\n positive Even ”);
}
}
Program:
Q : Write a c-program to find given number is Positive Even or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
Page 31
}
}
getch();
}
Q : Write a c-program to find given number is Negative Even or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
if(a%2= =0)
{
printf(“\n negative even”);
}
}
getch();
}
Q : Write a c-program to find given number is positive odd or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2! =0)
{
printf(“\n positive odd ”);
}
}
getch();
}
Q : Write a c-program to find given number is Negative odd or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
Page 32
scanf(“%d”,&a);
if(a<0)
{
if(a%2! =0)
{
printf(“\n negative odd ”);
}
}
getch();
}
Nested IF-Else: Placing the one if-else block into another if-else block is called as Nested IF-
else.
Syntax: if(condition-1)
{
if(condition-2)
{
Statement part 1;
}
else
{
Statement part 2;
}
}
else
{
if(condition-3)
{
statement part-1;
}
else
{
statement part-2;
}
}
Eg: if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
else
{
printf(“\n positive odd”);
}
}
else
{
Page 33
if(a%2= =0)
{
printf(“\n negative even”);
}
else
{
printf(“\n negative odd”);
}
}
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
else
{
printf(“\n positive odd”);
}
}
else
{
if(a%2= =0)
{
printf(“\n negative even”);
}
else
{
printf(“\n negative odd”);
}
}
getch();
}
IF –Else- If :
Syntax :
if(condition-1)
{
Page 34
statement part;
}
else if(condition-2)
{
statement part;
}
else if(condition-3)
{
statement part;
}
else
{
statement part;
}
Program :
Q : Write a c-program to find given Student mark list with Grade ?
#include<stdio.h>
#include<conio.h>
void main()
{
int t,e,h,m,ps,ss,tot;
flaot avg;
clrscr();
printf(“\n enter telugu marks ”);
scanf(“%d”,&t);
printf(“\n enter english marks ”);
scanf(“%d”,&e);
printf(“\n enter maths marks ”);
scanf(“%d”,&m);
printf(“\n enter physical science marks ”);
scanf(“%d”,&ps);
printf(“\n enter social studies marks ”);
scanf(“%d”,&ss);
tot=t+e+h+m+ps+ss;
printf(“\n the total is %d ”,tot);
avg=tot/6;
printf(“\n the average is %f ”,avg);
if((t>=35)&&(e>=35)&&(h>=35)&&(m>=35)&&(ps>=35)&&(ss=35&&))
{
printf(“\n the result is pass “);
if(avg>=80)
{
printf(“\n grade a ”);
}
else if(avg>=70)
{
printf(“\n grade b”);
}
Page 35
else if(avg>=55)
{
printf(“\n grade c”);
}
else if(avg>=40)
{
printf(“\n grade d”);
}
else
{
printf(“\n grade e ”);
}
}
else
{
printf(“\n No Grade ”);
}
getch();
}
Switch Case :
Syntax:
switch(variable name)
{
case 1 :statement part 1;
break;
case 2 :statement part 2;
break;
case 3 :statement part 3;
break;
case 4 :statement part 4;
break;
default:
statement part n;
}
Eg:
Switch(a)
{
case 1 :printf(“\n HAI ”);
Break;
case 2 :printf(“\n HELLO ”);
break;
case 3 :printf(“\n BYE ”);
break;
default:
printf(“\n welcome ”);
}
Page 36
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
switch(a)
{
case 1 :printf(“\n hai ”);
break;
case 2 :printf(“\n hello ”);
break;
case 3 :printf(“\n bye ”);
break;
default:
printf(“\n welcome ”);
}
getch();
}
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr();
printf(“\n enter a character ”);
scanf(“%s”,&a);
switch(a)
{
case „a‟: printf(“\n hai ”);
break;
case „s‟: printf(“\n hello ”);
break;
case „d‟: printf(“\n bye ”);
break;
default:
printf(“\n welcome ”);
}
getch();
}
Program :
#include<stdio.h>
Page 37
#include<conio.h>
void main()
{
int a,b,c;
char ch;
clrscr();
printf(“\n ener a value „);
scanf(“%d”,&a);
printf(“\n ener b value „);
scanf(“%d”,&b);
printf(“\n ener your choice „);
scanf(“%s”,&ch);
switch(ch)
{
case „+‟: c=a+b;
break;
case „-‟: c=a-b;
break;
case „*‟: c=a*b;
break;
case „/‟: c=a/b;
break;
case „%‟: c=a%b;
break;
default: printf(“\n invlaid choice ”);
}
printf(“\n the result is %d ”,c);
getch();
}
Break And Continue Statement
t is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking
the test expression.
Break Statement
The break statement terminates the loop (for, while and do...while loop) immediately when it is
encountered. The break statement is used with decision making statement such as if...else.
break;
Page 38
Continue Statement:
The continue statement skips some statements inside the loop. The continue statement
is used with decision making statement such as if...else.
Syntax of continue Statement
continue;
Page 39
GOTO Statement : jump statement is a statement which is used to jump the control from one
place to another place of a program with out observing any condition
C supports a special jump statement named “goto” goto can be used into 2 ways
those are
1. forward goto
2. backward goto
forward goto :
Syntax: statement part -1;
statement part -2;
statement part -3;
goto label;
Page 40
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
Clrscr();
printf(“\n HAI ”);
printf(“\n HELLO ”);
printf(“\n BYE ”);
printf(“\n NARASANNAPETA ”);
printf(“\n SRIKAKULAM ”);
printf(“\n VIZAG ”);
goto ramu;
printf(“\n JAMMU ”);
printf(“\n POLAKI ”);
printf(“\n NPETA ”);
printf(“\n SARAVAKOTA ”);
ramu:
printf(“\n CHENNAI ”);
printf(“\n BANGLOORE ”);
printf(“\n MUMBAI ”);
printf(“\n DELHI ”);
printf(“\n RAJASTAN ”);
getch();
}
Backward goto :
Syntax: statement part -1;
statement part -2;
statement part -3;
label:
statement part -4;
statement part -5;
statement part -6;
goto label;
statement part -7;
statement part -8;
statement part -9;
Page 41
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
Clrscr();
printf(“\n HAI ”);
printf(“\n HELLO ”);
printf(“\n BYE ”);
printf(“\n NARASANNAPETA ”);
printf(“\n SRIKAKULAM ”);
printf(“\n VIZAG ”);
ramu;
printf(“\n JAMMU ”);
printf(“\n POLAKI ”);
printf(“\n NPETA ”);
printf(“\n SARAVAKOTA ”);
goto ramu:
printf(“\n CHENNAI ”);
printf(“\n BANGLOORE ”);
printf(“\n MUMBAI ”);
printf(“\n DELHI ”);
printf(“\n RAJASTAN ”);
getch();
}
Page 42
LOOPING
Decision Making And Looping : As Long as the condition is satisfied the statement part will
get executed this technique is known as “Looping” in C there are three statements to support the
looping those are
1. while
2. do-while
3. for
While Loop: while loop is a loop it is an entry control loop why because it checks the condition
at checks the condition is first and allows the control into the block next.
Syntax:
initialization;
while(condition)
{
statement part ;
increment/decrement ;
}
Eg:
i=1;
while(i<=10)
{
printf(“\n gnanajyothi ”);
i=i+1;
}
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
printf(“\n %d ”,i);
i++;
}
getch();
}
Page 43
Do-While Loop: Do-while loop is a loop it is an exit control loop. If the condition is satisfied or
not once statement part will be executed after that it checks the condition.
Syntax:
initialization;
do
{
Statement part ;
Increment/decrement ;
} while(condition);
Page 44
Eg:
i=1;
do
{
printf(“\n gnanajyothi ”);
i=i+1;
} while(i<=10);
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
do
{
printf(“\n %d “,i);
i++;
} while(i<=n);
getch();
}
Q : Write a c program to print N below Even numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=2;
do
{
printf(“\n %d “,i);
i=i+2;
} while(i<=n);
getch();
}
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
do
{
printf(“\n %d “,i);
i=i+2;
}while(i<=n);
getch();
}
For Loop : It is an also entry control loop. As long as the condition is satisfied the statement part
will get executed.
Syntax:
for (initialization; condition; increment/decrement)
{
Statement part ;
}
Eg:
for(i=0i<=10;i++)
{
Printf(“\n Gnanajyothi ”);
}
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
printf(“\n %d “,i);
}
getch();
}
Q : Write a c program to print N below Even numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
Page 46
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i=i+1)
{
s=s+i;
printf(“\n the sum of %d below natural numbers is %d”,n,s);
}
getch();
}
if(k= =s)
{
printf(“\n given number is palindrome ”);
}
else
{
printf(“\n given number is not palindrome ”);
}
getch();
}
Q : Write a c program to find given number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,k;
clrscr();
printf(“\n enter n value ”);
scanf(“%d”,&n);
k=
while(n!=0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(k= =s)
{
printf(“\n given number is armstrong ”);
}
else
{
printf(“\n given number is not armstrong ”);
}
getch();
}
Q : Write a c program to a number and then calculate the sum of its digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t,s=0;
clrscr();
printf(“\n Enter a Number: ”);
scanf(“%d”,&n);
while(n!=0)
{
t=n%10;
Page 50
s=s+t;
n=n/10;
}
printf(“\n the sum of the digit = %d ”,s);
getch();
}
Q : Write a c program to find multification table of given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,r;
clrscr();
printf(“\n enter n value :”);
scanf(“%d”,&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf(“\n %d * %d =%d ”,n,i,r);
getch();
}
Page 51
*****
*****
*****
*****
*****
11111
22222
33333
44444
55555
12345
12345
12345
12345
12345
{
for(j=1;j<=i;j++)
{
printf(“ * ”);
}
printf(“\n”);
}
getch();
}
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf(„\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d ”,i);
}
printf(“\n”);
}
getch();
}
1
22
333
4444
Page 54
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf(„\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d ”,j);
}
printf(“\n”);
}
getch();
}
1
12
123
1234
12345
Page 55
ARRAYS
Array is collection of related data type items to share a common name. simply an array is
also a variable.
In array the memory is allocated to the data items in a sequential manner i.e linear
fashion.
Syntax.
datatype variable name[size];
Ex:
int a[5];
float b[5];
char c[5];
Initialization:
Syntax:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4]={11,88,99,44};
clrscr();
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}
Page 56
Program-2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[ ]={11,88,99,44};
clrscr();
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}
Program-3:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4];
clrscr();
a[0]=45;
a[1]=86;
a[2]=26;
a[3]=63;
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4];
clrscr();
printf(“\n enter a[0] ”);
scanf(“%d”,&a[0]);
printf(“\n enter a[1] ”);
Page 57
scanf(“%d”,&a[1]);
printf(“\n enter a[2] ”);
scanf(“%d”,&a[2]);
printf(“\n enter a[3] ”);
scanf(“%d”,&a[3]);
printf(“\n the values are”);
printf(“\n a[0]= %d ”,a[0]);
printf(“\n a[1]= %d ”,a[1]);
printf(“\n a[2]= %d ”,a[2]);
printf(“\n a[3]= %d ”,a[3]);
getch();
}
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
printf(“\n the values are ”);
for(i=0;i<10;i++)
{
printf(“\n A[%d]= %d ”,i,a[i]);
}
getch();
}
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
printf(“\n the values are ”);
for(i=0;i<n;i++)
{
printf(“\n A[%d]= %d ”,i,a[i]);
}
getch();
}
Write a C Program to find the sum of elements of an array.
#Include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,s=0;
clrscr();
printf(„\n enter the size of array ”);
scanf(“%d”,&n);
printf(„\n enter the elements of array ”);
for(i=0;i<n;i++)
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
printf(“\n the values are ”);
for(i=0;i<n;i++)
{
printf(“\n A[%d]= %d ”,i,a[i]);
}
Page 59
Two dimensional array is an array in which elements are identified with the help of
double subscript so it is also called as double subscripted variable or two dimensional array
Syntax.
datatype variable name[row size][coloumn size];
Ex:
int a[5][3];
float b[5][5];
char c[5][4];
Initialization:
Syntax:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2]={{11,88},{99,44}};
clrscr();
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}
Program-2:
#Include<stdio.h>
#include<conio.h>
void main()
{
int a[ ]={11,88,99,44};
clrscr();
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}
Program-3:
Page 60
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
clrscr();
a[0][0]=45;
a[0][1]=86;
a[1][0]=26;
a[1][1]=63;
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
clrscr();
printf(“\n enter a[0][0] ”);
scanf(“%d”,&a[0][0]);
printf(“\n enter a[0][1] ”);
scanf(“%d”,&a[0][1]);
printf(“\n enter a[1][0] ”);
scanf(“%d”,&a[1][0]);
printf(“\n enter a[1][1] ”);
scanf(“%d”,&a[1][1]);
printf(“\n the values are”);
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}
Page 61
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the values are ”);
for(i=0;i<2;i++)
{
for(j=0;j<2j++)
{
printf(“\n enter a[%d][%d] = %d ”,i,j,a[i]j]);
}
}
getch();
}
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(„\n enter the row size of array ”);
scanf(“%d”,&r);
printf(„\n enter the column size of array ”);
scanf(“%d”,&c);
printf(„\n enter the elements of array ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
Page 62
scanf(“%d”,&a[i][j]);
}
}
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(„\n enter the row size of matrix ”);
scanf(“%d”,&r);
printf(„\n enter the column size of matrix ”);
scanf(“%d”,&c);
printf(„\n enter the elements of matrix ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is \n ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],i,j,r,c;
clrscr();
Page 64
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(„\n enter the row size of matrix ”);
scanf(“%d”,&r);
printf(„\n enter the column size of matrix ”);
scanf(“%d”,&c);
printf(„\n enter the elements of matrix ”);
for(i=0;i<r;i++)
Page 65
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is \n ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the transpose of the matrix is \n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[j]i]);
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],i,j,r1,c1,r2,c2;
clrscr();
printf(„\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(„\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(„\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(„\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(„\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
Page 66
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(„\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],c[10][10]i,j,r1,c1,r2,c2;
clrscr();
printf(„\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(„\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(„\n enter the row size of second matrix ”);
Page 67
scanf(“%d”,&r2);
printf(„\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(„\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(„\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
if((r1= =r2)&&(c1= =c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
Page 68
}
printf(“\n the sum of two matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,c[i]j]);
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],c[10][10]i,j,r1,c1,r2,c2;
clrscr();
printf(„\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(„\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(„\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(„\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(„\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(„\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
Page 69
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
if((r1= =r2)&&(c1= =c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],c[10][10]i,j,k,r1,c1,r2,c2;
clrscr();
Page 70
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“\n the multiplication of two matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,c[i]j]);
}
printf(“\n”);
}
}
else
{
printf(“\n the multiplication of two matrix is not possible ”);
}
getch();
}
Page 72
STRINGS
A string is nothing but a character array it is also called as string variable. In C every
string ends with „\0‟ character (null terminator). Simply every string is null terminated. In C the
strings are checked.
Declaration of strings.
Initialization of strings .
Program-1
#include<stdio.h>
#include<conio.h>
void main()
{
{
char a[6]={„s‟,„u‟,„r‟,„y‟,„a‟,„\0‟};
char b[ ]={„r‟,„a‟,„m‟,„u‟,„\0‟};
char c[6]=“gmr”;
char d[4];
d[0]= „s‟;
d[1]= „a‟;
d[2]= „i‟;
d[3]= „\0‟;
printf(“\n A= %s ”,a);
printf(“\n B= %s ”,b);
printf(“\n C= %s ”,c);
printf(“\n D= %s ”,d);
getch();
}
Page 73
Program-2
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n the string is %s ”,a);
getch();
}
The process of manipulating string is called string simulations. The users have a right to
manipulate the strings with the help of some predefined functions in standard library. Those are
Finding the length of string: we can find the length of a string using a predefined function
called “strlen()” this function takes one argument nothing but sting argument and it returns an
integer value which is length of a given string.
Syntax. l=strlen(“string”);
Eg. x=strlen(“sai”);
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
int x;
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
x=strlen(a);
printf(“\n the length of string is %d ”,x);
getch();
}
Copying one string into another string: copying one string into another string we can using
with the help of predefined function “strcpy()” which is in string.h header file.
Page 74
The above function takes two arguments both are string arguments. The first one is must be a
string variable it copies the second string into the first string.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],b[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
strcpy(b,a);
printf(“\n the copying string is %s ”,b);
getch();
}
Concatenating the two strings: we can concatenate two strings with the help of a predefined
function “strcat()” which is in string.h header file.
The above function takes two arguments both are strings in that first argument must be a variable
it concatenates the given two strings and place the result in first string .
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],b[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n enter b string ” );
scanf(“%s”,&b);
strcat(a,b);
printf(“\n the concatenated string is %s ”,a);
getch();
}
Finding the reverse of string : we can reverse a string using a predefined function “strrev()”
which is in header file string.h
Syntax. strrev(“string”);
Page 75
Eg. strrev(a);
The above two strings takes one argument nothing but string variable. It reverse the given sting
and replace into it.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
strrev(a);
printf(“\n the reverse string is %s ”,a);
getch();
}
Comparing two strings: We can comparing two strings with the help of a predefined function
“strcmp()” which is in string.h header file
Syntax. strcmp(“string1”,“strcmp”);
Eg. x=strcmp(a,b);
The above function takes two arguments both are strings and returns integer value.
If it is return zero then the given string are equal.
If it is return a positive value then first string is > second string
If it is return a negative value then first string is < second string
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],b[10];
int x;
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n enter b string ” );
scanf(“%s”,&b);
x=strcmp(a,b);
if(x>0)
{
printf(“\n %s is greater than %s ”,a,b);
}
else if(x<0)
{
printf(“\n %s is less than %s ”,a,b);
Page 76
}
else
{
printf(“\n %s and %s are equal ”,a,b);
}
getch();
}
Page 77
FUNCTIONS
Function is a self contained block of a code which is used to perform a manageable task.
Every C program is a collection of functions each and every C program at least contains one
function it is nothing but main( )
With the help of functions we can reduce the execution time of a program and
also we can increase the efficiency
Components of a function:
Function declaration
Function body
Function definition
Function calling
Arguments(formal and actual)
Return statement
Function calling statement: A statement which is used to call or invoke another function is
called function calling statement.
Eg. clrscr();
Caller function: A function which is calls another function is called “ caller function ”
Calee function: A function which is called by another function is called “ calle function ”
Types of Function:
Page 78
1. predefined functions
2. user defined functions
Header files
|
Standard files
|
Predefined functions
Eg. getch()
clrscr()
isalnum()
isalpha()
User defined functions: A function whose meaning is defined by user is called user defined
functions.
Eg. raju()
anil()
sum()
Page 79
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; actual arguments
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b); caller function
c=sum(a,b); function calling statement
printf(“\n The sum is %d ”,c);
getch(); formal arguments function definition
} calee function
Return statement
Page 80
Program – 1:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sum(a,b);
printf(“\n The sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}
Program – 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sub(a,b);
printf(“\n The subtraction is %d ”,c);
getch();
}
int sub(int x,int y)
{
int z;
z=x-y;
return z;
}
Program – 3:
#include<stdio.h>
#include<conio.h>
void main()
Page 81
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=mul(a,b);
printf(“\n The multification is %d ”,c);
getch();
}
int mul(int x,int y)
{
int z;
z=x*y;
return z;
}
Program – 4:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=div(a,b);
printf(“\n The reminder is %d ”,c);
getch();
}
int div(int x,int y)
{
int z;
z=x%y;
return z;
}
Page 82
Categories of a function:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sum(a,b);
printf(“\n The sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
sum(a,b);
printf(“\n The sum is %d ”,c);
Page 83
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
printf(“\n the sum is %d ”,z);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int c;
clrscr();
c=sum( );
printf(“\n the sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
printf(“\n enter x value “);
scanf(“%d”,&x);
printf(“\n enter y value “);
scanf(“%d”,&y);
z=x+y;
return z;
}
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
sum( );
getch();
}
int sum(int x,int y)
{
int z;
printf(“\n enter x value “);
scanf(“%d”,&x);
printf(“\n enter y value “);
Page 84
scanf(“%d”,&y);
z=x+y;
printf(“\n the sum is %d ”,z);
}
Q :Write a c program to find factorial of given number using user defined functions .
#include<stdio.h>
#include<conio.h>
void main()
{
int f ,n;
clarscr();
printf(“\n Enter N value ”);
scanf(%d”,&n);
f=fact(n);
printf(“\n the factorial is %d ”,f);
getch();
}
int fact (int x)
{
int z,i;
for(i=1;i<=x;i++)
{
z=z*i;
}
return z;
}
RECURSION
The technique of calling one function into in its body is known as “ recursion ” in
other words a function calls itself known as “ recursion ”
In below example factorial function is called in its body so it is the best example
for recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int f ,n;
clarscr();
printf(“\n Enter N value ”);
scanf(%d”,&n);
f=fact(n);
printf(“\n the factorial is %d ”,f);
getch();
}
int fact (int x)
Page 85
{
if(x= =0)
return 0;
else
return x*fact(x-1);
}
STORAGE CLASSES
Variables in C not only a data type but also a storage class storage class provides
information about variables, location and visibility ….. the storage class decides the portion of
the program with in the which the variables are recognized.
Automatic storage class: the feature of a variable defined in automatic storage class as.
Keyword: auto
Storage: main memory
Default value: An unpredictable value(garbage value)
Scope: local to the block in which it is defined.
Life time: Till the control remains with in the block in which the variable is defined.
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a;
clrscr();
printf(“\n A=%d ”,a);
getch();
}
Register storage class: the feature of a variable defined is register storage class as
Page 86
Keyword: register
Storage: CPU registers
Default value: An unpredictable value (garbage value)
Scope: local to the block in which it is defined.
Life time: Till the control remains with in the block in which the variable is defined.
A variable stores in a CPU registers can always be accessed faster than the one i.e
stores main memory a variable which is used at many places in a program
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
register int a;
clrscr();
for(a=0;a<=100;a++)
printf(“\n A=%d ”,a);
getch();
}
Keyword: static
Storage: main memory
Default value: zero
Scope: local to the block in which it is defined.
Life time: value of the variable presents between different function calls
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
static int a;
clrscr();
printf(“\n A=%d ”,a);
getch();
}
Keyword: extern
Storage: main memory
Default value: zero
Scope: global
Page 87
Structure syntax:
struct student
{
int sno;
int sage;
char sname[10];
};
Declaring a structure variable:
Ex:
struct student
{
int sno;
char sname[10];
}x,y,z;
(Or)
struct student x,y,z;
Initializing of structure:
Method -1:
Program:
#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
}x={9,45.6,„m‟};
void main()
{
clrscr();
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}
Method -2:
Program:
#include<stdio.h>
#include<conio.h>
Page 89
struct Test
{
int a;
flaot b;
char c;
};
void main()
{
struct Test x={9,45.6,„m‟}
clrscr();
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}
Method -3:
Program:
#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
}x;
void main()
{
clrscr();
x.a=10;
x.b=45.6;
x.c= „s‟;
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}
Method -4:
Program:
#include<stdio.h>
#include<conio.h>
struct Test
{
Page 90
int a;
flaot b;
char c;
};
void main()
{
struct Test x;
clrscr();
x.a=10;
x.b=45.6;
x.c= „s‟;
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}
Method -5:
Program:
#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
};
void main()
{
struct Test x;
clrscr();
printf(“\n enter x.a value”);
scanf(“%d”,&x.a);
printf(“\n enter x.b value”);
scanf(“%f”,&x.b);
printf(“\n enter x.c value”);
scanf(“%c”,&x.c);
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}
Page 91
Program:
#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
flaot sage;
char sname[10];
};
void main()
{
struct Student x;
clrscr();
printf(“\n enter student number”);
scanf(“%d”,&x.sno);
printf(“\n enter student age”);
scanf(“%f”,&x.sage);
printf(“\n enter student name”);
scanf(“%s”,&x.sname);
printf(“\n the student number is %d ”,x.sno);
printf(“\n the student ahe is %f ”,x.sage);
printf(“\n the student name is %s ”,x.sname);
getch();
}
Page 92
Nested Structure:
The technique of placing one structure variable as a member of anther structure is
called “nested structure” or “structure with in structure”
Program:
#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
flaot sage;
char sname[10];
struct Marks
{
int m;
int p;
int c;
}y;
}x;
void main()
{
clrscr();
printf(“\n enter student number”);
scanf(“%d”,&x.sno);
printf(“\n enter student age”);
scanf(“%f”,&x.sage);
printf(“\n enter student name”);
scanf(“%s”,&x.sname);
printf(“\n enter Maths marks”);
scanf(“%d”,&x.y.m);
printf(“\n enter Physics marks”);
scanf(“%d”,&x.y.p);
printf(“\n enter Computers marks”);
scanf(“%d”,&x.y.c);
printf(“\n the student number is %d ”,x.sno);
printf(“\n the student ahe is %f ”,x.sage);
printf(“\n the student name is %s ”,x.sname);
printf(“\n the Maths marks is %d ”,x.y.m);
printf(“\n the Physics marks is %d ”,x.y.p);
printf(“\n the Computers marks is %d ”,x.y.c);
getch();
}
Page 93
Unions in C Language
Unions are conceptually similar to structures. The syntax of union is also similar to
that of structure. The only differences is in terms of storage. In structure each member
has its own storage location, whereas all members of union uses a single shared
memory location which is equal to the size of its largest data member.
#include<stdio.h>
#include<conio.h>
union Student
{
int sno;
flaot sage;
char sname[10];
};
void main()
{
union Student x;
clrscr();
printf(“\n enter student number”);
scanf(“%d”,&x.sno);
printf(“\n enter student age”);
scanf(“%f”,&x.sage);
printf(“\n enter student name”);
scanf(“%s”,&x.sname);
printf(“\n the student number is %d ”,x.sno);
Page 94
POINTERS
Pointer is a dynamic feature of “C” language it is a powerful tool to write a good
program there is a no of reasons to using a pointers .
Pointer is a variable which is used to store the address of a memory location i.e. a pointer is a
variable which can holds the address of another variable
Declaring pointers:
Ex 1. int *p;
In above example p is a name of the pointer variable and it is always points to any
other integer variable
Ex 2. float *p;
In above example p is a name of the pointer variable and it is always points to any
other float variable
Page 95
Ex. p=&a;
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
p=&a;
printf(“\n A is stored at %d ”,p);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,*p,*q*,r;
clrscr();
p=&a;
q=&b;
r=&c;
printf(“\n enter a value ”);
scanf(“%d”,p);
printf(“\n enter b value ”);
scanf(“%d”,q);
*r=*p+*q;
printf(“\n the result is %d ”,c);
getch();
}
Pointer Vs Functions
The communication between caller function and calee function is possible through
passing values using arguments and return statements.
This can be achieved in two ways those are.
Call by value
Call by address
Call By Value: In this is technique the value of the actual arguments are passed to formal
arguments. If any changes occur to the formal arguments doesn‟t effects the actual arguments.
Page 96
Why because here only the value of the actual arguments are passed from caller function to calle
function.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n before swapping a= %d b=%d ”,a,b);
swap(a,b);
printf(“\n after swapping a= %d b=%d ”,a,b);
getch();
}
swap(int x, int y)
{
int z;
z=x;
x=y;
y=z;
}
Call By Adress: In this is technique the value of the actual arguments are passed to formal
arguments. If any changes occur to the formal arguments will effects the actual arguments. Why
because here the address of the actual arguments are passed from caller function to calle
function.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n before swapping a= %d b=%d ”,a,b);
swap(&a,&b);
printf(“\n after swapping a= %d b=%d ”,a,b);
getch();
}
Page 97
Many real life problems involving large volumes of data and in each situation the console
oriented programming post the major problems
1. it becomes time consuming to handle large volumes of data
2. the entire data is lost when either the program is terminated or computer is turned off
File: A file is place on the disk where a group of related data items are stored
File management: we can perform the following operators on file using C language
1. Naming a file
2. Opening a file
3. IO operations on file
4. Closing a file
Naming a file: to name a file there some specific rules those are depend upon the OS the rules
for naming a file in DOS
Primary name
Secondary name
1. Primary name should not exceed 8 characters extension name should not exceed 3 characters
and they are separated by dot(.)
2.white spaces are not allowed
3.special characters are not allowed
Opening a File: To open a new file or open an existing file we have fopen( ) function in stdio.h
header file
f = fopen(“rama.txt”,“r”);
Page 98
closing a File: a file must be closed as soon as all operations on it have been completed it
prevents the accidental misuse of the file to close any file we use fclose( ) function which is in
stdio.h header file
p = fclose(“rama.txt”,“r”);
C-pre processor: the c-preprocessor is a collection of special statements called directories that
are executed at the beginning of the compilation process #define ,#if , #else ,#line these are all
the pre-processor directive the preprocessor directives are usually appear at the beginning of a
program.
Program:
#define ramu 10
void main()
{
int i,s=0;
clrscr();
for(i=0;i<=ramu;i++)
{
s=s+i;
}
printf(“\n the sum is %d ”,s);
getch();
}
Page 99