Get started !
online LTE test
online C test

Updated or New
CDF explained
5G Page
5G Intent (a presentation)
5G Intent (article)
CV2X Page
A Look at CV2X (a presentation)

Information Theory
Multiple Access
OSI Model
Data Link layer
Word about ATM
Standard Reference
Reference books
Resources on Web
Mind Map
Perl resources
Magic MSC tool
Bar graph tool
C programming
ASCII table
Project Management
Simple Google box
HTML characters
Site view counter
7 4 6 , 7 2 9
(May-2010 till Jan-2019)
another knowledge site

C guide page 1

Point Details

1) main declarations main has only two valid declarations:

int main(void);
int main(int argc, char** argv);

Following declarations mean or gets converted to one of the above two declarations:

void main(void);
int main(int argc, char *argv[]);
int main(int argc, char argv[][]);

2) Strange i=i++; Seemingly logical behaviour of i=i++; is i incremented by one at the end. But is it that simple or logical for comiler too ? Answer is No !

"i++" supposed to "use i value" and "then increment the same". In statement i=i++;, value of i is being used to assign to i itself and that is sort of an anamoly .... i gets modified twice in i=i++; one due to assignment and other due to side-effect of post-increment.

As per C standards, this is not correct !! So the behaviour of i=i++; statement (and any of the following statements) is considered to be "undefined" (even if syntactically correct).

Variations of (i++)*(i++)

Please refer C Programming FAQs by Steve Summit (section 3) for detailed discussion on the topic.

3) x=y=z=3 The statement is valid and it does what we expect i.e. it makes value of x, y, and z 3. The reason it works is because "=" operator has right-to-left associativity. So the statement assigns 3 to z, then assigns z to y, and then assigns y to x.

4) 3["0123456789abcdef"] works ? Yes, it does work. Compiler interprets both 3["0123456789abcdef"] and "0123456789abcdef"[3] same way ! The reason seems to be in the way C thinks of an array (as an constant pointer). array[index] is as good as *(array + index) or *(index + array) (addition operator works in the same way irrespective of order of operands).

5) \r and \n \r moves the cursor to the left-most position of *current* line.
\n moved the cursor to the left-most position of *next* line.

6) Declaring structure having an element pointing to structure of its own type. Useful for implementing linked list. There are number of ways to do it:

typedef struct struct_type_state state ;

struct struct_type_state {
int int_state_id;
state * ptr_next_state;


typedef struct struct_type_state {
int int_state_id;
struct struct_type_state * ptr_next_state;
} state;


struct struct_type_state {
int int_state_id;
struct struct_type_state * ptr_next_state;

typedef struct struct_type_state state;

7) -1>>1 -1 is (stored as 2's complement) 0xFFFFFFFF in (32 bit) machine. Right-shifted any number of times) will have the same value i.e.
-1>>1 = -1.

So following statement will increment i by one.

i = i - (-1>>7);

8) Reading or writing complex declarations Example: Declare an array of function pointers returning pointer to integer.

1st method (left-to-right or in-to-out):
array - a[]
of function pointers - (* (a[]) )() (braces around a[] are optional)
returning pointer - *(* a[])()
to integer - int *(* a[])()

2nd method (right-to-left or out-to-in):
integer - int
pointer to - int *
function pointers returning - int *(*)()
array of - int *(* a[])()

9) printf/scanf format characters
% printf scanf
d int (signed decimal) int* (decimal)
i int (signed decimal) int* (decimal, octal, hexa)
u int (unsigned decimal) unsigned int* (unsigned decimal)
o int (octal without leading 0) int* (octal with/without leading zero)
x,X int (hexa without leading 0x or 0X) int* (hexa with/without leading 0x or 0X)
c int (a character) char* (character)
s char* (till \0 or given precision) char* (non-whitespace characters)
f double (signed fraction) float* (signed fraction, e/E notation also accepted)
e,E double (signed fraction in e/E notation) float* (signed fraction, e/E notation also accepted)
p void* (implementation dependent) void* (implementation dependent)
n int* (number of characters printed so far is put into it) int* (number of characters printed so far is put into it)
% print % literal %
[...] NA longest string of input characters; characters given in [...].
[^...] NA longest string of input characters; characters other than given in [...].

10) variable number of arguments Example:


int algorithm( char* parameters, ... );

int main(void) {
printf( "algorithm: %d\n", algorithm( "ioioi", 5, '+', 2, '-', 3 ) );

int algorithm( char* parameters, ... ) {

va_list argp;
int result = 0;
char *pp;
char operation = '+';

va_start( argp, parameters );

for( pp = parameters; *pp != '\0'; ++pp ) {

switch( *pp ) {
case 'i':
switch( operation ) {
case '+': result += va_arg( argp, int ); break;
case '-': result -= va_arg( argp, int ); break;
default : /* ERROR */ break;

case 'o':
operation = (char)va_arg( argp, int );

/* ERROR */

va_end( argp );
return result;

Functions using variable number of arguments must have at least one argument before "...". Typically this would be char* having information about type of (variable) arguments that would be following.

References: The C Programming Language by Kernighan and Ritchie and C Programming FAQs by Steve Summit.

Copyright © Samir Amberkar 2010-11§

C Internet Links « C guide Index » C Guide page 2