从GATE纸C程序 [英] A c program from GATE paper

查看:157
本文介绍了从GATE纸C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿昨天我已经给我的门考试,这次考试有一个问题:

Hey all yesterday i have given my GATE exam and in this exam there was a question:

什么C程序打印下列片​​段?

char c [] = "GATE2011";
char *p =c;
printf ( "%s", p+p [3] − p [1]) ;

选项:

(A) GATE2011
(B) E2011
(C) 2011
(D) 011

答案是 C 。当我试图在我的电脑但如何在code?

Answer is 'C'. when i tried the code on my computer But how??

在此先感谢

推荐答案

这是指针运算:

char c [] = "GATE2011";
// indices:  01234567
char *p =c;

P [1] A 字符code, P [3 ] 电子字符code,它们的区别(电子 code减去 A code)为4,那么这种差别将被添加到 p 指针,你有 2011 地址子传递到的printf()

p[1] is A character code, p[3] is E character code, their difference (E code minus A code) is 4, that difference is then added to p pointer and you've got the address of 2011 substring passed into printf().

这篇关于从GATE纸C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆