- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
5: C Basics: Variables and Operators in Java
CHAPTER 5: C Basics: Variables and Operators Data Matrix ECC200 Printer In Java Using Barcode generation for Android Control to generate, create Data Matrix 2d barcode image in Android applications. www.OnBarcode.comPrinting Code 128B In Java Using Barcode creation for Android Control to generate, create Code-128 image in Android applications. www.OnBarcode.comOnce your guesses are locked in, select Build and Run from the Build menu. How d you do Compare your two guesses with the output in Figure 5-8. Let s look at the source code. Making Code-39 In Java Using Barcode printer for Android Control to generate, create Code 39 image in Android applications. www.OnBarcode.comGTIN - 13 Generator In Java Using Barcode maker for Android Control to generate, create EAN-13 image in Android applications. www.OnBarcode.comFigure 5-8. The output generated by postfix. Is this the output you expected
Create Data Matrix ECC200 In Java Using Barcode creation for Android Control to generate, create ECC200 image in Android applications. www.OnBarcode.comGenerating Barcode In Java Using Barcode creation for Android Control to generate, create Barcode image in Android applications. www.OnBarcode.comStepping Through the postfix Source Code
Painting UPC-A Supplement 5 In Java Using Barcode printer for Android Control to generate, create UPCA image in Android applications. www.OnBarcode.comUPC - 8 Printer In Java Using Barcode drawer for Android Control to generate, create EAN-8 image in Android applications. www.OnBarcode.comThe first half of main.c is pretty straightforward. The variable myInt is defined to be of type int. Then, myInt is assigned a value of 5. Next comes the tricky part. Data Matrix Printer In Java Using Barcode generation for Android Control to generate, create ECC200 image in Android applications. www.OnBarcode.comECC200 Drawer In Java Using Barcode creator for Java Control to generate, create ECC200 image in Java applications. www.OnBarcode.com#include <stdio.h> int main (int argc, const char * argv[]) { int myInt; myInt = 5; Code 3 Of 9 Generator In None Using Barcode creation for Word Control to generate, create ANSI/AIM Code 39 image in Word applications. www.OnBarcode.comEAN13 Scanner In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comThe 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 Making Code 128 Code Set B In .NET Using Barcode creation for Reporting Service Control to generate, create Code 128 Code Set A image in Reporting Service applications. www.OnBarcode.comGS1-128 Maker In Java Using Barcode printer for Java Control to generate, create EAN128 image in Java applications. www.OnBarcode.comprintf( "myInt ---> %d\n", myInt = myInt * 3 ); Code 128B Printer In Java Using Barcode generator for Java Control to generate, create Code128 image in Java applications. www.OnBarcode.comBarcode Generator In Java Using Barcode maker for BIRT reports Control to generate, create Barcode image in BIRT reports applications. www.OnBarcode.comfirst triples the value of myInt, and then passes the result (the tripled value of myInt) on to printf(). The same could have been accomplished using two lines of code: Barcode Printer In Objective-C Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comRecognize PDF 417 In VB.NET Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.commyInt = myInt * 3; printf( "myInt ---> %d\n", myInt ); Create EAN13 In None Using Barcode generator for Microsoft Excel Control to generate, create EAN-13 Supplement 5 image in Office Excel applications. www.OnBarcode.comGenerating QR-Code In None Using Barcode creator for Office Excel Control to generate, create QR Code image in Microsoft Excel applications. www.OnBarcode.comCHAPTER 5: C Basics: Variables and Operators
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.
|
|