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

(Created page with "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 va...")
 
(Naming Rules)
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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'''.
+
{{c title|Variables}}
 +
In [[c|C]], '''variables''' are named stored locations. Variables provide a simple mechanism by which programs can temporarily store values to be used for things such as calculations and accessed at a later time.
  
 
== Data Types ==
 
== Data Types ==
{{main|Data types - C|l1=Data Types}}
+
{{main|c/data types|l1=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.
+
Variables, in [[c|C]], must have a '''Data-type''', which specifies the kind of data it can 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 ===
 
=== Minimum Magnitudes ===
Line 12: Line 13:
  
 
For example, the following are valid variable names:
 
For example, the following are valid variable names:
x                 foo
+
{{collist
x_y_z              BaR
+
| count = 2
_foo              bar45
+
| width = 300px
_1                baz
+
|
thisIsAReallyLongVariableName
+
* <code>x</code>
 +
* <code>x_y_z</code>
 +
* <code>_foo</code>
 +
* <code>_1</code>
 +
* <code>foo</code>
 +
* <code>BaR</code>
 +
* <code>bar45</code>
 +
* <code>baz</code>
 +
* <code>b1ar</code>
 +
* <code>thisIsAReallyLongVariableName</code>
 +
}}
  
 
The following, however, are not valid variable names:
 
The following, however, are not valid variable names:
2                 $apples
+
{{collist
5foo              double
+
| count = 2
 +
| width = 300px
 +
|
 +
* <code>2</code>
 +
* <code>5foo</code>
 +
* <code>$apples</code>
 +
* <code>double</code>
 +
}}
 +
 
 +
Additionally, the name of a variable cannot be a {{C|reserved keywords|reserved word}}. For example, a variable named <code>while</code> or <code>register</code> is illegal.
 +
 
 +
== 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:
 +
* The {{C|storage class|object's lifetime}}
 +
* The object's {{C|data types|data type}}
 +
* Whether an object is {{C|volatile|volatile}}
 +
* Whether it's a {{C|restricted pointers|restricted pointer}}
 +
* Whether it's an {{C|atomic variables|atomic variable}}
 +
* Whether an object is {{C|const|modifiable}}
 +
 
 +
These properties are collectively named ''declaration-specifiers''.
 +
 
 +
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.
 +
 
 +
=== Initial value ===
 +
Upon the creation of a variable, if no explicit value been assigned, the value of the variable depends on the [[storage class - C|storage specifier]]. If no storage specifier was explicitly used, the automatic storage class is assumed. Automatic variables do not have a specified initial value, therefore it is important to initialize it prior to using the variable. The variable declaration statement allows you specify an initial value as well. The syntax for that is <code>declaration-specifiers identifier = <value>;</code>. For example,
 +
<source lang="C">
 +
int foo = 99;
 +
</source>
 +
 
 +
We can initialize multiple variables as well following the same pattern,
 +
<source lang="C">
 +
int foo = 1, bar = 2, baz = 3;
 +
</source>
 +
 
 +
== Object's size ==
 +
{{main|c/sizeof operator|l1=sizeof operator}}
 +
In many cases it might be useful to know the size of an object. Knowing the size of an object is also required if you want to allocate the object dynamically. The size of the object can be retrieved via the '''{{C|sizeof operator|sizeof}}''' operator which returns the amount of storage, in bytes, that is required to store an object of the type of the operand specified. The {{C|sizeof operator}} should always be used in order to not [[hard-code]] machine-dependent type sizes.
 +
 
 +
The sizeof operator has the following syntax:
 +
 
 +
<source lang="text">
 +
sizeof unary-expression
 +
sizeof (type-name)
 +
</source>
 +
 
 +
For example,
 +
 
 +
<source lang="C">
 +
#include <stdio.h>
 +
int main()
 +
{
 +
    printf("Size of 'int' is: %zu\n", sizeof (int));
 +
    return 0;
 +
}
 +
</source>
 +
 
 +
Will print the size of the <code>int</code> type on your machine. Note that we can apply the same operator on variables, for example:
 +
 
 +
<source lang="C">
 +
#include <stdio.h>
 +
int main()
 +
{
 +
    int x;
 +
    long y;
 +
    printf("Size of 'x' is: %zu, and 'y' is: %zu\n", sizeof x, sizeof y);
 +
    return 0;
 +
}
 +
</source>
 +
 
 +
=== Bytes, not octets ===
 +
It is important to note that the {{C|sizeof operator}} returns the size of the object in bytes and not octets. Various architectures might have different sizes for bytes. The {{c/limits.h|<limits.h>}} header provides the <code>CHAR_BIT</code> macro which expands to the number of bits in a byte. I.E. the size of an object consists of ''n * CHAR_BIT'' bits, where <code>n</code> is the size of an object. The standard does impose a minimum limit of 8 bits for a byte size. Other standards such as the POSIX standard requires that CHAR_BIT always be 8. For example,
 +
 
 +
<source lang="C">
 +
#include <stdio.h>
 +
#include <limits.h>
 +
int main()
 +
{
 +
    int x;
 +
    long y;
 +
    printf("'x' is: %zu bits\n", sizeof x * CHAR_BIT);
 +
    printf("'y' is: %zu bits\n", sizeof y * CHAR_BIT);
 +
    return 0;
 +
}
 +
</source>
  
 
[[Category:C programming language]]
 
[[Category:C programming language]]
 +
[[Category:C variables]]

Latest revision as of 15:07, 9 March 2016

In C, variables are named stored locations. Variables provide a simple mechanism by which programs can temporarily store values to be used for things such as calculations and accessed at a later time.

Data Types[edit]

Main article: Data Types

Variables, in C, must have a Data-type, which specifies the kind of data it can 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[edit]

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[edit]

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
  • x_y_z
  • _foo
  • _1
  • foo
  • BaR
  • bar45
  • baz
  • b1ar
  • thisIsAReallyLongVariableName

The following, however, are not valid variable names:

  • 2
  • 5foo
  • $apples
  • double

Additionally, the name of a variable cannot be a reserved word. For example, a variable named while or register is illegal.

Variable Declaration[edit]

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.

Initial value[edit]

Upon the creation of a variable, if no explicit value been assigned, the value of the variable depends on the storage specifier. If no storage specifier was explicitly used, the automatic storage class is assumed. Automatic variables do not have a specified initial value, therefore it is important to initialize it prior to using the variable. The variable declaration statement allows you specify an initial value as well. The syntax for that is declaration-specifiers identifier = <value>;. For example,

int foo = 99;

We can initialize multiple variables as well following the same pattern,

int foo = 1, bar = 2, baz = 3;

Object's size[edit]

Main article: sizeof operator

In many cases it might be useful to know the size of an object. Knowing the size of an object is also required if you want to allocate the object dynamically. The size of the object can be retrieved via the sizeof operator which returns the amount of storage, in bytes, that is required to store an object of the type of the operand specified. The sizeof operator should always be used in order to not hard-code machine-dependent type sizes.

The sizeof operator has the following syntax:

sizeof unary-expression
sizeof (type-name)

For example,

#include <stdio.h>
int main()
{
    printf("Size of 'int' is: %zu\n", sizeof (int));
    return 0;
}

Will print the size of the int type on your machine. Note that we can apply the same operator on variables, for example:

#include <stdio.h>
int main()
{
    int x;
    long y;
    printf("Size of 'x' is: %zu, and 'y' is: %zu\n", sizeof x, sizeof y);
    return 0;
}

Bytes, not octets[edit]

It is important to note that the sizeof operator returns the size of the object in bytes and not octets. Various architectures might have different sizes for bytes. The Template:c/limits.h header provides the CHAR_BIT macro which expands to the number of bits in a byte. I.E. the size of an object consists of n * CHAR_BIT bits, where n is the size of an object. The standard does impose a minimum limit of 8 bits for a byte size. Other standards such as the POSIX standard requires that CHAR_BIT always be 8. For example,

#include <stdio.h>
#include <limits.h>
int main()
{
    int x;
    long y;
    printf("'x' is: %zu bits\n", sizeof x * CHAR_BIT);
    printf("'y' is: %zu bits\n", sizeof y * CHAR_BIT);
    return 0;
}