CA-103-C-Language Notes
CA-103-C-Language Notes
E Society‟s
SNBP College of Arts,Commerce,Science & Management Studies
Morwadi,Pimpri-18
Department of BBA(CA)
Class FYBBA(CA) Subject:CA_103 C Language
History of C
The ALGOL is a root of all languages. It was introduced in 1960. It was the first computer language. The
reason behind wide use is the concept of Structured programming.
In 1967, the language BCPL (Basic Programming Language) was developed by Martin Richards for writing
System Software.
In 1970, the language B was developed by Ken Thompson. It has a many features of BCPL .The early versions
of UNIX developed by this language in Bell Laboratories.
In 1972 the language C was developed by Dennis Ritchie at AT & T‟s (American Telecommunication &
Telegraph) Bell Laboratories in USA.
1. Structured Programming –
It contains functions, Modules, Blocks, Selection, looping control constructs which helps to write
structured program.
2. Portable –
It is portable because it runs program on different machines and operating system.
3. Rich Set of built-in-functions –
Its strength is variety of built-in-functions provided by C library which helps to develop any complex
program easily.
4. Extendibility –
It supports built-in-functions as well as user defined functions. Programmer can add continuously
functions.
5. General Purpose Language-
It helps to develop most of the applications like Mathematical, Business, Scientific and System Software.
6. Debugging and Testing –
Due to structured programming it makes program easy to Debugging, Testing and Maintenance.
7. Variety of Keywords, Data types and Operators
It has a variety of Data types, Arithmetic Operators and 32 Keywords , which helps to develop program
easily.
Function n
(User Defined function)
1. Documentation Section-
Consist of set of comments helps to understanding program.
(e.g. Author or Other details)
2. Link Section-
It provides instructions to compiler to link function from the system library.
3. Definition Section-
It defines Symbolic Constants.
4. Global Declaration Section-
To declare global variables that is used in more than one functions.
5. main() function section-
The program must have main () function . It contains two parts
a) Declaration Part-
To declare all the variables which are used in executable part.
b) Executable Part-
It contains the executable statements and different function calls. The program execution begin with
theopening brace { and ends with closing brace }
6. Function Section-
It contains all the user defined functions that are called in main() function section.
Language Fundamental
TokensCharacter Set
Character set consist of Alphabets, Digits and Special Symbols. It helps to represent information. The C
character set is a Upper and Lowercase Alphabets, Digits , Special Characters and Alphanumeric character i.e.
combination of Alphabets and Digits
The smallest individual Chapters in C program are called as Token. C has following Tokens
C Tokens
1 Keywords
2 Identifiers
3 Variables
4 Data Types
5 Operators
Keywords and
IdentifiersKeywords
Keywords are reserved words. They have fixed and predefined meaning and these meaning cannot be changed.
The keywords cannot be used as variable name because which is not allowed in C. Keywords help in
buildingblocks of program. There are only 32 keywords available in C.
Identifiers
Identifiers are user defined words and are used to give names to variables, arrays, functions, structures etc.
Variables
Declaration of Variable
Variable must be declared before it is used in program. Its specifies its name and
data type.
Example int roll_no;
float amount;
char grade
Initialisation of Variable
To assign the value to variable at the time of declaration is called as variable initialisation. Otherwise in
only declaration it will takes garbage value.
Example
int count=1;
float x=4.2;
char ans=‟y‟;
Data Types in C
Every Program works on Data. Programming language provides way to store data with its type. When variable is
declared you have to specify that what type of data it can contain.
e Data types specify that size and which type of value stored in that variable.C
language is rich in its Data type.
Datatypes
Unsigned long
Char char int short int long int float double
double
An operator is a symbol that represents an operation. Which instruct compiler to perform some action?
Operators can be of
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Other Operators
1. Arithmetic Operators
It is used to perform arithmetic operations. It is called as binary operators because it operates on
two operands.
3. Logical Operators
It can be used to test more than one conditions and make decision. It used to combine two or more
expressions (relational). The entire expression is called logical expression which evaluates to True (1)
or False (0).
4. Assignment operators
It is denoted by ( = ). It is used to assign the value of an expression to variable.
Example
sum=a+b;
a = b;
Example
X+=y i.e. x=x+y;
x-=y i.e x=x-y;
k/=3 i.e. k=k/3;
j*=5 i.e. j=j*5;
5. Increment or Decrement Operator
C Provides the two unary operators i.e. Increment (++) or Decrement Operators (--)
++ Increments the value of operands by one
-- Decrements the value of operands by one
A. Prefix
In prefix the Increment or Decrement operators written before operand.
The Increment or Decrement done before the value of operand used in an expression
(e.g. ++a ,--b)
B. Postfix
In postfix the Increment or Decrement operators written after operand.
The Increment or Decrement done after the value of operand used in an expression
(e.g. a++ , b--)
6. Bitwise operator
A. C Programming provides us six operators for manipulation of data at bit level
B. These operators operate on an Integer and character but not on float or double.
C. Using Bitwise operators we can manipulate individual bit.
D. The Bitwise operators is for testing the bits or shifting them to the right or left.
Operator Name of Operator
~ One‟s Compliment
>> Right Shift
<< Left Shift
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
7. Other Operators
Assignment Operator
The Assignment Operator ( = ) is used to assign the value an expression to variable.
Syntax
variable = expression;
Example
sum=a+10;
a=5;
C automatically converts any intermediate values to the proper type so that the expression can be evaluated
without loosing any significance. This automatic conversion is known as implicit type of conversion.
The rule is that if the operands are of different types, the lower type is automatically converted to the higher type
before the operation proceeds.
If one operand is float and other is integer, the integer is converted into float and result is float.
If one operand is long double then the other will be converted to long double, and the result will be long double.
If one operand is double then the other will be converted to double, and the result will be double.
Example
int a;
float b;
a=9.7; When 9.7 is assigned to a it gets converted into 9
b=20; When 20 is assigned to b it gets converted into 20.000000
Syntax
(Type_name) expression;
The expression is converted to the specified data type locally only for the purpose of evaluation of the
expression.
Example
a=(int)9.4; Here 9.4 is converted to integer by conversion.
c=(int)a+b; Here a is converted to integer and then added to b.
c=(int)(a+b); Here the result of a+b is converted to integer.
CHAPTER 2: Managing I/O
1. Character Input/Output
2. String Input/Output
1. Character Input/Output-
Character Input/Output functions are useful for reading single character from the keyboard and writing single
character to the monitor.
Inorder to read single character we use getch(),getche(),getchar() functions.These functions returns the character
that has been most recently typed in.
a) getch()
This function gets one character input from keyboard but doesn‟t display on the screen .
Syntax
variable name=getch();
b) getche()
This function gets one character input from keyboard and display on the screen.
Syntax
variable name=getche();
c) getchar()
This function gets one character input from keyboard but doesn‟t display on the screen but it require the
user hit the enter key after the character typed in.
Syntax
variable name=getchar();
We make use of putch() and putchar() functions for writting single character on terminal
a) putch()
This function prints a single character on screen.
Syntax
variable name=putch();
b) putchar()
This function prints a single character on screen.
Syntax
variable name=putchar();
There are number of character functions supported by C. These are contained in file<ctype.h>
Functions Meaning
Isalpha() To check is alphabet
Isalnum() To check is alphanumeric
Isdigit() To check is digit
Islower() To check is lowercase
character
Isupper() To check is uppercase
character
tolower() To check is alphabet
toupper() To convert character in
uppercase
tolower() To convert character in
lowercase
2. String Input/Output
String Input/Output functions are useful for reading string from the keyboard and writing string to the monitor.
Inorder to read string we use gets() function and for writing string we use puts()function.
a) gets()
This function read the string character by characters contineously from input device until the enter key is
pressed.
Syntax
gets(variable name);
b) puts()
This function writes the string on screen.
Syntax
puts(variable name);
a) scanf()
This function reads the character from standard input device. Interprets them according to the format
specifiers and store them in corresponding argument
Syntax
scanf(“control String”,&arg1,&arg2, -------,&argn);
Example
scanf(“%d %f”,&x,&y);
scanf (“ %2d”,&y);
scanf (“ %d%d”,&n1,&n2);
scanf (“ %15s”,city);
scanf (“ %s”,name);
b) printf()
This function prints the captions and values on screen. It produces output easy to use and understandable
format.The printf() function provides features to control alignment and spacing output.
Syntax
printf(“control String”,arg1,arg2, ------ ,argn);
Example
printf(“Welcome to C Programming”);
printf(“%d %f”,x,y);
printf(“sum=%d”,s);
printf(“ %.2f”,y);
printf(“ %7.2f”,x);
printf(“ %6d”,y);
printf(“ %-6d”,y);
printf(“ %s”,name);
printf(“ \n\n”);
printf(“ ”);
Introduction
C program is a set of statements which are normally executed sequentially sometimes in programs there is need to
test some condition at some point and selecting the alternative paths depending upon the result of condition or
repeat a group of statements until certain specified conditions are met. C language processes such decision making
capabilities by supporting the following statements.
if statement
In simple if statement it test or evaluates the condition first if it is true then the Statement Block will get executed
and if the condition is false then the Statement Block is skipped and statement-a is executed
Syntax
If(test condition)
{
statement block;
}
Statement-a;
if...else Statement
In if...else statement it test or evaluates the condition first if it is true then the Statement Block1 (called if block) is
executed and if the condition is false then the Statement Block2(called the else block) is executed.
Syntax
If(test condition)
{
statement Block1;
}
else
{
Statement block2;
}
Q. Write a Program to find larger number between two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("larger no=%d",a);
}
else
{
printf("larger no=%d",b);
}
getch();
}
if(no%2==0)
{
printf("enter no is even");
}
else
{
printf("enter no is odd");
}
getch();
}
Synatax
If(test condition-1)
{
If(test condition-2)
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
statement-a;
Q. Write a Program to enter any three numbers and display larger
number between them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter three numbers");
scanf("%d %d %d",&a,&b&c);
if(a>b)
{
if(a>c)
{
printf("larger Number=%d”,a);
}
else
{
printf(" larger Number=%d”,c);
}
else if(c>b)
{
printf("larger Number=%d”,c);
}
else
{
printf("larger Number=%d”,b);
}
}
getch();
}
else if ladder
When multiple decisions or conditions are involved we make use of else...if ladder. The else...if ladder is a chain
of if where each else has an associated if.
In else...if ladder the conditions are evaluated from the top to downwards. As soon as the true condition is found,
the statement associated it is executed and the control is transferred to the statement-a by skipping the rest of
statements. When all n conditions becomes false then the final else containing the default statement will be
executed.
Syntax –
If(test condition1)
{
statement block1;
}
else If(test condition2)
{
statement block2;
}
else If(test condition n)
{
statement block n;
}
else
{
default-statement;
}
statement-a;
Q. Write a Program to enter any number upto 5 digit and find number
of digit in that number.
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
float per;
clrscr();
printf("enter a number");
scanf("%d",&no);
if(no>=9)
{
printf("Number is One Digit");
}
else if (no>=99)
{
printf("Number is Two Digit");
}
else if (no>=999)
{
printf("Number is Three Digit");
}
else if(no>=9999)
{
printf("Number is Four Digit");
}
else if(no>=99999)
{
printf("Number is Five Digit");
}
else
{
printf("enter number upto five digit");
}
getch();
}
The switch statement
C provides a switch statement to avoid the use of series of if. C has a built-in multi way decision statement known
as switch. The switch statement test the value of a given variable against a list of case values and when a match is
found a block of statement associated with that case is executed. When all test values becomes false then the final
default case block will be executed.
Syntax -
switch(expression)
{
case 1:
statement block1;
break;
case 2:
statement block2;
break;
case 3:
statement block3;
break;
..........
..........
..........
default :
default statement block;
break;
}
Statement-a;
Q. Write a program to enter any character and find character is vowel
or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter character");
scanf("%c”,&ch);
switch(ch)
{
case „A‟:
case „a‟:
printf("Character is Vowel");
break;
case „E‟:
case „e‟:
printf("Character is Vowel");
break;
case „I‟:
case „i‟:
printf("Character is Vowel");
break;
case „O‟:
case „o‟:
printf("Character is Vowel");
break;
case „U‟:
case „u‟:
printf("Character is Vowel");
break;
default:
printf("Character is Vowel");
break;
}
getch();
}
Conditional Operator
It is called as ternary operator in C. It requires three operands. The operator pair
( ? and : ) is used to construct conditional expression of the form.
expression1? expression2: expression3
Here expression1 is evaluated first. If it is true (non zero) then expression1 is evaluated and becomes the
value of conditional expression and if it is false (zero) then the value of entire expression is that of
expression3.
while loop
Loops are used when we want to execute a part of program or block of statements several times. Like a if
statement here we have single or block of statement it is known as body of loop. In while loop first condition is
tested or evaluated if it is true then statements in the body of loop are executed. While loop would keep on getting
executed till the condition being tested remains true when the condition becomes false the body of loop is skipped
and control is transferred to out of the loop i.e. statement-a.
Syntax
while(condition)
{
Block of statements;
}
Statement-a;
Q. Write a Program to display name 10 times.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
printf("Aditya\n");
i++;
}
getch();
}
while(i<=10)
{
printf("\n %d",i);
sum=sum+i;
i++;
}
printf(“sum of the digits=%d”,sum);
getch();
}
Q. Write a Program to enter any number and print in reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int no,rem,rev=0;
clrscr();
printf("\n enter the no");
scanf("%d=",&no);
while(no>0)
{
rem=no%10;
rev=rem+(rev*10);
no=no/10;
}
printf("\n Revers no=%d",rev);
getch();
}
statement-a;
Q. Write a Program to print name 20 times.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf(“Adti \n”);
i++;
} while(i<20);
getch();
}
do
{
prod=n*i;
printf(“\n%d”,prod);
i++;
} while(i<10);
getch();
}
Difference between while and do-while loop
while(condition) do
{ {
statement-a;
For loop
For loop is the most frequently use loop construct. For statement provides initialisation of counter. Test condition
and counter Increment/Decrement all in single line. For is entry controlled loop construct. It first checks the test
condition and if it is true only then it executes the loop body.
We can initialise more than one value in for loop. The test condition may have compound relation and the testing
need not be limited only to the loop control variable. Both the initialization and increment sections are omitted in
for statement. However the semicolon separating the section must be remain.
Syntax
for(initialisation; condition; Increment)
{
block of statements;
}
statement-a;
Q. Write a Program to find factorial of number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
clrscr();
printf("\n enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(" \n factorial=%d",fact);
getch();
}
for(i=1;i<=100;i++)
{
if(i%5==0)
{
printf(“\n%d”,i);
}
}
getch();
}
Nested for loop
When a loop is written inside the body of another loop, then it is known as nesting of loops. The for loop also can
be used nesting type i.e. one for statement within another for statement is allowed in C.
Syntax –
Jump Statements
break statement
continue statement
goto statement
exit
break statement
Break statement is used to unconditionally exit from the loop. It is useful in a situation where we want to jump
out of loop immediately without waiting to get back to the conditional test. When the loops are nested, the
break would only exit from the loop containing it. That is, the break will exit only a single loop.
Syntax
for(i=1;i<=10;i++)
{
printf("%d",i);
if(i==5)break;
printf("$$$$$");
}
getch();
}
continue statement
Continue command is used to skip the rest of the commands of the loop for the current occurrence and move
the program pointer to the top of the loop. When the keyword „continue‟ is encountered inside any C loop,
control automatically passes to the beginning of the loop.
for(i=1;i<=10;i++)
{
printf("%d",i);
if(i==5)continue;
printf("$$$$$");
}
getch();
}
goto statement
C supports goto statement for unconditional branching from one point in the program to another . The goto
statements requires label. The label identifies the place in the program where the program control is to be
transferred when the goto is encountered.
Syntax
goto label name
.......
.......
.......
label name:
Statement 1;
Statement 1;
Q. Write a Program to demonstrate goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
start:
{
int a=5;
printf(“%d”,a);
a++;
}
if(a<15)
goto start;
getch();
}
exit
We can jump out of the loop using break statement or goto statement. In similar way, we can jump out of the
program by using the library function exit(). We can break out of the program and return to the operating system,
we can use exit() function.
..........
.........
If(condition)
exit(0);
...........
...........
The exit function takes an interger value as its argument. Normally zero is used to indicate normal termination and
a nonzero value to indicate termination due to some error or abnormal condition.
The use of exit() functions requires to inclusion of header file <stdlib.h>.
Compound Statements
A compound statement consists of several statements enclosed within pair of curly braces {}.
Compound statement is also known as block os statements . Note that there is no semicolon after the closing brace.
Example
{
int l=10,b=6,h=8;
int volume;
volume=l*b*h;
}
The variables that are declared inside a block can be used only inside that block.
NULL Statements
The semicolon on a line is a null statement and this doesn‟t perform any action.
Example
main()
{
int x=2;
; //null statement
}
CHAPTER 4: Programs through Conditional and Looping Statements
Introduction
C program is a set of statements which are normally executed sequentially sometimes in programs there is need to
test some condition at some point and selecting the alternative paths depending upon the result of condition or
repeat a group of statements until certain specified conditions are met. C language processes such decision making
capabilities by supporting the following statements.
if(no%2==0)
{
printf("enter no is even");
}
else
{
printf("enter no is odd");
}
getch();
}
int a,b,c;
clrscr();
printf("enter three numbers");
scanf("%d %d %d",&a,&b&c);
if(a>b)
{
if(a>c)
{
printf("larger Number=%d”,a);
}
else
{
printf(" larger Number=%d”,c);
}
else if(c>b)
{
printf("larger Number=%d”,c);
}
else
{
printf("larger Number=%d”,b);
}
}
getch();
}
Program of else if ladder
Q. Write a Program to enter any number upto 5 digit and find number
of digit in that number.
#include<stdio.h>
#include<conio.h>
void main()
{
int no;
float per;
clrscr();
printf("enter a number");
scanf("%d",&no);
if(no>=9)
{
printf("Number is One Digit");
}
else if (no>=99)
{
printf("Number is Two Digit");
}
else if (no>=999)
{
printf("Number is Three Digit");
}
else if(no>=9999)
{
printf("Number is Four Digit");
}
else if(no>=99999)
{
printf("Number is Five Digit");
}
else
{
printf("enter number upto five digit");
}
getch();
}
Q. Write a simple program to enter a marks of five subject and display
Total, Percentage and Class.
#include<stdio.h>
#include<conio.h>
void main()
{
int sub1,sub2,sub3,sub4,sub5,total;
float per;
clrscr();
printf("enter the marks of five subject");
scanf("%d %d %d %d %d",&sub1,&sub2,&sub3,&sub4,&sub5);
total=sub1+sub2+sub3+sub4+sub5;
per=total/5;
printf("Total Marks =%d",total);
printf("Percentage=%f",per);
if(per>=70)
{
printf("Distinction");
}
else if(per>=60)
{
printf("First Class");
}
else if(per>=50)
{
printf("Second Classs");
}
else if(per>=40)
{
printf("Pass");
}
else
{
printf("Fail");
}
getch();
}
Program of switch statement
switch(ch)
{
case „A‟:
case „a‟:
printf("Character is Vowel");
break;
case „E‟:
case „e‟:
printf("Character is Vowel");
break;
case „I‟:
case „i‟:
printf("Character is Vowel");
break;
case „O‟:
case „o‟:
printf("Character is Vowel");
break;
case „U‟:
case „u‟:
printf("Character is Vowel");
break;
default:
printf("Character is Vowel");
break;
}
getch();
}
while(i<=10)
{
printf("Aditya\n");
i++;
}
getch();
}
Q. Write a Program to display 1 to 10 numbers and sum of the digits of
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
while(i<=10)
{
printf("\n %d",i);
sum=sum+i;
i++;
}
printf(“sum of the digits=%d”,sum);
getch();
}
while(no>0)
{
rem=no%10;
rev=rem+(rev*10);
no=no/10;
}
printf("\n Revers no=%d",rev);
getch();
}
Programs for Practice
1 Write a Program to find no is palindrome or not palindrome.
2 Write a program to print Fibonacci series upto 20 terms.
3 Write a Program to find no is Armstrong number or not Armstrong number.
4 Write a Program to enter number in digit and display in words (245- Two
Four Five)
5 Write a Program to find the L.C.M. and G.C.D. of a number
do
{
printf(“Adti \n”);
i++;
} while(i<20);
getch();
}
do
{
prod=n*i;
printf(“\n%d”,prod);
i++;
} while(i<10);
getch();
}
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(" \n factorial=%d",fact);
getch();
}
for(i=1;i<=100;i++)
{
if(i%5==0)
{
printf(“\n%d”,i);
}
}
getch();
}
for(i=2;i<n;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime==0)
{
printf(" no is not prime ");
}
else
{
printf("no is prime");
}
getch();
}
Jump Statements
break statement
continue statement
goto statement
exit
break statement
Q. Write a Program to demonstrate break statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",i);
if(i==5)break;
printf("$$$$$");
}
getch();
}
continue statement
for(i=1;i<=10;i++)
{
printf("%d",i);
if(i==5)continue;
printf("$$$$$");
}
getch();
}
goto statement
Introduction
The fundamental data types namely int, float, char etc. are very useful but variable of these data type can be stored
only one value at a time. So they can handle limited amount of data. In many applications we need to handle large
volume of data for that we have to need powerful data types that would facilitate efficient storing, accessing and
manipulation of data items.
C supports derived data type known as Array.
Definition
An array is collection of data items of the same data type
An array is fixed-size sequenced collection of elements of the same data type.
An array is also called as Subscripted Variables
Features of Array
1) An array is a collection of similar elements.
2) The location of array is the location of its first element.
3) The first element in array is numbered zero so the last element is less than the size of array.
4) The length of array is the number of its elements in array.
5) The type of an array is the data type of its element.
6) An array is known as subscripted variable.
7) Before using array it‟s type and dimension must be declared.
Definition
A list of items can be given one variable name using only one subscript and such a variable is called a
single subscripted or single Dimension Array.
Syntax
data type arrayname[size];
Example
int rollno[3];
float marks[5];
char name[30];
0 1 2
100 101 102
1340 1342 1344 ------- Memory Address
Array elements are always stored in contiguous memory locations and since the data type int occupies 2 bytes of
memory, each element will be allocated 2 bytes.
Declaration
We can declare array in the same way the ordinary variable.
Syntax
data type arrayname[size];
Example
int rollno[5];
int rollno[ ];
float num[5];
Initialization
We can initialise elements of array in the same way the ordinary variable.
Syntax
data type arrayname[size]={list of values};
Example
int rollno[5]={1,2,3,4,5};
int rollno[ ]={1,2,3,4,5};
float num[5]={2.5,7.2,9.2,6.2,3.3};
Accessing and displaying array elements
Definition
An array whose elements are specified by more than one subscript is known as multi dimension array (also called
Matrix)
Declaration
Syntax
data type arrayname[row size][column size];
Example
int student[5][2];
C0 C1
1 67 R0
2 73 R1
3 82 R2
4 90 R3
5 58 R4
Initialisation Array
int number[3][4]={8,12,25,37,42,52,68,79,81,92,100,102};
char city[5][10]={“Mumbai”,”Punei”,”Satara”,”Kolhapur”,”Sangli”};
Definition
In C character string simply treats as array character. The size in a character string represents the maximum
number of characters that the string can hold.
Declaration
char name[10];
It declares the name as a character array (string) variable that can hold a maximum 10 characters. Each character
of the string is treated as an element of the array name and is stored in the memory as follows.
A Character string terminates with an additional null character. Thus the element in name[10] holds the null
character „\0‟. When declaring character arrays, we must allow one extra element space for the null terminator.
Initialization
We can initialise string in array the same way as the ordinary variable.
Syntax
data type arrayname[size]={list of values};
Example
char name[5]={„s‟,‟w‟,‟a‟,‟p‟,‟n‟,‟i‟,‟l‟,‟\0‟};
char name[]=”siddhi”;
char name[5]={„P‟};
char *colour[]={“Red”,”Green”,”Blue”,”Yellow”};
Standard Library Function
The C library support large number of string manipulation functions in the header files <string.h> that can be used
to carry out many string manipulation functions.
strrev() This function reverse the given string contents and store it
again in same string variable.
Syntax strrev(string);
strcat() This function concatenates (joins) two strings means it
appends the second string at the end of first string.
Syntax strcat(string1,string2);
strcmp() This function compares two strings to check if they are equal
or not .If both strings are equal then it returns „0‟.If they are
not equal it returns value nonzero
Syntax strcmp(string1,string2);
CHAPTER 6: Functions
Introduction
Function is a group of statement which are used to performing specific task.
Every „C‟ program is the collection of function. We can avoid read and write same code over and over by using
function.
Purpose of Function
1. Modular or Structured programming can be done by use of function.
2. Troubleshooting and debugging become easier in structured programming.
3. Individual functions can be easily built and tested.
4. Program development becomes easy.
5. It is easier to understand the program logic.
6. A repetitive task can be put into a function that can be called whenever required. This reduces the size of
program.
Function Declaration
Function Definition
Calling Function
Function Definition
Function Declaration
Any function can be called simply using it‟s name and arguments in statement.
Syntax
function name(argument list)
Example
message();
sum(int,int);
Types of Functions
There are two types of functions in C
1. Call By Value
In this method, the contents of the arguments in the calling functions are not changed, even if
they are changed in the called function.
The contents of the actual parameters get copied into the corresponding formal parameters.
Introduction to Pointer
Definition
Pointers are the variables which holds the addresses of other variable within the memory.
A pointer is a variable that represents the location of a variable or an array element.
Pointer variables are denoted by „ * ptr ‟ . Pointers are the derived data types.
A pointer is used for creating data structure such as linked list trees, graphs etc.
Two pointer variable can not be added. Pointer variable can not be multiplied by constant. The value can not
be assigned to an arbitrary address &p=10 is illegal.
Declaration
Pointer are declared in the same way as any other variable but it is preceded with „*‟ in declaration.
int *p;
float *p;
char *p;
Initialization
The process of assigning the address of a variable to a pointer variable is known as initialisation of pointer .
Use of addressof (&) operator as a prefix to the variable name assigns its address to the pointer.
int *p;
P=&a;
int *p = NULL;
int *p=0;
Example
void main()
int i , *j;
i=4;
j= &i;
Output : :
The value of i is 4
The value of *j is 4
1. malloc ()
It allocates request size of memory bytes and returns a pointer to the first byte of the allocated space.
Syntax
ptr=(char*)malloc(10);
Syntax
3. realloc ()
It changes the size of previously dynamically allocated memory . realloc() takes two arguments . The first
is the pointer referencing the memory. The second is the total number of bytes that are to be reallocated.
Syntax
ptr=realloc(ptr,new size);
4. free ()
It frees previously allocated memory.
Syntax
free(ptr);
CHAPTER 8: Structure
Introduction to Structure
Structure are derived data types. We make use of structure to represent a collection of data items of different types.
Structure are useful for handling logically related data items.
Example
Definition
A structure contains a number of data types grouped together. The data type can be smaller or of different
types.
struct structname
{
Datatype member1;
Datatype member2;
};
Example
The structure students declared to consist of three fields the rollno, name and percentage. These fields are called as
structure members or structure elements. once you have defined this structure data type you can declare one or
more variables to be of that type.
Declaration
struct student
{
int rollno;
charname[40];
float percentage;
}s1,s2,s3;
OR
struct student s1,s2,s3;
Initializing Structure
Structure variables can be initialised at the time of declaration as follows.
struct student
{
int rollno;
charname[40];
float percentage;
};
struct student s1={101,”Jaydip”,87.25};
struct student s2={102,”Aditya”,90.57};
Structure Operations
Write a program to create structure employee and show employee details.
#include<stdio.h>
#include<conio.h>
struct employee
{
int eid;
char name[30],designation[20];
int salary;
};
void main()
{
struct employee x;
clrscr();
printf(" enter employee id");
scanf("%d",&x.eid);
printf("Enter the employee name");
gets(x.name);
printf("Enter employee designation");
gets(x.designation);
printf(" enter salary");
scanf("%d",&x.salary);
When a variable of structure is declared as array type then it is called as array of structure. It is used to store same
piece of information for more than one time. While accessing any member first we have to write structure variable
name followed by index number followed by dot
( . ) followed by member name.
Synatax
struct structname
{
Datatype member1;
Datatype member2;
};
struct structurename variable[number];
Example
struct student
{
int rollno;
charname[40];
float percentage;
}s1[50],s2[50];
OR
struct student s1[50],s2[50];
Here we declare a structure as a array type of size 50. So we get 50 student information.
Nested Structure
One structure can be nested in another structure . This means that you can have a structure within a structure. Then
it is called nested structure.
Syntax
struct structure1
{
Datatype member1;
Datatype member2;
struct structure2
{
Datatype member1;
Datatype member2;
}s1;
}s2;
Example
struct student
{
int rollno;
charname[40];
struct marks
{
int sub1;
int sub2;
}s2;
}s1;
Introduction to Union
Definition
Union which are very similar to structure. It is used to group number of different variable elements. Unions are
user defined data type like structure. The members of union share the same memory, only one member can be
active at a time .When we store another member values, the first member values are removed from the memory
and at this place, new member‟s values are stored. It can useful as they efficiently use computer‟s memory.
The size of union is equal to the maximum size occupied by its member.
It is used when we have to process each member sequentially .To declare a union keyword Union is used.
Declaration
union unionname
{
Datatype member1;
Datatype member2;
};
Example
Here the keyword union declares the union with the name student.
Write a program for enter patient details and show using Union.
#include<stdio.h>
#include<conio.h>
union patient
{
int pid;
char name[25];
int amount;
};
void main()
{
union patient x;
clrscr();
printf("\nenter patient id");
scanf("%d",&x.pid);
printf("\nPatient ID=%d",x.pid);
printf("\nnter the patient name");
scanf("%s",&x.name);
printf("\nPatient Name=%s",x.name);
printf("\nenter bill amount");
scanf("%d",&x.amount);
printf("\nBill Amount=%d",x.amount);
getch();
}
Differentiate between Structure and Union