From WikiChip
Difference between revisions of "c/variables"
< c

(Variable Declaration)
(Variable Declaration)
Line 36: Line 36:
 
The syntax for variable declaration is: <code>declaration-specifiers identifier;</code>
 
The syntax for variable declaration is: <code>declaration-specifiers identifier;</code>
  
 +
For example, one might create a new object called 'foo' with the type <code>int</code> with the following line of code:
 +
<source lang="C">
 +
int foo;
 +
</source>
 +
 +
Multiple variables can be declared at the same time if they have the same properties. For example,
 +
<source lang="C">
 +
int foo, bar, baz;
 +
</source>
 +
 +
Creates three variables, <code>foo</code>, <code>bar</code>, and <code>baz</code>. All three have an <code>int</code> data type. Sometimes, variables might have multiple properties. These properties can be specified in a series before the variable name. For example,
 +
<source lang="C">
 +
signed int foo;
 +
unsigned int bar;
 +
const long baz;
 +
</source>
 +
 +
Note that the order of declaration specifiers is unimportant. For example,
 +
<source lang="C">
 +
const unsigned int x;
 +
unsigned const int y;
 +
unsigned int const z;
 +
int unsigned const a;
 +
int const unsigned b;
 +
</source>
 +
Are all legal combinations of variable properties.
  
 
[[Category:C programming language]]
 
[[Category:C programming language]]
 
[[Category:C variables]]
 
[[Category:C variables]]

Revision as of 21:27, 26 December 2013

Many programs are far more complex than just printing a sentence or two on the screen. To be able to perform more complex operations and calculations we need a way to store values temporarily during the program execution. These named stored locations are called variables.

Data Types

Main article: Data Types

In C, every variable must have a type which specifies what kind of data it could hold. The type of variable determines how the variable is stored and what operations can be performed on it. For example, a variable of type int can store integers such as 2, -9, 83, and 5294. By contrast with float, which is capable of storing floating point values such as 3.4, 30, -943.234, and 2e6. C provides a wide variety of types in addition to an int and a float, including: char, short, and double.

Minimum Magnitudes

It is important to understand that the sizes of these objects are implementation-defined. That is, depending on the machine you use and the compiler used to compile the program, the actual range of values that each type can store can vary. The C Standard does impose a minimum magnitude for each value.

Naming Rules

Variables can be as short as a single character or as long as thirty. Variable names can be made up of uppercase and lowercase letters, digits, and underscores. All variables must not start with a digit. Variables may also not have the same name as a reserved keyword.

For example, the following are valid variable names:

x                  foo
x_y_z              BaR
_foo               bar45
_1                 baz
thisIsAReallyLongVariableName

The following, however, are not valid variable names:

2                  $apples
5foo               double

Variable Declaration

A variable declaration is a statement that announces to the translator the creation of a new object and the kind of properties it possess. These properties include:

These properties are collectively named declaration-specifiers.

The syntax for variable declaration is: declaration-specifiers identifier;

For example, one might create a new object called 'foo' with the type int with the following line of code:

int foo;

Multiple variables can be declared at the same time if they have the same properties. For example,

int foo, bar, baz;

Creates three variables, foo, bar, and baz. All three have an int data type. Sometimes, variables might have multiple properties. These properties can be specified in a series before the variable name. For example,

signed int foo;
unsigned int bar;
const long baz;

Note that the order of declaration specifiers is unimportant. For example,

const unsigned int x;
unsigned const int y;
unsigned int const z;
int unsigned const a;
int const unsigned b;

Are all legal combinations of variable properties.