简体   繁体   English

反向打印的字符串,不反转代码

[英]string printed in reverse without reversing in the code

I tried to print the string by declaring it via ternary operator. 我试图通过三元运算符声明它来打印字符串。 Instead of any error it prints string in reverse. 代替任何错误,它会反向打印字符串。

Can someone explain please? 有人可以解释一下吗?

I was expecting string to be printed as even or odd based on the input. 我期望基于输入将字符串打印为evenodd

 #include<stdio.h>

    void main()
    {
        int a;
        char *num;

        printf("Enter a number: ");
        scanf("%d", &a);

        num = (a % 2) ? 'odd' : 'even';

        printf("Its %s number", &num);

        getch();
    }

'odd' and 'even' are integer character constants. 'odd''even'是整数字符常量。

According to the C Standard (6.4.4.4 Character constants) 根据C标准(6.4.4.4字符常量)

10 An integer character constant has type int. 10整数字符常量的类型为int。 The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. 包含映射到单字节执行字符的单个字符的整数字符常量的值是解释为整数的映射字符表示形式的数值。 The value of an integer character constant containing more than one character (eg, 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. 包含多个字符(例如,“ ab”)或不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。

So in this call 所以在这次通话中

printf("Its %s number", &num);

you are trying to output the address of the variable num as an address of a string that is you are trying to output a string but num does not point to a string. 您正在尝试将变量num的地址输出为要尝试输出字符串的字符串的地址,但是num并不指向字符串。

You have to use string literals instead of the character constants. 您必须使用字符串文字而不是字符常量。

What you mean is the following 你的意思是以下

    num = (a % 2) ? "odd" : "even";

    printf("Its %s number", num);

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

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