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

C Programming Test And Answer 03

程序员文章站 2024-03-01 18:06:22
...

1.What will be the output of the program ?

#include<stdio.h>
int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

Explanation:
Note the below statement inside the struct:

int bit1:1; --> ‘int’ indicates that it is a SIGNED integer.

For signed integers the leftmost bit will be taken for +/- sign.

If you store 1 in 1-bit field:

The left most bit is 1, so the system will treat the value as negative number.

The 2’s complement method is used by the system to handle the negative values.

Therefore, the data stored is 1. The 2’s complement of 1 is also 1 (negative).

Therefore -1 is printed.

If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

0010 is 2

Therefore 2 is printed.

If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

Find 2’s complement of 1101:

1’s complement of 1101 : 0010
2’s complement of 1101 : 0011 (Add 1 to the result of 1’s complement)

0011 is 3 (but negative value)

Therefore -3 is printed.
2.What will be the output of the program ?

#include<stdio.h>
int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}

Explanation:
Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array with size ‘2’ and it’s first element is initialized to value ‘10’(means arr[0]=10)

Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.

Hence the output of the program is 10.
3.Which of the following statements are correct about the program?

#include<stdio.h>
char *fun(unsigned int num, int base);
int main()
{
    char *s;
    s=fun(128, 2);
    s=fun(128, 16);
    printf("%s\n",s);
    return 0;
}
char *fun(unsigned int num, int base)
{
    static char buff[33];
    char *ptr = &buff[sizeof(buff)-1];
    *ptr = '\0';
    do
    {
        *--ptr = "0123456789abcdef"[num %base];
        num /=base;
    }while(num!=0);
    return ptr;
}

Explanation:
char *ptr = &buff[sizeof(buff)-1]; makes ptr point to 32nd index of buff.
buff[32] is made null in the next statement. (*ptr=’\0’). Reason being strings are supposed to be terminated with null.

Now considering 128,2

128%2=0 so “0123456789abcdef”[num %base]; returns 0. ptr is decremented and then the value 0 is assigned. This value 0 is written on to the next six places from the right towards left till num becomes 1. Now when num becomes 1, “0123456789abcdef”[num %base] returns 1. and hence the binary rep of 128.

Similar analysis will lead one to see that with 128,16 the value returned is 80, the hexadecimal rep pf 128.
4.What will be the output of the program?

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

Explanation:
Step 1: int i = 0; here variable i is an integer type and initialized to ‘0’.
Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semi-colon at the end of this for loop tells, “there is no more statement is inside the loop”.

Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by ‘1’(one)
Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by ‘1’(one)
Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by ‘1’(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by ‘1’(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by ‘1’(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by ‘1’(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented.
Step 3: printf("%d", i); here the value of i is 6. Hence the output is ‘6’.
5.What will be the output of the program?

#include<stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

Explanation:
Step 1: int x = 3; here variable x is an integer type and initialized to ‘3’.
Step 2: float y = 3.0; here variable y is an float type and initialized to ‘3.0’
Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.
Hence it prints “x and y are equal”.

相关标签: sanfoundry