Exercise: Declarations And Initializations

Questions for: Declarations And Initializations

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: B

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;
}
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: A
No answer description is available. Let's discuss.
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("ExamAdept.com");
}
A:
No error
B:
display() doesn't get invoked
C:
display() is called before it is defined
D:
None of these
Answer: C

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("ExamAdept.com");
}

Output: ExamAdept.com

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

What will be the output of the program?
#include<stdio.h>
int main()
{
    int X=40;
    {
        int X=20;
        printf("%d ", X);
    }
    printf("%d\n", X);
    return 0;
}
A:
40 40
B:
20 40
C:
20
D:
Error
Answer: B
In case of a conflict between a local variable and global variable, the local variable gets priority.
In the following program how long will the for loop get executed?
#include<stdio.h>
int main()
{
    int i=5;
    for(;scanf("%s", &i); printf("%d\n", i));
    return 0;
}
A:
The for loop would not get executed at all
B:
The for loop would get executed only once
C:
The for loop would get executed 5 times
D:
The for loop would get executed infinite times
Answer: D

During the for loop execution scanf() ask input and then printf() prints that given input. This process will be continued repeatedly because, scanf() returns the number of input given, the condition is always true(user gives a input means it reurns '1').

Hence this for loop would get executed infinite times.

Ad Slot (Above Pagination)
Quiz