简体   繁体   English

无法确定输出C代码的原因

[英]Unable to determine reason for output of c code

I have the following code. 我有以下代码。 I cannot determine the reason for the output it's giving. 我无法确定输出结果的原因。

char abc[14] = "C Programming";

printf("%s\n", abc+abc[3]-abc[4]);

output:rogramming 输出:成图

It is a quiz question with the following options: What would this print? 这是一个具有以下选项的测验问题:该打印什么? a)C Programming b) rogamming c)Runtime Error d)Compilation Error a)C编程b)漫游c)运行时错误d)编译错误

Perhaps it will be clearer if I put in stepping stones v3 and v4 . 如果我添加垫脚石v3v4也许会更清楚。 These are used to calculate an offset into the string of 3 indices. 这些用于计算到3个索引的字符串中的偏移量。 So the print output begins at abc[3] . 因此,打印输出从abc[3]开始。

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    char abc[14] = "C Programming";
    int v3 = abc[3];
    int v4 = abc[4];
    printf("%d - %d = %d\n", v3, v4, v3 - v4);
    printf("%s\n", abc + v3 - v4);
    return 0;
}

Program output: 程序输出:

114 - 111 = 3
rogramming

This may caused by the following: the value of abc[3] is 'r',the value of abc[4] is 'o'.so the value of 这可能是由于以下原因引起的: abc[3]值为'r', abc[4]值为'o'。因此

abc[3]-abc[4]

is 'r'-'o',change the value into int is 3 .So when you use printf("%s\\n", abc+abc[3]-abc[4]); 是'r'-'o',将值更改为int3当您使用printf("%s\\n", abc+abc[3]-abc[4]); ,it means you print this string from abc[3]. ,表示您从abc [3]打印此字符串。 the following code will show you something. 以下代码将向您展示一些内容。

 int main()
  {
  char abc[14] = "C Programming";
  printf("%d\n",abc[3]-abc[4]);
  printf("%s\n", abc+abc[3]-abc[4]);

  }

In this expression abc+abc[3]-abc[4] there is used so-called the pointer arithmetic. 在该表达式abc+abc[3]-abc[4] ,使用了所谓的指针算法。

An array name in expressions is converted to pointer to the first element of the array, 表达式中的数组名称将转换为指向数组第一个元素的指针,

Thus in the expression above abc points to the first character of the string that is to 'C' abc + 1 points to space. 因此,在上面的表达式中,abc指向字符串的第一个字符,即'C' abc + 1指向空格。 abc + 2 points to 'P'. abc + 2指向“ P”。 abc + 3 points to 'r' and so on. abc + 3指向“ r”,依此类推。

expressions abc[3] and abc[4] are converted to type int and equal to internal codes of characters 'r' and 'o' The difference of these codes is equal to 3, 表达式abc [3]和abc [4]转换为int类型,并且等于字符'r''o'的内部代码。这些代码的差等于3,

Thus expression abc+abc[3]-abc[4] can be written like abc + 3 and points to the forth element of the array that is to 'r' So the string starting from 'r' is outputed by statement 因此,表达式abc+abc[3]-abc[4]可以像abc + 3一样编写,并指向数组的abc+abc[3]-abc[4]个元素,即'r'因此从“ r”开始的字符串由语句输出

printf("%s\n", abc+abc[3]-abc[4]);

On virtually every platform (eg one using ASCII), the behaviour is undefined. 在几乎所有平台上(例如,使用ASCII的平台),行为都是不确定的。 So, none of the answers is correct. 因此,没有答案是正确的。

abc+abc[3]-abc[4]

This expression is equal to 这个表达式等于

abc + 'r' - 'o'

the values of character constants are implementation-dependent; 字符常量的值取决于实现; assuming an ASCII system, where r and o are 0x6f and 0x72, respectively, abc + 0x6f is out of the bounds of the array abc , leading to undefined behaviour. 假设ASCII系统,其中ro分别为0x6f和0x72,则abc + 0x6f超出数组abc的范围,从而导致不确定的行为。 Note, that the later subtraction, appearing to bring the result back into bounds, doesn't change that; 注意,后面的减法似乎使结果回到了极限,并没有改变。 once reached, one cannot recover from undefined behaviour. 一旦达到,就无法从不确定的行为中恢复过来。

That said, the result is likely to be the same as abc + ('r' - 'o') , which, on an ASCII system, is abc + (0x6f - 0x72) , which in turn is abc + 3 and thus rogramming is outputted, as reported in the question. 也就是说,结果很可能与abc + ('r' - 'o') ,在ASCII系统上,其结果为abc + (0x6f - 0x72) ,而后者又是abc + 3 ,因此进行了rogramming如问题中所述,输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM