/**** Program to find the sizeof string literal ****/
#include<stdio.h>
int main(void)
{
printf("%d\n",sizeof("a"));
/***The string literal here consist of a character and null character,
so in memory the ascii values of both the characters (0 and 97) will be
stored respectively which are found to be in integer datatype each
occupying 4 bytes. why is the compiler returning me 2 bytes instead of 8 bytes?***/
return 0;
}
输出:我是c编程的初学者,需要sizeof()字符串常量的帮助吗?
2
“char”是一个1字节的整数类型。 –
它由一个字符和一个空字符组成? – Jaivvignesh
字符串文字具有“char”数组类型,并在内存中表示为以空字符结尾的字符数组。 –