10 Features of C You must know RIGHT NOW

1. Use of ?: to reduce code length

We all know about ?: operator but doesn’t use it in our daily programming practice though it may be helpful in many situations. It is simple and easy to use. For example consider the following :

if (y < 0)
  y = 100;
else
  x = 100;

Or you can simply use a single line to perform the above operation :

(y < 0 ? x : y) = 100;

In my opinion the second one i.e. the use of ?: is far much easier and less lengthy than the first if-else based approach.

2. Initialize all structure element to 0

Normally while using structures, we never assign values to structure elements that leads to garbage value initialization. Initializing each variable separately is also a lengthy and time consuming task.
But don’t worry, there is a single line of code that can assign a specific value, in my scenario 0, to all structure elements as shown below :

struct mystruct structObject = {0};

3. Accessing array element

While accessing an array we generally use the the term array_name[index]. Do you know there are other ways available using which you can access the specific array element. Consider the following three ways of accessing the array elements.

int array[]= {1,2,3,4,5};
array[3];   // It will print 4.
3[array];   // It will print 4.
*(array+3); // It will print 4.

All the above three ways of accessing an array element is valid.

4. Round off float value without using any function

I am showing you a simple example for rounding off float value that you may not know :

int x=3;
float y=6.13456;
printf("%.*f\n",x,y);

It will print 6.135 i.e the float number rounded off to three digits after decimal point.

5. Different ways to access value from pointer

Pointers are generally used to store address of variables and we can manipulate them in any way we want to. Let us see some of ways to access values from the address stored in a pointer variable:

int x=3;
int *ptr=&x;
printf("\n%d", *ptr);
printf("\n%d",*(*(&ptr)));
printf("\n%d",ptr[0]);
printf("\n%d",0[ptr]);

All the above print operation will print 3.

6. Manipulating printf() function

We generally use printf() function to print values and ignore the values returned by the printf() function. We can also calculate the number of characters successfully printed by using the printf() function or the length of any specific string printed by the printf() function. Consider the example given below that will demonstrate my given statement more clearly.

int count=printf("Hello World");
printf("Count : %d",count);

The above printf() function will print Hello World and Count = 11

int count;
printf("Hello%n World!\n", &count);
printf("The word \"Hello\" contains %d characters.\n", count);

The above two printf() function will print Hello World! / The word “Hello” contains 5 characters.

7. Manipulating scanf() return value

scanf() function returns the number of values that has been read successfully. With the help of the returned value we can check if all the values are successfully stored or not. Consider the following example:

int count=scanf("%d%d",&a,&b);
printf("\nCount : %d",count);

If both the values of a and b are successfully read then the printf() function will print Count : 2.

8. Predefined Macros in C

There are several predefined Macros in C. I am sharing with you some of the useful macros that may come handy to you while writing code.
__FILE__ expands to a string name of a current source file
__DATE__ date of compilation
__TIME__ time of compilation
__LINE__ current line number

printf("File name : %s\n",__FILE__);
printf("Compilation TimeStamp  : %s\n",__TIME__);
printf("Current date : %s\n",__DATE__);
printf("Current Line  : %d\n",__LINE__);

9. Changing value of ‘const’ data

const as the name indicates used to store constant value i.e. the values that can not be changed during the entire course of a program. But in C it is possible to alter the value of a const with the help of pointers.

const int x = 2;
*(int *)&x = 10;
printf("%d",x);    //outputs 10

So, as you can see in the above code I have successfully changed the value of a constant. Now we can say that there is noting like constants in C, everything is variable.

10. Power of scnaf() function

We all know scanf() function is used to take input from the user, and yes it is absolutely true but we can moderate the way of input in scanf(). Consider the following code assuming we have:
char str[100];

//To read a string until you meet '\n': 
scanf("%[^\n]", str);

//To read till a coma: 
scanf("%[^,]", str); 

If you want to skip some input, use * sign after %. For example you want to read last name from “Sam Jorden”, you can use one of the following approach :

//First approach using one temporary variable
scanf("%s %s", temp, last_name); 

//Second approach using 1 variable, but calls scanf twice
scanf("%s", last_name);
scanf("%s", last_name);

// Third approach, using neither extra temporary variable nor calling scanf twice
scanf("%*s %s", last);

Hopefully this information is useful to you. Don’t forget to leave your comments below.

YOU MAY ALSO LIKE