简体   繁体   English

C中“3”和“3”之间的差异

[英]Difference between “3” and '3' in C

I tried to run the following program in C and got some output. 我尝试在C中运行以下程序并获得一些输出。 Can you help me out why??? 你能帮帮我吗?

#include<stdio.h>

int main()
{
  char x='A';
  printf("%d%d%d",sizeof("3"),sizeof('3'),sizeof(3));
  return 0;
}

The output received is 2 4 4 using gcc in ubuntu 11.04 32 bit. 在ubuntu 11.04 32位中使用gcc收到的输出为2 4 4。

Similarly in other program:- 同样在其他计划中: -

#include<stdio.h>

int main()
{
  char x='A';
  printf("%d%d",sizeof('A'),sizeof(x));
  return 0;
}

The output received is 4 1 using GCC in ubuntu 11.04 32 bit. 在ubuntu 11.04 32位中使用GCC接收的输出为4 1。

Can you help me out why the output is this way??? 你能帮我解决为什么输出是这样的吗?

In C, char literals are of integer type, so sizeof('3') == sizeof(int) . 在C中, char文字是整数类型,因此sizeof('3') == sizeof(int) See this C FAQ for details. 有关详细信息,请参阅此C FAQ

This is one of the areas where C and C++ differ (in C++ sizeof('3') is 1 ). 这是C和C ++不同的领域之一(在C ++ sizeof('3')1 )。

Actually, correcting my previous assertion, sizeof("3") will yield 2 because "3" is treated as a 2-element character array. 实际上,纠正我之前的断言sizeof("3")将产生2,因为“3”被视为2元素字符数组。

6.3.2.1/3 6.3.2.1/3

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 除非它是sizeof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为''array of type''的表达式转换为类型为''指针的表达式type''指向数组对象的初始元素,而不是左值。

In C 在C.

3 : integer literal, type int 3 :整数文字,输入int
'3' : char literal, type int '3' :char literal,输入int
"3" : string literal: type char[2] "3" :字符串文字:类型char[2]

In your second example, x denotes an object of type char . 在第二个示例中, x表示char类型的对象。

"C" is “C”是

char mystring[2];
mystring[0] = 'C';
mystring[1] = '\0';

While 'C' is 虽然'C'是

int mychar;
mychar = 'C';

The '3' is converted to type int, which is 4 bytes. '3'转换为int类型,即4个字节。 However "3" is a string with two bytes. 但是“3”是一个包含两个字节的字符串。 The first byte is the char 3 and the second is the null terminator that gets appended to all strings. 第一个字节是char 3,第二个是附加到所有字符串的空终止符。

'3' is a character constant. '3'是一个字符常量。 In C, character constants have type int (in C++, they have type char ). 在C中,字符常量具有int类型(在C ++中,它们具有char类型)。 Thus, sizeof '3' == sizeof (int) . 因此, sizeof '3' == sizeof (int)

"3" is a string literal. "3"是一个字符串文字。 It is a 2-element array of char ( const char in C++) with the values {'3', '\\0'} . 它是一个2元素的char数组(C ++中的const char ),其值为{'3', '\\0'} sizeof (char) == 1 by definition, thus, sizeof "3" == 2. sizeof (char) == 1根据定义,因此, sizeof "3" == 2。

3 is an integer constant. 3是整数常数。 It has type int . 它有int类型。 Thus, sizeof 3 == sizeof (int) . 因此, sizeof 3 == sizeof (int)

The variable x is declared as char , thus sizeof x == 1. 变量x声明为char ,因此sizeof x == 1。

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

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