Category: C Programming Tips and Tricks

why learn C programming 3

Why learn C programming and C++ programming & How to

C Programming is considered as one of the best and easy to learn programming language. C programming was introduced many decades ago and still it is one of the most popular languages in the programming world. When I started with programming, I had a question in mind – How to learn programming? There were many programming languages available for me to start with, like C, C++, Java, C# and so on. To be frank, I had started with Java programming. Initial few says were awesome. Started with an introduction to Java, then started with java basics which included statements, loops,...

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:

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 will cause it to print out data from stack memory...

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:

you should still surround execute(); with braces as follows:

Now, if you want to go back to the code in future and add a second instruction

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.