Variables and constants

 

This page is about variables and constants, but also looks at data types, as these are very relevant to variables.

A variable can be thought of as an object with a name, that occupies part of RAM, and holds some kind of content or value which can be changed.

 

Types of variable

There are various kinds of variables, which correspond to the available data types that are specified within C. The amount of storage space allocated to a variable depends on the data type. The four basic types are :-

However these can be subdivided into different sub-types - there seems to some variation in what they are called, and the amount of space they are allocated. I think there is also a dependency on the type of hardware, ie, 8 bit, 16 bit, 32 bit, etc. So a possible fuller list, along with the amount of space they are allocated, and their data capacity, could be -
 

Although the use of floating point types allows for very large numerical values, the accuracy of the values is quite a bit lower than the values contained in integer variables.

There is another data type called "void" - it basically means that no data is available or returned. However a variable can`t contain nothing - it may start with the number zero in it, but the number zero is something, as far as variables are concerned. So since a variable can`t contain nothing, by definition, you can`t have a variable of type void.

Having gone through all the above data types, variables are declared using the same descriptors, as described in the next section.

 

Declaring variables

Variables must be declared in the C script before they are required to be used. There are two places they can be delared -

The declaration of global variables usually follows the various pre-processor directives such as #include and #define, and must come before the start of the "main" function. Each declaration is a statement, and should be closed with a ";".

The declaration takes the form of -

The content of a variable can be changed at any time during the programme. Providing an initial value isn`t mandatory, if none is given, the default put in is zero.

So a typical script would be -


        #include <stdio.h>

        #define SOME_MACRO

                          /* we now declare 5 variables */

        char text; 

        int alpha = 27;  
   
        unsigned long int gamma = 12345

        float beta;

        double bignumber;

                          /* start of the main function */

        void main (void) { .....  
        

Although the contents of a variable can of course be changed in the script, the data type is fixed.

When a value type of variable is declared, the compiler knows how many bytes of memory space have to be allocated to that variable. However this is not the case for the char type of variable, so a parameter should be added to the char variable name, to advise the compiler how many characters to allocate space for - at the rate of one byte per character. For example -


        #include <stdio.h>

        #define SOME_MACRO

        char text[40];
     

This means that the variable is allocated memory for up to 40 characters. Some texts suggest that this is actually an array - a single line array that can contain 39 characters. Character 40 is \0, which is the null character, and marks the end of the display. So 40 bytes of memory would have to be allocated.

Like other types of variable, char variables can be prefilled within the declaration as well -


        #include <stdio.h>

        #define SOME_MACRO

        char text[40] = "textstring";
   

Note the double quotes. The text string can be subsequently changed in the script, and the replacement string doesn`t have to be the same length as the original. In the above declaration, a replacement text string can be up to 39 characters long. If the replacement string is too long, the source code may compile, but the programme will not work properly.

Going back to the other kinds of variable, multiple variables can be included in the same declaration, but this does of course mean that they have to all have the same data type.


        #include <stdio.h>

        #define SOME_MACRO

        int alpha, beta, charlie;
        

Multiple variables can be given the same value -


        #include <stdio.h>

        #define SOME_MACRO

        int alpha, beta, charlie = 1;
        

 

Constants and constant variables

One of the ways to set up a constant is to define a macro using a pre-processor directive.


        #include <stdio.h>

        #define FIXED 20
   

This line defines the macro with the name of "FIXED", and during the pre-processing, the number 20 is put in, in place of the macro name. No storage space in memory is allocated to the macro. If the number 20 needs to be changed, then the programme will have to be compiled again.

An alternative to defining a constant is to declare a variable as a constant - by using the "const" keyword at the start of the declaration.


        #include <stdio.h>

        const int fixed = 20;
   

There may be a programming advantage in having a variable that can`t be changed - ie, a read-only variable.

It could be regarded as a disadvantage, in that because it is a variable, some memory space has been allocated to it. On the other hand, the fact that the constant has a memory location may be an advantage in some programming situations.

Most texts say that the contents of a constant variable can`t be subsequently changed, however I have seen reference to the fact that constant variables can be changed.

 

 

 

 

 

website design by ron-t

 

website hosting by freevirtualservers.com

 

© 2024   Ron Turner

 

+                                   +  

 

Link to the W3C website.   Link to the W3C website.