Your browser doesn't support JavaScript Variables - Windows Programming

Variables

Variables are named locations in a computer’s memory used to store data. The value of this variable can be changed one or more times during the execution of a program. In C, all variables and the type of values that the variable can hold must be declared before they are used and must adhere to the following rules:

  • The variable name can contain letters (a to z and A to Z), digits (0 to 9), and the underscore character ( _ )
  • The first character of the name must be a letter. or underscore. A digit cannot be used as the first character.
  • C is case-sensitive.
  • C keywords are not acceptable as variable names. A keyword is a word that is part of the C language.

Variable Declarations

A variable declaration will specify to the compiler the name and type of a variable. When a variable is declared the compiler automatically allocates memory for it and this size can vary depending on the computer platform. Any attempt to use a variable that hasn’t been declared will result in the compiler generating an error message. A variable declaration will consist of the variable type followed by the variable name. Multiple single-line variable declarations are possible by separating each variable name with a comma –

int x //declares variable of type int
float x,y,z; //declares variables of type float
short int x,y,z; //declares variables of type short int
unsigned int x,y,z; //declares variables of type unsigned int
char xyz; //declares variable of type char

Initialising Variables by Assigning Values

A variable can be assigned an initial value when created or after creation but these values must be in the correct format. In C, there are three ways to initialise a variable-

Using the assignment operator (=)

int value=10; //creates and initialises variable value of type int to 10

Using parenthesis ()

int value (20) ; //creates and initialises variable value of type int to 20

Using braces {}

int value{30} ; //creates and initialises variable value of type int to 30

An uninitialised variable is a variable that is declared but is not set to a definite known value. An uninitialised variable will have some unknown value known as a “garbage value”. This is due to the variable being assigned a memory location by the compiler and inheriting whatever value happens to be in that memory location. Using the value from an uninitialised variable can result in undefined behaviour and data corruption.

Smaller variables require less memory, and the computer can perform faster mathematical operations. In contrast, large integers and floating-point values require more storage space and thus more time for performing mathematical operations. Using the correct variable types ensures your program runs as efficiently as possible.

Variable types

Data typeNotes
charStore -127 to 127 or 0 to 255. 8 bits. Usually used for holding ASCII characters.
char16_tUnsigned integer type. Usually 16 bits. Used for UTF-16 character representation
char32_tUnsigned integer type. Usually 32-bit bits. Used for UTF-32 character representation
wchar_tOccupies between 16 or 32 bits depending on the compiler being used. Used for Unicode character representation
signed char8 bits. Can store values between -128 to +127. Used for dealing with binary data
signed short intUsually at least 16 bits. Can store values between –32,768 to 32,767
signed intUsually at least 16 bits. Can store values between –32,768 to 32,767
signed long intUsually at least 32 bits. Can store values between –2,147,483,648 to 2,147,483,647
signed long long intusually at least 64 bits. Can store values between –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned char8 bits. Can store values between 0 to 255
unsigned short intAt least 16 bits. Can store values between 0 to 65,535
unsigned intAt least 16 bits. Can store values between 0 to 65,535
unsigned long intAt least 32 bits. Can store values between –2,147,483,648 to 2,147,483,647
unsigned long long intAt least 64 bits. Can store values between 0 to 4,294,967,295
floatUsually 32 bits. Used for storing single precision floating point values.
doubleUsually 64 bits. Used for storing double precision floating point values
long doubleUsually 96 bites. Used for storing long double precision floating point values

Variable Scope

A scope is a region of a program, and the scope of variables refers to the area of the program where a variable exists and can be accessed after its declaration.

In C, programming variables can be declared in three places:

  • Inside a function or a code block. These are known as local variables and are only valid inside the block they are defined.
  • Outside of all functions. These are known as global variables and can be accessed in any part of the program
  • In the function parameters (formal parameter)

When a local variable is defined, it is not initialised by the system and must be initialised by the program. Global variables are initialised automatically by the system.

Using Type Definitions

The typedef declaration provides a way to declare an identifier as an alias or shortcut for an existing variable type. When compiled, the compiler substitutes the identifier with the variable type. Typedef does not create a distinctive variable type but establishes a synonym for an existing variable type –

typedef int aliasint; //create an alias for int name aliasint
aliasint v=12; //creates int variable v and set to 12

Sizeof

The sizeof() operator returns the size in bytes of a variable or a type. Since the sizes of a variable or data types can differ between computing environments, knowing the size of a variable in all situations can be useful. The following program lists the size of the C variable types

#include <stdio.h>
int main(void)
{
printf( "\nA char is %d bytes", sizeof( char ));
printf( "\nAn int is %d bytes", sizeof( int ));
printf( "\nA short is %d bytes", sizeof( short ));
printf( "\nA long is %d bytes", sizeof( long ));
printf( "\nA long long is %d bytes\n", sizeof( long long));
printf( "\nAn unsigned char is %d bytes", sizeof( unsigned char ));
printf("\nAn unsigned int is %d bytes", sizeof( unsigned int ));
printf("\nAn unsigned short is %d bytes", sizeof( unsigned short ));
printf("\nAn unsigned long is %d bytes", sizeof( unsigned long ));
printf("\nAn unsigned long long is %d bytes\n",sizeof( unsigned long long));
printf( "\nA float is %d bytes", sizeof( float ));
printf( "\nA double is %d bytes\n", sizeof( double ));
printf( "\nA long double is %d bytes\n", sizeof( long double ));
return 0;
}

Enumerations

An enumeration is a user-defined data type that consists of integral constants called enumerators. An enum variable takes only one value out of many prescribed values and returns a numeric value corresponding to the selected enumeration. These constants can then be used anywhere that an integer can. Enumerations are defined using the keyword enum followed by the type-name and then the variable-list.

The enumeration list is a comma-separated list of names representing the enumeration values. The variable list is optional because variables may be declared later using the enumeration type name. Although enumerated constants are automatically converted to integers, integers are not automatically converted into enumerated constants

The following code declares an enumerator called month with the first enum variable value set to one. An enumerator variable is declared called now and set to the value jun. The value of 6 is then output to the screen.

#include <stdio.h>
enum month {Jan=1, Feb, Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec };
int main()
{
    // creating now variable of enum month type
    enum month now;
    now = Jun;
    printf("Day %d",now);
    return 0;
}

Extern

The extern keyword enables the programmer to declare a variable without defining it. In programs consisting of several files, each file must be aware of the global variables defined outside that file. The declaration informs the compiler that a variable by that name and type exists and the compiler does not need to allocate memory for it.. The syntax is –

extern int x; //declares a int x value which has been defined elsewhere