Proper Variable Syntax

The overwhelming majority of data you'll work with will be in the form of variables (as opposed to constants, which you'll also learn about at the end of this chapter). To use a variable, you must first declare it—in other words, you state that a variable with a specific name will be of a certain type. The declaration statement looks like this:

					datatype variablename;

For example:

int age;
float cost;
char answer;

The different types are listed in Table 2.1, along with what version of C they are allowed in.

Table 2.1. The basic variable types for use in the C language and what standard they fall under.
C Data Types
TypeC Standard
intAll
long intAll
long long intAll
short intAll
unsignedAll
floatAll
doubleAll
long doubleAll
charAll
signedC90 and C99
voidC90 and C99
_BoolC99
_ComplexC99 (optional support)
_ImaginaryC99 (optional support)

The easiest to comprehend and use are the number types. For starters you have int, which is an integer. Then there are long and short ints, which increase and decrease the maximum size of their value (as well as how much memory each requires). Floats and doubles are two more types of numbers, each of which contains decimal points. Doubles are twice as precise as floats and therefore more reliable for many uses. Finally, all number types can be marked as signed or unsigned (those aren't actual types but rather modifiers for the different types). The former means that a number can be either positive or negative, whereas the latter asserts that the number is always non-negative (unsigned numbers can be 0 or greater in value).

Of the other types, char will be the only one demonstrated in this chapter. It lets you store other characters—like letters and punctuation—in variables.

Besides deciding on a data type when declaring your variables, you also have to state their names, following specific rules. Variable names

  • Can contain only letters, numbers, and the underscore

  • Must begin with either a letter or an underscore

  • Are case sensitive

  • Can consist of up to 63 characters (as of the C99 standard)

  • Cannot be the same as a reserved word (such as int, return, main, printf)

When you're trying to come up with variable name, there are many best practices to follow. For starters, variable names should be

  • Descriptive of what information the variable represents

  • Logically named, following consistent practices

  • Documented by comments

To begin working with variables, let's create a simple application that declares a couple of them.

To declare variables

1.
Create a new, blank text document in your text editor or integrated development environment (IDE).

2.
Begin by documenting the file (Script 2.1):

/* var1.c - Script 2.1 */

Script 2.1. This minimal C file merely defines two different variables.


Remember that thorough and accurate comments are the key to professional, reliable programming. In this book you'll see more minimal notes like this one, but you should not hesitate to go overboard with your own documentation.

3.
Type the required include line:

#include <stdio.h>

This line should be part of practically all of your C programs; its purpose is to add standard functionality to your applications.

4.
Begin defining the main() function:

int main (void) {

This line begins the definition of a user-defined function called main(), which will automatically be called first when the application is run. It takes no arguments and will return an integer value. The opening curly brace marks the beginning of the function's contents.

5.
Define the variables to be used:

int age; /* User's age in years. */
float hourly_wage; /* User's hourly 
 wage in dollars. */

This function uses two of the number data types. Each is declared on its own line, terminated by a semicolon. Comments have also been added that detail the purpose of each, in case it's not otherwise clear.

6.
Add the return line and close the main() function:

 return 0;
}

The function returns the value 0, which indicates that the function ran successfully. Then, the curly brace marks the end of the main() function definition.

7.
Save the file as var1.c.

If you want, you can create a new folder for this application, using whatever method you are using for maintaining your applications' source code.

8.
Compile the application (Figure 2.1).

Figure 2.1. Compiling var1.c should result in no errors. This is the result after compilation using Dev-C++ on Windows.


Since this application doesn't actually do anything, it may not be worth your time to execute, but compiling will confirm that no errors occurred.

Remember that how you compile a C application depends on what tools you are using to build them. See Chapter 1, “Getting Started with C,” and Appendix A, “Installing and Using C Tools,” for specific techniques.

✓ Tips

  • Over the next few chapters you'll learn which data type to use when. Properly selecting a data type affects the memory requirements of your applications, how well they perform, and what they can do.

  • There are two general naming conventions for creating compound variable names. One uses underscores to separate words; for example, first_name or shipping_cost. The second uses capitalization for this effect: firstName or shippingCost (or FirstName and ShippingCost). It doesn't matter which convention you follow as long as you remain consistent.

  • The length of a variable's name should also be reflective of its importance. Temporary variables—like those used in loops—can have terse, almost random names like i or num. Important variables, however, should have full, descriptive names like street_address or distance.

  • If you have multiple variables of the same type, you can quickly declare them all at once:

    double cost, budget, tax;
    
  • The C99 standard allows you to declare your variables anywhere in the code, as long as you do so before referring to it. It's best, though, to follow the old habits of declaring all of your variables at the beginning of a function.

  • The different data types, like int and char, are keywords, meaning they are reserved by C and cannot be used as a variable or function name.

  • C itself occasionally uses the __variable syntax, although you should not. By starting your variable names with underscores, you run the risk of your names conflicting with existing C variables, having disastrous affects on your application.

  • Many compilers will let you create variable names more than 63 characters in length, but you'd probably only want to make use of this if you really like typing.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset