Using Variables in Your C Programs

Using Variables in C

A variable is essentially a space in the computer's memory used to store a value that can be accessed or changes by the program. The C program below and its output demonstrates the use of some of the variables available in the C programming language. Since many other languages are built upon C, this approach is very similar in those other languages as well. The topic of variables in programming languages could fill a book on its own. Here we will cover the basics.

 

When we create a variable in C, we must tell the compiler the variable type so that the compiler knows how much memory needs to be allocated and how to format the data that is stored at the variable location. The types we will cover here are char, unsigned char, int, unsigned int, float and double. In the small program given below variables are created for each of these types and a value is assigned to them. In this case, we use defined constants from the limits.h and float.h libraries to assign the minimum or maximum value to variables to demonstrate the smallest and largest number each can hold. Later in the program, we use the 'sizeof()' function to show how many bytes each data type requires in memory.

 

Rules for declaring variables

  1. Syntax: [variable type] [variable name];
  2. Example: int myVariable;
  3. Variable number must only contain letters, numbers, and underscore.
  4. Variable names must not contain spaces.

 

Good practices for using variables in C

  1. Use descriptive names for variables. Avoid using ambiguous variable names such as 'x' or 'y'.  This isn't algebra class.
  2. Use camel case or underscores when multiple words are needed in a variable name. The example below uses camel case where the first letter of each work is capitalized. You can also use underscores between each word. Either makes the variable name easier to understand.
  3. Don't use all capital letters. Save that for naming constants.

 

char and unsigned char

The variable type char is short for character. A char value corresponds to an ASCII character. Every letter on this page is, at some point, represented as a char. A char is 8 bits or 1 byte as indicated by the output of the program below. In some programs, especially embedded programming, the char might be used simply to control bits in registries or to read or set the pins of I/O ports. If used to represent a numerical value a char can hold a number from -128 to 127 as can be seen in the demo program below. An unsigned char can hold a value from 0 to 255. You will see the char in other languages besides C.

 

int and unsigned int

The variable type int is short for integer. An int is any whole number such as -3, -2, 0, 1, 2, 3. In the program below you can see the size of the int is 4 bytes, but there are some implementations of C where it is only 2 bytes. In those implementations a variable type called 'long' might be useful where larger numbers are needed, but with the compile I'm using, an int and a long are exactly the same. An int can hold any whole number value from -2147483648 to -2147483648 and an unsigned int can hold any whole number value from 0 to -2147483648. You will see the int in other languages besides C.

 

float

The variable type float holds a floating point number. A floating point number is any real number. What this means is it can be a fractional number with decimal places. A float in the case of the program below takes up 4 bytes in memory, but that can change depending on the development environment. You will see the float in other programming languages besides C.

 

double

The variable type double is exactly like a float except it takes up 8 bytes instead of 4 bytes. As such it has much more precision and can handle a much greater range of values than the float at the expense of taking up twice as much space in memory.

 

Other variable types

It is possible to create our own variable types. In fact some other variable types have been created by others and are commonly available to us through libraries. The sample program below declares a few of these additional variable types that can be useful when a specific data size is needed.

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>

int main()
{
    /* Introducing the char. Max and Min values are defined in the limits.h library */
    char oneByteMax = SCHAR_MAX;
    char oneByteMin = SCHAR_MIN;
    unsigned char onePositiveByteMax = UCHAR_MAX;
    printf("The maximum value of a signed char is %d\n", oneByteMax);
    printf("The minimum value of a signed char is %d\n", oneByteMin);
    printf("The maximum value of an unsigned char is %u\n", onePositiveByteMax);

    /* Introducing the int. Max and Min values are defined in the limits.h library */
    int wholeNumberMax = INT_MAX;
    int whileNumberMin = INT_MIN;
    unsigned int possitiveIntegerMax = UINT_MAX;
    printf("The maximum value of an int is %d\n", wholeNumberMax);
    printf("The minimum value of an int is %d\n", whileNumberMin);
    printf("The maximum value of an unsigned int is %u\n", possitiveIntegerMax);

    /* Introducing the float. Max and Min values are defined in the float.h library */
    float anyFourByteRealNumberMax = FLT_MAX;
    float anyFourByteRealNumberMin = FLT_MIN;
    printf("The maximum value of a float is %E\n", anyFourByteRealNumberMax);
    printf("The minimum value of a float is %E\n", anyFourByteRealNumberMin);

    /* Introducing the double. Max and Min values are defined in the float.h library */
    double anyEightByteRealNumberMax = DBL_MAX;
    double anyEightByteRealNumberMin = DBL_MIN;
    printf("The maximum value of a double is %E\n", anyEightByteRealNumberMax);
    printf("The minimum value of a double is %E\n", anyEightByteRealNumberMin);

    /* How much memory do they occupy? sizeof comes from the stddef.h library*/
    printf("The size of a char is %d bytes\n",sizeof(char));
    printf("The size of an int is %d bytes\n",sizeof(int));
    printf("The size of a float is %d bytes\n",sizeof(float));
    printf("The size of a double is %d bytes\n",sizeof(double));

    /* Fixed size variable types coming from the stdint.h library */
    int8_t oneByteWholeNumber;
    int16_t twoByteWholeNumber;
    int32_t fourByteWholeNumber;
    int64_t eightByteWholeNumber;
    uint8_t oneBytePositiveInteger;
    uint16_t twoBytePositiveInteger;
    uint32_t fourBytePositiveInteger;
    uint64_t eightBytePositiveInteger;

    return 0;
}
This demo program demonstrates the size of different variable types in a C program.
Home | Products | Electronics | Programming | SpazzOuts | Contact
© Copyright 2014 - 2017 SpazzTech LLC. All Rights Reserved
Made with Adobe Muse