Wednesday, August 6, 2014

C Programming

1. 
What will be the output of the program?
#include<stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
    char *str1="India";
    char *str2="BIX";
    JOIN(str1, str2);
    return 0;
}
A.str1=IndiaBIX str2=BIXB.str1=India str2=BIX
C.str1=India str2=IndiaBIXD.Error: in macro substitution
2. 
What will be the output of the program?
#include<stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}
A.9, 4B.27, 4
C.27, 6D.Error
3. 
What will be the output of the program?
#include<stdio.h>
#define PRINT(int) printf("int=%d, ", int);

int main()
{
    int x=2, y=3, z=4;   
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}
A.int=2, int=3, int=4B.int=2, int=2, int=2
C.int=3, int=3, int=3D.int=4, int=4, int=4
4. 
What will be the output of the program?
#include<stdio.h>
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
    int a=10, b=12;
    SWAP(a, b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}
A.a = 10, b = 12
B.a = 12, b = 10
C.Error: Declaration not allowed in macro
D.Error: Undefined symbol 't'

5. 
What will be the output of the program?
#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%d\n", FUN(va1, 2));
    return 0;
}
A.10B.20
C.1020D.12

No comments: