GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Variables in C

A variable is the name given to the memory location. The memory to store the data is allocated on the RAM (Random Access Memory).

We have also discussed memory diagrams for variables at the end.

You may watch the video posted at the end of this page.

Syntax to declare a variable

datatype varName(s);
  • datatype can be int, float, double, char, struct, enum, union, array, pointer or signed, short or long of any primitive data type.
  • variable name is any valid identifier name.

Examples

  1. int a;
    • It creates a variable by name ‘a’ whose datatype is int. ‘a’ is a variable where any integer value (of integer range) can be stored.
  2. float b;
    • It creates a variable by name ‘b’ whose datatype is float. ‘b’ is a variable where any floating value (of float range) can be stored.
  3. double c;
    • It creates a variable by name ‘c’ whose datatype is double. ‘c’ is a variable where any double number (of double range) can be stored.
  4. char d;
    • It creates a variable by name ‘d’ whose datatype is char. ‘d’ is a variable where any character value (of character range) can be stored.
  5. int x, y;
    • Creates two variables ‘x’ and ‘y’ of type int.

Memory layout of RAM

  • RAM is divided into addresses.
  • The addresses are normally represented in hexa-decimal numbers.
  • Addresses of RAM are unique. There are no duplicate addresses.
  • Addresses of RAM belongs to set of whole numbers. These addresses are long long int (compiler dependent). Therefore, address of a computer takes 8 bytes of data.
  • Whenever a variable is created the memory is allocated from the RAM. The memory is allocated wherever the free space is available. 
  • The memory allocated for a variable is continuous. 
  • But for different variables the memory allocated is random, it may or may not be continuous.

Note: There is memory layout of C program with respect to RAM, which we shall discuss later.

memory layout of RAM

Whenever a variable is created the memory is allocated on the RAM. The contents of the memory allocated is Garbage or Junk value by default. Garbage is any un-known value.

Let us understand these with memory diagrams for some examples.

Memory diagrams for variables

int a;

variable_in_c
  • The address of the variable is randomly assumed.
  • The memory allocated for the variable depends on the datatype. It allocates 4 bytes of data, since it is an integer.
  • The value stored at the given address is GARBAGE. (By default)

int b = 10;

variable in c

The variable ‘b’ holds the value 10. If the value is not initialized then it will be GARBAGE, if initialized the value of variable will be the given value. Here it is 10.

double c;

variable in c
  • The memory allocated for the variable depends on the datatype. It allocates 8 bytes of data, since the data type is double.
  • The value stored at the given address is GARBAGE. (By default)

double d = 1.1;

variable in c

float e = 2.3;

variable in c

char ch = 'A';

  • The character values are written in single quote.
  • Character values are stores as ASCII (American Standard Code for Information Interchange) values internally.
  • ASCII values are integers, Different characters have different ASCII values.
  • There are 256 different ASCII values, as there are 256 characters (Range: 0 to 255) 
  • ASCII values for ‘A’ to ‘Z’ is 65 to 90
  • ASCII values for ‘a’ to ‘z’ is 97 to 122
  • ASCII values for ‘0’ to ‘9’ is 48 to 57
  • Rest all ASCII values are for special characters. 
variable in c

Note: That the value of variable ch is stored as ASCII value internally.

int g = b + 3;

Create a new variable ‘g’, store in ‘g’ b+3.

  • Evaluate RHS first, in RHS we have b+3, there is a variable ‘b’ whose value is 10, evaluate 10 + 3 = 13, store 13 in variable ‘g’.
variable in c

int h = a + 2;

variable in c

A new variable ‘h’ is created. Since the value of variable ‘a’ is Garbage (unknown), Garbage +2 = Garbage. Therefore, the value of variable ‘h’ is Garbage.

Note: If any one variable on the RHS of assignment operator (=) has Garbage value, then the variable on LHS will also have the Garbage value.

Hence make sure that all the variables on RHS of '=', their values are known before its first usage.

a = 25;

  • The value ’25’ is stored in variable ‘a’.
  • If no data type is written along with the variable name, then the existing variable will be used.
  • If data type is written along with variable name, then the new variable will be created.
  • There is an existing variable with name ‘a’. The value is Garbage, and it is overwritten with 25.
variable in c

m = 25;

  • Since we didn’t write any data type with the variable name, the existing variable has to be used. 
  • We see that there is no variable with name ‘m’. Hence it gives an error called undeclared variable m.
Note: A variable cannot be used unless it is declared.

double b = 9.8;

  • The above declaration produces an error. As the variable name ‘b’ is already taken.
Note: There cannot be duplicate variable names in the same scope. More about scopes we will discuss in functions chapter.

int n = ch + 4;

  • A new variable ‘n’ will be created. 
  • ‘n’ will have value of ch + 4, note that the variable ch is of type character, stored as ASCII value (int).
  • Evaluate RHS first, ch + 4 = 65 + 4 = 69
  • Therefore, n will have value of 69.
variable in c

a = a + 8;

  • Evaluate RHS first ( a+ 8) and store the value in ‘a’.
  • Take recent value of a i.e, 25, 25 + 8 = 33, store 33 in ‘a’. (25 will be over-written)
  • In shorthand notation we can write it as, a + = 8
variable in c

c = 2/3;

  • Note that variable ‘c’ is already declared of double datatype.
  • ‘c’ can store real numbers.
  • Evaluate RHS first, which is 2/3 = 0 (int/int = int)
  • Store 0 in variable ‘c’, we see that 0 is an integer, convert 0 to real number it becomes 0.0, store 0.0 in variable c.
variable in c
  • Even though, the datatype is double, we lose answer, because RHS is an integer (2/3).
Note: If the data on RHS and datatype on LHS don't match there is a possibility of loss of data.

h = 2.0 / 3;

  • Evaluate RHS first, 2.0 / 3 = 0.67 (real_number / integer = real_number)
  • We want to store 0.67 in variable ‘h’. But ‘h’ is of type int. We can only store int values. Therefore, we convert 0.67 to integer which becomes 0.
variable in c
Scroll to Top