Using Barcode reader for Java Control to read, scan read, scan image in Java applications.

www.OnBarcode.com

The first call to printf() actually has a statement embedded in it. This is another great feature of the C language. Where there s room for a variable, there s room for an entire statement. Sometimes, performing two actions within the same line of code is convenient. For example, this line of code

In general, when the compiler encounters an assignment statement where it expects a variable, it first completes the assignment, and then passes on the result of the assignment as if it were a variable. Let s see this technique in action. In main.c, our friend the postfix operator emerges again. Just prior to the two calls of printf(), myInt has a value of 5. The first of the two printf() s increments the value of myInt using postfix notation:

printf( "myInt ---> %d\n", myInt++ );

The use of postfix notation means that the value of myInt will be passed on to printf() before myInt is incremented. Therefore, the first printf() will accord myInt a value of 5. However, when the statement is finished, myInt will have a value of 6. The second printf() acts in a more rational (and preferable) manner. The prefix notation guarantees that myInt will be incremented (from 6 to 7) before its value is passed on to printf().

printf( "myInt ---> %d", ++myInt ); return 0; }

BREAKING THE PRINTF() INTO TWO STATEMENTS

Can you break each of these printf()s into two separate statements Give it a try; then, read on. The first printf() looks like this:

printf( "myInt ---> %d\n", myInt++ );

Here s the two-statement version:

printf( "myInt ---> %d\n", myInt ); myInt++;

Notice that the statement incrementing myInt was placed after the printf(). Do you see why The postfix notation makes this necessary. Run through both versions, and verify this for yourself. The second printf() looks like this:

printf( "myInt ---> %d\n", ++myInt );

Here s the two-statement version:

++myInt; printf( "myInt ---> %d\n", myInt );

This time the statement incrementing myInt came before the printf(). This time, it s the prefix notation that makes this necessary. Again, go through both versions, and verify this for yourself.

CHAPTER 5: C Basics: Variables and Operators

The purpose of demonstrating the complexity of the postfix and prefix operators is twofold. On one hand, it s extremely important that you understand exactly how these operators work from all angles. This will allow you to write code that works and will aid you in making sense of other programmers code. On the other hand, embedding prefix and postfix operators within function arguments may save you lines of code but, as you can see, may prove a bit confusing. So what s a coder to do Put clarity before brevity. Make sure your code is readable. After all, you will likely have to go back and edit it at some point. Readable code is much easier to maintain. As long as your code is correct, the compiler will do the same thing with it. So write for the programmer, not the machine.