C Programming Interview Questions And Answers

Mostly Asked C programming Interview Questions and Answers

C programming Interview Questions and Answers:

Most commonly asked C programming Interview Questions and Answers are as follows-

1) What is C language?

C is a mid-level and procedural programming language. The Procedural programming language is also known as the structured programming language is a technique in which large programs are broken down into smaller modules, and each module uses structured code. This technique minimizes error and misinterpretation

2) Why is C known as a mother language?

C is known as a mother language because most of the compilers and JVMs are written in C language. Most of the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust, javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these languages.

3) Why is C called a mid-level programming language?

C is called a mid-level programming language because it binds the low level and high -level programming language. We can use C language as a System programming to develop the operating system as well as an Application programming to generate menu driven customer driven billing system.

4) Who is the founder of C language?

Dennis Ritchie.

5) When was C language developed?

C language was developed in 1972 at bell laboratories of AT&T.

6) What are the features of the C language?

The main features of C language are given below:

Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts

Portable: C is highly portable means that once the program is written can be run on any machine with little or no modifications.

Mid Level: C is a mid-level programming language as it combines the low- level language with the features of the high-level language.

Structured: C is a structured language as the C program is broken into parts.

Fast Speed: C language is very fast as it uses a powerful set of data types and operators.

Memory Management: C provides an inbuilt memory function that saves the memory and improves the efficiency of our program.

Extensible: C is an extensible language as it can adopt new features in the future.

7) What is the use of printf() and scanf() functions?

printf(): The printf() function is used to print the integer, character, float and string values on to the screen.

Following are the format specifier:

%d: It is a format specifier used to print an integer value.

%s: It is a format specifier used to print a string.

%c: It is a format specifier used to display a character value.

%f: It is a format specifier used to display a floating point value.

scanf(): The scanf() function is used to take input from the user.

8) What is the difference between the local variable and global variable in C?

Following are the differences between a local variable and global variable:

9) What is the use of a static variable in C?global and local variable difference

Following are the uses of a static variable:

A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.

Static variables are used because the scope of the static variable is available in the entire program. So, we can access a static variable anywhere in the program.

The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is assigned.

The static variable is used as a common value which is shared by all the methods.

The static variable is initialized only once in the memory heap to reduce the memory usage.

 

10) What is the use of the function in C?

Uses of C function are:

C functions are used to avoid the rewriting the same code again and again in our program.

C functions can be called any number of times from any place of our program.

When a program is divided into functions, then any part of our program can easily be tracked.

C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.

 

11) What is the difference between call by value and call by reference in C?

Following are the differences between a call by value and call by reference are:

Example of call by value:

#include <stdio.h>

void change(int,int);

int main()

{

int a=10,b=20;

change(a,b); //calling a function by passing the values of variables.

printf(“Value of a is: %d”,a);

printf(“\n”);

printf(“Value of b is: %d”,b);

return 0;

}

void change(int x,int y)

{

x=13;

y=17;

}

Output:

Value of a is: 10

Value of b is: 20

Example of call by reference:

void change(int*,int*);

int main()

{

#include <stdio.h>

int a=10,b=20;

change(&a,&b); // calling a function by passing references of variables.

printf(“Value of a is: %d”,a);

printf(“\n”);

printf(“Value of b is: %d”,b);

return 0;

}

void change(int *x,int *y)

{

*x=13;

*y=17;

}

Output:

Value of a is: 13

Value of b is: 17

12) What is recursion in C?

When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

Recursive function comes in two phases:

Winding phase

Unwinding phase

Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.

Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.

13) What is an array in C?

An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

One-dimensional array: One-dimensional array is an array that stores the elements one after the another.

Syntax: data_type array_name[size];  

Multidimensional array: Multidimensional array is an array that contains more than one array.

Syntax: data_type array_name[size];

14) What is a pointer in C?

A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.

15) What is the usage of the pointer in C?

Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character ‘\0’.

Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution of a program.

Call by Reference: The pointers are used to pass a reference of a variable to other function.

Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.

16) What is a NULL pointer in C?

A pointer that doesn’t refer to any address of value but NULL is known as a NULL pointer. When we assign a ‘0’ value to a pointer of any type, then it becomes a Null pointer.

17) What is a far pointer in C?

A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains information outside the memory in a given section.

18) What is dangling pointer in C?

If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.

Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.

19) What is pointer to pointer in C?

In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer. Let’s understand this concept through an example:

#include <stdio.h>

int main()

{

int a=10;

int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.

ptr=&a;

pptr=&ptr;

printf(“value of a is:%d”,a);

printf(“\n”);

printf(“value of *ptr is : %d”,*ptr);

printf(“\n”);

printf(“value of **pptr is : %d”,**pptr);

return 0;

}

In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the address of ‘a’ variable.

20) What is static memory allocation?

In case of static memory allocation, memory is allocated at compile time, and memory can’t be increased while executing the program. It is used in the array.

The lifetime of a variable in static memory is the lifetime of a program.

The static memory is allocated using static keyword.

The static memory is implemented using stacks or heap.

The pointer is required to access the variable present in the static memory.

The static memory is faster than dynamic memory.

In static memory, more memory space is required to store the variable.

For example:  int a[10];

The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.

21) What is dynamic memory allocation?

In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.

The malloc() or calloc() function is required to allocate the memory at the runtime.

An allocation or deallocation of memory is done at the execution time of a program.

No dynamic pointers are required to access the memory.

The dynamic memory is implemented using data segments.

Less memory space is required to store the variable.

For example  : int *p= malloc(sizeof(int)*10);

The above example allocates the memory at runtime.

22) What functions are used for dynamic memory allocation in C language?

malloc():

The malloc() function is used to allocate the memory during the execution of the program.

It does not initialize the memory but carries the garbage value.

It returns a null pointer if it could not be able to allocate the requested space.

Syntax:

ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.

calloc():

The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.

Syntax: ptr = (cast-type*)calloc(n, element-size); // allocating the memory using calloc() function.

realloc():

The realloc() function is used to reallocate the memory to the new size.

If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.

Syntax : ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.

In the above syntax, ptr is allocated to a new size.

free():The free() function releases the memory allocated by either calloc() or malloc() function.

Syntax:

free(ptr); // memory is released using free() function.

The above syntax releases the memory from a pointer variable ptr.

23) What is the difference between malloc() and calloc()?

malloc() and calloc() difference

24) What is the structure?

The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.

The structure members can be accessed only through structure variables.

Structure variables accessing the same structure but the memory allocated for each variable will be different.

Syntax of structure

struct structure_name

{

Member_variable1;

Member_variable2

.

.

}[structure variables];

Let’s see a simple example.

#include <stdio.h>

struct student

{

char name[10];       // structure members declaration.

int age;

}s1;      //structure variable

int main()

{

printf(“Enter the name”);

scanf(“%s”,s1.name);

printf(“\n”);

printf(“Enter the age”);

scanf(“%d”,&s1.age);

printf(“\n”);

printf(“Name and age of a student: %s,%d”,s1.name,s1.age);

return 0;

}

Output:

Enter the name shikha

Enter the age 26

Name and age of a student: shikha,26

25) What is a union?

The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn’t occupy the sum of the memory of all members. It holds the memory of the largest member only.

In union, we can access only one variable at a time as it allocates one common space for all the members of a union.

Syntax of union

union union_name

{

Member_variable1;

Member_variable2;

.

.

Member_variable n;

}[union variables];

Let’s see a simple example

#include<stdio.h>

union data

{

int a;      //union members declaration.

float b;

char ch;

};

int main()

{

union data d;       //union variable.

d.a=3;

d.b=5.6;

d.ch=’a’;

printf(“value of a is %d”,d.a);

printf(“\n”);

printf(“value of b is %f”,d.b);

printf(“\n”);

printf(“value of ch is %c”,d.ch);

return 0;

}

Output:

value of a is 1085485921

value of b is 5.600022

value of ch is a

In the above example, the value of a and b gets corrupted, and only variable ch shows the actual output. This is because all the members of a union share the common memory space. Hence, the variable ch whose value is currently updated.

26) What is an auto keyword in C?

In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

27) What is the purpose of sprintf() function?

The sprintf() stands for “string print.” The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

Syntax

int sprintf ( char * str, const char * format, … );

 

Let’s see a simple example

#include<stdio.h>

int main()

{

char a[20];

int n=sprintf(a,”javaToint”);

printf(“value of n is %d”,n);

return 0;}

Output:

value of n is 9

 

 

28) Can we compile a program without main() function?

Yes, we can compile, but it can’t be executed.

But, if we use #define, we can compile and run a C program without using the main() function. For example:

#include<stdio.h>

#define start main

void start() {

printf(“Hello”);

}

29) What is a token?

The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:

Identifiers: Identifiers refer to the name of the variables.

Keywords: Keywords are the predefined words that are explained by the compiler.

Constants: Constants are the fixed values that cannot be changed during the execution of a program.

Operators: An operator is a symbol that performs the particular operation.

Special characters: All the characters except alphabets and digits are treated as special characters.

30) What is command line argument?

The argument passed to the main() function while executing the program is known as command line argument. For example:

main(int count, char *args[]){

//code to  be executed

}

31) What is the acronym for ANSI?

The ANSI stands for ” American National Standard Institute.” It is an organization that maintains the broad range of disciplines including photographic film, computer languages, data encoding, mechanical parts, safety and more.

 

32) What is the difference between getch() and getche()?

The getch() function reads a single character from the keyboard. It doesn’t use any buffer, so entered data will not be displayed on the output screen.

The getche() function reads a single character from the keyword, but data is displayed on the output screen. Press Alt+f5 to see the entered character.

Let’s see a simple example

#include<stdio.h>

#include<conio.h>

int main()

{

 

char ch;

printf(“Enter a character “);

ch=getch(); // taking an user input without printing the value.

printf(“\nvalue of ch is %c”,ch);

printf(“\nEnter a character again “);

ch=getche(); // taking an user input and then displaying it on the screen.

printf(“\nvalue of ch is %c”,ch);

return 0;

}

Output:

Enter a character

value of ch is a

Enter a character again a

value of ch is a

In the above example, the value entered through a getch() function is not displayed on the screen while the value entered through a getche() function is displayed on the screen.

33) What is the newline escape sequence?

The new line escape sequence is represented by “\n”. It inserts a new line on the output screen.

34) Who is the main contributor in designing the C language after Dennis Ritchie?

Brain Kernighan.

 

35) What is the difference between near, far and huge pointers?

A virtual address is composed of the selector and offset.

near pointer doesn’t have explicit selector whereas far, and huge pointers have explicit selector. When you perform pointer arithmetic on the far pointer, the selector is not modified, but in case of a huge pointer, it can be modified.

These are the non-standard keywords and implementation specific. These are irrelevant in a modern platform

 

36) What is the maximum length of an identifier?

It is 32 characters ideally but implementation specific.

37) What is typecasting?

The typecasting is a process of converting one data type into another is known as typecasting. If we want to store the floating type value to an int type, then we will convert the data type into another data type explicitly.

Syntax

(type_name) expression;

 

38) What are the functions to open and close the file in C language?

The fopen() function is used to open file whereas fclose() is used to close file.

39) Can we access the array using a pointer in C language?

Yes, by holding the base address of array into a pointer, we can access the array using a pointer.

40) What is an infinite loop?

A loop running continuously for an indefinite number of times is called the infinite loop.

41) Write a program to print “hello world” without using a semicolon?

#include<stdio.h>

void main(){

if(printf(“hello world”)){} // It prints the ?hello world? on the screen.

}

 

42) Write a program to swap two numbers without using the third variable?

#include<stdio.h>

#include<conio.h>

main()

{

int a=10, b=20;    //declaration of variables.

clrscr();        //It clears the screen.

printf(“Before swap a=%d b=%d”,a,b);

 

a=a+b;//a=30 (10+20)

b=a-b;//b=10 (30-20)

a=a-b;//a=20 (30-10)

 

printf(“\nAfter swap a=%d b=%d”,a,b);

getch();

}

43) Write a program to print Fibonacci series without using recursion?

#include<stdio.h>

#include<conio.h>

void main()

{

int n1=0,n2=1,n3,i,number;

clrscr();

printf(“Enter the number of elements:”);

scanf(“%d”,&number);

printf(“\n%d %d”,n1,n2);//printing 0 and 1

 

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed

{

n3=n1+n2;

printf(” %d”,n3);

n1=n2;

n2=n3;

}

getch();

}

 

44) Write a program to print Fibonacci series using recursion?

#include<stdio.h>

#include<conio.h>

void printFibonacci(int n) // function to calculate the fibonacci series of a given number.

{

static int n1=0,n2=1,n3;    // declaration of static variables.

if(n>0){

n3 = n1 + n2;

n1 = n2;

n2 = n3;

printf(“%d “,n3);

printFibonacci(n-1);    //calling the function recursively.

}

}

void main(){

int n;

clrscr();

printf(“Enter the number of elements: “);

scanf(“%d”,&n);

printf(“Fibonacci Series: “);

printf(“%d %d “,0,1);

printFibonacci(n-2);//n-2 because 2 numbers are already printed

getch();

}

45) Write a program to check prime number in C Programming?

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,m=0,flag=0;    //declaration of variables.

clrscr();    //It clears the screen.

printf(“Enter the number to check prime:”);

scanf(“%d”,&n);

m=n/2;

for(i=2;i<=m;i++)

{

if(n%i==0)

{

printf(“Number is not prime”);

flag=1;

break;    //break keyword used to terminate from the loop.

}

}

if(flag==0)

printf(“Number is prime”);

getch();    //It reads a character from the keyword.

}

 

46) Write a program to check palindrome number in C Programming?

#include<stdio.h>

#include<conio.h>

main()

{

int n,r,sum=0,temp;

clrscr();

printf(“enter the number=”);

scanf(“%d”,&n);

temp=n;

while(n>0)

{

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

printf(“palindrome number “);

else

printf(“not palindrome”);

getch();

}

 

47) Write a program to print factorial of given number without using recursion?

#include<stdio.h>

#include<conio.h>

void main(){

int i,fact=1,number;

clrscr();

printf(“Enter a number: “);

scanf(“%d”,&number);

 

for(i=1;i<=number;i++){

fact=fact*i;

}

printf(“Factorial of %d is: %d”,number,fact);

getch();

}

 

48) Write a program to print factorial of given number using recursion?

#include<stdio.h>

#include<conio.h>

long factorial(int n)    // function to calculate the factorial of a given number.

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));    //calling the function recursively.

}

void main()

{

int number;    //declaration of variables.

long fact;

clrscr();

printf(“Enter a number: “);

scanf(“%d”, &number);

fact = factorial(number);    //calling a function.

printf(“Factorial of %d is %ld\n”, number, fact);

getch();   //It reads a character from the keyword.

}

 

49) Write a program to check Armstrong number in C?

#include<stdio.h>

#include<conio.h>

main()

{

int n,r,sum=0,temp;    //declaration of variables.

clrscr(); //It clears the screen.

printf(“enter the number=”);

scanf(“%d”,&n);

temp=n;

while(n>0)

{

r=n%10;

sum=sum+(r*r*r);

n=n/10;

}

if(temp==sum)

printf(“armstrong  number “);

else

printf(“not armstrong number”);

getch();  //It reads a character from the keyword.

}

 

50) Write a program to reverse a given number in C?

#include<stdio.h>

#include<conio.h>

main()

{

int n, reverse=0, rem;    //declaration of variables.

clrscr(); // It clears the screen.

printf(“Enter a number: “);

scanf(“%d”, &n);

while(n!=0)

{

rem=n%10;

reverse=reverse*10+rem;

n/=10;

}

printf(“Reversed Number: %d”,reverse);

getch();  // It reads a character from the keyword.

}

 

51) What is scope of a variable? How are variables scoped in C?
Ans: Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped.

52) What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

 

53) What is memory leak? Why it should be avoided
Ans: Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.

/* Function with memory leak */

#include <stdlib.h>

void f()

{

int* ptr = (int*)malloc(sizeof(int));

/* Do some work */

return; /* Return without freeing ptr*/

}

 

54) What are local static variables? What is their use?
Ans:A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0

 

55) What are static functions? What is their use?
Ans:In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.

56) What is keyword auto for?  Or what is auto keyword in C ?

By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.

void f() {

int i;

auto int j;

}

NOTE − A global variable can’t be an automatic variable.

57) what is the purpose of keyword typedef in C language?

It is used to alias the existing type. Also used to simplify the complex declaration of the type.

58) what is Ivalue and Rvalue?

The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is assigned to lvalue, which appears on left side of the assignment operator. The lvalue should designate to a variable not a constant.

59) What is an advantage of declaring void pointers?

When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.

60) What is preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

61) What is null Statement?

A null statement is no executable statements such as ; (semicolon).

Eg: int count = 0;

while( ++count<=10 ) ;

Above does nothing 10 times.

62) What is static functions?

A function’s definition prefixed with static keyword is called as a static function. You would make a function static if it should be called only within the same source code.

63) Define an array.

Array is collection of similar data items under a common name. There are three types of arrays, namely,

One Dimensional Array

Two Dimensional Array

Multi-Dimensional Array

64) which build-in function can be used to re-sized the allocated dynamic memory ?

Realloc()

65) What are the basic Datatypes supported in C Programming Language?

Ans: The Datatypes in C Language are broadly classified into 4 categories. They are as follows:

Basic Datatypes

Derived Datatypes

Enumerated Datatypes

Void Datatypes

66) What are static variables and functions?

Ans: The variables and functions that are declared using the keyword Static are considered as Static Variable and Static Functions. The variables declared using Static keyword will have their scope restricted to the function in which they are declared.

67) What are the valid places where the programmer can apply Break Control Statement?

Ans: Break Control statement is valid to be used inside a loop and Switch control statements.

68) Differentiate between Actual Parameters and Formal Parameters.

Ans: The Parameters which are sent from main function to the subdivided function are called as Actual Parameters and the parameters which are declared a the Subdivided function end are called as Formal Parameters.

 

69) What do you mean by a Nested Structure?

Ans: When a data member of one structure is referred by the data member of another function, then the structure is called a Nested Structure.

70)  What is /0 character?

Ans: The Symbol mentioned is called a Null Character. It is considered as the terminating character used in strings to notify the end of the string to the compiler.

71) What is the main difference between the Compiler and the Interpreter?

Ans: Compiler is used in C Language and it translates the complete code into the Machine Code in one shot. On the other hand, Interpreter is used in Java Programming Langauge and other high-end programming languages. It is designed to compile code in line by line fashion.

72) Can I use int datatype to store 32768 value?

Ans: No, Integer datatype will support the range between -32768 and 32767. Any value exceeding that will not be stored. We can either use float or long int.

73) How is a Function declared in C Language?

Ans: A function in C language is declared as follows,

return_type function_name(formal parameter list)

{

Function_Body;

}

 74) Where can we not use &(address operator in C)?

Answer: We cannot use & on constants and on a variable which is declared using the register storage class.

 

75) Differentiate between getch() and getche()

Ans: Both the functions are designed to read characters from the keyboard and the only difference is that

getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not displayed on the screen.

getche(): reads characters from the keyboard and it uses a buffer. Hence, data is displayed on the screen.

#include<stdio.h>

#include<conio.h>

int main()

{

char ch;

printf(“Please enter a character “);

ch=getch();

printf(“nYour entered character is %c”,ch);

printf(“nPlease enter another character “);

ch=getche();

printf(“nYour new character is %c”,ch);

return 0;

}

77) Explain toupper() with an example.

Ans. toupper() is a function designed to convert lowercase words/characters into upper case.

//Example

#include<stdio.h>

#include<ctype.h>

int main()

{

char c;

c=a;

printf(“%c after conversions  %c”, c, toupper(c));

c=B;

printf(“%c after conversions  %c”, c, toupper(c));

//Output:

a after conversions A

B after conversions B

78) Write a code to generate random numbers in C Language

Ans: Random numbers in C Language can be generated as follows:

#include<stdio.h>

#include<stdlib.h>

int main()

{

int a,b;

for(a=1;a<=10;a++)

{

b=rand();

printf(“%dn”,b);

}

return 0;

}

 

79) Can I create a customized Head File in C language?

Ans: It is possible to create a new header file. Create a file with function prototypes that need to be used in the program. Include the file in the ‘#include’ section in its name.

 

80) Explain Local Static Variables and what is their use?

Ans: A local static variable is a variable whose life doesn’t end with a function call where it is declared. It extends for the lifetime of the complete program. All calls to the function share the same copy of local static variables.

81) Which variable can be used to access Union data members if the Union variable is declared as a pointer variable?

Ans: Arrow Operator( -> ) can be used to access the data members of a Union if the Union Variable is declared as a pointer variable

82) Mention File operations in C Language.

Ans: Basic File Handling Techniques in C, provide the basic functionalities that user can perform against files in the system.

Function Operation
fopen() To Open a File
fclose() To Close a File
fgets() To Read a File
fprint() To Write into a File

 

83) What are the different storage class specifiers in C?
Ans: The different storage specifiers available in C Language are as follows:

auto

register

static

extern

84) Write a code to print the following pattern.

1
12
123
1234
12345

Ans: To print the above pattern, the following code can be used.

#include<stdio.h>

int main()

{

for(i=1;i<=5;1++)

{

for(j=1;j<=5;j++)

{

print(“%d”,j);

}

printf(“n”);

}

return 0;

}

85) Explain the # pragma directive.

Ans: The following points explain the Pragma Directive.

This is a preprocessor directive that can be used to turn on or off certain features.

It is of two types #pragma startup, #pragma exit and pragma warn.

#pragma startup allows us to specify functions called upon program startup.

#pragma exit allows us to specify functions called upon program exit.

#pragma warn tells the computer to suppress any warning or not.

 

86) What is Bubble Sort Algorithm? Explain with a program.

Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

 

The following code executes Bubble Sort.

int main()

{

int array[100], n, i, j, swap;

printf(“Enter number of elementsn”);

scanf(“%d”, &n);

printf(“Enter %d Numbers:n”, n);

for(i = 0; i<n; i++)

scanf(“%d”, &array[i]);

for(i = 0 ; i<n – 1; i++)

{

for(j = 0 ; j < n-i-1; j++) { if(array[j]>array[j+1])

{

swap=array[j];

array[j]=array[j+1];

array[j+1]=swap;

}

}

}

printf(“Sorted Array:n”);

for(i = 0; i < n; i++)

printf(“%dn”, array[i]);

return 0;

}

87) What is Round-robin algorithm? Write a code for Round Robin Scheduling

Ans: Round-robin Algorithm is one of the algorithms employed by process and network schedulers in computing in order to evenly distribute resources in the system.

The following code will execute Round Robin Scheduling

#include<stdio.h>

int main()

{

int i, limit, total = 0, x, counter = 0, time_quantum;

int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];

float average_wait_time, average_turnaround_time;

printf(“nEnter Total Number of Processes:t”);

scanf(“%d”, &limit);

x = limit;

for(i = 0; i<limit; i++)

{

printf(“nEnter Details of Process[%d]n”, i + 1);

printf(“Arrival Time:t”);

scanf(“%d”, &arrival_time[i]);

printf(“Burst Time:t”);

scanf(“%d”, &burst_time[i]);

temp[i] = burst_time[i];

}

printf(“nEnter Time Quantum:t”);

scanf(“%d”, &time_quantum);

printf(“nProcess IDttBurst Timet Turnaround Timet Waiting Timen”);

for(total = 0, i = 0; x != 0;)

{

if(temp[i] <= time_quantum && temp[i] > 0)

{

total = total + temp[i];

temp[i] = 0;

counter = 1;

}

else if(temp[i]>0)

{

temp[i] = temp[i] – time_quantum;

total = total + time_quantum;

}

if(temp[i] == 0 && counter == 1)

{

x–;

printf(“nProcess[%d]tt%dtt %dttt %d”, i + 1, burst_time[i], total – arrival_time[i], total – arrival_time[i] – burst_time[i]);

wait_time = wait_time + total – arrival_time[i] – burst_time[i];

turnaround_time = turnaround_time + total – arrival_time[i];

counter = 0;

}

if(i == limit – 1)

{

i = 0;

}

else if(arrival_time[i + 1] <= total)

{

i++;

}

else

{

i = 0;

}

}

 

average_wait_time = wait_time * 1.0 / limit;

average_turnaround_time = turnaround_time * 1.0 / limit;

printf(“nnAverage Waiting Time:t%f”, average_wait_time);

printf(“nAvg Turnaround Time:t%fn”, average_turnaround_time);

return 0;

}

//Output

88)  Which structure is used to link the program and the operating system?

Ans: The answer can be explained through the following points,

The structure used to link the operating system to a program is file.

The file is defined in the header file “stdio.h”(standard input/output header file).

It contains the information about the file being used, its current size and its location in memory.

It contains a character pointer that points to the character that is being opened.

Opening a file establishes a link between the program and the operating system about which file is to be accessed.

89) What are the limitations of scanf() and how can it be avoided?

Ans: The Limitations of scanf() are as follows:

scanf() cannot work with the string of characters.

It is not possible to enter a multiword string into a single variable using scanf().

To avoid this the gets( ) function is used.

It gets a string from the keyboard and is terminated when enter key is pressed.

Here the spaces and tabs are acceptable as part of the input string.

90) Differentiate between the macros and the functions.

Ans: The differences between macros and functions can be explained as follows:

Macro call replaces the templates with the expansion in a literal way.

The Macro call makes the program run faster but also increases the program size.

Macro is simple and avoids errors related to the function calls.

In a function, call control is transferred to the function along with arguments.

It makes the functions small and compact.

Passing arguments and getting back the returned value takes time and makes the program run at a slower rate.

 

91) When do we use the register keyword?

Answer: The register storage specifier, i.e., the register keyword, is used for storing a variable in the machine register. This is typically used for heavily-used variables to the likes of a loop control variable. The main intent behind using the register keyword is to speed-up the program by minimizing variable access time.

 

92) What do you understand by modular programming?

Answer: Modular approach to programming involves dividing an entire program into independent, interchangeable sub-programs, i.e., functions and modules for accomplishing the desired functionality. Each of the functions or modules involved in modular programming has everything required for executing a single aspect of the desired functionality of the entire program.

 

93) What is a far pointer in C?

Answer: A far pointer is a 32-bit pointer capable of accessing all the 16 segments, i.e., the whole residence memory of RAM. It can access information outside the computer memory in a given segment. To use the far pointer, it is required to:

Allocate the sector register to store data address in the segment, and

Store another sector register within the most recent sector

94) What are the advantages of using C language over other programming languages?

Answer: C language has the edge over the other programming language, which certainly makes it “The mother of programming language.” Some of the benefits of using C language are stated below:

Middle – Level Language: As the C language is in the midway of a high-level language and low-level language, it brings together the features of both of them. So this distinctive feature of the language makes it possible to be used for low as well as high-level programming.

Structured Level Language: C language is a structured programming language that allows a complex program to be divided into simpler programs called the functions. Thus making it quite user-friendly.

Case Sensitive Language: It is a case-sensitive language due to which the lower and the upper case letters are treated differently.

Portable Language: C language is a highly flexible language that enables it to be used for scripting system applications, which makes it a part of many well-known operating systems.

Powerful and Efficient Language: It is a user-friendly language and can effectively operate on games, graphics, enterprise applications, applications that need some calculations, etc.

95) What are some of the limitations of C language?

Answer: As everything has a finite potential, so the C language stands in no exception. The following are some of the drawbacks of C languages:

Concept of OOPs: C language prohibits the concept of OOPs as it is based on the procedural approach. (Inheritance, Polymorphism, Encapsulation, Abstraction, Data Hiding).

Run Time Checking: C language does not do the running checking which means that errors are not detected after every line of coding, but only once the complete coding is done making it inconvenient to correct the bugs

Concept of the Namespace: C language does not exhibit the property of Namespace, so there cannot be two variables with the same name in the C language program.

Lack of Exception Handling: The language doesn’t exhibit the important feature of exception handling. The feature of exception handling doesn’t allow the user to detect the errors and bugs while compiling the code.

Insufficient Level for Abstraction: C language doesn’t have a very wide data handling capacity, which poses a threat to the security of the language.

96) Explain fast speed and memory management as a unique feature of C language?

Answer:

Fast speed: The operators and data types used in C programming are very effective and powerful that increases the speed of working. Hence it helps in fastening up the speed.

Memory management: It has a very unique feature of in build memory that can handle a large amount of data very effectively and efficiently. This helps in improving the overall performance of C programming.

 

97) What do you know about logical errors and bring out the difference between syntax and logical errors.

Answer:

Logical errors: Logical errors always take place in complication process and can pass it very easily but in that case, we will not get the desired results. Such errors occurred when we pass the wrong formula into code.

Syntax error: The mistakes that occur in the programming language is called syntax error. It usually takes place when the command is misspelt by the program and because of that, it performs the wrong function. It can also take place when the command is to be given in lower case but by mistake, it is given in the upper case. Moreover, sometimes we make use of wrong symbols. All such problem and mistakes bring errors in the C programming.

98) What is volatile keyword?
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler.
Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

99) What are stack and heap areas?

Heap Area:It is used for the objects allocated dynamically (Using malloc() and calloc()).

Stack Area:It is used to store local variables and arguments of a method. This stays in memory only till the termination of that particular method.

100) What is debugging?

Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely. At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors and plays an important role in ensuring that the expected program output is met.

Related Posts:

For more Interview Questions And Answers click here

Leave a Reply

Your email address will not be published.