Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Monday, March 12, 2018

C Programming Interview Questions


A list of top 50 frequently asked C programming interview questions and answers are given below.

1) What is C language?

C is a mid level and procedural programming language. More details...

2) Why C is known as a mother language?

C is known as a mother language because most of the compilers, kernals and JVMs are written in C language. More details...

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

It supports the feature of both low-level and high level languages that is why it is known as a mid level programming language. More details...

4) Who is the founder of C language?

Dennis Ritchie. More details...

5) When C langauge was developed?

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

6) What are the features of C language?

The main features of C language are given below:
  • Simple
  • Portable
  • Mid Level
  • Structured
  • Fast Speed
  • Memory Management
  • Extensible
More details...

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

The printf() function is used for output and scanf() function is used for input.
More details...

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

Local variable: A variable which is declared inside function or block is known as local variable.
Global variable: A variable which is declared outside function or block is known as global variable.
  1. int value=50;//global variable  
  2. void function1(){  
  3. int x=20;//local variable  
  4. }  
More details...

9) What is the use of static variable in C?

A variable which is declared as static is known as static variable. The static variable retains its value between multiple function calls.
  1. void function1(){  
  2. int x=10;//local variable  
  3. static int y=10;//static variable  
  4. x=x+1;  
  5. y=y+1;  
  6. printf("%d\n",x);//will always print 11  
  7. printf("%d\n",y);//will always increment value, it will print 11, 12, 13 and so on  
  8. }  
More details...

10) What is the use of function in C?

A function in C language provides modularity. It can be called many times. It saves code and we can reuse the same code many times. More details...

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

We can pass value to function by one of the two ways: call by value or call by reference. In case of call by value, a copy of value is passed to the function, so original value is not modified. But in case of call by reference, an address of value of passed to the function, so original value is modified. More details...

12) What is recursion in C?

Calling the same function, inside function is known as recursion. For example:
  1. void function1(){  
  2. function1();//calling same function  
  3. }  

13) What is array in C?

Array is a group of similar types of elements. It has contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. More details...

14) What is 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. More details...

15) What are the usage of pointer in C?

  • Accessing array elements
  • Dynamic memory allocation
  • Call by Reference
  • Data Structures like tree, graph, linked list etc.

16) What is NULL pointer in C?

A pointer that doesn't refer to any address of a value but NULL, is known as NULL pointer. For example:
  1. int *p=NULL;  

17) What is far pointer in C?

A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer.

18) What is dangling pointer in C?

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

19) What is pointer to pointer in C?

In case of pointer to pointer concept, one pointer refers to the address of another pointer. More details...

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 array. More details...

21) What is dynamic memory allocation?

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

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

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

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

malloc(): The malloc() function allocates single block of requested memory. It has garbage value initially.
calloc(): The calloc() function allocates multiple block of requested memory. It initially initializes all bytes to zero.

24) What is structure?

Structure is a user-defined data type that allows to store multiple types of data in a single unit. It occupies the sum of memory of all members. More details...

25) What is union?

Like Structure, union is a user-defined data type that allows to store multiple types of data in a single unit. But it doesn't occupies the sum of memory of all members. It occupies the memory of largest member only. More details...

26) What is auto keyword in C?

In C, every local variable of a function is known as automatic (auto) variable. Let's explain with an example:
  1. void f()  
  2. {  
  3. int i ;  
  4. auto int j;  
  5. }  
Here, both 'i' and 'j' variables are automatic variables.

Note: A global variable can't be an automatic variable.


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

It is used to print the formatted output into char array.

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 C program without using main() function. For example:
  1. #include<stdio.h>    
  2. #define start main    
  3. void start() {    
  4.    printf("Hello");    
  5. }    

29) What is token?

Token is an identifier. It can be constant, keyword, string literal etc.

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:
  1. main(int count, char *args[]){  
  2. //code to  be executed  
  3. }  
More details...

31) What is the acronym for ANSI?

American National Standard Institute.

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

The getch() function reads a single character from keyboard. It doesn't uses any buffer, so entered data is not displayed on the output screen.
The getche() function reads a single character from keyword but data is displayed on the output screen. Press Alt+f5 to see the entered character.

33) What is new line escape sequence?

The new line escape sequence is represented by "\n". It inserts a new line on the output screen.
More details...

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 selector and offset.
near pointer doesn't have explicit selector whereas far and huge pointers have explicit selector. When you perform pointer arithmetic on far pointer, selector is not modified but in case of huge pointer it can be modified.
These are the non-standard keywords and implementation specific. These are irrelevant in modern platform.

36) What is the maximum length of an identifier?

It is 32 characters ideally but implementation specific.

37) What is typecasting?

Converting one data type into another is known as typecasting. For example:
  1. float f=3.4;  
  2. int a=(int)f;//typecasting  

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

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

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

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

40) What is infinite loop?

A loop running continuously for indefinite number of times is called infinite loop.
Infinite For Loop:
  1. for(;;){  
  2. //code to be executed  
  3. }  
Infinite While Loop:
  1. while(1){  
  2. //code to be executed  
  3. }  
Infinite Do-While Loop:
  1. do{  
  2. //code to be executed  
  3. }while(1);  

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

There are various ways to do so. Let's see a program to print "hello world" using if.
  1. #include<stdio.h>    
  2. void main(){    
  3.    if(printf("hello world")){}    
  4. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. main()    
  4. {    
  5. int a=10, b=20;      
  6. clrscr();      
  7. printf("Before swap a=%d b=%d",a,b);      
  8.     
  9. a=a+b;//a=30 (10+20)    
  10. b=a-b;//b=10 (30-20)    
  11. a=a-b;//a=20 (30-10)    
  12.     
  13. printf("\nAfter swap a=%d b=%d",a,b);    
  14. getch();    
  15. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. void main()    
  4. {    
  5.  int n1=0,n2=1,n3,i,number;    
  6.  clrscr();    
  7.  printf("Enter the number of elements:");    
  8.  scanf("%d",&number);    
  9.  printf("\n%d %d",n1,n2);//printing 0 and 1    
  10.     
  11.  for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
  12.  {    
  13.   n3=n1+n2;    
  14.   printf(" %d",n3);    
  15.   n1=n2;    
  16.   n2=n3;    
  17.  }    
  18. getch();    
  19. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. void printFibonacci(int n){    
  4.     static int n1=0,n2=1,n3;    
  5.     if(n>0){    
  6.          n3 = n1 + n2;    
  7.          n1 = n2;    
  8.          n2 = n3;    
  9.          printf("%d ",n3);    
  10.          printFibonacci(n-1);    
  11.     }    
  12. }    
  13. void main(){    
  14.     int n;    
  15.     clrscr();    
  16.     printf("Enter the number of elements: ");    
  17.     scanf("%d",&n);    
  18.     
  19.     printf("Fibonacci Series: ");    
  20.     printf("%d %d ",0,1);    
  21.     printFibonacci(n-2);//n-2 because 2 numbers are already printed    
  22.     
  23.     getch();    
  24. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. void main()    
  4. {    
  5. int n,i,m=0,flag=0;    
  6. clrscr();    
  7. printf("Enter the number to check prime:");    
  8. scanf("%d",&n);    
  9. m=n/2;    
  10. for(i=2;i<=m;i++)    
  11. {    
  12. if(n%i==0)    
  13. {    
  14. printf("Number is not prime");    
  15. flag=1;    
  16. break;    
  17. }    
  18. }    
  19. if(flag==0)    
  20. printf("Number is prime");    
  21. getch();    
  22. }     
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. main()    
  4. {    
  5. int n,r,sum=0,temp;    
  6. clrscr();    
  7. printf("enter the number=");    
  8. scanf("%d",&n);    
  9. temp=n;    
  10. while(n>0)    
  11. {    
  12. r=n%10;    
  13. sum=(sum*10)+r;    
  14. n=n/10;    
  15. }    
  16. if(temp==sum)    
  17. printf("palindrome number ");    
  18. else    
  19. printf("not palindrome");    
  20. getch();    
  21. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. void main(){    
  4.   int i,fact=1,number;    
  5.   clrscr();    
  6.   printf("Enter a number: ");    
  7.   scanf("%d",&number);    
  8.     
  9.   for(i=1;i<=number;i++){    
  10.       fact=fact*i;    
  11.   }    
  12.   printf("Factorial of %d is: %d",number,fact);    
  13.   getch();    
  14. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3.     
  4. long factorial(int n)    
  5. {    
  6.   if (n == 0)    
  7.     return 1;    
  8.   else    
  9.     return(n * factorial(n-1));    
  10. }    
  11.      
  12. void main()    
  13. {    
  14.   int number;    
  15.   long fact;    
  16.   clrscr();    
  17.   printf("Enter a number: ");    
  18.   scanf("%d", &number);     
  19.      
  20.   fact = factorial(number);    
  21.   printf("Factorial of %d is %ld\n", number, fact);    
  22.   getch();    
  23. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. main()    
  4. {    
  5. int n,r,sum=0,temp;    
  6. clrscr();    
  7. printf("enter the number=");    
  8. scanf("%d",&n);    
  9. temp=n;    
  10. while(n>0)    
  11. {    
  12. r=n%10;    
  13. sum=sum+(r*r*r);    
  14. n=n/10;    
  15. }    
  16. if(temp==sum)    
  17. printf("armstrong  number ");    
  18. else    
  19. printf("not armstrong number");    
  20. getch();    
  21. }    
More details...

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

  1. #include<stdio.h>    
  2. #include<conio.h>    
  3. main()    
  4. {    
  5. int n, reverse=0, rem;    
  6. clrscr();    
  7. printf("Enter a number: ");    
  8.   scanf("%d", &n);    
  9.   while(n!=0)    
  10.   {    
  11.      rem=n%10;    
  12.      reverse=reverse*10+rem;    
  13.      n/=10;    
  14.   }    
  15.   printf("Reversed Number: %d",reverse);    
  16. getch();    
  17. }    
More details... 
Share:

1 comments:

subham kumar said...

Nice article . There is good coding example collection visit
Top coding program example

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

Unordered List