PPS-Important Questions With Answers-3!10!18
PPS-Important Questions With Answers-3!10!18
The computer performs basically five major operations of functions irrespective of their size
and make. These are
1) it accepts data or instruction by way of input
2) it stores the data
3) it can process data as required by the user
4) it gives results in the form of output
5) it controls all operations inside a computer.
1. Input: This is the process of entering data and programs into the computer system.
2. Central Processing Unit (CPU): The ALU and the CU of a computer system are jointly known
as the central processing unit (CPU).
3. Control Unit (CU): The process of input, output, processing and storage is performed under
the supervision of a unit called 'Control Unit'. It decides when to start receiving data, when to
stop it, where to store the data etc. It takes care of step-by-step processing of all operations
inside the computer.
4. Arithmetic Logic Unit (ALU): The major operations performed by the ALU are addition,
subtraction, multiplication, division, logic and comparison etc.
5. Memory Unit: Memory unit is used to store data and instructions.
6. Output: This is the process of producing results from the data for getting useful information.
2|Page
Documentation section
Link section
Definition section
Global declaration section
main()
{
Declaration part;
Executable part;
}
subprogram section/user defined section
{
function1;
function2;
.
.
function n;
4|Page
2. Link section: It provides instructions to the compiler to link the functions from the
system library i.e., header files.
Examples:
#include<stdio.h>
# is the pre-processor directive command sign.
include is a pre-processor directive command.
stdio means a standard input & output.
.h means extension of header file.
#include<conio.h>
conio is a console input & output
4.Global declaration section: There are some variables used more than one function
such variables are called global variables. These variables are declared before the main
( ) function section.
5.main( )section: Every C program should contain one main() function and the C
program execution start with main( ). This section contains two parts.
1. Declaration part: This part declares all the variables which are used in the executable
part.
Example:
int a, b, c;
float x, y, z;
char a, b, c;
2. Executable part: This part contains a single statement or a group of statements.
All the statements in the declaration and executable parts should be end with semicolon
(;).
6. subprogram section/user defined section: It is also called user defined function
section i.e. it is developed by the user at the time writing a C program. All the user defined
functions are placed before or after the main() function section.
3.Constant/Literals:-
Constants are the fixed values that do not change during the execution of a
program.
Constant are divided into two types.
1. Numeric constants
2. Non-Numeric /Character constants
1.Numeric Constants: There are two types of Numeric constants
1. Integer constants
2. Real or floating point constants
1.int: -
1. It is indicated by the keyword ‘int’.
2. The control string of ‘int’ is %d.
3. It occupies of 2 bytes of memory location i.e., 16bits.
4. The range of int is -32768 to 32767.
Ex: - int x,y;
scanf (“%d%d”,&x,&y);
2.short int: -
1. It is indicated by the keyword ‘short’.
2. When the given value is less than the int range then short int is used.
3. The control string of short int is %d.
4. It occupies 1byte of memory location i.e., 8bits.
5. The range of short int is -128 to 127.
2) When the given value is greater than the float range then double is
used.
3) The control string of double is “%lf”.
4) It occupies 8bytes of memory location i.e., 64bits.
5) The range of double is 1.7E-308 to 1.7E+308.
Ex: - double a,b;
scanf(“%lf%lf”,&a,&b);
3.long double: -
1) When the given value is greater than the double range then long
double is used.
2) The control string of long double is “%Lf”.
3) It occupies 10 bytes of the memory location i.e., 80bits.
4) The range of long double is 3.4E-4932 to 1.1E+4932.
Ex: long double a,b;
scanf(“%Lf%Lf”,&a,&b);
3.Logical operators:-
1. Logical operators are used to combine two or more relational
conditions.
2. Logical operators gives the result either in 1 (true) or 0 (false).
3. There are 3 types of Logical operators in c language
1. Logical AND (&&).
2. Logical OR (||).
9|Page
UNIT-2
1.if statement: It can be used to execute a single statement or a group of statements based
on the condition. It is a one way branching statement in C language.
Syntax:
if (condition)
{
Statements;
}
next statement;
Operation: First the condition is checked if it is true then the statements will be executed
and then control is transferred to the next statement in the program.
if the condition is false, control skips the statements and then control is transferred to the
next statement in the program.
2.if else statement: It can be used to execute a single statement or a group of statements
based on the condition. It is a two way branching statement in C language.
Syntax:
11 | P a g e
if (condition)
{
statement1;
}
else
{
statement2;
}
next statement;
Operation: First the condition is checked if the condition is true then statement1 will be
executed and it skips statement2 and then control is transferred to the next statement in
the program.
if the condition is false then statement1 is skipped and statement2 will be executed and
then control is transferred to the next statement in the program.
3.nested if else statement: If we want to check more than one condition then nested if else
is used. It is multi way branching statement in C language.
Syntax:
if (condition1)
{
if (condition2)
{
statement1;
}
else
{
statement2;
}
}
else
{
statement3;
}
next statement;
Operation: First the condition1 is checked if it is false then statement3 will be executed
and then control is transferred to the next statement in the program.
If the condition1 is true then condition2 is checked, if it is true then statement1 will be
executed and then control is transferred to the next statement in the program.
If the condition2 is false then statement2 will be executed and then control is transferred
to the next statement in the program.
4.switch statement: If we want to select one statement from more number of statements
then switch statement is used. It is a multi way branching statement in C language.
Syntax:
switch(expression)
{
case value1:block1;
break;
case value2:block2;
break;
--------
--------
case valuen:blockn;
12 | P a g e
break;
default :default block;
break;
}
next statement;
Operation: First the expression value (integer constant/character constant) is compared
with all the case values in the switch. if it is matched with any case value then the
particular case block will be executed and then control is transferred to the next
statement in the program.
If the expression value is not matched with any case value then default block will be
executed and then control is transferred to the next statement in the program.
1.while loop statement: It is used when a group of statements are executed repeatedly
until the specified condition is true. It is also called entry controlled loop. The minimum
number of execution takes place in while loop is 0.
Syntax:
while(condition)
{
body of while loop;
}
next statement;
Operation: First the condition is checked if the condition is true then control enters into
the body of while loop to execute the statements repeatedly until the specified condition
is true.
if the condition is false, the body of while loop is skipped and control comes out of the
loop and continues with the next statement in the program.
3.for loop statement:- It is used when a group of statements are executed repeatedly until
the specified condition is true. It is also called entry controlled loop. The minimum
number of execution takes place in for loop is 0.
Syntax:-
for (initialization;condition;increment/decrement )
{
body of for loop;
}
next statement;
Operation: First initial value will be assigned. Next condition is checked if the condition
is true then control enters into the body of for loop to execute the statements. After the
execution of statements the initial value will be incremented/decremented. After initial
value will be incremented/decremented the control again checks for the condition. If the
condition is true then the control is again enters into the body of for loop to execute the
statements repeatedly until the specified condition is true.
if the condition is false, the body of for loop is skipped and control comes out of the loop
and continues with the next statement in the program.
#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
if (n%2==0)
printf("The given number is even");
else
printf("The given number is odd");
getch();
}
#include<conio.h>
void main()
{
int digit,n,sum=0;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf("The sum of individual digits of a given number is %d",sum);
getch();
}
10. Write a C program to check whether the given number is Armstrong or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int digit,n,sum=0,temp;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
digit=n%10;
sum=sum+(digit*digit*digit);
n=n/10;
17 | P a g e
}
if(temp==sum)
printf("The given number is Armstrong");
else
printf("The given number is not a Armstrong");
getch();
}
getch();
}
13. Write a C program to check whether the given number is strong number or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,digit,sum=0,temp;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
i=1;
fact=1;
digit=n%10;
while(i<=digit)
{
fact=fact*i;
i++;
}
sum=sum+fact;
n=n/10;
}
if(temp==sum)
printf("The given number is strong number");
else
printf("The given number is not a strong number");
getch();
}
14. Write a C program to check whether the given number is perfect number or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,sum=0;
clrscr();
printf(“Enter any number\n“);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
printf(“The given number is a perfect number”);
19 | P a g e
else
printf(“The given number is not a perfect number”);
getch();
}
UNIT-3
1. Define a function and explain the different types of functions with an example
program?
Ans:
1. A Function is a sub program/self-contained block of one or more statements that
performs a special task when called.
2. Every C program execution starts with main().
3. main() calls other functions to share the work.
4. The large programs can be divided into small programs using functions.
20 | P a g e
int p;
p = x+ y;
return (p);
}
2. Explain the categories of functions based on the arguments and return values?
Ans:
Function Categories (Based on Arguments and Return Values)
There are 4 types
1. Functions with arguments and functions with return values.
2. Functions without arguments and functions with no return values.
3. Functions with no arguments and functions with return value.
4. Functions with arguments and functions with no return values.
Example program
#include<stdio.h>
#include<conio.h>
int add(int x, int y);
void main()
{
int a,b,c;
clrscr();
printf(“enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
c=add(a,b);
printf(“the sum is %d”,c);
getch();
}
int add(int x , int y)
{
return(x+y);
}
Example program
#include<stdio.h>
#include<conio.h>
void add();
void main()
{
22 | P a g e
clrscr();
add();
getch();
}
void add()
{
int a,b,c;
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
printf(“the sum is %d”,a+b);
}
Example program
#include<stdio.h>
#include<conio.h>
int add();
void main()
{
int c;
clrscr();
c=add();
printf(“the sum is %d”,c);
getch();
}
int add()
{
int a,b;
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a,&b);
return(a+b);
}
Example program
#include<stdio.h>
#include<conio.h>
void add(int x, int y);
23 | P a g e
void main()
{
int a,b;
clrscr();
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
add(a,b);
getch();
}
void add(int x,int y)
{
printf(“The sum is %d”,x+y);
}
1.Call by value:
Sending or passing the values as actual arguments to the called function is known as call
by value.
In this method the changes are made in formal arguments of the called function will not
effect the actual arguments of the calling function.
Ex: Write a C program to interchange the values of two variables using call by value.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf("Before swapping a=%d, b=%d \n", a,b);
swap(a,b);
printf(" After swapping a=%d, b=%d", a,b);
getch();
}
int swap(int x, int y)
{
int temp;
printf(" Before swapping x=%d, y=%d \n",x,y);
temp=x;
x=y;
y=temp;
printf(" After swapping x=%d, y=%d \n",x,y);
}
2.Call by address:
Sending or passing addresses as an actual arguments to the called function is known as
call by address.
24 | P a g e
In this method changes are made in formal arguments of the called function will effect
the actual arguments of the calling function.
Ex: Write a C program to interchange the value of two variable using call by address.
#include<stdio.h>
#include<conio.h>
void main()
{
int a =10,b=20;
clrscr();
printf("Before swapping a=%d, b=%d \n",a,b);
swap(&a,&b);
printf(" After swapping a=%d, b=%d",a,b);
getch();
}
int swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
5. Explain recursion and write a C program to find factorial of a given number using
recursion?
Ans:
Recursion:
When a called function in turn calls the same function then chaining occurs. Recursion is
a special case of this process or a repetitive process where a function calls itself.
C program to find the factorial of a given number using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int r,n;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
r=factorial(n);
printf("the factorial of the given number is %d",r);
getch();
}
int factorial(int x)
{
int fact;
if(x= =0)
return(1);
else
fact=x*factorial(x-1);
return(fact);
}
6. Explain recursion and write a C program to find GCD of a given number using recursion?
Ans:
Recursion:
When a called function in turn calls the same function then chaining occurs. Recursion is
a special case of this process or a repetitive process where a function calls itself.
#include<conio.h>
void main()
{
int a,b,r;
clrscr();
printf(“Enter the two numbers\n”);
scanf((“%d%d”,&a,&b);
r=gcd(a,b);
printf(“GCD of a,b is %d”,r);
getch();
}
int gcd(int x,int y)
{
if(y= =0)
return (x);
else
return gcd(y,x%y);
}
UNIT-4
1. Define array and explain how one dimensional arrays are declared and initialized in C?
Ans:
1. Array is a collection of elements of the same data type.
2. Array is a collection of homogeneous elements.
3. Array range starts from 0 to n-1.
4. Array elements are stored in contiguous memory locations or consecutive memory
locations or successive memory locations.
1.One dimensional arrays (1DA) :-
If an array contains one subscript then it is called one dimensional array.
Declaration of one dimensional arrays (1DA)
Syntax:
datatype arrayname[size];
In the above syntax data type maybe any basic data type such as int,float, etc.
arrayname is any valid identifier.
size indicates number of elements can be stored in an array name.
Example: int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-
1 i.e first element of arr array will be stored at arr[0] address and last element will occupy
arr[9].
Syntax :
datatype arrayname[size] ={ value1, value2,……, value n};
Example:
int x[5]={10, 20, 30, 40, 50};
In the above example five integer elements can be stored in the array name ‘x’
Here x[0] refers to the 1st element stored in the array i.e.10
x[1] refers to the 2nd element stored in the array i.e.20
x[2] refers to the 3rd element stored in the array i.e.30
x[3] refers to the 4th element stored in the array i.e.40
x[4] refers to the 5th element stored in the array i.e.50
2. Write a C program to find both the largest and smallest elements from the given array?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],i,large,small;
clrscr();
printf(“ Enter any five integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
large=x[0];
small=x[0];
for(i=0;i<5;i++)
{
if(x[i]>large)
large=x[i];
if(x[i]<small)
small=x[i];
}
printf(“The largest element from the given array is %d \nThe smallest element from the
given array is %d”,large,small);
getch();
}
size1 indicates no. of rows and size2 indicates no. of columns stored in an array name.
Example:
int a [3][4];
Transpose[i][j]=A[j][i];
}
}
printf(‘The Transpose of matrix A is\n”);
for(i=0;i<3<i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,Transpose[i][j]);
}
printf(“\n”);
}
getch();
}
5. Write a C program to perform addition of two matrices and print the result in another
matrix of order 3*3?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3], B[3][3], C[3][3],i,j;
clrscr();
printf(“Enter the elements of matrix A\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enter the elements of matrix B\n”);
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&B[i][j]);
}
}
for(i =0;i <3;i++)
{
for(j= 0;j <3;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
printf(“The result in C matrix is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
30 | P a g e
{
printf(“%d\t”,C[i][j]);
}
printf(“\n”);
}
getch();
}
6. Write a C program to perform multiplication of two matrices and print the result in
another matrix of order 3*3.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3], B[3][3], C[3][3],i,j,k;
clrscr();
printf(“Enter the elements of matrix A\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enter the elements of matrix B\n”);
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&B[i][j]);
}
}
for(i =0;i <3;i++)
{
for(j= 0;j <3;j++)
{
C[i][j]=0;
for(k= 0;k<3; k++ )
{
C[i][j]=C[i][j]+(A[i][k]*B[k][j]);
}
}
}
printf(“The result in C matrix is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,C[i][j]);
31 | P a g e
}
printf(“\n”);
}
getch();
}
7. Define string and explain how strings are declared and initialized in C?
Ans:
1.String is a group of characters enclosed within double quotation marks
2.String is also called array of characters or character arrays.
3.Character Array elements are stored in contiguous memory locations or consecutive
memory locations or successive memory locations.
4.The string should end with “\0” (null character) in C language.
5.The size of the string is equal to no.of characters in the string + 1 (required for null
character(‘\0’)).
1.strcpy()
This function is used to copy one string into another string.
Syntax: strcpy(string1,string 2);
32 | P a g e
Here, string 2 is copied into string 1 and after copying both the contents of string 1 and
string 2 are same.
Example: Write a c program to copy one string into another string using strcpy().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
strcpy(name1,name2);
printf(“After copying the name1=%s,name2=%s”,name1,name2);
getch();
}
2.strcat():-
This function is used to combine two strings.
Syntax: strcat (string1,string2);
Here, string2 is added to the end of the string1.
Example : Write a C program to combine two strings using strcat().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
strcat(name1,name2);
printf(“After concatenation the name is %s”,name1);
getch();
}
3.strrev() :-
This function is used to reverse a given string.
Syntax: strrev(string);
Example: Write a c program to find the reverse of given string using strrev().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
clrscr();
printf(“Enter any name\n”);
gets(name);
strrev(name);
printf(“The reverse of a given name is %s”,name);
33 | P a g e
getch();
}
4.strlen():-
This function is used to find the length of the given string.
This function does not include the ‘\0’(null character).
Syntax: strlen(string);
Example: Write a c program to find the length of a given string using strlen().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int x;
clrscr();
printf(“Enter any name\n”);
gets(name);
x=strlen(name);
printf(“The length of a given string is %d”,x);
getch();
}
5.strlwr():-
This function is used to convert any uppercase letters into lowercase letters.
Syntax: strlwr(string);
Example: Write a c program to convert any uppercase letters into lowercase letters using
strlwr().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name [20];
clrscr();
printf(“Enter any name\n”);
gets(name);
strlwr(name);
printf(“The converted name is %s”,name);
getch();
}
6.strupr():-
This function is used to convert any lowercase letters into uppercase letters.
Syntax: strupr(string);
Example: Write a c program to convert any lowercase letters into letters uppercase using
strupr().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name [20];
clrscr();
printf(“Enter any name\n”);
gets(name);
34 | P a g e
strupr(name);
printf(“The converted name is %s”,name);
getch();
}
7.strcmp():-
This function is used to compare two strings character by character based on ASCII values
(American Standard Code for Information Interchange) and returns zero when both the
strings are equal.
Syntax: strcmp(string1,string2);
Example1 : Write a c program to check whether the two strings are equal or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
if(strcmp(name1,name2)= =0);
printf(“Both the given strings are equal”);
else
printf(“Both the given strings are not equal”);
getch();
}
else
printf(“The given string is not a palindrome”)
getch();
}
UNIT-5
1. Define pointer and explain how the pointer variable is declared and initialized in C?
Ans:
Pointer is a variable that stores the address of another variable. C Pointer is used to
allocate the memory dynamically i.e. at run time.
Declaration of a pointer:
datatype *variable;
Example: int *p;
Here p is a pointer variable which always points to integer data type.
& is called reference operator/address operator which specifies where the value would
be stored.
Example: Write a c program to print the values and its corresponding addresses on the
monitor.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p,x=10;
clrscr();
p=&x;
printf(“the address of p is %u\n”,&p);
printf(“the address of x is%u\n”,&x);
printf(“the value of p is%u\n”,p);
printf(“the value of x is%d\n”,x);
36 | P a g e
When an array is declared, compiler allocates sufficient amount of memory to contain all
the elements of the array. The base address is the location of the first element (index 0)
of the array.
int arr[5]={ 1, 2, 3, 4, 5 };
Assuming that the base address of arr is 1000 and each integer requires two bytes, the
five elements will be stored as follows
Here variable arr will give the base address, which is a constant pointer pointing to the
element, arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.
int *p;
p = &arr[0];
Now we can access every element of array arr using p++ to move from one element to
another.
Example: Write a C program to read and print any 5 integer array elements using
pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],*p,i;
clrscr();
37 | P a g e
p=&x[0];
printf(“ Enter any five integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
printf(“ The 5 integer array elements are \n”);
for(i=0;i<5;i++)
{
printf(“%d is stored at address %u\n”,*p,p);
p++;
}
getch();
}
3. Write a C program to find the sum of all elements stored in array using pointers?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],*p,i,sum=0;
clrscr();
p=&x[0];
printf(“ Enter any 5 integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
for(i=0;i<5;i++)
{
sum=sum+*p;
p++;
}
printf(“ The sum of array elements using pointers is %d”,sum);
getch();
}
.
datatype variable n;
};
In the above syntax struct is a keyword that declares/stores the members or fields of a
structure.
tag name is name of the structure.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are structure members or fields of a structure.
The body of a structure should be end with semicolumn(;).
Example:
struct student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a structure variable:-
Syntax:-
struct tagname variable;
Example:
struct student s;
Inilization of a structure variable:-
Syntax:-
struct tagname variable={member1,member2,…member n};
Example:
struct student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a structure using the structure variable we have to use dot
operator ( . ) in c language.
Syntax:-
structurevariable.structuremember
Example Program:
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
char branch[20];
float marks;
};
void main()
{
struct student s;
clrscr();
printf(“Enter the student rno,name,branch and marks\n”);
scanf(“%d%s%s%f”,&s.rno,&s.name,&s.branch,&s.marks);
printf(“the student details are\n”);
printf(“%d\t%s\t%s\t%f”,s.rno,s.name,s.branch,s.marks);
getch();
}
39 | P a g e
In the above syntax struct is a keyword that declares/stores the members or fields of a
structure.
tag name is name of the structure.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are structure members or fields of a structure.
The body of a structure should be end with semicolumn(;).
Example:
struct student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a structure variable:-
Syntax:-
struct tagname variable;
Example:
struct student s;
Inilization of a structure variable:-
Syntax:-
struct tagname variable={member1,member2,…member n};
Example:
struct student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a structure using the structure variable we have to use dot
operator ( . ) in c language.
Syntax:-
structurevariable.structuremember
union
union is a collection of elements of different data types.
union is similar to structures except the storage allocation i.e., in structures each member
has its own storage allocation but in unions all the members use the same memory
allocation.
A union can contain any numbers of members but it can handle one member at a time i.e.,
at a time only one member can be stored in union.
Declaration of a union
union tagname
{
datatype variable1;
datatype variable2;
datatype variable3;
.
.
datatype variable n;
};
In the above syntax union is a keyword that declares/stores the members or fields of a
union.
tag name is name of the union.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are union members or fields of a union.
The body of a union should be end with semicolumn(;).
Example:
41 | P a g e
union student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a union variable:-
Syntax:-
union tagname variable;
Example:
union student s;
Inilization of a union variable:-
Syntax:-
union tagname variable={member1,member2,…member n};
Example:
union student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a union using the union variable we have to use dot
operator ( . ) in c language.
Syntax:- unionvariable.unionmember