36-Univ Imp QA-BPOPS103-DrPNSingh
36-Univ Imp QA-BPOPS103-DrPNSingh
Documentation Section: This section is used to write Problem, file name, developer,
date etc in comment lines within /*….*/ or separate line comments may start with // .
Compiler ignores this section. Documentation enhances the readability of a program.
Link section : To include header and library files whose in-built functions are to be
used. Linker also required these files to build a program executable. Files are included
with directive # include
Definition section: To define macros and symbolic constants by preprocessor directive
#define
Global section: to declare global variables – to be accessed by all functions
main() is the user defined function which is recognized by the compiler first. So, all C
program must have user defined function main() { …………. }. It should have declaration
part first then executable part.
Sub program section: There may be other user defined functions to perform specific
task when called.
Valid Characters (alphabets, numbers or symbols) used in C program are known as C – tokens.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are the basic buildings blocks in C language which are constructed together to write
a C program.
where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, sum – identifier
main, {, }, (, ), int, x, y, sum – tokens
5. What is an identifier? Explain the rules to construct identifier in C language. Give examples of valid
and invalid variables.
Ans:
A variable is an identifier for which a value can be assigned and changed. It is name of the location
of memory where some value is stored temporarily.
Rules to construct identifier/variable:
They may contain A-Z, a-z, and _ (underscore) (no blanks)
An identifier name but must be started with an alphabet or _ (specific purposes)
Maximum length of name of identifier should be 31 characters (ANSI C standards) however in
newer versions length of identifier name may be more.
Keywords & compiler constants should not be identifier name
6. What are operators? Explain the different types of operators supported in C. Explain ternary & bit-
wise operator with suitable examples.
Ans:
List of operators used in C Language:
Ans:
i. Arithmetic Operators :
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
ii. Relational Operators:
< (less than)
<= (less than or equal to)
> (greater than)
>= (greater than or equal to)
==(equal to)
!=(not equal to)
iii. Logical Operators:
&& (and)
|| (or)
! (Not)
iv. Increment & Decrement:
++, --
v. Assignment Operator :
=, +=, -=, */, /=, %=
vi. Pre-processor Operator : #
vii. Member operator: . and ->
viii. Bitwise Operators : & (Bit-wise AND), | (Bit-wise OR), ! (Bit-wise NOT), ^ (Bit-wise XOR),
>> (Bit-wise right shift), <<(Bit-wise left shift)
ix. Ternary Operator(Conditional if): ? :
x. Address of operator: &
xi. Pointer (dereference) operator: *
xii. Comma Operator: ,
xiii. Statement terminator operator: ;
7. Explain operator precedence and associativity with examples of arithmetic operators with
relational operators.
Ans:
Operator Precedence:
If more than one operators are involved in a single expression, C language has a predefined rule
of priority for the operators. This rule of priority of operators is called Operators precedence.
Example1:
printf(“%d”, 3 % 10 + 5); /* gives the output 8 */
Explanation: Modulas operator % has greater precedence than addition +. So, 3 % 10 will be 3
and adding 5 will give the result 8
Example2:
printf(“%d”, 3 + 4>5); /* gives the output 1 and not 3 */
Explanation: Arithmetic operator has greater precedence than relational operator so, 3 will be
added with 4 first then 7 will be compared with 5 which gives the boolean output 1.
Example 3:
printf(“%d”, 1 > 2 + 3 && 4);
This expression is equivalent to:
((1 > (2 + 3)) && 4)
i.e, (2 + 3) executes first resulting into 5
then, first part of the expression (1 > 5) executes resulting into 0 (false)
then, (0 && 4) executes resulting into 0 (false)
In C, precedence of arithmetic operators( *, %, /, +, -) is higher than relational operators(==, !=,
>, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).
ii) Associativity:
If two operators of same precedence (priority) is present in an expression, Associativity of
operators indicate the order in which they execute (left to right or right to left).
Most of the operators have the asociativity left to right.
Example 1:
printf(“%d”, 10/5/2); /* output 1 – There should not be confusion that 10 is divided by 5/2 */
Example 2:
printf(“%d”, 1==2 !=3); /* output 1, 1==2 will give 0 and 0 != 3 will give 1 */
increment and decrement operators have associativity from right to left:
Example:
int n=7;
printf(“%d %d %d”,n++,n++,n++); /* gives output 9 8 7 and not 7 8 9 */
8. What are data types? Explain the different data types & their sizes supported by ANSI- C language.
Ans:
A data type is a classification of data, which can store a specific type of information.
Primitive data types are primary, fundamental or predefined types of data, which are supported
by the programming language.
In C basic/fundamental/primitive data types with their sizes as per ANSI-C standard:
Type Size
char 1 Byte
int 2 Bytes
float 4 bytes
double 8 Bytes
There are 4 data type specifiers/quantifiers also:
I.signed : Left most bit is reserved as signed bit (1 for negative, 0 for positive)
By default all declarations are signed.
II.unsigned: All bits are used( Where only positive values are expected)
III.short : Assures that size of data type will be <= normal size
IV.long : Assures that size of data type will be >= normal size
Programmers can use these data types when creating variables in their programs. For example, a
programmer may create a variable called marks and define it as a int data type type.
Examples:
int marks;
double num = 0.00;
char c;
float sum;
Derived data types are non-primitive data types or composed data types from primary data types.
Non-primitive data types are not defined by the programming language, but are instead created
by the programmer. Arrays, pointers, structures are examples of derived data type.
9. What is type conversion? Explain two types of type conversion in C with examples.
Ans:
Type conversion concept in C language is used to modify a variable from one date type to another
data type. New data type should be mentioned before the variable name or value in brackets
which to be typecast.
Example:
result = (float) 20/3;
It is best practice to convert lower data type to higher data type to avoid data loss.
Data will be truncated when higher data type is converted to lower. For example, if float is
converted to int, data which is present after decimal point will be lost
Type conversion can be done 2ways:
a) Implicit Type conversion / Coercion / Automatic type conversion
It is done by compiler automatically:
example:
printf(“%f”, 20/3.00);
(type) expression;
10. Explain conditional branching. Explain if, if-else, nested if-else and cascaded if- else with syntax and
examples.
Ans:
In C programming language branching statements are:
if, if else & switch
if/if else can be extended to:
ladder if & nested if
if statement: syntax
if (expression/condition)
{
statement1;
statement2;
...
}
Example:
#include <stdio.h>
int main()
{
int age;
printf(“Enter your age : “);
scanf(“%d”, &age);
if(age >= 25)
printf(“Celebrate valentine day\n”);
else
printf(“Just keep looking cute only.\n”);
return (0);
}
Nested if syntax:
if (expression/condition)
{
if (expression/condition)
{
statement1;
statement2;
.....
}
else
{
statement1;
statement2;
.....
}
}
else
{
if (expression/condition)
{
statement1;
statement2;
.....
}
else
{
statement1;
statement2;
.....
}
}
// Example: to find maximum in 3 numbers
#include <stdio.h>
int main()
{
int n1,n2,n3,max;
printf(“Enter 3 numbers : “);
scanf(“%d%d%d”, &n1, &n2, &n3);
if(n1>n2)
if(n1>n3)
max=n1;
else
max=n3;
else
if(n2>n3)
max=n2;
else
max=n3;
Ans:
goto statement of C unconditional control statement and it transfers the control from one place to
other followed by a label name. It may corrupt the structure of C program so, programmers avoid
to use it.
Example of working of goto:
#include <stdio.h>
int main() {
int marks;
again:
printf("Enter marks (0-100) : ");
scanf("%d",&marks);
if(marks < 0 || marks > 100)
{
printf("Invalid Marks!!!\n");
goto again; /* Control will be transferred to label again: where it is written in program*/
}
if (marks >= 40)
printf("Pass\n");
else
printf("Fail\n");
return (0);
}
Expected output:
Enter marks (0-100): 120
Invalid marks
Enter marks (0-100): 67
Pass
int main()
{
double a, b, c, d, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
d = b*b-4*a*c; /* discriminant */
if (d == 0)
{
r1 = r2 = -b/(2*a);
printf("Roots are real : root1 = root2 = %.2lf;", r1);
}
else
if (d > 0)
{
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("Roots are distinct: root1 = .2lf root2 = %.2lf\n",r1, r2);
}
else
{
real = -b/(2*a);
imag = sqrt(-d)/(2*a);
printf("Roots are imaginary:\n");
printf("root1 = %.2lf+%.2lfi and root2 = %.2lf-%.2lfi", real, imag, real, imag);
}
return (0);
}
Output:
Enter coefficients a, b and c:
Enter coefficients a, b and c: 2 4 2
Roots are real : root1 = root2 = -1.00;
}
printf("\n");
}
return (0);
}
Expected output: Plotting Pascal’s triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
13. What are looping structures? Explain with syntax, flowchart and example.
Ans:
There are 3 looping control statements in C:
while
do…while
for
do…while statement syntax: (It is a post tested loop & must executes at least once)
do
{
statement1;
statement2;
......
} while (condition);
The main difference is that do-while loop must execute once (because condition is checked
at the end.)
do…while statement syntax: (It is a post tested loop & must executes at least once)
do
{
statement1;
statement2;
......
} while (condition);
/*working example – to display squares of numbers from 1 to 20 */
#include <stdio.h>
int main()
{
int x;
x=1;
do
{
printf(“%d %d\n”, x, x*x);
x++;
}while(x<=20);
return (0);
}
int n,d,prime=1;
printf(“Enter a number : “);
scanf(“%d”, &n);
for (d=2;d<n/2;d++)
{
if (n%d==0)
prime=0;
break; /* there is no need to check further */
}
if (prime)
printf(“Yes %d is a prime number.\n”, n);
else
printf(“No, %d is not a prime number.\n”, n);
return (0);
}
The continue statement is used in loops to skip the following statements in the loop and to
continue with the next iteration (current iteration in for loop).
//Example program to enter marks (0-100) in 6 subjects for sum
#include<stdio.h>
int main()
{
int marks,sum=0, x;
for (x=1;x<=6;x++)
{
printf(“Enter marks %d : “, x)
scanf(“%d”, &marks);
if(marks <0 || marks > 100)
{
printf(“Invalid marks!!!\n”);
continue; /* again read marks for same paper */
}
sum+=marks;
}
printf(“sum of marks = %d.\n”, sum);
return (0);
}
Ans:
Output:
31
Explanation
Associativity of increment/decrement operator is right to left. First it checks value of m++ that is
1 and m is post incremented by 1 then it checks ++m and again pre-incremented 1 and m becomes
3. So, final output is 3 and 1
16. What is an array? Explain how a single dimensional and two dimensional arrays is declared and
initialized?
Ans:
An array is an identifier that refers to the collection of data items which all have the same
name. The individual data items are represented by their corresponding array elements.
Address of an element (subscript) ranges from 0 to n-1, where n is total number of elements.
17. A. For a array declared as int a[50], compute the address of a[35] if a’s base address is
1000 and word size=2
Ans:
If array is declared int a[50]; in C then elements are from 0 to 49.
Given base address = 1000
Word size = 2
Address of element number = base address + element number*size of data
So,
Address of a[35] = 1000+ 35*2 = 1070 Ans
Expected Output:
Enter string1 : Sanchika Chakraborty
Enter string2 : Ayush Thakral
Swapped strings: str1 = Ayush Thakral str2 = Sanchika Chakraborty
18. Write a C function that can be called to find the largest element of an integer array of
size n
/* function to find largest element of an integer array of size n */
int largest(int a[], int n) {
int max=a[0]; /* first alement is assumed as maximum */
int x;
for(x=1;x<n;x++)
/* if any element is > max then max will be replaced by that element */
if(a[x] > max) max = a[x];
return max;
}
19. How actual parameters are different from formal parameters? Explain
Ans:
Actual parameters are values that are passed to a function when it is invoked from calling
function.
The formal parameter list is enclosed in parenthesis in the called function. The list
contains variable names and data types of all the necessary values for the method.
Value of actual parameters replaces value of formal parameters.
Example:
#include <stdio.h>
void add2 (int x, int y) /* here x and y are formal parameters */
{
int sum = x+y;
printf("%d",sum);
}
int main () {
add2 (2,3);
/*here 2 and 3 in first call and 40 and 50 in the next call are actual parameters that will replace
value of x and y in add2 function*/
add2 (40,50);
return (0);
}
20. Write a program to demonstrate working of selection sort & bubble Sort.
Ans:
Selection Sort:
/* Code Selection Sort */
#include <stdio.h>
void selesort(int a[], int size)
{
int x,y,temp;
for(x=0;x<size-1;x++)
for(y=x+1;y<size;y++)
if(a[y] > a[y+1])
{ /* swapping adjacent element */
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
main()
{
int a[1000], tot, x;
printf("Total elements : ");
scanf("%d",&tot);
for(x=0;x<tot;x++)
{
printf("Enter element %d :", x+1);
scanf("%d",&a[x]);
}
selesort(a,tot);
printf("Sorted array:\n");
for(x=0;x<tot;x++)
printf("%5d",a[x]);
return (0);
}
Bubble Sort
/* Code Bubble Sort */
#include <stdio.h>
void bubsort(int a[], int size)
{
int x,y,temp;
for(x=0;x<size-1;x++)
for(y=0;y<size-x-1;y++)
if(a[y] > a[y+1])
{ /* swapping adjacent element */
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
}
main()
{
int a[1000], tot, x;
printf("Total elements : ");
scanf("%d",&tot);
for(x=0;x<tot;x++)
{
printf("Enter element %d :", x+1);
scanf("%d",&a[x]);
}
bubsort(a,tot);
printf("Sorted array:\n");
for(x=0;x<tot;x++)
printf("%5d",a[x]);
return (0);
}
21. Write a C program to search a key integer element in the given array of N elements using binary
search technique. Assure that numbers are sorted before search operation.
Ans:
/* Binary search in given array of N elements */
#include <stdio.h>
int main()
{
int a[1000],x, y,temp,N, first, last, mid, skey, found;
printf("Enter number of elements : ");
scanf("%d", &N);
for(x=0;x<N;x++)
{
printf("Enter element %d : ", x+1);
scanf("%d",&a[x]);
}
/* now sorting - because Binary search requires sorted elements */
for(x=0;x<N;x++)
for(y=0;y<N-x-1;y++)
if(a[y] > a[y+1])
{
temp=a[y];
a[y]=a[y+1];
a[y+1]=temp;
}
printf("The sorted array:\n");
for(x=0;x<N;x++)
printf(" %d",a[x]);
return (0);
}
Expected Ouput:
22. How are strings declared and initialized? Explain any 5 string manipulation functions with
examples.
Ans:
A string is terminated with NULL ‘\0’ character. When a string is entered by default one NULL
character is added at the end.
String declaration and initialization:
Declaration:
char str[size];
example:
char str[50];
Initialization
char name[ ]=’SINGH’;
char name[5]={‘S’,’I’,’N’,’G’,H’,’\0’};
A function is a self-contained block of statements that performs a coherent task of some kind.
Mathematical definition of a function is like that “A function f(x) returns value for the
argument x.”
Functions are classified of two types – In-built function and User/Programmer defined
functions.
A function name is followed by a pair of ( ).
A function may or may not have arguments. Arguments are written within parentheses
separating by , (comma).
An user defined function is defined by user as per task is to be performed by function. Now
a days user defined functions are also called method as a function is a method to perform a
task.
Yes, main( ) is a user defined function which is specially recognised function in C. Every program
must have a function main( ) to indicate where the program has to begin its execution.
Function prototyping : Formal declaration of a function with data type like variables, arrays prior
to its use is known as function prototyping. Without it most of the compilers displays an error
message “abcd function should have a prototype.” Called function should receive the same data
type as declared.
Function based on argument:
A function may or may not send back any value to the calling function. The data type void says
that the function will not return any value and suppresses the warning message that “Function
should return a value.” If any function returns any value, it is done through the return
statement.
return; or return (expression);
Function without parameter:
void gao()
{
printf(“Kudi Punjaban dil chura ke lay gai – Sona Sona, Dil Mera Sona);
}
int main()
{
gao(); /* calling function */
return (0);
}
A function may have arguments. Formal declaration of the data type of arguments to be passed
with function should be done also.
Arguments are passed as i. Pass/call by value ii. Pass/call by reference
/*i. Examples of call by value – Function is called by passing value. It will not effect the value of
calling function */
int area( int l, int w) { return (l*w);
int main()
{
int len, wid;
printf(“Enter length and width of rectangle : “);
scanf(“%d%d”,&len,&wid);
printf(“Area of rectangle = %d square unit\n”, area(len,wid));
return (0);
}
In the called function arguments are received as parameters declaring their type:
int mul( int a, int b)
{
......
}
or int mul(a,b)
int a,b; /* T & R classical style */
{ ...... }
24. What is call by value and call by reference? Explain with example (from 5th unit but related to
functions).
Ans:
A function may have arguments. Formal declaration of the data type of arguments to be passed
with function should be done also.
Arguments are passed as i. Pass/call by value ii. Pass/call by reference
/*i. Examples of call by value – Function is called by passing value. It will not effect the value of
calling function */
int area( int l, int w) { return (l*w);
int main()
{
int len, wid;
printf(“Enter length and width of rectangle : “);
scanf(“%d%d”,&len,&wid);
printf(“Area of rectangle = %d square unit\n”, area(len,wid));
return (0);
}
In the called function arguments are received as parameters declaring their type:
int mul( int a, int b)
{
......
}
or int mul(a,b)
int a,b; /* T & R classical style */
{ ...... }
25. What is recursion? Write a function to check if a given number is prime or not, using recursion and
without using recursion.
Ans:
Recursion is a process where a function calls itself directly or indirectly. The simplest example of
recursion is finding factorial of a number. The corresponding function is called as recursive function.
factorial of n = n * factorial of (n-1)
Using recursive algorithm, certain problems can be solved quite easily.
Using Recursion:
#include <stdio.h>
#include <math.h>
int checkprime(int n,int d)
{
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
if(checkprime(n,2))
printf("Yes %d is prime",n);
else
printf("No, %d is not prime\n",n);
printf("\n");
return (0);
}
int main()
{
int n;
printf("Enter any number for factorial : ");
scanf("%d",&n);
printf("Factorial of % d = %ld\n",n,fact(n));
return (0);
}
O/P:
Enter any number for factorial : 6
Factorial of 6 = 720
b. Fibonacci series
Ans:
/* Fibonacci series up to nth terms using recursion */
#include <stdio.h>
int fibo(int n)
{
if (n==1)
return (0);
if (n==2)
return (1);
else
return (fibo(n-1)+fibo(n-2));
}
int main()
{
int n,x;
printf("Enter nth term for Fibonacci series : ");
scanf("%d",&n);
for(x=1;x<=n;x++)
printf(" %d",fibo(x));
return (0);
}
Output:
Enter nth term for Fibonacci series : 10
0 1 1 2 3 5 8 13 21 34
Enter nth term for Fibonacci series : 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
27. What is structure? How is it declared, defined and used? Write a C program to store and print
Name, USN, Subject and IA Marks of 60 students.
Ans:
Structure is a data structure whose individual elements can differ in type. It may contain integer
elements, character elements, pointers, arrays and even other structures can also be included as
elements within a structure. struct is keyword to define a structure.
struct tag { type member1;
type member2;
type member3; ...... type member n;};
i) Array of structure:
Whole structure can be an element of the array. A student structure with members roll, name and
marks:
struct student
{
int roll;
char name[30];
int marks;
};
Now we can use this template to create array of 60 students for their individual roll, name and
marks.
struct student s[60]; /* array of structure *
to access roll of student x : s[x].roll
ii ) Array within structure:
A member of structure may be array. In above example name is also array of characters. We
can create a structure of student with members roll, name & marks (array of integers) for 6
papers:
struct student
{
int roll;
char name[30];
int marks[6]; /* array within structure */
}s[60];
To access the marks of student x in paper y: s[x].marks[y]
struct employee
{
int eno;
char name[30];
long int salary;
struct date /*structure within structure */
{
int dd, mm, yy;
}dob,doj;
}e[1000];
Or structure variable of date structure can be defined within employee if date structure is
separately defined:
struct date
{
int dd, mm, yy;
};
struct employee
{
int eno;
char name[30];
long int salary;
struct date dob, doj; /* variable of date structure within employee*/
}e[1000];
To access day(dd) of date structure we will use member operator (.) dot operator in following
way:
e[x].dob.dd
28. Write a program to input & sort array of structure of 60 students with fields roll, name and marks.
Ans:
/*sorting array of structure - based on marks in descending order*/
#include <stdio.h>
struct student
{
int roll;
char name[20];
int marks;
}s[1000]; /* array of structure */
int main()
{
int n,x,y;
struct student temp;
printf("How many students : ");
scanf("%d",&n);
for(x=0;x<n;x++)
{
printf("Enter roll, name and marks for student %d : ", x+1);
scanf("%d%s%d",&s[x].roll,s[x].name,&s[x].marks);
}
/*now sorting */
for(x=0;x<n-1;x++)
for(y=0;y<n-x-1;y++)
if(s[y].marks < s[y+1].marks)
{
temp=s[y];
s[y]=s[y+1];
s[y+1]=temp;
}
printf("roll name marks:\n");
for(x=0;x<n;x++)
printf("%5d%20s%5d\n",s[x].roll,s[x].name,s[x].marks);
return (0);
}
29. Define pointer variables. Explain with an example declaration and initialization of pointer variable.
Ans:
A pointer keeps address of data. A pointer is variable that represents the location ( rather than the
value) of a data item, such as a variable or an array element.
Pointers is an important feature of C and frequently used in C. They have a number of useful
applications.
Pointer operator is asterisk ( * ).
To declare a pointer: datatype *pointervariable;
To initialize: pointervariable = &data;
& is address of operator.
Example1:
int n;
int *p; /* here p is pointer variable & can be used to keep address */
p=&n; /* here p keeps address of n */
Example2:
int a[5] = { 10,20.30,40,50};
int *p;
p = a; /* It is equivalent to p = &a[0]; Here it keeps address of first element */
30. Distinguish between the following types of variables/storage class with examples:
a. auto (automatic)
b. extern (global)
c. static
d. register
Ans:
The storage class specifies the portion of the program within which the variables are recognized.
variables can be categorized by storage class as well as data type. Storage class refers to the
permanence of a variable and its scope within the program, that is, the portion of the program
over which the variable is recognized.
i. auto (Automatic variables): They are local variables, available to the function in which it is
declared. Their scope is confined to that function only. Value of the variable defined within a
function is no way available to other function. Variables declared within a function are by default
of auto storage class.
int man()
{
auto int x,y,z;
int num; /* num is also interpreted as auto storage class
/* ….. other code */
}
ii. extern (External/Global variables): They are global variables, known to all the functions of the
program from point of definition. Hence, they usually span more than one functions, and often
an entire program. Any of the function can change/alter/corrupt extern variables. Variables
defined outside of the program are by default of extern storage class.
iii. static (Static variable): Static variables have the same scope as automatic variables, i.e. they
are local to the functions in which they are defined. Unlike automatic variables, however, static
variables retain their values throughout the life of the program. As a result, if a function is called
again, the static variables defined in the called function will retain their former values (They will
not be reinitialized again. They are confined at the first initialization).
#include <stdio.h>
int func()
{
static int k = 0; /* by default also zero */
k++;
return (k);
}
int main()
{
int x, sum=0;
for(x=1;x<=5;x++)
sum+=func();
print(“sum = %d\n”, sum);
return (0);
}
iv. register (Register variables) : Registers are special storage area within a computer’s central
processing unit (In-built memory with CPU). The actual arithmetic and logical operations that
comprise a program are carried out within this registers. The execution time can be reduced
considerably if certain values can be stored within these registers rather than in computer’s
memory.
The register variables are local to the function in which they are declared. The address of
operator cannot ‘&’ cannot be applied to register variables.
register int x, y, z;
31. Give the general syntax to initialize a structure to store a Book information
Ans:
Structure is collection of different data types which represents a record.
Syntax:
Struct tag_template { type1 var; type2 var;….)structvar1, structvar2;
32. Give the syntax for declaring and initializing a union with a suitable example
Ans:
The important difference between structures and unions is that in structures each member has it's
separate memory locations but for union is declared the compiler allocates only one memory
sufficient to hold the largest member of the union. Members of the union can be accessed one by
one only and only one value will be there.
Syntax is similar to structure
union tagname
{
data_type member_1;
data_type member_2;
data_type member_3;
...
data_type member_N;
};
33. What are enumeration variables? How are they declared? What are the advantages of using them
in a program?
Ans:
enum is a keyword of C language which is used to define user defined data types in C. Enumeration
(enum) is mainly used to assign names to integral constants, the names make a program easy to
read and maintain. As enumeration means listing, user can select the value of enum type
variable from listed values.
Syntax:
enum enumtype;
Example:
#include<stdio.h>
enum weekday{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum weekday holiday;
holiday = Sun;
printf("%d",holiday);
return 0;
}
Output: 6 /* By default it is counted from 0 */
34. What is an ASCII or text file? Write a C program to display the contents of a text file.
Ans:
⚫ A computer file is stream of data for input and output stream both.
⚫ A file refers to a source in which a program stores the information/data in the form of bytes of
sequence on a disk (permanent storage device).
⚫ We can do text file (ASCII file) operations in C: Create, display, copy, append contents, overwrite
contents
35. Write a C program to copy contents of a source file to target file given from user.
Ans:
36. Explain:
a. File opening modes
b. Any 5 file handling functions
Ans:
a.
B. File handling function (Any 5 can be written) fopen(), fclose(), fgetc(), fputc(), fprintf(), fscanf(),
feof(), fseek(), tell(),rewind()
*********** O’Lord I am with you, unto the end of the world. ***************