C Language Notes PDF
C Language Notes PDF
C Language Notes PDF
NARASANNAPETA-SRIKAKULAM
C – Programming Language
Unit-I
Chapter – 2 Introduction to C
Unit – II
Chapter 4. Functions
Unit – III
Chapter 5. Arrays
Chapter 6. Strings
Unit – IV
Unit – V
Chapter9. Files
Reference Book: 1. Ashok N Kamthane: Programming with ANSI and Turbo C, Pearson Edition
Publ, 2002 2. Henry Mullish & Huubert L. Cooper: The Sprit of C, Jaico Pub. House, 1996
Page 1
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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.
Decision: Decision statements are used when the outcome of the process depends on some
condition.
Q: an alagoritham to test the equality of two numbers
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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)
Page 4
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Introduction to C
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Files
used in
a C
program
Every C pogram has four kinds of files associated with it
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 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 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
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.
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Page 10
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 11
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 12
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 13
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Integer type
Floating type
Character type
Page 14
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
void type
void type means no value. This is usually used to specify the type of functions.
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
Page 15
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
values. A constant is an explicit data value specified by the programmer. The compiler knows the
1. Arithmetic operators
2. Relation operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Special operators
Page 16
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Logical operators
C language supports following 3 logical operators. Suppose a=1 and b=0,
Page 17
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
|| 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 :
a = 0001000
b= 2
Page 18
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Special operator
variable x
variable
Page 20
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 21
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Sample C-Programs
Page 22
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Q : Write a c-program to initialize the values into a variables using input instruction?
#include<stdio.h>
#include<conio.h>
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();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=a-b;
printf(“\n the subtraction is %d”,c);
getch();
}
Q : Write a c-program to print Division of two numbers ?
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%f”,&a);
printf(“\n enter b value ‘);
scanf(“%f”,&b);
c=a/b;
printf(“\n the division is %f”,c);
getch();
}
Q : Write a c-program to print Reminder of two numbers ?
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=a%b;
printf(“\n the reminder is %d”,c);
getch();
}
Page 24
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Type Conversion
Implicit type conversion :- implicit type conversion is a type conversion which works
automatically
Eg:
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:
main()
{
int a,b;
flat 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();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
main()
{
int p;
float t,r,i;
clrscr();
printf(“ \n enter principle amount value ”);
scanf(“ %d ”,&p);
printf(“ \n enter time value ”);
scanf(“ %f ”,&t);
printf(“\n enter rate value ”);
scanf(“ %f ”,&r);
i=(p*t*r)/100;
printf(“\n the interest is %f ”,c);
getch();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
main()
{
char g;
clrscr();
printf(“\n enter a character (lower case) ”);
scanf(“%c”,&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>
main()
{
char g;
clrscr();
printf(“\n enter a character (upper case) ”);
scanf(“%c”,&g);
g=g+32;
printf(“\n the character is %c”,g);
Page 27
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
getch();
}
Page 28
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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:
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
printf(“\n negative ”);
}
getch();
}
Page 30
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a%2= =0)
{
Page 31
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
}
getch();
Page 32
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
}
Q : Write a c-program to find given number is Negative Even or Not ?
#include<stdio.h>
#include<conio.h>
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>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =1)
{
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>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
Page 33
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
if(a%2= =1)
{
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
{
if(a%2= =0)
{
printf(“\n negative even”);
Page 34
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
}
else
{
printf(“\n negative odd”);
}
}
Program:
#include<stdio.h>
#include<conio.h>
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)
{
statement part;
}
else if(condition-2)
Page 35
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
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>
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”);
}
else if(avg>=55)
{
printf(“\n grade c”);
Page 36
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
}
else if(avg>=40)
{
printf(“\n grade d”);
}
else
{
printf(“\n grade e ”);
}
}
else
{
printf(“\n no grade e ”);
}
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 ”);
}
Program :
#include<stdio.h>
#include<conio.h>
Page 37
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
#include<conio.h>
main()
{
int a,b,c;
Page 38
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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();
}
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;
statement part -4;
statement part -5;
statement part -6;
label;
statement part -7;
statement part -8;
statement part -9;
Program :
#include<stdio.h>
Page 39
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<conio.h>
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;
Program :
#include<stdio.h>
#include<conio.h>
main()
{
Clrscr();
printf(“\n HAI ”);
printf(“\n HELLO ”);
printf(“\n BYE ”);
printf(“\n NARASANNAPETA ”);
printf(“\n SRIKAKULAM ”);
printf(“\n VIZAG ”);
Page 40
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 41
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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 42
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 43
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
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();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
main()
{
int n,i;
clrscr();
Page 45
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i=i+2)
{
s=s+i;
printf(“\n the sum of %d below natural numbers is %d”,n,s);
}
getch();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
main()
{
int n,t,s=0;
clrscr();
printf(“\n Enter a Number: ”);
scanf(“%d”,&n);
while(n!=0)
{
t=n%10;
Page 49
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
s=s+t;
s=n/10;
}
printf(“\n the sum of the digit = %d ”,s);
getch();
}
Page 50
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
*****
*****
*****
*****
*****
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
11111
22222
33333
44444
55555
12345
12345
12345
12345
12345
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
for(j=1;j<=i;j++)
{
printf(“ * ”);
}
printf(“\n”);
}
getch();
}
*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
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(“ * ”);
}
printf(“\n”);
}
getch();
}
1
22
333
4444
Page 53
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
55555
#include<stdio.h>
#include<conio.h>
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(“ * ”);
}
printf(“\n”);
}
getch();
}
1
12
123
1234
12345
Page 54
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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.
An array in which elements are identified with the help of single subscript is called one
dimensional array or single subscripted variable.
Syntax.
datatype variable name[size];
Ex:
int a[5];
float b[5];
char c[5];
Initialization:
Syntax:
#include<stdio.h>
#include<conio.h>
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 55
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Program-2:
#include<stdio.h>
#include<conio.h>
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>
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>
main()
{
int a[4];
clrscr();
printf(“\n enter a[0] ”);
scanf(“%d”,&a[0]);
printf(“\n enter a[1] ”);
Page 56
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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 enter a[%d]= %d ”,i,a[i]);
}
getch();
}
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
printf(“\n the values are ”);
for(i=0;i<n;i++)
{
printf(“\n enter 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>
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 enter a[%d]= %d ”,i,a[i]);
}
Page 58
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
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 59
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
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>
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 60
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Program-1
#include<stdio.h>
#include<conio.h>
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>
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);
scanf(“%d”,&a[i][j]);
}
Page 61
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
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>
main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(‘\n enter the row size of matrix ”);
scanf(“%d”,&r);
Page 63
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
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++)
Page 64
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
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]);
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
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++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
Page 65
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
}
}
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>
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);
Page 66
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
for(j=0;j<c1;j++)
{
printf(“%d ”,c[i]j]);
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
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 ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
Page 68
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
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>
main()
{
int a[10][10], b[10][10],c[10][10]i,j,k,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 ”);
Page 69
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 ”);
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(c1= =r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
Page 70
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 71
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
main()
{
{
char a[6]={‘s’,‘u’,‘r’,‘y’,‘a’,‘\0’};
char b[ ]={‘r’,‘a’,‘m’,‘u’,‘\0’};
char c[6]=“malli”;
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 72
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Program-2
#include<stdio.h>
#include<conio.h>
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>
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 73
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
main()
{
char a[10],b[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
strcmp(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>
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 74
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
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 75
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
}
else
{
printf(“\n %s and %s are equal ”,a,b);
}
getch();
}
Q. Write a c-program to convert the given string into upper case letter.
#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string(lower case) ”);
scanf(“%s”,&a);
a=a-32;
printf(“\n the converted string is %s ”,a);
getch();
}
Q. Write a c-program to convert the given string into lower case letter.
#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string(upper case) ”);
scanf(“%s”,&a);
a=a+32;
printf(“\n the converted string is %s ”,a);
getch();
}
Page 76
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 77
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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 78
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “); actual arguments
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 79
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Program – 1:
#include<stdio.h>
#include<conio.h>
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>
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>
main()
Page 80
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
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>
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 81
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Categories of a function:
#include<stdio.h>
#include<conio.h>
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>
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 82
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
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 83
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
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 84
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
if(x= =1)
Return 0;
else
Return x=fact(x-1);
}
Page 85
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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
Keyword: register
Storage: CPU registers
Default value: An unpredictable value (garbage value)
Scope: local to the block in which it is defined.
Page 86
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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>
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>
main()
{
static int a;
clrscr();
printf(“\n A=%d ”,a);
getch();
}
Keyword: extern
Storage: main memory
Default value: zero
Scope: global
Life time: As long as the program execution doesn’t comes to end.
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Structure syntax:
struct student
{
int sno;
int sage;
char sname[10];
};
Declaring a structure variable:
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
.
Data type n member n;
} var-1, var-2, var-3;
(Or)
struct structure name var-1,var-2,var-3;
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’};
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>
struct Test
{
int a;
flaot b;
char c;
Page 89
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
};
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;
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
{
int a;
flaot b;
char c;
};
main()
Page 90
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
{
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;
};
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();
}
Program:
Page 91
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
flaot sage;
char sname[10];
};
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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;
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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
Ex. p=&a;
Page 94
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
#include<stdio.h>
#include<conio.h>
main()
{
int a,*p;
clrscr();
p=&a;
printf(“\n A is stored at %d ”,p);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,*p,*q*,r;
clrscr();
p=&a;
p=&b;
p=&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.
Why because here only the value of the actual arguments are passed from caller function to calle
function.
Page 95
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
Program:
#include<stdio.h>
#include<conio.h>
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>
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)
{
Page 96
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
int *z;
*z=*x;
*x=*y;
*y=*z;
}
Page 97
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
FILE MANAGEMENT IN C
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”);
Opening 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
Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM
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
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
Programming in “ C ”