C Variables and Expression

A variable is a named location in memory. Variables are used to give a value a name so we can refer to it later.

The name of a variable is an identifier token. Here are rules for declaring a variable:

  • Identifiers may contain numbers, letters, and underscores(_).
  • Variable name can not start with a number.
  • Keywords can't be used as variable name.

Let us first dig into some important terms.



These are reserved words i.e. their meaning is already defined by compiler. The C language has 32 keywords which may not be used as identifiers. Here is list of keywords in C.

auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Table: Keywords in C


  • Identifiers are name given to variable, functions, structures etc
  • These names are made up of letters and digits, and are case-sensitive.
  • The first character of an identifier must be a letter, which includes underscore (_).
  • keywords can not be used as identifiers.


Symbolic Constants

They represent a constant values, from the set of constant types (eg. an integer constant 1234 is of type int) by a
symbolic name.

For example,

#define MY_STRING "Hello World\n"
#define PI 2.7183

After defining these symbolic constants, wherever they appear in the code, it is equivalent to direct text-replacement with the constant it defines.

The program below makes the idea more clear.

#include <stdio.h>

#define MY_STRING "Hello World\n"
#define PI 2.7183

int main ()
    printf("Printing the value of symbolic constants defined in header:\n");
    printf("MY_STRING: %s",MY_STRING);
    printf("PI: %f",PI);

The output of the program is:

Printing the value of symbolic constants defined in header:
MY_STRING: Hello World 
PI: 2.718300

The main importance of symbolic constants is that they keep constants together in one place so that making changes is easy and safe.

Note: For standard practices, symbolic constants are by convention given uppercase names to make them distinct from variables and functions.


Variable Declaration and Initialization

A section of code enclosed in brackets { and } form a block. All variables must be declared before they are used.

Variables must be declared at the top of a block  before any statements. They may be initialized by a constant or an expression when declared.

You must declare all variables before they can be used. The basic form of a variable declaration is shown here:

 data type variable [ = value][, variable [= value] …] ;

where the assignment to an initial value is optional.

Here data type is one of C's data types and variable is the name of the variable.

To declare more than one variable of the specified type, you can use a comma-separated list. Following are valid examples of variable declaration in C:

int a, b, c; // Declares three ints, a, b and c.


The declared variable can be initialized with compatible value.


type variable_name;

variable_name = initial_value; 


int a,b;        //Declares variables a & b
a = 10, b = 10; // initializes them with value of 10 each


Declaration and initialization can be combined as:

data_type variable [ = value][, variable [= value] …] ;

For example;

int a = 10, b = 10; //Declares variables a & b and initializes them with value of 10 each

And here is  an example of initialization

byte B = 22; // initializes a byte type variable B.

double pi = 3.14159; // declares and   assigns a value of PI.

char a = 'a'; // the char variable a is initialized with value 'a'

Here is an example of declarations.

{ /* bracket signifies top of a block */
    int lower, upper, step; /* 3 uninstalled ints */
    char tab = ’\t’;        /* a char initialized with ’\t’ */
    char buf[10];           /* an uninstalled array of chars */
    int m = 2+3+4;          /* constant expression: 9 */
    int n = m + 5;          /* initialized with 9+5 = 14 */
    float limit = 9.34f;
    const double PI = 3.1416;
} //this signifies end of a block



An expression is a statement that has a value – for instance, a number, a string, the sum of two numbers, etc.

Expressions combine variables and constants to produce new values. For example, 4+2, x-1, and "Hello, world!\n" are all expressions.

And, a statement is a unit of code that does something. Statements are a basic building block of a program.

Note: Not every statement is an expression. For instance, it makes no sense to talk about the value of an #include statement.


Here, is example program to illustrate all points mentioned above.

// initialization of variables, statement and expression example

#include <stdio.h>

int main ()
  int a = 10;             // initial value: 10
  int b = 30, c = 20;     // initial value: 30 for b and 20 for c
  int result;             // initial value undefined

  a = a + b;              // statement but a+b is expression
  result = a - c;         // statement
  printf("Result: %d", result);

  return 0;

The output of the program is:

Result: 20


Basic Input

Just as printf() is a function for outputting values, scanf() is the syntax for inputting values.

Syntax of scanf() function is:

                 scanf (“format_string”, argument_list);

The format_string must be a text enclosed in double quotes. It contains the information for interpreting the entire data for connecting it into internal representation in memory.

Example: integer (%d) , float (%f) , character (%c) or string (%s).

The argument_list contains a list of variables each preceded by the address list and separated by comma.


The following program illustrates basic input.

//example program for inputting value

int main(){
    int integer;
    float decimal;
    printf(" Enter integer number: ");
    printf(" You entered: %d",integer);

    printf("\n Enter decimal number: ");
    printf(" You entered: %f",decimal);

The output of the program is:

Enter integer number: 12 
You entered: 12 
Enter decimal number: 12.12 
You entered: 12.120000 

So, scanf() function inputs the value.

The number of argument is not fixed; however corresponding to each argument there should be a format specifier. Inside the format_string the number of argument should tally with the number of format specifier.

Example: if i is an integer and j is a floating point number, to input these two numbers we may use 

scanf (“%d%f”, &i, &j);

We'll discuss more about input function in preceding chapters.