本文介绍了请解释这个结果. printf(“%c",'abcd')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

int main()
{
    printf("%c\n", 'abcd');
    printf("%p\n", 'abcd');
    printf("%c\n", 0x61626364);
    printf("%c\n", 0x61626363);
    printf("%c\n", 0x61626365);
    return 0;
}

我想问这一行:printf(%c \ n",'abcd');
在此行中,结果为"d",但我不明白为什么会出现"d".
我试图寻找其他回忆.在这种情况下,我发现其他记忆都包含所有字母.
请向我解释为什么结果为"d",以及为什么其他记忆包含所有字母.
谢谢.

解决方案

您的代码错误.当您使用最新的 GCC 编译器进行编译时,使用以下警告功能

gcc -Wall -Wextra u.c

你得到

 u.c: In function 'main':
 u.c:5:20: warning: multi-character character constant [-Wmultichar]
      printf("%c\n", 'abcd');
                     ^
 u.c:6:20: warning: multi-character character constant [-Wmultichar]
      printf("%p\n", 'abcd');
                     ^
 u.c:6:5: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'int' [-Wformat=]
      printf("%p\n", 'abcd');
      ^

从技术上讲,您身处令人反感的未定义行为案例 (对于多字符常量,则为 未指定行为 ),以及任何符合标准的实施都可能发生.

要解释真正发生的情况,它是特定于实现的(取决于编译器,处理器,优化标志,ABI,运行时环境……),您需要深入研究细节(首先看一下gcc -fverbose-asm -S生成的汇编代码)并放入您的 libc 特定的printf实现中. /p>

根据经验,您应该改进代码,以消除编译器能够给您的所有警告(您的编译器有助于警告您).它们是一些微妙的异常(但是您应该在代码中注释掉它们).

#include <stdio.h>

int main()
{
    printf("%c\n", 'abcd');
    printf("%p\n", 'abcd');
    printf("%c\n", 0x61626364);
    printf("%c\n", 0x61626363);
    printf("%c\n", 0x61626365);
    return 0;
}

I want to ask this line : printf("%c\n", 'abcd');
In this line, the result is 'd' but, I can't understand why 'd' is come out.
I tried to look other memories. In this situation, I found other memories have all alphabets.
Please explain me why result is 'd' and why other memories have all alphabets.
Thank you.

解决方案

Your code is wrong. When you compile it with a recent GCC compiler enabling warnings with

gcc -Wall -Wextra u.c

you get

 u.c: In function 'main':
 u.c:5:20: warning: multi-character character constant [-Wmultichar]
      printf("%c\n", 'abcd');
                     ^
 u.c:6:20: warning: multi-character character constant [-Wmultichar]
      printf("%p\n", 'abcd');
                     ^
 u.c:6:5: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'int' [-Wformat=]
      printf("%p\n", 'abcd');
      ^

Technically, you are in the awful undefined behavior case (and unspecified behavior for the multi-character constants), and anything could happen with a standard compliant implementation.

To explain what really happens, it is implementation specific (depends upon the compiler, the processor, the optimization flags, the ABI, the runtime environment, ....) and you need to dive into gory details (first look at the generated assembler code with gcc -fverbose-asm -S) and into your libc particular printf implementation.

As a rule of thumb, you should improve your code to get rid of every warnings your compiler is able to give you (your compiler is helpful in warning you). They are few subtle exceptions (but then you should comment your code about them).

这篇关于请解释这个结果. printf(“%c",'abcd')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:42