在C / C单引号和双引号++ [英] Single and double quotes in C/C++

查看:145
本文介绍了在C / C单引号和双引号++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题单引号与双引号用C 。我不能完全理解给出这样的解释我写了一个程序

I was looking at the question Single quotes vs. double quotes in C. I couldn't completely understand the explanation given so I wrote a program

#include <stdio.h>
int main()
{
  char ch = 'a';
  printf("sizeof(ch) :%d\n", sizeof(ch));
  printf("sizeof(\'a\') :%d\n", sizeof('a'));
  printf("sizeof(\"a\") :%d\n", sizeof("a"));
  printf("sizeof(char) :%d\n", sizeof(char));
  printf("sizeof(int) :%d\n", sizeof(int));
  return 0;
}

我同时使用gcc和g ++编译它们,这些是我的输出

I compiled them using both gcc and g++ and these are my outputs

GCC:

sizeof(ch)   : 1  
sizeof('a')  : 4  
sizeof("a")  : 2  
sizeof(char) : 1  
sizeof(int)  : 4  

G ++:

sizeof(ch)   : 1  
sizeof('a')  : 1  
sizeof("a")  : 2  
sizeof(char) : 1  
sizeof(int)  : 4  

在G ++输出对我来说很有意义,我没有这方面的任何疑问。在GCC什么是需要有的sizeof('A')是的sizeof(char)的距离不同的。有一些背后的实际原因,或只是历史?

The g++ output makes sense to me and I don't have any doubt regarding that. In gcc what is the need to have sizeof('a') to be different from sizeof(char). Is there some actual reason behind it or is it just historical?

如果还用C 字符'A'有不同大小不,当我们正在做的意思
字符CH ='A'; 我们正在做的隐式类型转换

Also in C if char and 'a' have different size does that mean when we are doing char ch = 'a'; we are doing implicit type-conversion?

推荐答案

在C,字符常量如'A'具有类型 INT ,在C ++中它的字符

In C, character constants such as 'a' have type int, in C++ it's char.

至于最后一个问题,是的,

Regarding the last question, yes,

char ch = 'a';

引起 INT 的隐式转换字符

这篇关于在C / C单引号和双引号++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆