C Programming Test And Answer 05

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.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值