Академический Документы
Профессиональный Документы
Культура Документы
Development of C:-
Alphabets: A…………….Z,
a……………..z.
Digits: 0,1,2,3,4,5,6,7,8,9
Variables:
A variable can be considered as a name given to the in the memory
where this constant is stored.
Note: the constant of the variable can change.
For ex: 5x+2y=100.
Here 5, 2 are cannot change. They are called constants.
The quantities x & y can be vary or change. They are variables.
TYPES OF CONSTANTS:
C constants can be divided in to two major categories.
A. Primary constants.
B. Secondary constants.
C constants
Primary Secondary
Integer constants
Array
Real constants
Pointers
Character
Structures
constants
Union...Etc.
Primary constants:
Rules for Integer constant:
9 Integer constants must have at least one digit.
9 It must not have decimal value.
9 It could be positive or negative.
9 If no sign is precedes integer constant it is assumed to be
positive.
9 No comma or blanks are allowed with in an integer constant.
9 The integer specified by ‘int’ keyword.
9 The integer range is -32768 to +32767.
Real constants are often called floating point constants. The real
constants can be written in two forms.
Fractional form
Exponential form
Fractional form:
9 A real constant must have at least one digit.
9 It must have a decimal point.
9 It could be either positive or negative.
9 Default sign is positive.
9 No commas or blanks are allowed with in a real constant.
Ex: +325.67.
+4875.2569
+245.00
-8745.2410.
In the exponent form of the representation, is represents two parts.
The part appearing before ‘e’ is called Mantissa.
The part appearing after ‘e’ is called Exponent.
Rules for exponent values:
‘C’ keywords:
Keywords are the words whose meaning has already been explained
to the C compiler. The keywords cannot be used as variable names. The
keywords are also called “Reserved words”.
C Instructions:
There are basically four types of instructions in C:
A) Type declaration Instruction.
B) Input/Output Instructions.
C) Arithmetic Instructions.
D) Control Instructions.
The purpose of each of these instructions is given below:
Hierarchy of operations:
Priority operators description
CONTROLS instructions in C:
There are four types of control instructions in C. they are
A) Sequence Control Instruction: - it ensures that the instructions
are executed in the same order in which they appear in the
program.
B) Decision and case control instructions: - it allows the computer
to take a Decision as to which instruction is to be executed next.
C) Loop control instructions: - these statements can help computer
to execute a group of statements repeatedly.
D) Case control Instructions: These statements are use to control
the case nature.
DISPLAY VARIABLES:
The function printf() statement is used to output simple string.
For ex: printf(“%d”,n);
Printf(“%d%d”,m,n);
Printf(“\n the values are %d is m and %d is n”,m,n);
READING VARIABLES:
The function scanf() statement is used to input simple string or
variable.
For ex: scanf(“%d”,&n);
scanf(“%d%f%c”,&interger,&floatvalue,&character);
OPERATORS:
An operator is a symbol that operates on a certain data type.
An expression is a combination of variables, constant and operators
written according to the syntax of the language. In C every expression
evaluates to a value i.e., every expression results in some value of a certain
type that can be assigned to a variable.
Types of operators:
In C, operators can be classified into various categories based on their
utility and action. A list of operator types is given below:
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and decrement operators
• Conditional operators
• Bitwise operators
• Comma operators
• Other operators
Arithmetic operators:
The arithmetic operators perform arithmetic operations and can be
classified into unary and binary arithmetic operators. The arithmetic
operators can operate on any built-in data type. A list of arithmetic
operators and their meaning are given below:
Operator meaning
+ Addition or unary plus
- subtraction or unary minus
* Multiplication.
/ Division.
% modulo division (reminder after division).
Note:
 C has no operator for exponention. However, a function
pow(x,y) exists in math.h which returns xy.
 The unary minus operator has the effect of multiplying its
operand is -1.
Relational operators:
R.O are used to compare arithmetic, logical and character expression.
We often compare two similar quantities and depending on their relation,
take some decisions. These comparisons can be done with the help of
relational operators. Each operator compares their left hand side with their
right hand side.
Operator meaning
< less than.
<= less than or equals to.
> greater than.
>= greater than or equal to.
== equal to.
!= not equal to.
Logical operators:
A logical operator is used to compare to evaluate logical and
relational expressions. These are three logical operators in C language.
They are:
Operator meaning
&& logical AND
|| logical OR
! logical NOT
Bitwise operators:
A bitwise operator on each bit of data. These operators are used for
testing, complementing or shifting bits to the right and left. Usually
bitwise operators are not useful in cases of float and double variables.
Lists of bitwise operators are given below:
Operator meaning
& bitwise AND
| bitwise OR
^ bitwise XOR
<< shift left
>> shift right
~ bitwise complement
Arithmetic assignment operators:
The assignment operator is = (equal sign). This operator evaluates the
expression on the right, and assigns the resulting value to the variable on the
left.
The statement a+=5; will add the number 5 to the value of a. The
other assignment operators are:
-= /= *= |= &= ^=
These have obvious meanings. They evaluate the expression on their
right, and use the result performs the corresponding operation on the
variable on the left.
Increment and decrement operators:
The increment and decrement operators are very useful in c language.
They are extensively used in for and while loops. The syntax of the
operators is given below.
++<variable name> --<variable name>
<variable name>++ <variable name>--
The operator ++ adds 1 to the operand and – subtract 1 from the
operand. These operators manifest in the two forms: prefix and postfix.
Conditional operator (ternary operator):
The conditional operator consists of two symbols: the question mark
(?) and the colon (:). The syntax and the flowchart for the ternary operator
are given in the following figures. This is the only operator in C that takes
three operands.
larger = i > j? i : j;
Comma operator:
A set of expressions separated by commas is a valid construct in the c
language. For ex: i, j are declared as followed nature.
int i, j;
Other operators:
The operator sizeof gives the size of the data type or the variable in
terms of bytes occupied in the memory. Another operator is the member
selection operator (and ->) which is used with structures and unions.
DATA TYPES REVISITED:
The primary data types in C were introduced. The primary types are
again classified into several types.
The if statement:
The keyword if tells the compiler that what follows, is a decision control
instruction. The condition following the keyword if is always enclosed with
in a pair of parentheses. If the condition is not true then the statement is not
executed, instead the program skips past it.
If we want to execute such multiple statements are to be executed then
they must be placed within a pair of braces. The syntax is followed
if (this condition is true)
{
executable statement 1;
executable statement 2;
executable statement 3;
…….
……
executable statement n;
}
the flow chart of the if construct is follows:
The if-else statement:
In the above syntax weather the condition is true than the if blocks of
statements are to be executes. If the condition is not true then the else block
of the statements are to be executes.
The flow chart is follows:
Note:-
Nested if-else:
It is perfectly all right if we write an entire if-else construct with in
either the body of the if statement or the body of an else statement. This is
called ‘nesting’ of ifs. This is shown in the following syntax.
if (this condition is true)
{
executable statement 1;
…….
……
executable statement n;
}
else
{
if(condition)
{
executable statement n;
}
else
{
executable statement n;
}
}
Form of if:
b) if(condition)
{
do this;
and this;
}
c) if(condition)
do this;
else
do this;
d) if(condition)
{
do this;
and this;
}
else
{
do this;
and this;
}
e) if(condition)
do this;
else
{
if (condition)
do this;
else
{
do this;
and this;
}
}
f) if(condition)
{
if( condition )
do this;
else
{
do this;
and this;
}
}
else
do this
The loop control structure:
The versatility of the computer lies in its ability to perform a set of
instructions repeatedly. This involves repeating some portion of the
program either a specified number of times of until a particular condition is
being satisfied. This repetitive operation is done through a loop control
structure.
While statement:
The while loop is most fundamental looping statement. It repeats a
statement or block while its controlling is true. Here is the general form is
follows:
while(condition)
{
statement 1;
statement 2;
…………
…………
statement n;
}
Note:
 The statements with in the while loop would keep on getting
executed till the condition being tested remains true. When the
condition becomes false, the control passes to the first
statement that follows the body of the while loop.
 The condition being tested may use relational or logical
operations as shown in the following examples:
while(i<=10)
while(i<10&&j<=15)
while(j>10&&(b<15||C<20))
do-while loop:
If the conditional expression controlling a while loop is initially false,
then the body of the loop will not be executed at all. However, some times it
is desirable to execute the body of a while loop at least once, even if the
conditional expression is false to being with. In other words, there are times
when you would like to test the termination expression at the end of the loop
rather than at the beginning. C supplies a loop that does just that: do-while.
The do while loop always executes its body at least once, because its
conditional expression is at the bottom of the loop its general form is.
do
{
statement 1;
statement 2;
………….
………….
statement n;
} while (condition)
Each iteration of the do while loop first executes the body of the loop
and then evaluates the conditional expression. If this expression is true, the
loop will repeat. Otherwise the loop terminates. Hence do while is called
postcheck loop.
The do-while flow chart is follows:
FOR LOOP:
The for loop is useful while executing a statement a number of times.
The syntax of the for loop can be given below:
………….
…………..
statement n;
}
The for loop operation as follows:
While the loop first starts, the initial expression of the loop is
executed. Generally, this is an expression that sets the value of the loop
control variables, which acts as a counter that controls the loop. It is
important to understand that the initialization expression is only executed
once. Next, text expression is evaluated. It usually tests the loop control
variable against a target value. If this expression is true, then the body of the
lop is executed. This is usually an expression is that increments or
decrements the loop control variable. The loop then iterates, first evaluating
the conditional expression, then executing the body of the loop, and then
executing the iteration expression with each pass. This process repeats until
the controlling expression is false.
SWITCH STATEMENTS:
The control statement that allows us to make a decision form the
number of choices is called switch. Or more correctly a switch-case-
default, since these three keywords go together to make up the control
statements.
The general form of the case is follows:
switch (integer expression)
{
case constant1:
do this;
and this ;
break;
case constant 2:
do this;
and this ;
break;
case constant 3:
do this;
and this ;
break;
………………..
…………………
………………..
……………….
case constant n;
do this;
and this ;
break;
default:
do this;
and this ;
}
The flow chart of the switch statement is follows:
JUMP STATEMENTS:
C supports three statements: break, continue and goto.
Using break statement:
We often come across situations where we want to jump out of a loop
instantly, without waiting to get back to the conditional test. The keyword
break allows us to do this. When the keyword break is encountered inside
any c loop, control automatically passes to the first statement after the loop.
A break is usually associated with an if.
The break flow chart is follows:
FUNCTIONS
A function is a self contained block of statements that perform a
coherent task of some kind. Every c program can be thought of as a
collection of these functions.
The general form of the function is follows:
main()
{
function1 ();
function2 ();
……………;
function n();
}
function1 (arg1, arg2, arg3)
type arg1,arg2,arg3;
{
statement 1;
statement 2;
statement 3;
…………..;
statement n;
}
Note:
 Any C program contains at least one function.
 If a program contains only one function, it must be main().
 If a C program contains more than one function, then one of
 These functions must be main(), because program execution
always begins with main().
 There are no limit on the number of functions that might be
present in a C program.
 Each function in a program is called in the sequence specified
by the function calls in the main();
 After each function has done its thing, control returns to main()
 When main () runs out of function calls the program ends.
 A function can be called any no of times.
 A function can call itself. Such a process is called ‘recursion’.
 A function can be called form other functions, but a function
cannot be defined in another function.
 There are basically two types of functions:
Library functions Ex: printf (), scanf ()……etc.
User-defined functions Ex: India (), move ()…etc.
Use of the function:
There are mainly two reasons are there.
a) Writing function avoid rewriting the same code over and over.
b) Using functions it becomes easier to write programs and keep track
of what they are doing.
sum=calsum(a,b,c);
printf("\n the sum= %d",sum);
getch();
}
calsum(x,y,z)
int x,y,z;
{
int d;
d = x+y+z;
return(d);
}
Output:
Enters the numbers 10 30 30
The sum is 70.
Note:
 In this program, from the function main() the values of a,b,c are
passed on to the function calsum(), by making a call to the
function calsum() and mentioning a,b,c in the parentheses;
In the calsum() function these values get collected in three
variables x,y,z.
calsum(x,y,z)
int x,y,z;
 The variables a, b, c are called ‘actual arguments’ where as the
variables x, y, z are called ‘formal arguments’.
 There are two methods of declaring the formal arguments. That
one that we have to use in our program is known as Kernighan
and Ritchie (k&R) method.
calsum(x,y,z)
int x,y,z;
Another method is
calsum(int x, int y, int z)
This method is called ANSI method and is more commonly
used these days.
 The return serves two purposes:
1) On executing the return statement it immediately transfers
the control back to the calling statement.
2) It returns the value present in the parentheses after return to
the calling program. In the above program the value of sum
of three numbers is being returned.
3) There is no restriction on the number of return statements.
Recursion:
main()
{
int fact(int);
int result;
result=fact(3);
printf(“\n the factorial of 3 is “,result);
result=fact(4);
printf(“\n the factorial of 4 is “,result);
result=fact(5);
printf(“\n the factorial of 5 is “,result);
}
int fact(int n)
{
int r;
if(n==1)
return(1);
r= fact(n-1)*n;
return(r);
}
Output:
The factorial of 3 is 6
The factorial of 4 is 24
The factorial of 5 is 120.
STOTAGE CLASSES:
From C compiler’s point of view, a variable name identifies some
physical location with in the computer were the string or bits representing
variable value is stored. There are basically two kinds of locations in a
computer where such a value may be kept: Memory and CPU registers. It is
the variable’s storage class which determinates in which two locations the
value is stored.
#include<stdio.h>
#include<conio.h>
main()
{
auto int i,j;
printf(“\n %d%d”,I,j);
getch();
}
Output:
2345 78912
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
void increment; void increment;
increment(); increment();
increment(); increment();
increment(); increment();
} }
Like auto variables, static variables are also local to the block in which they
are declare. The difference between them is that static variables don’t
disappear when the function is no longer active. Their values persist. If the
control come back to the same function again the static variables have the
same value they had last time around.
EX:-
int var [10];
float number [20];
char ch[100];
This size is often called the dimension of the array.
Array initilisation:
Till the array elements are not given any specific values, they
are supposed to contain garbage values, they are supposed to contain
garbage values if declared as auto. However can managed to store values in
them during program execution.
Let us now see how to initialize an array while declaring it.
Following are few examples that demonstrate this
int num[6] = { 2,3,4,5,6,7};
int n[] = {2,3,4,5,6,7};
float press[] = { 12.33,34.55,34.56,-98.78};
Note:
 Till the array elements are not given any specific values, they are
supposed to contain garbage values.
 If the array is initialised where it is declared, mentioning the
dimension of the array is important.
Accessing elements of an array:
Once an array is declared, then it can be referred. All the array
elements are numbered, stating with 0 to n-1 elements.
for(i=0;i<10;i++)
{
sum=sum+num[i];
}
avg=sum/10;
printf(“\n the average of the numbers is %d”,avg);
Notes:
 An array is a collection of similar elements.
 The first element in the array is numbered 0, so the last element
is 1 less than the size of the array.
 An array is also known as a subscripted variable.
 Before using an array its type and dimension must be declared.
 However big an array its element are always stored in
contiguous memory locations.
Array elements in memory:
int array[5];
Array bounding:
In C there is no check to see if the subscript used for an array exceeds
the size of the array. Data entered with a subscript exceeding the array size
will simply be placed in memory outside the array; probably on top of other
data, or on the program itself. This will lead to unpredictable results, to say
the least, and there will be no error message to warn that you are going
beyond the array size. In some cases the computer may just hang.
display(int m)
{
printf("\t %d",m);
}
OUTPUT:
55 35 45 65 75
Examples:
int arr[3][5];
float var[5][6];
char name[10][10];
Initialising a two dimensional array:
Just like one dimensional array even two dimensional array can be
initialized at the time of declaration.
For ex: int a[2][3] = {
{ 23,45,56},
{21,43,65}
};
float f[][]= {
{ 1.53,4.63,6.92}
{ 2.34,5.87.95.23}
};
int stud[2][2]={2,3,4,5};
Note:
It is important to remember that while initializing a array it is
necessary to mention the second (column) dimension, where as the first
dimension (row) is optional.
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“\n enter the number”);
scanf(“%d”,&arr[i][j]);
}
}
STRINGS:
The way a group of integers can be stored in an integer array,
similarly a group of characters can be stored in a character array. Character
array are many a times also called strings. Character arrays or strings are
used by programming languages to manipulate text such as word and
sentences.
A string constant is a one dimensional array of characters terminated
by a null (“\0”). For ex: char name = { ‘K’,’A’,’E’,’S’,’H’,’\0’ }
Each character in the array occupies one byte of memory and the last
character is always ‘\0’. This is looks like two characters. But it is actually
only one character, with the \ indicating that what follows it is something
special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same.
ASCII value of ‘0’ is 48. The figure below shows the way a character array
is stored in memory. That the elements of the character array is stored in
contiguous memory locations.
The terminating null(‘\0’) is important, because it is the only way the
function that work with a string can know where the string ends. In fact, a
string not terminated by a ‘\0’ is not really a string, but merely a collection
of characters.
B H A S K A R
1001 1002 1003 1004 1005 1006 1007
C considers the fact that you would use string very often and hence
provides a shortcut for Initialising string. For ex: the string used above can
also be initialised as,
char name[] =”Bhaskar”;
Note that in this declaration ‘\0’ is not necessary. C inserts the null
character automatically.
Note:
 Character takes only a single byte to store a character.
 String takes one extra character to store the null value.
main()
{
char arr[] =”character”;
int len1,len2;
Strcpy():
This function copies the contents of one string into another. The base
addresses of the source and target strings should be supplied to this function.
Ex:
#include<stdio.h>
#include<conio.h>
main()
{
char source[]="nayagara";
char target[20];
clrscr();
strcpy(target,source);
printf("\n the source string is:: %s",source);
printf ("\nTHE TARGET STRING IS:: %s",target);
getch();
}
Two dimensional array of characters:
The following example is can be follows:
#include<stdio.h>
#include<conio.h>
#define FOUND 1
#define NOTFOUND 0
main()
{
char
masterlist[6][10]={"raman","surya","kumar","nagesh","gopal","rajesh"};
int i,flag,a;
char yourname[10];
printf("\n enter your name:");
scanf("%s",yourname);
flag = NOTFOUND;
for(i=0;i<=5;i++)
{
a=strcmp(&masterlist[i][0],yourname);
if(a==0)
{
printf("\n Welcome, you can enter the palace");
flag =FOUND;
break;
}
}
if(flag==NOTFOUND)
printf("\n sorry, you are a trespasser");
}
Output:
Enter the name Bhaskar
Sorry, you are a trespasser.
Enter your name surya
Welcome, you can enter the palace
POINTERS
The address of data item is the address of its first storage location.
This address can be stored in another data item and manipulated in a
program. The address of a data item is called pointer to the data item and a
variable that holds an address is called a pointer variable.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,*c;
int * check(int,int);
printf("\n enter two numbers:");
scanf("%d%d",&a,&b);
c=check(a,b);
printf("\n greater no is %d",*c);
getch();
}
int* check(int i,int j)
{
int *p,*q;
p=&i;
q=&j;
if(*p>=*q)
return(p);
else
return(q);
}
Output:
greater no is 20
for(i=0;i<5;i++)
{
printf("\n %d - %d \n",ptr,*ptr);
ptr++;
}
getch();
}
Output:
404 – 10 406 – 20 408 – 30 410 – 40 412 - 50
OUTPUT
array elements are:25 49 17 90 11
#include<stdio.h>
char z[]="this string is declared externally\n\n";
main()
{
static char x[]="this string is declared with in main";
clrscr();
printf("\n %s",z);
printf("\n %s",x);
}
Output:
this string is declared externally
this string is declared with in main
/* Here is a different version of the same program. The string are now
assigned to pointer variable rather than to conventional one_di array */
#include<stdio.h>
char *z="this string is declared externally\n\n";
main()
{
static char x="this string is declared with in main";
clrscr();
printf("\n %s",z);
printf("\n %s",x);
}
Output:
this string is declared externally
this string is declared with in main
1. malloc()
malloc() is used to obtain storage for an object. The allocation of
storage by calling malloc() yields a pointer to the beginning of the storage
allocated and is suitable aligned and allocated, so that it may be assigned to a
pointer variable.
Suppose that x is to be defined as a one dimensional, 10-elements
array of integers. Our first step is to declare x as a pointer variable. At this
movement, 2 bytes are stored in memory for this pointer variable.
int *x;
To assign sufficient memory for x(i.e. 10 integers), we can make use
of the library function malloc () , as follows.
x=(int *)malloc(10 *sizeof (int));
Format:
Void *malloc(size);
Size: Is the number of bytes required.
Malloc returns a pointer to void it needs to be typecasted. Void*
return type can be used to make general purpose function with out having to
known the actual use of the function.
To be consistent with the definition of x as a pointer to integer, we
want the pointer to be an integer. Hence, we include a type cast that is,
x= (int *) malloc (10 * sizeof (int));
calloc ():
calloc() function works exactly similar to malloc() except for the fact
that it needs two arguments as against the one argument required by
malloc().
void *calloc(n items, size);
nitems : the number of items to allocate.
Size : Size of each item
For ex:
ar=(int*)calloc(10,sizeof(int));
Allocate the storage to hold an array of 10 integers and assigns the pointer to
this storage to ar.
While allocating memory using calloc(), the number of items which are
allocated, are initialised.
realloc()
general format for memory allocation using realloc() is
void *realloc (void *block, size);
Where
Block: points to a memory block previously obtained by calling
malloc(), calloc() or realloc().
Size: New size for allocated block.
realloc() returns a pointer to the new storage and Null if it is not
possible to resize the object, in which case the object(*block) remains
unchanged. The new size is larger or smaller than the original size. If the
new size is larger, the original contents are preserved and the remaining
space is uninitilised, if smaller, the contents are unchanged up to the new
size.
The function realloc() works like malloc() for the specified size if
block is a null pointer.
For ex:
cp=(char *)realloc(cp, sizeof(“computer”));
free():
The memory allocated malloc(), calloc() or realloc() function is not
destroyed automatically. It has to be cleared by using the function free()
free(ptr);
STRUCTURES
Ordinary variables can hold one piece of information and array can hold a
number of pieces of information the same data type. These two data types
can handle a great variety of situations. But quite often we deal with entities
that are collection of dissimilar data types. C deals with this situation with
use of structures.
Declaring a structure:
The general form of a structure declaration statement is given below:
Struct<structure name>
{
structure element1;
structure element 2;
…………………
structure element n;
};
once the new structure data type has been defined one or more variables can
be declared to be of that type.
Ex: struct <structure name> variables;
struct book
{
char name;
float price;
int pages;
};
or
struct bookb1,b2,b3;
{
char name;
float price;
int pages;
}b1,b2,b3;
OUTPUT:
Arrays of a structure:
The array of a structure is similar to a normal array declaration and follows
down.
struct value b[10];
This provides the space in memory for 10 structures of the type struct book.
The syntax we use to reference each element of the array b is similar to the
syntax used for arrays in ints and chars.
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b[10];
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("\n enter the name::");
scanf("%c",&b[i].name);
printf("\n enter the price");
scanf("%f",&b[i].price);
printf("\n enter the pages::");
scanf("%d",&b[i].pages);
}
for(i=0;i<10;i++)
{
printf("\n %c%f%d",b[i].name,b[i].price,b[i].pages);
}
}
ASSIGNING A STRUCTURE VARIABLE TO ANOTHER
STRUCTURE VARIABLE:
The values of a structure variable can be assigned to another structure
variable of the same type using the assignment operator. This shown in the
following program example:
/* program demonstration assigning a structure variable to another*/
#include<stdio.h>
main()
{
struct employee
{
char name[10];
int age;
float salary;
};
struct employee e1= {"xyz",24,15000.00};
struct employee e2,e3;
clrscr();
strcpy(e2.name,e1.name);
e2.age=e1.age;
e2.salary=e1.salary;
e3=e2;
printf("\n %s%d%f",e1.name,e1.age,e1.salary);
printf("\n %s%d%f",e2.name,e2.age,e2.salary);
printf("\n %s%d%f",e3.name,e3.age,e3.salary);
getch();
}
OUTPUT:
xyz 24 15000.00
xyz 24 15000.00
xyz 24 15000.00
NESTING OF STRUCTURES:
One structure can be bested with in another structure. Using this facility
complex data types can be created.
The following ex. demonstrates the nested structures.
#include<stdio.h>
main()
{
struct address
{
char phone[15];
char city[30];
int pin;
};
struct emp
{
char name[30];
struct address a;
}
struct emp e{“xyz,”9885141414”,”Bombay”,32516);
printf(“\n name = %s phone = %s”,e.name,e.a.phone);
printf(“\n city = %s pin = %d”,e.a.city,e.a.pin);
}
Ex:
#include<stdio.h>
main()
{
struct book
{
char name[30];
char author[30];
int callno;
};
struct book b1={“wings of fire”, Abdul kalam ”, 23456);
struct book *ptr;
prt = &b1;
printf(“\n %s%s%d”,b1.name,b1.author,b1.callno);
printf(“\n %s%s%d”,ptr->name,ptr->author,ptr->callno);
printf(“\n %s%s%d”,*ptr.name,*ptr.author,*ptr.callno);
}
output: