C Interview Questions- header file, string functions, iterative loops, array pointers, bitwise operators

by Dhoka Ratan on January 19, 2009


c interview questions Here is another set of challenging questions that could be asked in C language interviews. Interviewers and interviewees can use questions and answers in this blog as a reference guide before conducting/attending an interview. Question.46 is about basics of header files in C. It tests whether the candidate under test is aware of the best practice of declaring rather than defining functions in a header file. Question.47 tests the candidate’s ability to discern between strdup and strcpy functions included in string.h header file. Question.48 is about hte subtle differences between for loop and while loop in C. Question.49 tests an advanced concept of pointer notation of multi-dimensional arrays. Question.50 is a question on bitwise operators in C. It tests the candidate’s knowledge of the operator used for left shift and right shift. See all C interview questions in this blog in one page.

  1. What are header files? Are functions declared or defined in header files ?
  2. Functions and macros are declared in header files. Header files would be included in source files by the compiler at the time of compilation.

    Header files are included in source code using #include directive.#include<some.h> includes all the declarations present in the header file ‘some.h’.

    A header file may contain declarations of sub-routines, functions, macros and also variables which we may want to use in our program. Header files help in reduction of repetitive code.

    Syntax of include directive:
    #include<stdio.h> //includes the header file stdio.h, standard input output header into the source code

    Functions can be declared as well as defined in header files. But it is recommended only to declare functions and not to define in the header files. When we include a header file in our program we actually are including all the functions, macros and variables declared in it.

    In case of pre-defined C standard library header files ex(stdio.h), the functions calls are replaced by equivalent binary code present in the pre-compiled libraries. Code for C standard functions are linked and then the program is executed. Header files with custom names can also be created. Read more about header files

    Program: Custom header files example

    /****************
    Index: restaurant.h
    ****************/
    int billAll(int food_cost, int tax, int tip);

    Download code

    /****************
    Index: restaurant.c
    ****************/
    #include<stdio.h>
    int billAll(int food_cost, int tax, int tip) {
     int result;
     result = food_cost + tax + tip;
     printf("Total bill is %d\n",result);
     return result;
    }

    Download code

    /****************
    Index: main.c
    ****************/
    #include<stdio.h>
    #include"restaurant.h"
    
    int main() {
     int food_cost, tax, tip;
     food_cost = 50;
     tax = 10;
     tip = 5;
     billAll(food_cost,tax,tip);
     return 0;
    }

    Download code
    Back to top

  3. What is the difference between the functions strdup and strcpy in C?
  4. strcpy function: copies a source string to a destination defined by user. In strcpy function both source and destination strings are passed as arguments. User should make sure that destination has enough space to accommodate the string to be copied.

    ‘strcpy’ sounds like short form of “string copy”.

    Syntax:
    strcpy(char *destination, const char *source);
    Source string is the string to be copied and destination string is string into which source string is copied. If successful, strcpy subroutine returns the address of the copied string. Otherwise, a null pointer is returned.

    Program: Example program.

    #include<stdio.h>
    #include<string.h>
    
    int main() {
     char myname[10];
     //copy contents to myname
     strcpy(myname, "nodalo");
     //print the string
     puts(myname);
     return 0;
    }

    Download Code

    Output:

    nodalo

    Explanation:
    If the string to be copied has more than 10 letters, strcpy cannot copy this string into the string ‘myname’. This is because string ‘myname’ is declared to be of size 10 characters only.
    In the above program, string “nodalo” is copied in myname and is printed on output screen.

    strdup function: duplicates a string to a location that will be decided by the function itself. Function will copy the contents of string to certain memory location and returns the address to that location. ‘strdup’ sounds like short form of “string duplicate”

    Syntax:
    strdup (const char *s);
    strdup returns a pointer to a character or base address of an array. Function returns address of the memory location where the string has been copied. In case free space could not be created then it returns a null pointer. Both strcpy and strdup functions are present in header file <string.h>

    Program: Program to illustrate strdup().

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main() {
     char myname[] = "nodalo";
     //name is pointer variable which can store the address of memory location of string
     char* name;
     //contents of myname are copied in a memory address and are assigned to name
     name = strdup(myname);
     //prints the contents of 'name'
     puts(name);
     //prints the contents of 'myname'
     puts(myname);
     //memory allocated to 'name'is now freed
     free(name);
     return 0;
    }

    Download Code

    Output:

    nodalo
    nodalo

    Explanation:
    string myname consists of “nodalo” stored in it. Contents of myname are copied in a memory address and memory is assigned to name. At the end of the program, memory can be freed using free(name);
    Back to top

  5. What is difference between for loop and while loop in C language?
  6. for loop: When it is desired to do initialization, condition check and increment/decrement in a single statement of an iterative loop, it is recommended to use ‘for’ loop.

    Syntax:

    for(initialisation;condition;increment/decrement)
    {
    //block of statements
    increment or decrement
    }

    Program: Program to illustrate for loop

    #include<stdio.h>
    int main() {
     int i;
     for (i = 1; i <= 5; i++) {
      //print the number
      printf("\n %d", i);
     }
     return 0;
    }

    Download Code

    Output:

    1
    2
    3
    4
    5

    Explanation:
    The loop repeats for 5 times and prints value of ‘i’ each time. ‘i’ increases by 1 for every cycle of loop.

    while loop: When it is not necessary to do initialization, condition check and increment/decrement in a single statement of an iterative loop, while loop could be used. In while loop statement, only condition statement is present.

    Syntax:

    #include<stdio.h>
    int main() {
     int i = 0, flag = 0;
     int a[10] = { 0, 1, 4, 6, 89, 54, 78, 25, 635, 500 };
     //This loop is repeated until the condition is false.
     while (flag == 0) {
      if (a[i] == 54) {
       //as element is found, flag = 1,the loop terminates
       flag = 1;
      }
      else {
       i++;
      }
     }
     printf("Element found at %d th location", i);
     return 0;
    }

    Download Code

    Output:

    Element found at 5th location

    Explanation:
    Here flag is initialised to zero. ‘while’ loop repeats until the value of flag is zero, increments i by 1.
    ‘if’ condition checks whether number 54 is found. If found, value of flag is set to 1 and ‘while’ loop terminates.
    Back to top

  7. Write down the equivalent pointer expression for referring the same element as a[i][j][k][l]. Explain.
  8. Consider a multidimensional array a[w][x][y][z].

    In this array, a[i] gives address of a[i][0][0][0] and a[i]+j gives the address of a[i][j][0][0]
    Similarly, a[i][j] gives address of a[i][j][0][0] and a[i][j]+k gives the address of a[i][j][k][0]
    a[i][j][k] gives address of a[i][j][k][0] and a[i][j][k]+l gives address of a[i][j][k][l]

    Hence a[i][j][k][l] can be accessed using pointers as *(a[i][j][k]+l)
    where * stands for value at address and a[i][j][k]+l gives the address location of a[i][j][k][l].

    Program: Example program to illustrate pointer denotation of multi-dimensional arrays.

    #include<stdio.h>
    #include<string.h>
    int main() {
     int a[3][3][3][3];
     //it gives address of a[0][0][0][0] .
     printf(" \n address of array a is %u", a);
     printf("\n address of a[2][0][0][0] is %u ,given by a[2], %u given by a+2",
     a[2], a + 2);
     printf("\n address of a[2][2][0][0] is %u ,given by a[2][2], %u given by a[2]+2",
     a[2][2], a[2] + 2);
     printf("\n address of a[2][2][1][0] is %u ,given by a[2][2][1] , %u given by a[2][2]+1",
     a[2][2][1], a[2][2] + 1);
     return 0;
    }

    Download Code

    Output:

    address of array a is 65340
    address of a[2][0][0][0] is 65448, given by a[2] , 65448 given by a+2
    address of a[2][2][0][0] is 65484, given by a[2][2] ,65484 given by a[2]+2
    address of a[2][2][1][0] is 65490, given by a[2][2][1] , 65490 given by a[2][2]+1

    Explanation:
    This output may differ from computer to computer as the address locations are not same for every computer.
    Back to top

  9. Which one is equivalent to multiplying an unsigned int by 2,left shifting by 1 or right shifting by 1?
  10. Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2.

    Eg1: 14<<1;
    consider a number 14—–00001110 (8+4+2)is its binary equivalent
    left shift it by 1————–00011100(16+8+4) which is 28.

    Eg2: 1<<1;
    consider the number as 1—00000001(0+0+1).
    left shift that by 1————00000010(0+2+0) which is 2.
    left shift by 1 bit of a number=2*number
    left shift by 1 bit of 2*number=2*2*number
    left shift by n bits of number=(2^n)*number

    Program:

    Program to illustrate left shift and right shift operations.

    #include<stdio.h>
    int main(void)
    {
     int x=10,y=10;
     printf("left shift of 10 is %d \n",x<<1);
     printf("right shift of 10 is %d \n",y>>1);
     return 0;
    }

    Download code

    Output:

    left shift of 10 is 20
    right shift of 10 is 5

    Explanation:
    Left shift (by 1 position) multiplies a number by two. Right shift divides a number by 2.
    More about bitwise operators
    Back to top


About the Author:  This post was written by Ratan Dhoka. He is an alumnus of Kakatiya Institute of Technology. He enjoys coding in C and Java. You can reach him at ratan_jain77@yahoo.com


2,274 readers have already subscribed to email updates!

Enter your email address:

  • Srikanth

    Please refer to your Q50: Dealing with Left and Right Shift operators. I guess the explanation is partially correct only in case where the shift number is 1.
    But the actually , its done as this
    a < < b
    This expression returns the value of " a " multiplied by 2 to the power of " b "

    and

    You have not spoken about the right shift operator:
    a >> b
    This expression returns the value of ” a ” divided by 2 to the power of ” b”.

  • Anonymous

    can anyone say me how to write a c programme to print numbers in following fashion

    1
    0 1
    1 0 1
    0 1 1 1

  • http://www.interviewmantra.net Sridhar

    /****************************************
    * Copy Right- 2009, Interview Mantra
    * Some Rights Reserved
    ****************************************/

    #include
    int main(){
    int i,j;
    for(i=0;i<4;i++){
    for(j=0;j<=i;j++){
    if(((i+j)%2)==0){
    printf("0");
    }
    else{
    printf("1");
    }
    printf("\t");
    }
    puts("\n");
    }
    return 0;
    }

    I hope this solution solves your problem.

    Thanks,
    Editor of Interview mantra

  • shiva

    can anyone tell me how to print following pattern in c/c++
    . 1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1
    1 2 3 4 5 4 3 2 1

  • mukund

    can any one tell me wt’s the codes for:
    ABCDEDCBA
    ABCD DCBA
    ABC CBA
    AB BA
    A A

  • jyoti

    #include
    int main()
    {
    int i,j,k;
    printf(“11\n”);
    for(i=1;i<=4;i++)
    {for(j=1;j=1;k–)
    {
    printf(“%d”,k);
    }
    }
    printf(“\n”);
    }
    return 0;
    }

  • jyoti

    #include
    int main()
    {
    int i,j,k;
    printf(“11\n”);
    for(i=1;i<=4;i++)
    {for(j=1;j=1;k–)
    {
    printf(“%d”,k);
    }
    }
    printf(“\n”);
    }
    return 0;
    }
    output:
    11
    121
    12321
    1234321
    123454321

  • jyoti

    #include
    int main()
    {
    int i,j,k;
    printf(“11\n”);
    for(i=1;i<=4;i++)
    {for(j=1;j=1;k–)
    {
    printf(“%d”,k);
    }
    }
    printf(“\n”);
    }
    return 0;
    }
    output:
    11
    121
    12321
    1234321
    123454321

    #include
    int main()
    {
    int i,j,k;
    printf(“AA\n”);
    for(i=65;i<=69;i++)
    {for(j=65;j=65;k–)
    {
    printf(“%c”,k);
    }
    }
    printf(“\n”);
    }
    return 0;
    }
    output:

    AA
    ABA
    ABCBA
    ABCDCBA
    ABCDEDCBA
    ABCDEFEDCBA

  • Rajan verma

    Rajan verma (9838628498) it is not your answer, but after considering this you will able to fix your own problem.

    #include

    #include

    void main()

    {

    clrscr();

    int i,j,k,l;

     

    for(i=5;i>=1;i–)

    {

                for(j=5;j>=i;j–)

                {

                cout<<"
    ";

                }

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

                {

                cout<=1;l–)

                {

                cout<<l;

                }

      
    cout<<"n";

    }

    getch();

    }
    run it
    enjoy!!!!!!!!!

  • Guest

    What is the value of i at the end of this piece of code
    void fnx() { int i = 0;  for(i=1;i<10;++i) { i = i*5;} printf(i); }

Previous post:

Next post: