user-0 提问: 关于C的一个小问题

回复:
user: user-1
content: 看看ASCII码就知道，空格符码值是32，同'a', 'b', 'c'一样属于可打印字符。它本身是一个字符，占用的字节数是1。 如果哪天C标准规定char型值是2个字节或4个字节，那空格符就占2个或4个字节，但它本身还是一个字符。

user: user-2
content: 我觉得像这中情况，自己编程可以更快得到答案！sizeof

user: user-3
content: 空格应该就是占1个字符吧，我也是新手

user: user-4
content: 空格占 1个字符

user: user-5
content: 空格多了！ 参考下面：#pragma comment(lib,"gdi32") #include <windows.h> #include <stdio.h>  int main() {     const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;      PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);     pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);     pbmi->bmiHeader.biWidth = uWidth;     pbmi->bmiHeader.biHeight = uHeight;     pbmi->bmiHeader.biPlanes = 1;     pbmi->bmiHeader.biBitCount = 1;     pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;     pbmi->bmiColors[0].rgbBlue = 0;     pbmi->bmiColors[0].rgbGreen = 0;     pbmi->bmiColors[0].rgbRed = 0;     pbmi->bmiColors[1].rgbBlue = 255;     pbmi->bmiColors[1].rgbGreen = 255;     pbmi->bmiColors[1].rgbRed = 255;      HDC hDC = CreateCompatibleDC (0);     void * pvBits;     HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);     SelectObject (hDC, hBitmap);     HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体"); //  HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, "宋体");     SelectObject (hDC, hFont);     BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);      char c[4];     int i, j;     for (i = 128; i < 256; i++) {         sprintf (c, "%02X", i);         TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);     }     for (j = 0; j < 256; j++) {         sprintf (c, "%02X", j);         TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);     }     for (i = 128; i < 256; i++) {         for (j = 0; j < 256; j++) {             c[0] = (char) i;             c[1] = (char) j;             TextOut (hDC, (j + 1) * 17 + 1, (i - 127) * 17 + 1, c, 2);         }     }     for (i = 0; i < 130; i++) {         MoveToEx (hDC, 0, i * 17, NULL);         LineTo (hDC, uWidth, i * 17);     }     for (j = 0; j < 258; j++) {         MoveToEx (hDC, j * 17, 0, NULL);         LineTo (hDC, j * 17, uHeight);     }      BITMAPFILEHEADER bmfh;     bmfh.bfType = *(PWORD) "BM";     bmfh.bfReserved1 = 0;     bmfh.bfReserved2 = 0;     bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;     bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;      HANDLE hFile = CreateFile ("goal.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);     if (hFile != INVALID_HANDLE_VALUE) {         DWORD dwWritten;         WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);         WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);         WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);          CloseHandle (hFile);     }      DeleteObject (hFont);     DeleteObject (hBitmap);     DeleteDC (hDC);     LocalFree (pbmi);      return 0; }  生成的goal.bmp

user: user-5
content: 不要迷信书、考题、老师、回帖； 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实！

user: user-6
content: #include <stdio.h> int main() {     printf("size of char: %ld byte(s)\n", sizeof(char));     const char space = ' ';     printf("size of one space: %ld byte(s)\n", sizeof(space));     int char_num_per_space = sizeof(space) / sizeof(char);     printf("amount of char in one space: %d\n", char_num_per_space); }  Output:

user: user-7
content: 建议查阅ascii，空格占1个字符，但是tab,'\t'可能是4个空格或者8个空格（可以配置的），但是'\t'就是一个字符

user: user-8
content: 空格占 1个字符

user: user-9
content: 1个                                      。