2. Which of the following errors would be reported by the compiler on compiling the program given below?

#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
	case 1:
	printf("First");
 
	case 2:
	printf("Second");
 
	case 3 + 2:
	printf("Third");
 
	case 5:
	printf("Final");
	break;
 
    }
    return 0;
}
  • There is no break statement in each case.
  • Expression as in case 3 + 2 is not allowed.
  • Duplicate case case 5:
  • No error will be reported.

4. What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=2;
    printf("%d, %d\n", ++i, ++i);
    return 0;
}
  • 3, 4
  • 4, 3
  • 4, 4
  • Output may vary from compiler to compiler

Explanation The order of evaluation of arguments passed to a function call is unspecified. Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3. In TurboC, the output will be 4, 3. In GCC, the output will be 4, 4.

8. What will be the output of the program?

#include<stdio.h>
#define SQR(x)(x*x)
 
int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%d\n", a);
    return 0;
}
  • 25
  • 11
  • Error
  • Garbage value

Explanation The macro function SQR(x)(x*x) calculate the square of the given number ‘x’. (Eg: 102)

Step 1int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3. Step 2a = SQR(b+2); becomes,  a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .  a = 3+2 * 3+2;  a = 3 + 6 + 2;  a = 11; Step 3printf(“%d\n”, a); It prints the value of variable ‘a’.

Hence the output of the program is 11


14. If the file ‘source.txt’ contains a line “Be my friend” which of the following will be the output of below program?

#include<stdio.h>
 
int main()
{
    FILE *fs, *ft;
    char c[10];
    fs = fopen("source.txt", "r");
    c[0] = getc(fs);
    fseek(fs, 0, SEEK_END);
    fseek(fs, -3L, SEEK_CUR);
    fgets(c, 5, fs);
    puts(c);
    return 0;
}
  • friend
  • frien
  • end
  • Error in fseek();

Explanation

  • The file source.txt contains “Be my friend”.
  • fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file.
  • fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters.
  • fgets(c, 5, fs); read the file from the current position of the file pointer.
  • Hence, it contains the last 3 characters of “Be my friend”.
  • Therefore, it prints “end”.

19. Point out the error in the following program.

#include<stdio.h>
#include<stdarg.h>
void display(char *s, ...);
int fun1();
int fun2();
 
int main()
{
    int (*p1)();
    int (*p2)();
    p1 = fun1;
    p2 = fun2;
    display("IndiaBIX", p1, p2);
    return 0;
}
void display(char *s, ...)
{
    int (*pp1)();
    int (*pp2)();
    va_list ptr;
 
    va_start(ptr, s);
    pp1 = va_arg(ptr, int(*)());
    (*pp1)();
 
    pp2 = va_arg(ptr, int(*)());
    (*pp2)();
 
}
int fun1()
{
    printf("Hello");
}
int fun2()
{
    printf("Hi");
}
  • Error: invalid function display() call
  • Error: invalid va_start(ptr, s);
  • Error: va_arg cannot extract function pointer from variable argument list.
  • Error: Rvalue required for t

Explanation: According to K&R (Section 7.3), the type argument you pass to va_arg(ptr, type) must be written such that “a pointer to an object of that type can be obtained simply by appending a * to type.”

pp1 = va_arg(ptr, int(*)());

The type passed is int(*)() (a pointer to a function). If the compiler follows the K&R macro logic and appends a * to this type to create a pointer-to-the-pointer, it generates: int(*)() * This is invalid C syntax. We cannot declare a pointer to a function pointer by simply sticking a * at the end of the declaration.

The Fix: We should use a typedef to create a simple name for the function pointer.

typedef int (*func_ptr)(); // Define a simple name
// ...
pp1 = va_arg(ptr, func_ptr); // Now the macro expands to 'func_ptr *', which is va

20. What do the following declaration signify?

int (*ptr)[30];
  • ptr is a pointer to an array of 30 integer pointers.
  • ptr is a array of 30 integer function pointer.
  • ptr is a array of 30 integer pointers.
  • ptr is a array 30 pointers.