C Programming Test And Answer 07

1.What is the output of the program

#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        int age;
        float sal;
    };
    struct emp e = {"Tiger"};
    printf("%d, %f\n", e.age, e.sal);
    return 0;
}

A. 0, 0.000000
B. Garbage values
C. Error
D. None of above

Explanation:

When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
If we use: printf("%s %d, %f\n", e.name, e.age, e.sal);

struct emp e = {“Tiger”, 5, 1500.50}; // Full Initialization
//Ouput: Tiger, 25, 1500.500000

struct emp e = {“Tiger”}; // Partial Initialization
//Ouput: Tiger, 0, 0.000000

struct emp e ; // No Initialization
//Ouput: GarbageValue, GarbageValue, GarbageValue!

2.What will be the output of the program?

#include<stdio.h>

int main()
{
    printf("%d %d\n", 32<<1, 32<<0);
    printf("%d %d\n", 32<<-1, 32<<-0);
    printf("%d %d\n", 32>>1, 32>>0);
    printf("%d %d\n", 32>>-1, 32>>-0);
    return 0;
}

Explanation:
In case:

32<<1 means 0010 0000 are add +1 0100 0000 = 64.

32<<0 means 0010 0000 are add +0 0010 0000 = 32.

32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.

32<<-0 means 0010 0000 L less -0 0010 0000 = 32.

32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.

32>>-0 means 0010 0000 L less -0 0010 0000 = 0.

3.What will be the output of the program, if a short int is 2 bytes wide?

#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}

A. 1 … 65535
B. Expression syntax error
C. No output
D. 0, 1, 2, 3, 4, 5

Explanation:

for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.

In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it increases i by one.

An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)

4.What will be the output of the program ?

#include<stdio.h>
int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}

Explanation:
p is an array of pointers. ptr is a pointer to a pointer, initailly referring to p.
Step 1:
ptr++
ptr is incremented by 1, i.e. ptr now points to p+1 or (arr+1)
ptr-p=1, *ptr=arr+1, so ptr-arr=1, **ptr=(arr+1)=1
Step 2:
*ptr++
is *(ptr++), i.e. ptr=ptr+1=arr+2, the * part is not of any work here.
ptr-p=2, *ptr=arr+2, so ptr-arr=2, **ptr=(arr+2)=2
Step 3:
*++ptr
Similar to step 2, ++ptr=(++ptr)
ptr-p=3, *ptr=arr+3, so ptr-arr=3, **ptr=(arr+3)=3
Step 4:
++*ptr
is ++(*ptr), i.e. ptr now points to arr+4, but point to be noted that *p also changes,since ptr is pointer to p. Here ptr does not change its reference position unlike all the other steps, but changes the value it is pointing to
*p[]={arr, arr+1, arr+2, arr+4, arr+4}
so, ptr-p=3, *ptr=p[3]=arr+4, so ptr-arr=4, **ptr=(arr+4)=4

5.Point out the error in the program?

#include<stdio.h>
int main()
{
    struct emp
    {
        char name[25];
        int age;
        float bs;
    };
    struct emp e;
    e.name = "Suresh";
    e.age = 25;
    printf("%s %d\n", e.name, e.age);
    return 0;
}

A. Error: Lvalue required/incompatible types in assignment
B. Error: invalid constant expression
C. Error: Rvalue required
D. No error, Output: Suresh 25

Explanation:
We cannot assign a string to a struct variable like e.name = “Suresh”; in C.

We have to use strcpy(char *dest, const char *source) function to assign a string.

Ex: strcpy(e.name, “Suresh”);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值