Previous Post | Top | Next Post |
TOC
This was originally written and created around 2013 and may require to be updated. (2021)
C Language
Except for some functional languages such as Scheme, many procedual languages share quite a bit of similarities. C language is a good start in terms of its gramatical simplicity.
Program control sequence keywords
C syntax | meaning |
---|---|
expression; |
simple expression statement |
{statement...} |
block statement |
if (expression) statement |
conditional execution |
if (expression) statement else statement |
conditional executions |
switch (expression) {case const-express: {statement... break;}... default: statement} |
conditional branch execution |
while (expression) statement |
conditional loop execution |
do statement while (expression); |
conditional loop execution |
for (i = 0; i++; i < n) statement |
conditional loop execution i = 0, 1, 2, …, n-1 |
return expression; |
return by exiting function |
continue; |
continue to the next loop execution |
break; |
break out of the loop/switch execution |
goto identifier |
goto identifier label in the current function |
Type, structure, union, storage class declarations
C declaration | meaning |
---|---|
void |
pointer type |
char |
byte type |
int |
integer type |
float |
single-precision floating point type |
double |
double-precision floating point type |
enum |
enumeration constant type |
short |
type qualifier |
long |
type qualifier |
signed |
type qualifier |
unsigned |
type qualifier |
const |
type qualifier |
volatile |
type qualifier |
union |
union declaration |
struct |
structure declaration |
auto |
storage class |
register |
storage class |
static |
storage class |
extern |
storage class |
typedef |
storage class |
Unary operators
C operator | meaning |
---|---|
- |
arith. minus |
~ |
bitwise not |
++ |
increment. |
-- |
decrement. |
sizeof |
object size |
(typename) |
type conversion |
* |
reference (pointer, indirection) |
& |
dereference (pointer, address of) |
Binary operators
C operator | meaning |
---|---|
+ |
arith. addition |
- |
arith. substraction |
* |
arith. multiplication |
/ |
arith. division |
% |
arith. modulo |
& |
bitwise and |
| |
bitwise or |
^ |
bitwise xor |
<< |
bitwise shiftL |
>> |
bitwise shiftR |
== |
equal. test. |
!= |
non-equal. test. |
< |
less than |
<= |
less or equal. |
> |
more than |
>= |
more or equal. |
! |
bool. not |
&& |
bool. and |
|| |
bool. or |
= |
assign. |
+= |
aug. assign. with arith. addition |
-= |
aug. assign. with arith. substraction |
*= |
aug. assign. with arith. multiplication |
/= |
aug. assign. with arith. division |
%= |
aug. assign. with arith. modulo |
&= |
aug. assign. with bitwise and |
|= |
aug. assign. with bitwise or |
^= |
aug. assign. with bitwise xor |
<<= |
aug. assign. with bitwise shiftL |
>>= |
aug. assign. with bitwise shiftR |
. |
member select. (structure) |
-> |
member select. (structure, pointed by parent) |
, |
sequencing |
Ternary and other operators
C operator | meaning |
---|---|
? : |
cond. eval. |
() |
subexpression grouping |
() |
calling function argument |
[] |
array subscript |
Despite its simplicity, writing good code under C is not easy. If you write C code carelessly with fixed width character array buffers, you are sure to hit a buffer overflow bug which is serious bug. Memory management to cope with variable length character array buffers under C is non-trivial task.
Operator Precedence
operator type | operator expression | precidence | space |
---|---|---|---|
func. operator, array operator, structure member operator | func(...) array[...] parentptr->id parent.id |
L <- R | no |
prefix unary operator (address-of, pointer-to, plus, minus, bitwise not, logical not) | & * + - ~ ! |
L <- R | no (yes before) |
binary operator (mul., div., mod.) | * / % |
L -> R | yes |
binary operator (add, sub) | + - |
L -> R | yes |
bitwise shift operator (shr, shl) | >> << |
L -> R | yes |
logical relational operator (gt,lt,ge,le) | > < >= <= |
L -> R | yes |
logical equity operator (eq,ne) | == != |
L -> R | yes |
bitwise and operator (and) | & |
L -> R | yes |
bitwise xor operator (xor) | ^ |
L -> R | yes |
bitwise or operator (or) | | |
L -> R | yes |
logical and operator (and) | && |
L -> R | yes |
logical or operator (or) | || |
L -> R | yes |
conditional operator | ? : |
L <- R | yes |
assignment operator | = *= /= %= += -= <<= >>= &= ^= |= |
L <- R | yes |
comma operator | , |
L -> R | no (yes for after) |
Please note:
- logical xor operator can be “
!a != !b
” - “
foo = *p++
” is “foo = *(p++)
” or “foo = *p, p++
”. (FIXME TEST!) - “
int *foo()
” is a function “foo
”. The function returns a pointer to an integer value. Equivalent of “int *(foo())
”. - “
int (*foo)()
” is a function “(*foo)” pointed by “foo
”. The function returns an integer value.
Previous Post | Top | Next Post |