Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

C Programming Tutorial

Learn C programming in an easy way.This C programming tutorial is completly free for your use.

How you can help us back is let us know about more tutorials and send us the tutorial with your small photo and name.We will publish tutorial in your name.

 

<< Tutorial Home

PRE/POST INCREMENT/DECREMENT OPERATORS
PRE means do the operation first followed by any assignment operation. POST means do the operation after any assignment operation. Consider the following statements

++count; /* PRE Increment, means add one to count */
count++; /* POST Increment, means add one to count */

In the above example, because the value of count is not assigned to any variable, the effects of the PRE/POST operation are not clearly visible.
Lets examine what happens when we use the operator along with an assignment operation. Consider the following program,

#include <stdio.h>

main()
{
int count = 0, loop;

loop = ++count; /* same as count = count + 1; loop = count; */
printf("loop = %d, count = %d\n", loop, count);

loop = count++; /* same as loop = count; count = count + 1; */
printf("loop = %d, count = %d\n", loop, count);
}

Sample Program Output
loop = 1, count = 1
loop = 1; count = 2

If the operator precedes (is on the left hand side) of the variable, the operation is performed first, so the statement

loop = ++count;
really means increment count first, then assign the new value of count to loop.

Which way do you write it?
Where the increment/decrement operation is used to adjust the value of a variable, and is not involved in an assignment operation, which should you use,
++loop_count;
or
loop_count++;

The answer is, it really does not matter. It does seem that there is a preference amongst C programmers to use the post form.

Something to watch out for
Whilst we are on the subject, do not get into the habit of using a space(s) between the variable name and the pre/post operator.
loop_count ++;
Try to be explicit in binding the operator tightly by leaving no gap.

GOOD FORM
Perhaps we should say programming style or readability. The most common complaints we would have about beginning C programmers can be summarized as,
they have poor layout
their programs are hard to read
Your programs will be quicker to write and easier to debug if you get into the habit of actually formatting the layout correctly as you write it.
For instance, look at the program below

#include<stdio.h>
main()
{
int sum,loop,kettle,job;
char Whoknows;

sum=9;
loop=7;
whoKnows='A';
printf("Whoknows=%c,kettle=%d\n",whoknows,kettle);
}

It is our contention that the program is hard to read, and because of this, will be difficult to debug for errors by an inexperienced programmer. It also contains a few deliberate mistakes!
Okay then, lets rewrite the program using good form.

#include <stdio.h>

main()
{
int sum, loop, kettle = 0, job;
char whoknows;

sum = 9;
loop = 7;
whoknows = 'A';
printf( "Whoknows = %c, kettle = %d\n", whoknows, kettle );
}

We have also corrected the mistakes. The major differences are
the { and } braces directly line up underneath each other
This allows us to check ident levels and ensure that statements belong to the correct block of code. This becomes vital as programs become more complex
spaces are inserted for readability
We as humans write sentences using spaces between words. This helps our comprehension of what we read (if you dont believe me, try reading the following sentence. wishihadadollarforeverytimeimadeamistake. The insertion of spaces will also help us identify mistakes quicker.
good indentation
Indent levels (tab stops) are clearly used to block statements, here we clearly see and identify functions, and the statements which belong to each { } program body.
initialization of variables
The first example prints out the value of kettle, a variable that has no initial value. This is corrected in the second example.

 

 

<< Tutorial Home