C Programming - Declarations and Initializations

1. 

Point out the error in the following program (if it is compiled with Turbo C compiler).

#include<stdio.h>
int main()
{
    display();
    return 0;
}
void display()
{
    printf("IndiaBIX.com");
}

A. No error
B. display() doesn't get invoked
C. display() is called before it is defined
D. None of these

Answer: Option C

Explanation:

In this program the compiler will not know that the function display() exists. So, the compiler will generate "Type mismatch in redeclaration of function display()".

To over come this error, we have to add function prototype of function display().
Another way to overcome this error is to define the function display() before the int main(); function.


#include<stdio.h>
void display(); /* function prototype */

int main()
{
    display();
    return 0;
}
void display()
{
    printf("IndiaBIX.com");
}

Output: IndiaBIX.com

Note: This problem will not occur in modern compilers (this problem occurs in TurboC but not in GCC).


2. 

Point out the error in the following program.

#include<stdio.h>
int main()
{
    void v = 0;

    printf("%d", v);

    return 0;
}

A. Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
B. Program terminates abnormally.
C. No error.
D. None of these.

Answer: Option A

Explanation:

No answer description available for this question. Let us discuss.

3. 

Point out the error in the following program.

#include<stdio.h>
struct emp
{
    char name[20];
    int age;
};
int main()
{
    emp int xx;
    int a;
    printf("%d\n", &a);
    return 0;
}

A. Error: in printf
B. Error: in emp int xx;
C. No error.
D. None of these.

Answer: Option B

Explanation:

There is an error in the line emp int xx;

To overcome this error, remove the int and add the struct at the begining of emp int xx;

#include<stdio.h>
struct emp
{
    char name[20];
    int age;
};
int main()
{
    struct emp xx;
    int a;
    printf("%d\n", &a);
    return 0;
}

4. 

Which of the following is correct about err used in the declaration given below?

 typedef enum error { warning, test, exception } err;

A. It is a typedef for enum error.
B. It is a variable of type enum error.
C. The statement is erroneous.
D. It is a structure.

Answer: Option A

Explanation:

A typedef gives a new name to an existing data type.
So err is a new name for enum error.


5. 

Point out the error in the following program.

#include<stdio.h>
int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}

A. Error: in int(*p)() = fun;
B. Error: fun() prototype not defined
C. No error
D. None of these

Answer: Option B

Explanation:

The compiler will not know that the function int fun() exists. So we have to define the function prototype of int fun();
To overcome this error, see the below program


#include<stdio.h>
int fun(); /* function prototype */

int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}


Have a question?

Ask your question now !