28.2.08

Swap the two numbers

This is the most frequently asked C question.

How to swap two numbers without using temporary variable.

There are many ways to do that, here are some.

Code snippets:
1)

int x, y; /* are the two numbers */
x = x+y;
y = x - y;
x = x - y;

2)
x = x*y;
y = x/y;
x = x/y;
3)
x = x ^ y;
y = x ^ y ;
x = x ^ y ;
NOTE: These logics have some limitations
Logic 1 has the Overflow problem when we add (x+y).
Logic 2 has the Overflow problem when we multiply ( x*y) and y should not be Zero.
Logic 3 has the same and its not works on the float numbers.

27.2.08

Even/Odd number

This is also one of the C Interview questions.

How to tell the given number is even or odd number in single line C code.

Code snippet:
int num;
num&1 ? printf("Odd Number\n") : printf("Even number\n");

Logic is every odd number will have least significant bit has 1
in binary representation of the number.

So when we do bitwise AND with number 1, obviously the
result is '1' for odd numbers and '0' for even numbers.

power of 2

This is one tricky bit usually asked in C interviews

How to tell the given number is Power of 2 or not in a one line 'C 'code.

There are many ways to do this, here is my logic.

Code snippet:

int num; /* holds the integer value */

num & (num-1) ? printf("Not Power of 2\n") : printf("Power of 2\n");


Boolean type

C99 supports _Bool keyword, which declares a two valued ( '0' or ' 1 ') integer.

Code snippet:

_Bool x; /* x will contain either ‘0’ or ‘1’.*/

x = 0;


Another way

include the header stdbool.h , it provides bool same as _Bool ( typedef ) and

two macros true and fase.

Code snippet:

#incldue "stdbool.h"

bool flag;

flag = true; /* flag value is true which is 1 */

flag = false; /*flag value false which is 0 */


Variable length Arrays/Dynamic Arrays

C99 added the feature called dynamic arrays in C.

Means we can specify the length of the array in the run time.

But there is one limitation that once the length of the array specified,

it’s not possible to change the length of array again.


Code fragment.

int length;

int arr[length];

Printf(“Enter the Length of the array \n”);

Scanf (“%d”, &length);



NOTE: Here the length of the arr is length and it should not be changed.

22.2.08

Array Initialization

Here i am discussing how many ways we can initialize the array in
the 'C' language

Normal initialization
int arr[10] = { 0, 1, 2, 3 , 4, 5, 6, 7, 8, 9 };

This is very normal way of initialization of the array.

-->Let say there is a situation that all of the array elements holds the same
element.Different ways to initialize that is

int arr[ 10] = {0,0,0,0,0,0,0,0,0,0,}; /* this is not a good way */
int arr[10] = {0}; /* Good Practice */

--> let say there is another situation where an array of length "10"
which contains some elements up to index 4 ( means 5 elements)
after that the array contains only zeros.

int arr[10] = {1,12,3,4,5,0,0,0,0,0}; /* This is not good practice */
int arr[10] = { 1,12,3,4,5}; /* rest filled with zeros. */

--> Let say there is another strange situation where first 3 elements in array
holds same value and next 2 elements hold the same value.

int arr[10] = {4,4,4,7,7,1,2,3,4,5}; /* This is normal initialization */
int arr[10] = {[0 ... 2]=4,[3 ... 4]=7,1,2,3,4,5}; /* This is good practice */

--> Let say there is another situation where 3rd element holds 90 ,
8th element holds 100 and rest all holds the zeros.

int arr[10] = {0,0,90,0,0,0,0,100,0,0};/* General practice */
int arr[10] = {[2]=90, [7]=100};/* Good practice */