May 132017

Generic programming, loosely means that the code we have written is type-independent. It is a larger topic, macros in C language also comes under generic programming. In this post we are only discussing generic pointers.

Continue reading »

Feb 062013

While comparing a variable with a constant (literal) in C/C++ language, which of the two ways of comparison should we use ?

    if( x == 2 ) ...

    if( ptr == NULL ) ...


    if( 2 == x ) ...

    if( NULL == ptr ) ...

In the first comparison we are writing variable on the Left side of == and literal (constant) on right side. whereas, in the second form we are writing the literal (2, NULL, etc.) on the left side of ==.
Continue reading »

Nov 062012

sizeof operator returns size in bytes. For example, In an implementation where 2 bytes (16bits) of memory is allocated to an int type, the below code will output 2

int x = 10;
printf("%d", sizeof(x)); // Output amount of memory allocated to int data type.

We talked about the sizeof operator in great detail in the last post.

If we use sizeof operator on arrays, as shown below:

int arr[10];
printf("%d", sizeof(arr)); //Will output 20

the output will be 20.

But sometimes, its not a good idea to use this operator on arrays.
Continue reading »

Nov 062012

sizeof is a unique operator in C/C++ language which returns the amount of memory (in bytes) allocated to its operand. For example, if an implementation stores an integer in 2 bytes then both the statements below will output 2.

printf("%d", sizeof(int));

int x = 10;
printf("%d", sizeof x);

Note than sizeof operator is used like a function when applied on data types.


Let’s understand some more facts about sizeof operator:
Continue reading »

Oct 192012

Comma in C and C++ comes in two flavours

  1. As separator (used to separate parameters in a function call)
  2. As operator (rarely used)

The two flavors are as different as it can be. Lets discuss them one by one:
Continue reading »