Category Archives: C Language

freestanding program

https://en.cppreference.com/w/c/language/basic_concepts 裡提到 A C program is a sequence of text files (typically header and source files) that contain declarations. They undergo translation to become an executable program, which is executed when the OS calls its main function (unless it is … Continue reading

Posted in C Language | Leave a comment

division & modulus

a / b, a % b 當b為0時, behavior undefined,這邊討論其中一個operand為負數的情況 C89 整數除法(有負數情況下 implementation defined) -9/7 有可能是 -1 or -2 (rounded up or rounded down) 當然此時對應的 % 就會根據除法的結果而不同 (這邊討論整數的modulus,floating point可以參考fmod) 要符合 dividend = divisor * quotient + remainder -9 = 7 * … Continue reading

Posted in C Language, C++ Language | Leave a comment

show gcc predefined macro

gcc -dM -E – < /dev/null 這邊僅列出有__STDC的作為範例 #define __STDC_HOSTED__ 1 #define __STDC_UTF_16__ 1 #define __STDC_IEC_559__ 1 #define __STDC_ISO_10646__ 201505L #define __STDC_NO_THREADS__ 1 #define _STDC_PREDEF_H 1 #define __STDC_IEC_559_COMPLEX__ 1 #define __STDC_VERSION__ 201112L #define __GNUC_STDC_INLINE__ 1 #define __STDC_UTF_32__ 1 #define __STDC__ 1 … Continue reading

Posted in C Language | Leave a comment

C99 _Bool

C99定義了關鍵字 _Bool, 又另外新增了stdbool.h 將 #define bool _Bool #define true 1 #define false 0 為什麼用_Bool這樣看起來有點奇怪的命名而不直接定義bool? 最主要的原因是因為在c89 bool並非保留字, 因此在擴展規範時只能從reserved identifier下手了 在c89的規範 7.1.3第一項提到 All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any … Continue reading

Posted in C Language | Leave a comment

gcc flags about c standard

以gcc 5.1.0為例 https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Standards.html The default, if no C language dialect options are given, is -std=gnu11. 如果要採用c89(or c90) 可使用下面的選項之一 -ansi -std=c89 -std=c90 -std=iso9899:1990 這樣會關掉一些gnu extension 如果要對應C標準的要求 則再加上 -pedantic or -pedantic-errors 即 -std=c89 -pedantic

Posted in C Language | Leave a comment