关闭

some basic interview question

标签: c
199人阅读 评论(0) 收藏 举报
分类:

(the header is omitted,sorry)
1.about the pointer and the ++ ,–

int main()
{
    int x = 4;
    int *p = &x;
    int *k = p++;
    int r = p - k;
    printf("%d", r);
}

>
explain:
C本身无法防止非法的指针减法运算,它无法为你提出任何警告或提示。
for more information
(http://blog.csdn.net/gdmmhym/article/details/6451554)
2.

int main()
{
unsigned int i = 23;
signed char c = -23;
if (i > c)
printf("Yes\n");
else if (i < c)
printf("No\n");
}

answer :No
for more information
(http://blog.csdn.net/ljianhui/article/details/10367703)
3.

int main()
{
    int x = -2;
    x = x >> 1;
    printf("%d\n", x);
}

answer:-1
it is simple,but i just want to write it here.
4.8. What is the output of this C code?

int main()
{
    int y = 1;
    if (y & (y = 2))
        printf("true %d\n", y);
    else
        printf("false %d\n", y);

}

a) true 2
b) false 2
c) Either option a or option b
d) true 1
View Answer

Answer:c

5.
void main()
{
char a = ‘a’;
int x = (a % 10)++;
printf(“%d\n”, x);
}
answer:compile error
please note that :
[Error] lvalue required as increment operand
6.concentrate on the detail
example 1:
void main()
{
1 < 2 ? return 1: return 2;
}

answer:compile error
note that the ‘return 1’ is not a expression
example 2:

int main()
{
int x = 1, y = 0;
x &&= y;
printf("%d\n", x);
}

please note that ‘&&= ’ does not exist
+=、-=、*=、/=、%=、<<=、>>=、&=、^=、|=
here
example 3

int main()
{
int y = 1, x = 0;
int l = (y++, x++) ? y : x;
printf(“%d\n”, l);
}
a) 1
b) 2
c) Compile time error
d) Undefined behaviour
View Answer

Answer:a

7.some thing i want to figure out ,

int main()
{
    int x = 1;
    short int i = 2;
    float f = 3;
    if (sizeof((x == 2) ? f : i) == sizeof(float))
        printf("float\n");
    else if (sizeof((x == 2) ? f : i) == sizeof(short int))
        printf("short int\n");
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12104次
    • 积分:596
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:1篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论