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.
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.
It was developed in 1972 by Dennis Ritchie at Bell Laboratories of AT&T.
The use of printf() function is for output and scanf() is used for input.
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.
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.
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.
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.
'\0' is referred to as a terminating null character. And it is used primarily to show the end of a string value.
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 |
It is an unary operator. It returns the size of data in bytes.
Syntax sizeof(variablename) Example int a; printf("%d", sizeof(a));
There are six decision making statements-
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.
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 |
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.
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);
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 -
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); }
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.
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-
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".
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.
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)
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.
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.
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];
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.
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.
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; ... }
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; ... }
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.
Pointer to structure is variable that contains the address of the structure in memory.(write eg)
Segmentation fault means we are trying to access memory location for which we are not allowed to access.
Example int *p; printf("%d", *p);
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.
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
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!"); }
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.
#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; }
Without Recursion #includeint 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; } }
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"); } }
Preprocessor directives. Main function heading. { Declarations. Executable statements. }