用printf打印字符

时间:2011-01-19 14:58:45

标签: c++ c printf sizeof

这两个代码是否相同

char ch = 'a';
printf("%d", ch);

它会打印垃圾值吗?

我对此感到困惑

printf("%d", '\0'); 

这会打印0还是垃圾值? 因为当我这样做时

printf("%d", sizeof('\n')); 

它打印4.为什么sizeof('\n') 4个字节? C ++中的相同内容打印1个字节。那是为什么?

所以这是主要问题

c语言中的

printf("%d", '\0')应该打印0

并且在C ++中printf("%d", '\0')应该打印垃圾?

6 个答案:

答案 0 :(得分:35)

%d打印一个整数:它将打印角色的ascii表示。您需要的是%c

printf("%c", ch);

printf("%d", '\0');打印'\0'的ascii表示形式,为0(通过转义为0,告诉编译器使用ascii值为0。

printf("%d", sizeof('\n'));打印4,因为字符文字是int,在C中,而不是char

答案 1 :(得分:8)

这应该打印字符的ASCII值,因为%d是整数的转义序列。因此,作为printf参数给出的值在打印时被视为整数。

char ch = 'a';
printf("%d", ch);

同样适用于printf("%d", '\0');,其中NULL字符被解释为0整数。

最后,sizeof('\n')是4,因为在C中,这个字符表示法代表相应的ASCII整数。所以'\ n'与整数10相同。

这完全取决于你给字节的解释。

答案 2 :(得分:6)

在C中,'\n''a'等字符常量表达式具有类型int(因此sizeof '\n' == sizeof (int)),而在C ++中,它们具有类型char

语句printf("%d", '\0');应该只打印0;表达式'\0'的类型为int,其值为0.

语句printf("%d", ch);应打印ch中值的整数编码(对于ASCII,'a' == 97)。

答案 3 :(得分:5)

在C char中,表达式中的int被提升为{{1}}。如果你考虑的话,这几乎可以解释每个问题。

资料来源:Brian W.Kernighan和Dennis M.Ritchie的C编程语言

如果你想学习C,必须阅读。

另见this stack overflow page,那些经验丰富的人可以比我更好地解释它。

答案 4 :(得分:3)

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

int func(char a, char b, char c) /* demonstration that char on stack is promoted to int !!!
                                    note: this promotion is NOT integer promotion, but promotion during handling of the stack. don't confuse the two */
{
  const char *p = &a;
  printf("a=%d\n"
         "b=%d\n"
         "c=%d\n", *p, p[-(int)sizeof(int)], p[-(int)sizeof(int) * 2]); // don't do this. might probably work on x86 with gcc (but again: don't do this)
}


int main(void)
{
  func(1, 2, 3);

  //printf with %d treats its argument as int (argument must be int or smaller -> works because of conversion to int when on stack -- see demo above)
  printf("%d, %d, %d\n", (long long) 1, 2, 3); // don't do this! Argument must be int or smaller type (like char... which is converted to int when on the stack -- see above)



  // backslash followed by number is a oct VALUE
  printf("%d\n", '\377');             /* prints -1   -> IF char is signed char: char literal has all bits set and is thus value -1.
                                                     -> char literal is then integer promoted to int. (this promotion has nothing to do with the stack. don't confuse the two!!!) */
                                      /* prints 255  -> IF char is unsigned char: char literal has all bits set and is thus value 255.
                                                     -> char literal is then integer promoted to int */


  // backslash followed by x is a hex VALUE
  printf("%d\n", '\xff');             /* prints -1   -> IF char is signed char: char literal has all bits set and is thus value -1.
                                                     -> char literal is then integer promoted to int */
                                      /* prints 255  -> IF char is unsigned char: char literal has all bits set and is thus value 255.
                                                     -> char literal is then integer promoted to int */


  printf("%d\n", 255);                // prints 255


  printf("%d\n", (char)255);          // prints -1   -> 255 is cast to char where it is -1
  printf("%d\n", '\n');               // prints 10   -> Ascii newline has VALUE 10. The char 10 is integer promoted to int 10
  printf("%d\n", sizeof('\n'));       // prints 4    -> Ascii newline is char, but integer promoted to int. And sizeof(int) is 4 (on many architectures)
  printf("%d\n", sizeof((char)'\n')); // prints 1    -> Switch off integer promotion via cast!

  return 0;
}

答案 5 :(得分:-2)

是的,除非你很幸运,否则会打印GARBAGE。

非常重要。

printf / sprintf / fprintf参数的类型必须与关联的格式类型char匹配。

如果类型不匹配并且编译,则结果非常不确定。

许多较新的编译器了解printf并在类型不匹配时发出警告。如果你收到这些警告,请修复它们。

如果要为变量函数的参数转换类型,则必须提供强制转换(即显式转换),因为编译器无法确定是否需要执行转换(因为它可以与函数原型一起使用)键入的参数)。

printf("%d\n", (int) ch)

在这个例子中,printf是TOLD,堆栈上有一个“int”。演员确保无论sizeof返回什么东西(通常是某种长整数),printf都会得到一个int。

printf("%d", (int) sizeof('\n'))