C Programming Language Question & Answers for Exam , Interviews


What is a pointer on pointer?

It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.
Eg: int y = 5, *s=&x, **p=&s;
Therefore ‘y’ can be accessed by **p.


Distinguish between malloc() & calloc() memory allocation.

Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s.


What is keyword auto for?

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.


What are the valid places for the keyword break to appear.

Break can appear only with in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.


What is the 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.


When should we use the register storage specifier?

If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.


Can a program be compiled without main() function?

Yes, it can be but cannot be executed, as the execution requires main() function definition.


What is the meaning of base address of the array?

The starting address of the array is called as the base address of the array.


Explain the syntax for for loop.

for(expression-1;expression-2;expression-3) {
   //set of statements
}
When control reaches for expression-1 is executed first. Then following expression-2, and if expression-2 evaluates to non-zero ‘set of statements’ and expression-3 is executed, follows expression-2.


Where an automatic variable is stored?

Every local variable by default being an auto variable is stored in stack memory.


Explain the purpose of the function sprintf().

Prints the formatted output onto the character array.


What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
void f() { 
   static int i; 
   ++i; 
   printf(“%d “,i); 
}
If a global variable is static then its visibility is limited to the same source code.


What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.
main( int count, char *args[]) {
}


What is a nested structure?

A structure containing an element of another structure as its member is referred so.


What is a NULL pointer?

A pointer pointing to nothing is called so. Eg: char *p=NULL;


What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable.


Define a structure.

A structure can be defined of collection of heterogeneous data items.


What is a static function?

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.


Who designed C programming language?

Dennis M Ritchie.


Can the main() function left empty?

Yes, possibly the program doing nothing.


How do you construct an increment statement or decrement statement in C?

There are actually two ways you can do this. One is to use the increment operator ++ and decrement operator ?. For example, the statement ?x++? means to increment the value of x by 1. Likewise, the statement ?x ?? means to decrement the value of x by 1. Another 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?.


What is the equivalent code of the following statement in WHILE LOOP format?
for (a=1; a<=100; a++)
printf ("%d\n", a * a);

a=1;
while (a<=100) {
printf ("%d\n", a * a);
a++;
}


What is the difference between the = symbol and == symbol?

The = symbol is often used in mathematical operations. It is used to assign a value to a given variable. On the other hand, the == symbol, also known as ?equal to? or ?equivalent to?, is a relational operator that is used to compare two values.


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

It is referred to as a terminating null character, and is used primarily to show the end of a string value


Why is it that not all header files are declared in every C program?

The choice of declaring a header file at the top of each C program would depend on what commands/functions you will be using in that program. Since each header file contains different function definitions and prototype, you would be using only those header files that would contain the functions you will need. Declaring all header files in every program would only increase the overall file size and load of the program, and is not considered a good programming style.


Which of the following operators is incorrect and why? ( >=, <=, <>, ==)

<> is incorrect. While this operator is correctly interpreted as ?not equal to? in writing conditional statements, it is not the proper operator to be used in C programming. Instead, the operator != must be used to indicate ?not equal to? condition.


Write a simple code fragment that will check if a number is positive or negative.

If (num>=0)
printf("number is positive");
else
printf ("number is negative");


Write a loop statement that will show the following output:
1
12
123
1234
12345

for (a=1; a<=5; i++) {
for (b=1; b<=a; b++)
printf("%d",b);
printf("\n");
}


When is the ?void? keyword used in a function?

When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then ?void? is to be placed at the leftmost part of the function header. When a return value is expected after the function execution, the data type of the return value is placed instead of ?void?.


How will you print “Hello World” without semicolon?

int main(void)
{
    if (printf("Hello World")) ;
}


What are different storage class specifiers in C?

auto, register, static, extern


What is difference between i++ and ++i?

1) The expression ‘i++’ returns the old value and then increments i. The expression ++i increments the value and returns new value.
2) Precedence of postfix ++ is higher than that of prefix ++.
3) Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left.
4) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-value.


How will you print numbers from 1 to 100 without using loop?

void printNos(unsigned int n)
{
  if(n > 0)
  {
    printNos(n-1);
    printf("%d ",  n);
  } 
}


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


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

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


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.
int value=50;//global variable  
void function1(){  
int x=20;//local variable  
} 


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.


What is typecasting?

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


Share this article on