Category: Learn C Online

Introduction to File Operations in C Programming language 4

Introduction to File Operations in C Programming language

As we all know, the operations we perform via. printf and scanf, reads and stores data in the memory. But this memory is limited for use. If in case we need to read and store large amount of data then this limited memory can’t be used. We need to find some ways to store this data permanently so that we can read and write to it at later point of time. This is where file operations comes into picture. C provides us with the facility to perform File operations in C programming language. File Operations in C Programming Language: There...

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.

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