C Reference


Built in operators

OperatorsDescriptionExample UsageAssociativity
Postfix operatorsLeft to right
()function call operatorswap (x, y)
[]array index operatorarr [i]
.member access operator
for an object of struct/union type
or a reference to it
obj.member
->member access operator
for a pointer to an object of
struct/union type
ptr->member

Unary OperatorsRight to left
!logical not operator!eof_reached
~bitwise not operator~mask
+ -[2]unary plus/minus operators-num
++ --post-increment/decrement operatorsnum++
++ --pre-increment/decrement operators++num
&address-of operator&data
*indirection operator*ptr
sizeofsizeof operator for expressionssizeof 123
sizeof()sizeof operator for typessizeof (int)
(type)cast operator(float)i

Multiplicative OperatorsLeft to right
* / %multiplication, division and
modulus operators
celsius_diff * 9.0 / 5.0

Additive OperatorsLeft to right
+ -addition and subtraction operatorsend - start + 1

Bitwise Shift OperatorsLeft to right
<<left shift operatorbits << shift_len
>>right shift operatorbits >> shift_len

Relational Inequality OperatorsLeft to right
< > <= >=less-than, greater-than, less-than or
equal-to, greater-than or equal-to
operators
i < num_elements

Relational Equality OperatorsLeft to right
== !=equal-to, not-equal-tochoice != 'n'

Bitwise And OperatorLeft to right
&bits & clear_mask_complement

Bitwise Xor OperatorLeft to right
^bits ^ invert_mask

Bitwise Or OperatorLeft to right
|bits | set_mask

Logical And OperatorLeft to right
&&arr != 0 && arr->len != 0

Logical Or OperatorLeft to right
||arr == 0 || arr->len == 0
Conditional OperatorRight to left
?:size != 0 ? size : 0

Assignment OperatorsRight to left
=assignment operatori = 0
+= -= *= /=
%= &= |= ^=
<<= >>=
shorthand assignment operators
(foo op= bar represents
foo = foo op bar)
num /= 10

Comma OperatorLeft to right
,i = 0, j = i + 1, k = 0

Data types

TypeSize in BitsCommentsAlternative Names
Primitive Types in ANSI C (C89)/ISO C (C90)
char≥ 8sizeof gives the size in units of chars. These “C bytes” need not be 8-bit bytes (though commonly they are); the number of bits is given by the CHAR_BIT macro in the limits.h header. Signedness is implementation-defined. Any encoding of 8 bits or less (e.g. ASCII) can be used to store characters. Integer operations can be performed portably only for the range 0 ~ 127. All bits contribute to the value of the char, i.e. there are no “holes” or “padding” bits.
signed charsame as charCharacters stored like for type char. Can store integers in the range -127 ~ 127 portably[3].
unsigned charsame as charCharacters stored like for type char. Can store integers in the range 0 ~ 255 portably.
short≥ 16, ≥ size of charCan store integers in the range -32767 ~ 32767 portably[4]. Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to using int.short int, signed short, signed short int
unsigned shortsame as shortCan store integers in the range 0 ~ 65535 portably. Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to using int.unsigned short int
int≥ 16, ≥ size of shortRepresents the “normal” size of data the processor deals with (the word-size); this is the integral data-type used normally. Can store integers in the range -32767 ~ 32767 portably[4].signed, signed int
unsigned intsame as intCan store integers in the range 0 ~ 65535 portably.unsigned
long≥ 32, ≥ size of intCan store integers in the range -2147483647 ~ 2147483647 portably[5].long int, signed long, signed long int
unsigned longsame as longCan store integers in the range 0 ~ 4294967295 portably.unsigned long int
float≥ size of charUsed to reduce memory usage when the values used do not vary widely. The floating-point format used is implementation defined and need not be the IEEE single-precision format. unsigned cannot be specified.
double≥ size of floatRepresents the “normal” size of data the processor deals with; this is the floating-point data-type used normally. The floating-point format used is implementation defined and need not be the IEEE double-precision format. unsigned cannot be specified.
long double≥ size of doubleunsigned cannot be specified.

Primitive Types added to ISO C (C99)
long long≥ 64, ≥ size of longCan store integers in the range -9223372036854775807 ~ 9223372036854775807 portably[6].long long int, signed long long, signed long long int
unsigned long longsame as long longCan store integers in the range 0 ~ 18446744073709551615 portably.unsigned long long int
intmax_tthe maximum width supported by the platformCan store integers in the range -(1 << n-1)+1 ~ (1 << n-1)-1, with ‘n’ the width of intmax_t. Used by the “j” length modifier in the C Programming/File IO#Formatted output functions: the printf family of functions.
uintmax_tsame as intmax_tCan store integers in the range 0 ~ (1 << n)-1, with ‘n’ the width of uintmax_t.

User Defined Types
struct≥ sum of size of each memberSaid to be an aggregate type.
union≥ size of the largest memberSaid to be an aggregate type.
enum≥ size of charEnumerations are a separate type from ints, though they are mutually convertible.
typedefsame as the type being given a nametypedef has syntax similar to a storage class like static, register or extern.

Derived Types[7]
type*

(pointer)
≥ size of char0 always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer. Pointers to different types may have different representations, which means they could also be of different sizes. So they are not convertible to one another. Even in an implementation which guarantess all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other. For functions taking variable number of arguments, the arguments passed must be of appropriate type, so even 0 must be cast to the appropriate type in such function-calls.
type [integer[8]]

(array)
integer × size of typeThe brackets ([]) follow the identifier name in a declaration. In a declaration which also initializes the array (including a function parameter declaration), the size of the array (the integer) can be omitted. type [] is not the same as type*. Only under some circumstances one can be converted to the other.
type (comma-delimited list of types/declarations)

(function)
Functions declared without any storage class are extern. The parentheses (()) follow the identifier name in a declaration, e.g. a 2-arg function pointer: int (* fptr) (int arg1, int arg2).