Author: learnconline

Fast multiplication 0

Tip of the week#5: Fast multiplication

It is well known that bit shifting enables programmers to do multiplication when dealing with numbers such as 2, 4, 8, 16, 32, 2^n, … For instance: a=a<<4; equals a*=16; But multiplying by 100 (and for many other numbers) is also possible: a*=100; equals a= a*64 + a*32 + a*4; equals a=(a<<6)+(a<<5)+(a<<2); Some compilers might optimise this by themselves.

Don’t let functions fail silently 0

Tip of the week#4: Don’t let functions fail silently

Sometimes it is tempting to have a function return null without doing anything if a certain pre-condition isn’t satisfied (i.e., with something like if (foo->bar < 0) return;). After all, the entire program shouldn’t abort just because the function sees an input that is inappropriate, right? I disagree. I think that you should rarely have a function return without doing anything. Turn these conditions into asserts so that the function fails with a bang when it sees invalid input. Why are you even passing invalid inputs into the function in the first place? If many of your functions fail silently, then bugs can go un-noticed...

Use of ++ and -- 3

Tip of the week#3: Use of ++ and —

++ and — are the unary operators for incrementing and decrementing the variables. Let us take an example. Example 1: int i = 10; i = i + 1; Example 2: int i = 10; i++; Although the above two example perform the same task, there is a significant difference (in ms) in the time required to execute the above two. The execution of the instruction “i = i + 1” takes more time than “i++”. Unary operators produce fewer instructions and run faster.

Initialize all the data variables 1

Tip of the week#2: Initialize all the data variables

Whenever you declare a local variable, it is not initialized to any value. It contains a garbage value. Garbage value is never useful. It is just an uninitialized data. Presence of garbage value might introduce some bugs into your program. Hence whenever we declare a local variable, the best practice is to initialize it at the same time. Instead of writing “int variable1”, we can write “int variable1 = 0”. This will eliminate the risk of the program code giving unwanted output.

Looping performance 0

Tip of the week#1: Looping performance

Never use two loops if one loop will suffice. Example: for(i=0; i&lt;5; i++) { /*Do something 1*/ } for(j=0; j&lt;5; j++) { /*Do something 2*/ } In the above example, first and the second “for” loop will execute 5 times each. That is the total of 10 times. The performace of the above code can be optimized by using only one for loop as follows: for(i=0; i<5; i++) { /*Do something 1*/ /*Do something 2*/ } The above loop will be executed only 5 times. Thus the time required for execution is reduced. Note: If the instructions in the loop...

Tip of the Week Series 1

Tip of the Week Series

Hello Readers, Welcome to LearnCOnline.com Thanks a lot to all the readers for overwhelming response. Thanks once again for the wonderful feedback provided by you all. After reviewing the feedback we have planned to start “Tip of the Week” series. This series will provide you with the tips on the C Programming language which will be very much useful for the beginners and the advanced programmers as well. We are looking forward to receive your valuable feedback. Thanks, LearnCOnline Team

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet