
C Programming Cheatsheet String Format Specifiers Character Escapes %[flags][width][.precision][length]specifier • \0 - NULL Constructs %c char • \b - backspace Do-While Loop • \f - form feed (new page) %hhd||%hhi signed char i=0; • \n - newline do { printf("%d\n", i); ++i;} %hhu unsigned char • \r - carriage return while(i<10); • \t - tab %hhn signed char* For Loop • \v - vertical tab for (i=0; i<10; ++i) { %lc wint_t Arithmetic Operators printf("%d\n", i); %ls wchar_t* + Addition } While Loop %s string - Subtraction register int i=0; %d || %i signed int * Multiplication while (i<10) { ++i; } If, else if, else %u unsigned int / Division if (0 == 1) { %hi short int % Modulus/Remainder register signed int ZERO = 0; } else if (8 <= 4) { %hu unsigned short int ++ Increment by 1 const float PIf = 3.14F; %hn short int* -- Decrement by 1 } else { static char SYM[3] = "π\0"; %l signed long int ++> Pre-increment and compare } %ln long int* --> Pre-decrement and compare Macros if #ifdef __linux__ %ll signed long long int Equality Operators # include "custom_header.h" %lln long long int* == Equal to # include <system_header.h> #endif %llu unsigned long long int != Not equal to Switch-case %f || %F float or double (%F is uppercase) < Less than switch (INPUT) { case 0: break; %Lf || %Le long double > Greater than default: break; %e || %E scientific notation (mantissa/exponent) <= Less than or equal to } Ternary Operator >= Greater than or equal to %g || %G shortest representation of %e||%E int out = (input == 7 ? 5 : 3); %o octal unsigned int Logical Operators Goto Operand Meaning Example label: %x lowercase hex unsigned int goto label; %X uppercase hex unsigned int && And (x && y) Define Datatype typedef struct { int x, y; } point_t; %a || %A hexadecimal float-point || Or (x || y) typedef union __number { %ji intmax_t ! Not !(x < y) int i; double d; } number_t; %ju uintmax_t Bitwise Operators Define Enum %jn intmax_t* enum cardsuit { & AND %zi || %zu size_t || ssize_t CLUBS = 0, | OR DIAMONDS, HEARTS, SPADES %zn size_t* }; ^ Exclusive OR (XOR) %ti || %tu ptrdiff_t Variable Aliases and Constants ~ Ones Complement (NOT) const double PI = 3.14159; %tn ptrdiff_t* << Left-shift const double *ARCHIMEDES_NUM = &PI; %p pointer address extern const double PI; // In Header >> Right-shift char PI_SYM[3] = "π\0"; // Unicode %n NULL Assignment Operators char PI_UTF8[] = u8"π\0"; %% literal % char16_t PI_UTF16[] = u"π\0"; Operand Meaning Equivalent char32_t PI_UTF32[] = U"π\0"; Width and Precision = Assign None Arrays %.3f float precision of 3 (like 3.141) double num[2] = { 3.14, 5.0 }; += Add X = X + Y %4d 4 digit wide int (like 2015) unsigned int LargeArray[2][4] = { -= Subtract X = X - Y { 0, 1, 2, 3 }, { 4, 5, 6, 7 } }; %2.2f 2 digits wide and 2 precise (19.95) char words[2][] = { "BSD", "AIX" }; *= Multiply X = X * Y Flags Order of Operations /= Divide X = X / Y - Left-justify () [] -> . :: ! ~ - + * & ++ -- %= Modulus X = X % Y + Right-justify * / % + - <<= Left-shift X = X << Y SPACE Blank space << >> < <= > >= >>= Right-shift X = X >> Y # Preceded hex & octal with "0x" || "0" != == & (Bitwise) &= AND X = X & Y 0 Left-pad with zeros ^ (Bitwise) | (Bitwise) Integer from variable - printf("%d", num); |= OR X = X | Y && || (Logical) Ternary operator Save integer to variable - scanf("%d", &num); ^= XOR X = X ^ Y Save string to variable - scanf("%s", str_var); Assignment Comma Operator Created by Devyn Collier Johnson <[email protected]> (2017) More cheatsheets at DCJTech.info C Programming Cheatsheet Datatypes int_fast16_t uint_fast16_t Literal Constant Prefixes NULL void int_fast32_t uint_fast32_t Octal 0 _Bool bool - <stdbool.h> int_fast64_t uint_fast64_t Binary 0b char16_t - <uchar.h> char32_t - <uchar.h> intptr_t uintptr_t Hexadecimal 0x char double <stdfix.h> char \u enum EOF - <stdio.h> _Fract _Accum wchar_t string L FILE - <stdio.h> fpos_t - <stdio.h> _Sat _Fract _Sat _Accum UTF-8 string u8 float imaxdiv_t - <inttypes.h> <decimal.h> UTF-16 string u int long _Decimal32 _Decimal64 UTF-32 string U long double long int _Decimal128 _Complex _Decimal32 Raw literal string R"delimiter(STRING)delimiter" long long long long int Literal Constant Suffixes Storage Classes • auto - Default specifier; Local-scope nullptr_t - <stddef.h> ptrdiff_t - <stddef.h> unsigned U || u • extern - Lasts the whole program, block, or sig_atomic_t - <signal,h> short unsigned long long ULL compilation unit; globally visible short char short int long L • register - Stored in stack or CPU-register during the code block size_t - <stddef.h> ssize_t - <stddef.h> float F • static - Lasts the whole program, block, or struct union double D compilation unit; private in program • typedef - Specifies a new datatype wctrans_t - <wchar.h> wctype_t - <wctype.h> long double L • _Thread_local - Thread-local-storage; one wchar_t - <wchar.h> __ibm128 __float80 W || w instance per thread Type Qualifiers WEOF - <wchar.h> wint_t - <wchar.h> __float128 Q || q • const - Value does not change; read-only signed unsigned __ibm128 W • restrict - For the lifetime of the pointer, the object can only be accessed via the pointer signed char unsigned char _Imaginary i • volatile - Optimizing-compilers must not signed int unsigned int _Complex128 KC change • _Atomic - Objects free from data races signed long unsigned long exponent E Function Specifiers signed long int unsigned long int __Decimal32 df || DF • inline - Inline the function when compiling signed long long unsigned long long __Decimal64 dd || DD • _Noreturn - The function does not return signed long long int unsigned long long int __Decimal128 dl || DL Function Attributes (__attribute__(())) GNU-GCC only signed short unsigned short short _Fract || _Sat short _Fract HR || hr Use in function declaration (header) https://gcc.gnu.org/onlinedocs/gcc/Function- signed short int unsigned short int _Fract || _Sat _Fract R || r Attributes.html __float80 __float128 long _Fract || _Sat long _Fract lr || LR https://gcc.gnu.org/onlinedocs/gcc/Common- Function-Attributes.html <complex.h> long long _Fract || _Sat long long llr || LLR _Fract • alias - The function is an alias for another; complex _Complex Example: void f () __attribute__ unsigned short _Fract || _Sat unsigned uhr || float complex float _Complex ((weak, alias ("__f"))); short _Fract UHR • aligned - Set alignment double complex double _Complex unsigned _Fract || _Sat unsigned ur || UR • always_inline - Inline the function long double complex long double _Complex _Fract • cold - Unlikely to execute; used for optimizations imaginary _Imaginary unsigned long _Fract and _Sat ulr || ULR unsigned long _Fract • constructor - Call function before main() float imaginary float _Imaginary • destructor - Call function after main() unsigned long long _Fract || _Sat ullr || • deprecated - Emit warning msg when called double imaginary double _Imaginary unsigned long long _Fract ULLR • error - Emit error message when called long double imaginary long double _Imaginary short _Accum || _Sat short _Accum hk || HK • flatten - Inline all functions in the function; __attribute__((flatten)) _Complex80 _Complex128 Accum || _Sat _Accum k || K • hot - Very likely to execute; used for <stdint.h> long _Accum || _Sat long _Accum lk || LK optimizations • nonnull - None of the input pointers are NULL intmax_t uintmax_t long long _Accum || _Sat long long llk || LLK • nothrow - The function is guaranteed not to _Accum int8_t uint8_t throw an exception unsigned short _Accum || _Sat uhk || • optimize - Set specific optimization options for int16_t uint16_t unsigned short _Accum UHK the function int32_t uint32_t unsigned _Accum || _Sat unsigned uk || UK • pure - The function accepts arguments, has single return, and has no other effects int64_t uint64_t _Accum • returns_twice - Returns two separate values int_least8_t uint_least8_t unsigned long _Accum || _Sat ulk || ULK • simd - Create multiple functions that can unsigned long _Accum process arguments using SIMD instructions int_least16_t uint_least16_t unsigned long long _Accum || _Sat ullk || • warning - Emit warning message when called int_least32_t uint_least32_t unsigned long long _Accum ULLK https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html int_least64_t uint_least64_t int_fast8_t uint_fast8_t Created by Devyn Collier Johnson <[email protected]> (2017) More cheatsheets at DCJTech.info C Programming Cheatsheet Type Attributes complex float-point • <locale.h> - Category macros GNU-GCC only • HC - Half Complex; 2 byte half-precision complex • <math.h> - Mathematical and trigonometric functions float-point <monetary.h> - Monetary unit string formatting https://gcc.gnu.org/onlinedocs/gcc/Type- • • SC - Single Complex; 4 byte single-precision complex• <mqueue.h> - Message queue Attributes.html float-point • <ndbm.h> - NDBM database operations • aligned - Set alignment • DC - Double Complex; 8 byte double-precision • <net/if.h> - List local network interfaces • deprecated - Emit warning msg when called complex float-point • <netdb.h> - Translating protocol and hostnames into • mode - Set type mode. Example: typedef • XC - Extended Complex; 12 byte extended-precision numeric addresses _Complex float complex float-point • <netinet/in.h> - Internet protocol and address family __attribute((mode(TC))) _Complex128; • TC - Tetra Complex; 16 byte tetra-precision complex definitions float-point • <netinet/tcp.h> - Additional TCP control options packed - Members of a struct or union are • • QQ - Quarter-Fractional; 1-byte • <nl_types.h> - Localization message catalog
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-