3. What will be the output of the program?

#include<stdio.h>
 
int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}
 
int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d, %d\n", k, l);
    return 0;
}
  • 12, 12
  • 7, 7
  • 7, 12
  • 12, 7

Explanation Step 1int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable ij are initialized to 3, 4 respectively. The function addmult(i, j); accept 2 integer parameters.

Step 2k = addmult(i, j); becomes k = addmult(3, 4) In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll; kk = ii + jj; becomes kk = 3 + 4 Now the kk value is ‘7’. ll = ii * jj; becomes ll = 3 * 4 Now the ll value is ‘12’. return (kk, ll); It returns the value of variable ll only. The value 12 is stored in variable ‘k’.

Step 3l = addmult(i, j); becomes l = addmult(3, 4) kk = ii + jj; becomes kk = 3 + 4 Now the kk value is ‘7’. ll = ii * jj; becomes ll = 3 * 4 Now the ll value is ‘12’. return (kk, ll); It returns the value of variable ll only. The value 12 is stored in variable ‘l’.

Step 4printf(“%d, %d\n”, k, l); It prints the value of k and l Hence the output is “12, 12”.


9. What will be the output of the program ?

#include<stdio.h>
 
int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is empty\n");
    else
        printf("The string is not empty\n");
    return 0;
}
  • The string is empty
  • The string is not empty
  • No output
  • 0

Explanation The function printf() returns the number of charecters printed on the console.

Step 1char a[] = “\0”; The variable a is declared as an array of characters and it initialized with “\0”. It denotes that the string is empty.

Step 2if(printf(“%s”, a)) The printf() statement does not print anything, so it returns ‘0’(zero). Hence the if condition is failed. In the else part it prints “The string is not empty”.


11. If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?

struct ex
{
    char ch;
    int i;
    long int a;
};
  • Yes
  • No

Explanation A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensure that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.


12. Point out the error/warning in the program?

#include<stdio.h>
 
int main()
{
    unsigned char ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    fclose(fp);
    return 0;
}
  • Error: in unsigned char declaration
  • Error: while statement
  • No error
  • It prints all characters in file “trial”

Explanation Here, EOF is -1. As ‘ch’ is declared as unsigned char it cannot deal with any negative value.


15. What will be the output of the program?

#include<stdio.h>
int fun(int **ptr);
 
int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}
  • Address of i
    Address of j
  • 10
    223
  • Error: cannot convert parameter 1 from ‘_const int _’ to ‘int **
  • Garbage value