頭段時間我的代碼,有一個 unsigned char,我需要從sscanf 中讀取字符串為這個值。但是一般char 是用%c的,我是要值得。
所以我使用了%d %u來讀,結果報警告:
unsigned char a;
sscanf("15","%u",&a);
warning: format ‘%u’ expects type ‘unsigned int*’, but argument 3 has type ‘unsigned char*’
警告挺煩人的,查了下,才發現自己沒有注意過的細節:
I'm trying to use this code to read values between 0 to 255 (unsigned char). #include<stdio.h> int main(void) { unsigned char value; /* To read the numbers between 0 to 255 */ printf("Please enter a number between 0 and 255 \n"); scanf("%u",&value); printf("The value is %u \n",value); return 0; } I do get the following compiler warning as expected. warning: format ‘%u’ expects type ‘unsigned int *’, but argument 2 has type ‘unsigned char *’ And this is my output for this program. Please enter a number between 0 and 255 45 The value is 45 Segmentation fault I do get the segmentation fault while running this code. What is the best way to read unsigned char values using scanf? c share|edit asked Jan 3 at 20:09 user1293997 766 use %c to read a byte – TJD Jan 3 at 20:12 4 Actually %hhu for unsigned char. – Joe Jan 3 at 20:15 @TJD. I don't want to read a char. I would like to read values between 0 to 255. – user1293997 Jan 3 at 20:15 @Joe. That worked great. Thanks a lot. – user1293997 Jan 3 at 20:17 user1293997: you might want to write an answer and accept it (assuming @Joe is not interested in doing so). The only currently standing answer is quite incorrect. – user4815162342 Jan 3 at 20:34
%hhu 指定signed char 或 unsigned char char是1個字節
%h 指定short int 或者unsigned short int short 是2個字節
C中格式字符串printf()的一般形式為: %[標志][輸出最小寬度][.精度][長度]類型, 其中方括號[]中的項為可選項。各項的意義介紹如下: 1.類型: 表示輸出類型的格式字符 格式字符意義 a 浮點數、十六進制數字和p-計數法(C99) A 浮點數、十六進制數字和p-計數法(C99) c 輸出單個字符 d 以十進制形式輸出帶符號整數(正數不輸出符號) e 以指數形式輸出單、雙精度實數 E 以指數形式輸出單、雙精度實數 f 以小數形式輸出單、雙精度實數 g 以%f%e中較短的輸出寬度輸出單、雙精度實數,%e格式在指數小於-4或者大 於等於精度時使用 G 以%f%e中較短的輸出寬度輸出單、雙精度實數,%e格式在指數小於-4或者大於等於精度時使用 i 有符號十進制整數(與%d相同) o 以八進制形式輸出無符號整數(不輸出前綴O) p 指針 s 輸出字符串 x 以十六進制形式輸出無符號整數(不輸出前綴OX) X 以十六進制形式輸出無符號整數(不輸出前綴OX) u 以十進制形式輸出無符號整數 2.標志 標志字符為-、+、#、空格和0五種,其意義下表所示: 標志格式字符 標 志 意 義 - 結果左對齊,右邊填空格 + 輸出符號(正號或負號) 空格 輸出值為正時冠以空格,為負時冠以負號 # 對c,s,d,u類無影響;對o類,在輸出時加前綴0;對x類, 在輸出時加前綴0x或者0X; 對g,G 類防止尾隨0被刪除;對於所有的浮點形式,#保證了即使不跟任何數字,也打印一個小數點字符 0 對於所有的數字格式,用前導0填充字段寬度,若出現-標志或者指定了精度(對於整數),忽略 3.輸出最小寬度 用十進制整數來表示輸出的最少位數。若實際位數多於定義的寬度,則按實際位數輸出,若實際位數少於定義的寬度則補以空格或0。 4.精度 精度格式符以“.”開頭,后跟十進制整數。本項的意義是:如果輸出數字,則表示小數的位數;如果輸出的是字符,則表示輸出字符的個數;若實際位數大於所定義的精度數,則截去超過的部分。 5.長度 長度格式符為h,l兩種,h表示按短整型量輸出,l表示按長整型量輸出。 h和整數轉換說明符一起使用,表示一個short int 或者unsigned short int類型的數值 ,示例: %hu,%hx,%6.4hd hh和整數轉換說明符一起使用,表示一個short int 或者unsigned short類型的數值 ,示例: %hhu,%hhx,%6.4hhd j和整數轉換說明符一起使用,表示一個intmax_t或者uintmax_t類型的數值 ,示例: %jd,%8jx l和整數轉換說明符一起使用,表示一個long int 或者unsigned long int類型的數值 ,示例: %ld,%8lu ll和整數轉換說明符一起使用,表示一個long int 或者unsigned long int類型的數值 (C99),示例: %lld,%8llu L和浮點轉換說明符一起使用,表示一個long double的值,示例:%Lf,%10.4Le t和整數轉換說明符一起使用,表示一個ptrdiff_t值(兩個指針之間的差相對應的類型)(C99),示例: %td,%12ti z和整數轉換說明符一起使用,表示一個size_t值(sizeof返回的類型)(C99),示例:%zd,%12zx 使用printf函數時還要注意一個問題,那就是輸出表列中的求值順序。不同的編譯系統不一定相同,可以從左到右,也可從右到左。Turbo C是按從右到左進行的。
http://www.cppblog.com/zenliang/archive/2010/11/07/132858.html
http://m.blog.csdn.net/blog/kangear/8952033
http://www.cnblogs.com/ilegend/archive/2011/11/22/2258701.html
順道 道一句:%uud 也是可以得,是有符號的
http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html
