欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C Programming Test And Answer 05

程序员文章站 2024-03-01 16:53:22
...

1.What will be the output of the program ?

#include<stdio.h>
int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}

Explanation:
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to

a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .

Step 2: int i, j, m; The variable i,j,m are declared as an integer type.

Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m

Hence the output of the program is 3, 2, 15

2.What will be the output of the program ?

#include<stdio.h>
int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

Explanation:
Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.

Step 1 : vp = &ch;
/Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e “J”/

Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as “A”*/

Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to “C” from sring “JACK” and since we hava given %S in printf so it will print content from c onwords ie “CK”*/

So final combined output will be JACK.

3.What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    for(i=1; i<argc; i++)
        printf("%c", argv[i][0]);
    return 0;
}

Explanation:
argv[0]=Base address=myprog
argv[1]=One
argv[2]=Two
argv[3]=Three

Given i=1;i<4; i++;
So,argv[1][0]=O(from One )
argv[2][0]=T(from Two )
argv[3][0]=T(from Three )

So Result=ott;

4.Will the following program work?

#include<stdio.h>
int main()
{
    int n=5;
    printf("n=%*d\n", n, n);
    return 0;
}

Explanation:
Here in printf(“n=%*d\n”, n, n);

("%*d",n,n) tells the compiler to skip the first n value.

Thus second n value gets printed.

相关标签: sanfoundry