可能重复:
为什么C字符文字的内部而不是字符?
int main(void)
{
printf("%d %d\n", sizeof('c'), sizeof(char));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么sizeof('c')返回4而不是1?
在C中,以下代码:
#include<stdio.h>
int main()
{
char c='a';
printf("%d %d",sizeof(c),sizeof('a'));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产生结果1和4?请解释逻辑?
另外,为什么会sizeof(main())导致4但sizeof(main)导致1:
#include<stdio.h>
int main()
{
printf("%d %d\n",sizeof(main), sizeof(main()));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++中为什么会sizeof('a')导致1而sizeof('av')导致4?
我正在学习物理,在我们的编程课程的讲义中,写了一个字符常量,在C中有char类型,其中字符常量我的意思是像'x'这样的表达式.在询问我的讲师是否是一个错误之后,他说不.在向他展示了C90,C99和C11之后,清楚地写明了一个字符常量有int类型,他仍然没有说这是一个错误.
所以在再次问他之前,我想向自己保证我得到了正确的方法,为什么会这样,因为它似乎浪费了记忆.我发现的一切都是为什么会这样,是因为历史原因,这是相当含糊的.另外我想知道为什么在C++中,他们将字符常量的类型更改为char.
编辑:非常感谢你的答案.
根据此Unix文档http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html
The memset() function is preferred over bzero().
For maximum portability, it is recommended to replace
the function call to bzero() as follows:
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
Run Code Online (Sandbox Code Playgroud)
但是void *memset(void *s, int c, size_t n);第二个arg是一个整数,所以为什么他们建议使用'\ 0'而不是0?memset()文档说:“ memset()函数应将c(转换为无符号char)arg 复制到s所指向对象的前n个字节中的每个字节中。” 是更有效还是更明确?这里有最佳实践吗?
#include<stdio.h>
int main()
{
char ch;
fflush(stdin);
ch=getchar();
printf("ch= %d a=%d char=%d", sizeof(ch),sizeof('a'),sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)
我输入'a'(不带引号)作为输入,我在gcc 4.5.1版中得到的输出是:
ch= 1 a=4 char=1
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如果sizeof(c)是1,那又怎么sizeof('a')会4?
我是C的新手,在我的一次练习中遇到了一些我无法绕过的东西.当我检查一个tabel元素的大小(这里是'b')比我得到4.然而,如果我检查'char'而不是我得到1.怎么样?
# include <stdio.h>
int main(){
char tabel[10] = {'b','f','r','o','a','u','v','t','o'};
int size_tabel = (sizeof(tabel));
int size_char = (sizeof('b'));
/*edit the above line to sizeof(char) to get 1 instead of 4*/
int length_tabel = size_tabel/size_char;
printf("size_tabel = %i, size_char = %i, lengte_tabel= %i",size_tabel,
size_char,length_tabel);
}
Run Code Online (Sandbox Code Playgroud) void main()
{
printf("%d\n",sizeof('1'));
}
Run Code Online (Sandbox Code Playgroud)
产量:4
void main()
{
char a='1';
printf("%d\n",sizeof(a));
}
Run Code Online (Sandbox Code Playgroud)
输出:1
有人可以说为什么会有所不同?
最近在CCS标准库中我见过这样的东西
函数正在使用unsigned int参数.
delay(unsigned int)
Run Code Online (Sandbox Code Playgroud)
这是以这种方式使用的
delay(~(0));
Run Code Online (Sandbox Code Playgroud)
现在如何解释(〜(0))?
如果我这样使用
printf("%d",(~(0)));
Run Code Online (Sandbox Code Playgroud)
它如何处理签名int?unsigned int?长整数?还是unsigned char?
它如何依赖于系统?c规格说的是什么?
直到今天,我的回答是:"不,它必须有一个整数,它决定了数组的位置."
但现在我从我们的教授那里获得了这段代码片段(用于base64解码),我也在stackoverflow和其他网站上找到了它:
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
void build_decoding_table1() {
int i; …Run Code Online (Sandbox Code Playgroud) 下面的代码在C中输出为1,4,4,而在C++中输出为1,1,4
#include<stdio.h>
int main()
{
char ch = 'A'; //initialise
//printing output
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}
Run Code Online (Sandbox Code Playgroud)