Some of the interesting features of the C Language

Some of the interesting features of the C Language

Here in this article we are listing out few of the interesting features with the C language. Some of the points listed out here are not used normally during the implementation of a production code. But these tips are worthy of noting down. The more we understand the C language the more confident we would become in coding with this magnificent language.

The features listed out below will be updated and added with more information and points going forward. Also feel free to post your views as comments to this article..

So let’s start with….  🙂

 

1. Function called with multiple arguments

In C if a function definition does not specify any parameters, then this function can be called with any number of arguments or without any. For example, see the below sample code:

2. The terms “parameters” and “arguments”

The term ‘parameter’ refers to any declaration within the parentheses following a function name in a function declaration or definition; the term ‘arguments’ refers to any expression within the parentheses of a function call.

3. Order of evaluation of function parameters

There is no guarantee about the evaluation of the order of the function parameters in C. The order of evaluation is purely implementation dependent, so while writing programs we should not use expressions which can cause side-effects. This would be better explained with the below example:

In the above example, the result values of x and y will be implementation dependent.

4. Multiple declaration of global variable

C allows a global variable to be declared multiple times; having said so, there should only be one definition for the variable. See the below example:

Here in the above example you will be getting the answer as 10.
But if you try to re-declare a local variable, then you would be getting the redeclaration of ‘x’ with no linkage error during compilation.

Bitwise operators in C: 2 Simple Programs to start with

Bitwise operators in C: 2 Simple Programs to start with

Bitwise operators in C can be very handy in writing simple and efficient codes to solve many logical requirements.
Just to get an overview of how the bitwise operators in C can solve logical problems easily, here we are giving two examples which are tested in Linux environment.

1. Find whether the given input number in even or odd.
2. Find the missing number from the series of natural numbers starting from 1.

Note: To make the code easily understandable, some of the common defensive programming techniques are omitted.
So let’s start with the coding,
.
.
1. Find whether the given input number is even or odd.

Here in this simple program, the input is read from the ‘stdin’ and read into the variable ‘input’. If the entered value is zero, we are jumping to ‘end’ and exits the program.

The code, res = !(input & 1); is computing whether the given number is even or odd. Here the variable ‘res’ will be set to ‘TRUE’ if the variable ‘input’ is containing an even number. Since the last bit of even number will be zero, the ANDing of ‘input’ with 1 will result in zero ((input & 1)), thereby making the entire right hand side operation to ‘TRUE’ (!(input & 1))

2. Find the missing number from the series of natural numbers starting from 1.

In this program we can find the missing number from the given list of natural numbers starting from 1. The input list of numbers is stored in the array variable ‘real_input’. The function ‘GetTheOneMissingNumber()’ will find out the missing number from the given list.

In this program we are performing XOR between all the real numbers starting from 1 to ‘count+1’ and saving it to the local variable ‘xor_res2’. Similarly all the elements in the given list ‘input_list’ are also XOR’ed and saved into the local variable ‘xor_res1’. A final XOR between the values of ‘xor_res2’ and ‘xor_res1’ will give the missing number.

Computation explained,
xor_res1 = X1 ^ X2 ^ X3 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res2 = X1 ^ X2 ^ X3 ^ X4 ^ X5 ^ X6 ^ X7 ^ X8 ^ X9 ^ X10
xor_res1 ^ xor_res2 = (X1 ^ X1) ^ (X2 ^ X2) ^ (X3 ^ X3) ^ (X4) ^ (X5 ^ X5) ^… (X10 ^ X10) = X4