0% found this document useful (0 votes)
2 views

Important questions with answers-1

The document provides an overview of various programming concepts in C, including call by value vs. call by reference, storage classes, recursive functions, array declarations and initializations, string manipulation functions, pointers, structures vs. unions, enumeration variables, and file handling functions. Each concept is explained with examples and syntax. Additionally, it covers methods to detect the end of a file in C programming.

Uploaded by

kumarshrikhande5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Important questions with answers-1

The document provides an overview of various programming concepts in C, including call by value vs. call by reference, storage classes, recursive functions, array declarations and initializations, string manipulation functions, pointers, structures vs. unions, enumeration variables, and file handling functions. Each concept is explained with examples and syntax. Additionally, it covers methods to detect the end of a file in C programming.

Uploaded by

kumarshrikhande5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

IMPORTANT QUESTIONS WITH ANSWERS

1. Differentiate between call by value and call by reference using suitable example.

In Call by value method, the values of variables are passed by the calling function to the called function.
In Call by reference method, the address of variables is passed by the calling function to the called function.
Ex: Program to swap the value of 2 variables using call by value & call by reference methods:
#include<stdio.h>
void swap_call_by_value(int a, int b);
void swap_call_by_ref(int *c, int *d);
int main()
{
int a=1, b=2, c=3, d=4;
printf(“\n In main, a=%d & b=%d”,a,b);
swap_call_by_value(a, b);
printf(“\n In main, a=%d & b=%d”,a,b);
printf(“\n In main, c=%d & d=%d”,c,d);
swap_call_by_ref(&c, &d);
printf(“\n In main, c=%d & d=%d”,c,d);
return 0;
}
void swap_call_by_value(int a, int b);
{
int temp;
temp=a;
a=b;
b=temp;
printf(“\n In function, a=%d & b=%d”,a,b);
}
void swap_call_by_ref(int *c, int *d);
{
int temp;
temp=*c;
*c=*d;
*d=temp;
printf(“\n In function, c=%d & d=%d”,c,d);
}
Output:
In main, a=1 & b=2
In function, a=2 & b=1
In main, a=1 & b=2
In main, c=3 & d=4
In function, c=4 & d=3
In main, c=4 & d=3

2. Distinguish between the various storage classes.

Storage class
Feature
auto extern register static
Accessability within the within all within the Local: within the function
function or block program files function or block or block in which it is
in which it is that are a part in which it is declared
declared of a program declared Global: within the
program in which it is
declared
Storage Main memory Main memory CPU register Main memory
Existence when the function throughout the when the function Local: retains the value
or block in which execution of or block in which between function calls or
it is declared is the program it is declared is block entries
entered and entered and Global: preserves the
ceases to exist ceases to exist value in the program files
outside outside
Default value Garbage Zero Garbage Zero

3. Illustrate the concept of recursive function with example.


A recursive function is defined as a function that calls itself to solve a smaller version of its task until a final
call is made which does not require a call to itself.
Ex: Program to calculate factorial of a number:
#include<stdio.h>
int Fact(int n);
int main()
{
int num, factorial;
printf(“\n Enter the number: ”);
scanf(“%d”, &n);
factorial=Fact(n);
printf(“\n Factorial of %d = %d”,num,factorial);
return 0;
}
int Fact(int n)
{
if(n==1)
return 1;
else
return (n*Fact(n-1));
}

4. Explain the declaration and initialization of one dimensional and two dimensional arrays with an
example.
One dimensional array is declared by specifying the (i) data type of the values it can store (ii) name to
identify the array, and (iii) size i.e, maximum number of values it can hold.
Syntax: type name[size];
Ex: int marks[5];
1st element 2nd element 3rd element 4th element 5th element
marks[0] marks[1] marks[2] marks[3] marks[4]

Initialization of one dimensional array: there are 3 ways to intialize i.e.,storing the values in an array:
(i) during declaration of the array :
type array_name[size]={list of values};
Examples: int marks[5]={90,82,78,95,88}; OR int marks[]={90,82,78,95,88};
(ii) input values for the elements from the key board:
Ex: int i, marks[5];
for(i=0;i<5;i++)
scanf(“%d”, &marks[i]);
(iii) asssign values to the individual elements.
Ex: marks[0]=90; marks[1]=82; etc.
Two dimensional array is declared as:
Syntax: data_type array_name[row_size] [column_size]
Ex: int marks[2][3];
1st element 2nd element 3rd element 4th element 5th element 6th element
marks[0][0] marks[0][1] marks[0][2] marks[1][0] marks[1][1] marks[1][2]
A two-dimensional m×n array contains m×n data elements and each element is accessed using teo subscripts
i and j where i<m and j<n.

Initialization of two dimensional array: the values are initialized i.e., stored in the two dimentional array
as follows - Ex: consider to store 2 students marks in 3 different subjects
(i) during declaration of the array :
int marks[2][3]={90,87,78,68,62,71};
OR int marks[2][3]={{98,87,78},{68,62,71};
(ii) input values for the elements from the key board:
Ex: int i, marks[2][3];
for(i=0;i<2;i++)
for(j=0;i<3;j++)
scanf(“%d”,&marks[i][j]);
(iii) asssign values to the individual elements.
Ex: marks[0][0]=90;

5. Define String. Explain any 2 string manipulation function with suitable example.
A string is a null terminated character array.
The general form of declaring string is as follows:
char str[size];
Ex: char str[10]=”HELLO”;

H E L L O \0 \0 \0 \0 \0

Beginning of string End of string End of character array

String manipulation functions:


(i) strcat(str1, str2): This library function concatenate the string str2 to string str1.
Ex: #include<stdio.h>
#include<string.h>
int main()
{
char str1[]=”Programm”;
char str2[]=”ing”;
strcat(str1,str2);
printf(“str1: %s”, str1);
return 0;
}
output: Programming

(ii) strcmp(str1, str2): This function compares the string pointed by str1 to the string pointed by str2. The
function returns zero if the strings are equal otherwise it returns a value less or greater than zero.
Ex: #include<stdio.h>
#include<string.h>
int main()
{
char str1[]=”Programm”;
char str2[]=”ing”;
if(strcmp(str1,str2)==0)
printf(“The two strings are identical”);
else
printf(“The two strings are not identical”);
return 0;
}
output: The two strings are not identical

6. What is pointer? Differentiate between a null pointer and a void pointer.

A pointer is a variable which stores the memory address of another variable.


Syntax: data_type *ptr_name;

Examples: int *num;


char *pch;
float *pfnum; etc.
NULL pointer: It is a special pointer that doesn’t point to any valid memory address. It is a predefined
constant defined in several standard header files. In the program its used as follows
int *ptr=NULL;
if (ptr==NULL)
{
Statement Block;
}
which is equivalent to: int ptr;
ptr=0;

Generic or void pointer: It is a pointer variable that has void as its data type. It is a special type of pointer
that can be used to point to variables of any data type.
Syntax: void *ptr;
Ex:
#include<stdio.h>
int main()
{
int x=10;
char ch=’A’;
void *gp;
gp=&x;
printf(“Generic pointer to the integer value = %d”, *(int*)gp);
gp=&ch;
printf(“\n Generic pointer to the character = %c”, *(char*)gp);
return 0;
}

output: Generic pointer to the integer value = 10


Generic pointer to the character = A

7. Differentiate structures and unions with syntax and example.


A structure in C programming is a user defined data type that groups variables of different or same data
type under one name to store related information.
Syntax:
struct struct_name
{
data_type var_name1;
data_type var_name2;
-----------------------
};
Ex:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
}stud1, stud2={02,”Rahul”,”BCA”,4500};

Example shows the under structure student, 2 variables are declared i.e., stud1 and stud2, and also stud2 is
initialized with values for individual data members.
It can also be done for stud1like
struct student stud1={01,”Raju”, “BCA”,4500};
and structure variable declaration can be done also like
struct student stud3;
A structure members are accessed using a ‘.’ (dot) operator.
Syntax: struct_var.member_name;
To assign values: stud1.name=”Rahul”
stud1.fees=45000;
To input the value: scanf(“%d”, &stud1.r_no);
scanf(“%s”, &stud1.name;
To input the value: printf(“%d”, &stud1.r_no);
printf(“%s”, &stud1.name);
Size of the structure is the total size of all the member variables. In the above example of structure student
total size is given by,
size=2+1×20+1×20+4=46bytes

A union in C programming is a user defined data type that groups variables of different or same type under
one name but only one variable can store data at the given point in time.
Unions are used to save memory and are useful for applications that involve multiple members, where value
need not be assigned to all the members at any one time.
Syntax:
union union_name
{
data_type var_name1;
data_type var_name2;
-----------------------
};
Union similar to structure except it can store only one-member variable at given point of time therefore the
size of union is the size of largest field of member variable.
Ex:
#include<stdio.h>
struct POINT1
{
int x,y;
};
union POINT2
{
int x:
int y;
};
int main()
{
struct POINT1 p1={2,3};
union POINT2 p2; // union POINT2 p2={4, 5}; is illegal with union.
p2.x=4;
p2.y=5;
printf(“The coordinates of p1 are %d and %d”,p1.x,p1.y);
printf(“The coordinates of p2 are %d and %d”,p2.x,p2.y);
return 0;
}

output: The coordinates of p1 are 2 and 3


The coordinates of p2 are 5 and 5

8. What are enumeration variable? How are they declared.

An enumeration variables are declared same as other variables. They belong to user defined data type called
enumerated data type which consists set of named integer constants known as enumeration constants.

Syntax of enumerated data type:


enum enumeration_name{identifier1, identifier1, identifier1, …….. identifier};
Ex: enum COLORS{RED, BLUE, BLACK, GREEN, YELLOW, PURPLE, WHITE};
If the values of the constants are not initialized then they are initialized as RED=0, BLUE=1,
BLACK=2…and so on.
If explicitly assigned like
enum COLORS{RED=2, BLUE, BLACK=5, GREEN, YELLOW=7, PURPLE, WHITE};
as a result, now RED=2, BLUE=3, BLACK=5, GREEN=6, … and so on

Syntax to declare enumerated variable:


enumeration_name variable_name;
Ex: enum COLORS bg_color;
OR
enum COLORS{RED,BLUE,BLACK,GREEN,YELLOW,PURPLE,WHITE}bg_color, fore_color;
we can assign values to enumerated variables as:
enum COLORS bg_color, border_color;
bg_color=BLACK:
border_color=bg_color

9. List the functions for reading data from file and writing data to files. Explain any one from each.
Functions for reading data from file:
fscanf()
fgets()
fgetc()
fread()
Functions for writing data to files:
fprintf()
fputs()
fputc()
fwrite()
fgets(): the function stands for “file get string”. It is used to get a string from a stream.
Syntax: char *fgets(char *str, int size, FILE *stream);
Reads at most one less than the number of characters specified by size and terminates as soon as it encounters
either a new line character, EOF, or any other error.
#include<stdio.h>
int main()
{
FILE *fp;
char str[80];
fp=fopen(“ABC.DAT”, “r”);
if(fp==NULL)
{
printf(“The file could not be opened”);
exit(1);
}
while(fgets(str, 80, fp)!=NULL) // reads 79 characters during each iteration
printf(“\n %s”, str);
printf(“\n File read. Now closing the file”);
fclose(fp);
return 0;
}
Output: ABCDEFGHIJKL……
File read. Now closing the file
fputs(): the function stands for “file put string”. It is used to write a line to a file.
Syntax: int fputs(const char *str, FILE *stream);
The fputs() function writes the string pointed to by str to the stream pointed to by stream.
On successful, it returns 0, and in case of any error it returns EOF.
#include<stdio.h>
int main()
{
FILE *fp;
char feedback[100];
fp=fopen(“comments.TXT”, “w”)
if(fp==NULL)
{
printf(“The file could not be opened”);
exit(1);
}
printf(“\n provide feedback on this book: ”);
gets(feedback);
fflush(stdin);
fputs(feedback,fp);
fclose(fp);
return 0;
}
Output: provide feedback on this book: good

10. How to detect END-OF-FILE.


There are two ways to detect EOF:
(i) while reading the file in text mode character by character, the programmer can compare the character that
has been read with EOF which is a symbolic constant defined in stdio.h with a value -1.
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
printf(“%c”, c);
}
(ii) The other way is to use the standard library function feof() which is defined in stdio.h.
Prototype:
int feof(FILE *fp);
The function takes a pointer to the FILE structure of the stream to check as an argument returns zero(false)
when the end of the file has not been reached and a one(true) if the end of file has been reached.
Ex: Assume student.DAT contains: 1 Adithya 2 Chaithanya 3 Goransh
#include<stdio.h>
int main()
{
FILE *fp;
char str[80];
fp=fopen(“student.DAT”, “r”);
if(fp==NULL)
{
printf(“The file could not be opened”);
exit(1);
}
while(!feof(fp))
{
fgets(str,79,fp); // Reads 79 bytes at a time
printf(“\n %s”, str);
}
printf(“\n File read. Now closing the file”);
fclose(fp);
return 0;
}
Output: 1 Adithya 2 Chaithanya 3 Goransh

You might also like