Home | API | MFC | C++ | C | Previous | Next

Programming With C

Variables

Variables are named locations used to store data or information to be referenced and used by programs. 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:

Variable Declarations

A variable declaration will specify to the compiler the name and type of a variable. A variable definition tells the compiler where and how much storage to create for the variable although the size of the data types can vary depending on your computer platform. Any attempt to use a variable that hasn’t been declared will result in the compiler generating an error message. Multiple single-line variable declarations are separated by the use of a comma. A variable declaration has the following format:

typename varname;

The variable declaration may also initialise the variable to a specific value eg

int v=3; (declares and initialises the variable v to value 3)

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

Integer variables

Integer variables hold values that have no fractional part. Signed Integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values. The common Supported Integer types are-

Type Storage size Value Range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
double 8 bytes 2.3E-308 to 1.7E+308 (15 decimal places)
Long double 10 bytes 3.4E-4932 to 1.1E+4932 (19 decimal places)

Floating-Point Types

A variable declared to be of type float can be used for storing values containing decimal places.

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal place

The typedef keyword

The typedef declaration is used to assign alternative names to existing types. When compiled, the identifier is substituted with the variable type however the compiler does not create a distinctive variable type but rather establishes a synonym for an existing variable type -

typedef int integer; (will create a synonym for an integer for the existing variable type int)

sizeof

The sizeof() operator indicates 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;
}

Variable Scope

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

In C programming variables can be declared in three places:

  1. Inside a function or a code block. These are known as local variables and are only valid inside the block they are defined
  2. Outside of all functions. These are known as global variables and can be accessed in any part of the program
  3. 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.

Extern

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

extern int x; //declares a int x value which has been defined elsewhere
Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables | Constants | Expressions and Operators | Controlling Program Flow | Input & Output | C++ Functions | Arrays | Characters and Strings | String Manipulation | Pointers | Structures | Unions | File Input and Output | Advanced Pointers | Type Conversion | Dynamic Memory Allocation

Last Updates:25 October 2022