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

C Programming Test And Answer 09

程序员文章站 2024-03-01 16:43:28
...

1.What will be the output of the program?

#include<stdio.h>
int main()
{
    char ch;
    if(ch = printf(""))
        printf("It matters\n");
    else
        printf("It doesn't matters\n");
    return 0;
}

A. It matters
B. It doesn’t matters
C. matters
D. No output

Explanation:

printf() returns the number of charecters printed on the console.

Step 1: if(ch = printf("")) here printf() does not print anything, so it returns ‘0’(zero).
Step 2: if(ch = 0) here variable ch has the value ‘0’(zero).
Step 3: if(0) Hence the if condition is not satisfied. So it prints the else statements.
Hence the output is “It doesn’t matters”.

2.What will be the output of the program ?

#include<stdio.h>
int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}

A. 10
B. 20
C. 30
D. 0

Explanation:
Because union can initialize only one variable at a time. It overwrites the memory with binary value of 20 where it was initialized with binary value of 10 before.

It takes the last initialized value of its member variables.

3.In which order do the following gets evaluated

  1. Relational
  2. Arithmetic
  3. Logical
  4. Assignment

A. 2134
B. 1234
C. 4321
D. 3214

Explanation:

  1. Arithmetic operators: *, /, %, +, -
  2. Relational operators: >, <, >=, <=, ==, !=
  3. Logical operators : !, &&, ||
  4. Assignment operators: =
相关标签: sanfoundry