C Interview Questions & Answers

C is considered as the mother of all programming languages. Without learning C language, one will find difficult in understanding the concepts. So, questions based on C programming are generally asked in an interview. Following are the top 42 C Interview Questions that are designed for you to crack the interview.

1) What is C programming language?

C language is a high level and procedural programming language. It is used for writing system software. It is also known as the mother of all languages.

2) When was C language developed?

It was developed in 1972 by Dennis Ritchie at Bell Laboratories of AT&T.

3) What is the use of printf() function and scanf() function in C?

The use of printf() function is for output and scanf() is used for input.

4) What is static variable in C?

Static variable is those variable which is declared as static. The default values of static variable are 0, and if the global variable is static, then its visibility in the same source code is limited.

5) What is the difference between = and == in C?

The = symbol is mainly used in mathematical operations. It is used to assign a value of given variable. On the other side, the == symbol also called as equal to or equivalent to, is a relational operator which is used to compare two values.

6) Why do we use comments in C programming?

Comments are used to provide additional information to increase the readability of the code. They are ignored by the compiler. You can use comments to create program documentation.

7) In C programming, how do you insert quotes('' or "") in output screen?

To insert the quote character as part of the output, use the format specifiers \' (for single quote), and \" (for double quote). This problem is common for beginners. Because generally, quotes are the part of printf statement.

8) What is the use of a '\0' character?

'\0' is referred to as a terminating null character. And it is used primarily to show the end of a string value.

9) What are keywords?

Keywords are reserved words. They have specific meaning in C language. They cannot be used as a variable name. Following table lists all the keywords present in C programming language-

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

10) What is sizeof() operator?

It is an unary operator. It returns the size of data in bytes.

Syntax 
sizeof(variablename)

Example
int a;
printf("%d", sizeof(a));

11) How many decision-making statements are there in C?

There are six decision making statements-

  • if statement
  • if...else statement
  • else if ladder
  • switch statement
  • conditional operator statement
  • goto statement

12) What is variable initialization and why it is important?

Variable initialization means a variable is assigned an initial value before it is used in the program. Without initialization, a variable could have a garbage value, which can lead to unpredictable outputs when used in computations or other operations.

13) What are the rules for naming a variable?

  • A variable name contains letters, underscore and numbers only. No special characters are allowed.
  • The first character of a variable name must begin with a letter or underscore from second character onwards we can digit also.
    Example of valid variables are - playerscore, _counter, counter123, etc.
    Example of invalid variables are - @123, !counter, etc.

14) How many data types are there in C language?

Date types determine the amount of space a variable will occupy in memory. There are 7 data types in C, but if we consider signed and unsigned, then they are 11 in number. The following table represents the data type present in C-

Data Type Range Size Format Specifier
signed char -128 to +128 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
signed int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to +3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf

15) What is garbage value?

When you declare a variable and no value is assigned to it. In that case, that variable contains garbage value. It is always better to design a value to a variable before using it.

16) How can we create infinite loop?

A loop that runs continuously is called infinite loop. There are three ways by which you can create infinite loop-

while(1){
	//code to be excuted
}
for(;;){
	//code to be excuted
} 
do{
	//code to be excuted
}while(1);

17) What is function in C?

The function is a block of programming statement, under one name that performs a particular task. You must declare a function before using it. There are two types of functions C programming language -

  • Libary function
  • User defined fuction

18) What is function prototype?

Function prototype is function name and arguments is together called function prototype.

return_type return_type function_name{argument(s){
	//Statements to be executed
} 
int add(int a, int b){
	return (a+b);
}

19) What are formal and actual arguments?

Arguments that are passed to a function during function call are called actual arguments. The called function access the values using corresponding formal arguments. For example-

int add(int i, int j){
	return i+j;
} 
int a=10, b=20;
add(a, b);

Here a and b are actual arguments. i and j are formal arguments.

20) What do mean by type casting?

Type casting is technique using which we can convert a variable of one data type to another data type. There are two types of typecasting-

  1. implicit type casting
  2. explicit type casting.
In implicit typecast the compiler automatically converts the type depending upon expression. For e.g. int pi = 3.14; then pi will be assigned the value 3 not 3.14. This conversion is always done from lower datatype to higher datatype.
In explicit typecasting we explicitly convert the variable's data type from one to another. For eg. int i = 5; float y = i/2; then y will have 2.0 value instead of 2.5. To get 2.5, you must use explicit typecast float y = (float) i/2;.

21) Write down an increment statement or decrement statement in C?

There are two ways to write down increment statement or decrement statement. One way is to use the increment operator ++ and decrement operator. For example, the statement "x++" means to increment the value of by 1. On the other hand, the statement "x--" means to decrement the value of by 1. The second way of writing increment statements is to use the conventional + plus sign or - minus sign. In the case of "x++," another way to write it is "x = x +1".

22) What is the difference between call by value and call by reference?

Whenever we called a function and passed something to it, we have always passed the values of variables to the called function. Such function calls are called calls by value. Whereas when we give the address instead of passing the value of a variable, this is called call by reference.

23) What do you mean by array?

An array is a group of homogeneous elements stored continuously under a single name. The size of an array can not be changed after its declaration. You can access array element by indices. Array index starts at zero.

Syntax
datatype arrayname[size];

Example
int noOfStudents[30];

(image)

24) What is the way of accessing the values stored in an array?

Elements stored in an array is accessed by indices. Suppose you declared an array noOfStudents as above. The first element is noOfStudents[0], noOfStudents[1], noOfStudents[2] and so on.

25) Why do we use void keyword in a function?

During function declaration, you have to specify the type of value that function will return. If a function does return a value, then in that case void keyword is used as a return type.

26) What is multidimensional array?

Array or array is called Multidimensional array. In C programming language, we mostly use 2D (two-dimensional) array and 3D (three-dimensional) array. Two-dimensional array is a collection of rows where each row is considered as a one dimension array.

Syntax of 2D array
data-type array_name[row][column];

27) Is it valid to access array using pointer?

Yes, it is valid to access array elements using pointer. With the help of an example we can understand this-

int a[3];
int *p=&a;
*(p+0)=12;
*(p+1)=15;
*(p+2)=9;
printf("First Element=%d\n", *a);
printf("Second Element=%d\n", *(a+1));
printf("Third Element=%d\n", *(a+2));

Here we have assigned the address of array to pointer and then assign the value to array element using * operator.

28) What is pointer in C?

Pointers are the most dominant feature of c programming language. It is not present in Python and Java. Is is a special variable that stores the address of another variable.

Syntax
data_type *pointer_variable_name;

Example
int *ptr;

The pointer must store the address of that variable which has same data type as that of a pointer.

29) What is structure in C?

To understand structure lets take real life an example, we consider a 'book' which is the collection of things such as title, author, publisher, the number of pages, date of publication, etc. As you can see, all this data is dissimilar for example author is a string whereas the number of pages is an integer. To handle such type of collections, C provides a data type called 'structure.' A structure gathers different atoms of information that comprise a given entity. So, a structure contains a number of data types grouped together. These data types may or may not be of the same type.

Syntax		
struct structure_name{
	data_type member1; 
	data_type member_2; 
	...
}

30) What is union in C?

It is a special type of structure that contains members whose data types may be same or different, but the members share the same storage area.

Syntax
union union_name{
	datatype member_1;
	datatype member_2; 
	...
}

31) What are the use of period (.) and arrow (->) operators in C?

The period operator is used to access members of structure and union. The arrow operator is used to access members of structure and union pointed to by pointer.

32) What do you mean by pointer to structure?

Pointer to structure is variable that contains the address of the structure in memory.(write eg)

33) What is segmentation fault?

Segmentation fault means we are trying to access memory location for which we are not allowed to access.

Example	 	
int *p;
printf("%d", *p);

34) What do mean by dangling pointer in C?

If a pointer is pointing to a memory location, but meanwhile that memory location gets deleted. While the pointer is still pointing to that memory location, then that pointer is called dangling pointer.

Example
int *p;
{ 
	int i = 10; 
	p =&i; 
} 
//p is now dangling pointer.

35) What is storage class in C?

It specifies the part of memory where storage is allocated for a variable and how long the storage allocation continues to accessed. There are four types of storage class - i) automatic ii) external iii) static iv) register

36) Is main() function necessary for a program?

No, we can compile the c program with main() function, but it can not be executed. If you want to compile and run c program without main() function, then use #define preprocessor directive.

#include 
#define begin main
int begin(){ 
	printf("Hello World!"); 
}

37) How to use command line arguments in a C program?

Parameters that are specified to the program during execution are called command line arguments. They are passed to main() function. Syntax - int main(int arg_count, char *args[]) Here arg_count means the number of arguments passed on the command line & args[] is a pointer array of char type that points to the arguments passed during program execution.

38) Which functions are used for dynamic memory allocation in C?

  • malloc()
  • calloc()
  • realloc()
  • free()

39) Write a program to reverse a number?

#include <stdio.h>
int main(){
    int num, rev=0, temp;
    printf("Enter a number to reverse: ");
    scanf("%d", &num);
    temp=num;
    while(temp!=0){
        rev=rev*10 + temp%10;
        temp=temp/10;
    }
    printf("Reverse of %d is %d\n", num, rev);
    return 0;
}

40) Write a program to display a factorial of a number with and without using recursion?

Without Recursion
#include 
int fact(int a);
void main(){
	int n, f;
	printf("Enter a number: ");
	scanf("%d", &n);
	f=fact(n);
	printf("Factorial of %d is %d\n", n,f);
}
int fact(int a){
	int i=1,fact=1;
	while(i<=a){
		fact=fact*i;
		i++;
	}
	return fact;
}
With Recursion
#include 
int fact(int a);
void main(){
	int n, f;
	printf("Enter a number: ");
	scanf("%d", &n);
	f=fact(n);
	printf("Factorial of %d is %d\n", n,f);
}
int fact(int a){
	if(a>=1){
	    return a*fact(a-1);
	}else{
	    return 1;
	}
}

41) Write a program that will print the following pattern?

    1
   12
  123
 1234
12345
#include <stdio.h>
int main(){
    int i,j;
    for(i=1; i<=5; i++){
	    for(j=(5-i); j>=1; j--){
		    printf(" ");
	    }
	    for(j=1;j<=i;j++){
        	printf("%d", j);
	    }
	    printf("\n");
    }
}

42) What is the general form of c program?

Preprocessor directives.
Main function heading.
{
Declarations.
Executable statements.
}