Category: C Programming Tips and Tricks

Do not use printf without s to print a string 0

Tip of the week#8:Do not use printf without %s to print a string

Since the printf() function takes strings as arguments, you might think that you do not need the format specifier “%s” while printing a string. Example: int main() { char string[30]=”Hello c programers”; printf(string); return 0; } However, this can be very dangerous. What if your string includes a format specifier like %s or %d? Because printf is a varargs function, it uses the format string to decide how many arguments it takes. If you provide one argument, but put in the format specifier, it will assume it has more arguments than it does, and read them off the stack. This...

Write if statements with braces 0

Tip of the week#6: Write if statements with braces

By putting braces around every block of code you write, you ensure that future edits won’t introduce bizarre bugs. If you have a one-line if statement: if(condition) execute(); you should still surround execute(); with braces as follows: if(condition) { execute(); } Now, if you want to go back to the code in future and add a second instruction if(condition) { execute(); execute2(); } you don’t have to worry about putting in the braces.

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.