3. Point out the error in the following program.

#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
  • Suspicious pointer conversion
  • Floating point formats not linked (Run time error)
  • Cannot use scanf() for structures
  • Strings cannot be nested inside structures

Explain The program terminates abnormally at the time of entering the float value for e[i].sal.


7. Will the program compile in Turbo C?

#include<stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u\n", j, k);
    return 0;
}
  • Yes
  • No

Explain Error in statement k++. We cannot perform arithmetic on void pointers. The following error will be displayed while compiling above program in TurboC. Compiling PROGRAM.C: Error PROGRAM.C 8: Size of the type is unknown or zero.


10. If char=1, int=4, and float=4 bytes size, What will be the output of the program?

#include<stdio.h>
 
int main()
{
    char ch = 'A';
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
    return 0;
}
  • 1, 2, 4
  • 1, 4, 4
  • 2, 2, 4
  • 2, 4, 8

Explanation Step 1char ch = ‘A’; The variable ch is declared as a character type and initialized with value ‘A’.

Step 2:

  • printf(“%d, %d, %d”, sizeof(ch), sizeof(‘A’), sizeof(3.14));
  • The sizeof function returns the size of the given expression.
  • sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
  • sizeof(‘A’) becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
  • sizeof(3.14f). The size of float is 4 bytes.
  • Hence the output of the program is 1, 4, 4

11. What will be the output of the program?

#include<stdio.h>
 
int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}
  • Kagpur, Kanpur
  • Nagpur, Kanpur
  • Kagpur, anpur
  • Error

Explanation The statement str = “Kanpur”; generates the LVALUE required error. We have to use strcpy function to copy a string. To remove error we have to change this statement str = “Kanpur”; to strcpy(str, “Kanpur”); The program prints the string “anpur”


17. What is the output of the program?

typedef struct data;
{
    int x;
    sdata *b;
}sdata;
  • Error: Declaration missing ’;’
  • Error: in typedef
  • No error
  • None of above

Explanation since the type ‘sdata’ is not known at the point of declaring the structure


19. Point out the error in the following program.

#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);
 
int main()
{
    varfun(3, 7, -11.2, 0.66);
    return 0;
}
void varfun(int n, ...)
{
    float *ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}
  • Error: too many parameters
  • Error: invalid access to list member
  • Error: ptr must be type of va_list
  • No error