ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C Programming Test And Answer 07

2020-05-01 13:37:40  阅读:224  来源: 互联网

标签:arr ++ 32 Programming Answer int printf Test ptr


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”);

标签:arr,++,32,Programming,Answer,int,printf,Test,ptr
来源: https://blog.csdn.net/qq_45645641/article/details/105860286

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有