Open main menu
Newer edit →
4,559 bytes added
14 years ago
no edit summary
== Key ==
[[Concepts in Programming|Concepts]]
[[Variables in Programming|Variables]]
[[Variable Types in Programming|Types]]
[[Variable Arrays in Programming|Arrays]]
[[C in Programming|C]] Concepts
[[Variables in C in Programming|Variables]]
[[Variable Types in C in Programming|Types]]
[[Variable Arrays in C in Programming|Arrays]]
== Simple C types ==
In C, there are a small key set of types. The rest are modifiers, arrays and other such low-level data structures. The keywords for these types are '''<tt>int</tt>''', '''<tt>char</tt>''' and '''<tt>float</tt>'''.
When we wish to store an integer, we use an integer type. The C name for this type is called <tt>int</tt>. Ints usually require 4 bytes per variable.
When we wish to store a single character or byte, we use a character type. The C name for this type is called <tt>char</tt>. Chars always use one byte each.
When we wish to store a floating-point (non-integer) value, we use either a single precision or a double precision floating point type. For single precision, C terms this type <tt>float</tt>. For double, <tt>double</tt>. Variables of type <tt>float</tt> usually use 4 bytes of memory and those of type <tt>double</tt>, 8.
There is no fundamental predefined string type in C. Strings in C are more complicated (they are ''arrays'' of chars), requiring manual handling, and we will deal with them later.
== Type modifiers ==
'''<tt>long</tt>''' is a type modifier for numeric variables (those of type <tt>int</tt> or type <tt>double</tt>) which simply allocates more memory for the variable, allowing it to contain and process larger numeric values than usual. Some C implementations allow this modifier to be used twice in succession as a new modifier in itself ('<tt>long long</tt>') to define even larger variables.
'''<tt>signed</tt>''' is a type modifier which explicitly allows a variable to have either positive or negative values at the expense of a smaller possible range of values. By default all variables in a C program are defined as 'signed' variables.
'''<tt>unsigned</tt>''' is a type modifier which allows a variable to only have nonnegative values (zero or greater). However, it allows a range of values twice as large as that of the equivalent <tt>signed</tt> variable.
'''<tt>register</tt>''' is a hint to the compiler to attempt to optimise the storage of the given variable by storing it in a register of the computer's CPU when the program is run. Most optimising compilers do this anyway, so use of this keyword is often unnecessary.
'''<tt>volatile</tt>''' is a special type modifier which informs the compiler that the value of the variable may be changed by external entities other than the program itself. This is necessary for certain programs compiled with optimisations - if a variable were not defined <tt>volatile</tt> then the compiler may assume that certain operations involing the variable were safe to optimise away when in fact they aren't.
Arrays could be considered an extension of an existing variable's type: arrays cannot exist as a type on their own but act just to extend an existing variable. An array applied to a variable implies not a single variable but a series of variables of a single type indexed by integers starting from zero. The characters used to indicate an array are '[<var>x</var>]' where '<var>x</var>' is the number of variables in the series. For example, a single integer variable would be defined by <tt>int</tt>, but an array (series) of seven integer variables could be defined by <tt>int</tt>. Assuming this variable had the name <var>a</var>, the individual integer variables could be accessed as <tt>a</tt>, <tt>a</tt>, <tt>a</tt> and so on until <tt>a</tt>. The integer used to index the series of variables is also known as a ''subscript''.
== Declaring variables ==
All type declarations using the above simple types follow a standard format
where <tt>type_name</tt> represents one of the C type names, and variable name a generic name for the variable.
For example, if we want to declare a variable called <tt>number</tt> and of type <tt>int</tt>, we write
In C, all statements are terminated by semicolons. If we wish to declare a number of variables of the same type, we can write
:<tt>int variable1, variable2, variable3;</tt>
which delcares all <tt>variable1</tt>, <tt>variable2</tt>, and <tt>variable3</tt> to be of type integer.
Retrieved from "