Variables, Data Types And Constants In C

Variables In 'C'?

A variable is a data name that may be used to store a data value. Eg: name, rollno.

Rules Of Variable Names In 'C'

See the rules for constructing variable names
  • Variable name is any combination of 1 to 31 alphabets, digits or underscore but the length is compiler dependent.
  • Uppercase and lowercase are significant
  • It should not be a keyword
  • White space is not allowed
  • The first letter must be an alphabet or underscore

Data Types Of 'C'

Data types refer to an extensive system used for declaring variables or functions of different types. The available data types are in c:
  • Primary
  • Derived
  • User defined data types

Primary Data Types Of 'C'

The primary data types include:
  1. integer
  2. float
  3. character (char)
  4. void

Integer

Integers are whole. C has 3 classes of integer storage namely
  1. short int
  2. int
  3. long int in both signed and unsigned forms

Float

  • Floating point (real) numbers are defined in ‘C’ by the keyword “float”. Its range is –3.4e38 to +3.4e38. When the accuracy provided by float is not sufficient, double can be used. To extend the precision further, long double can be used
  • A single character can be defined as char data type. The qualifiers signed or unsigned are applied to char which can have values 0 to 255 and –128 to +127 respectively
  • The void type of has no value. This is usually used to specify the types of functions that do not return any value

Derived Data Types Of 'C'

The derived data type consists of
  1. Arrays (used to manage large numbers of objects of the same type.)
  2. Functions (does a specific job)
  3. Pointers (represents the address and type of a variable or a function)

User Defined Data Types Of 'C'

User defined data type includes
  • Structures (Different data items that make up a logical unit are grouped together as a structure data type)
  • Unions (A union permits references to the same location in memory to have different types) and
  • Enumeration (used to define variables that can only be assigned certain possible values throughout the program)

Declaration Of Variables In 'C'

After designing suitable variable names, we must declare them to the compiler. The declaration does 2 things:
  • It tells the compiler what the variable is
  • It specifies what type of data the variable will hold

Primary Data Type Declaration

A variable can be used to store a value of any data type Syntax
data-type v1,v2,…vn;
where v1,v2…vn are the names of variables. Example int count; double ratio
DATA TYPE KEYWORD USED
Character char
Unsigned character unsigned char
Signed character signed char
Signed integer signed int (or int)
Signed short integer signed short int (short int or short)
Signed long integer signed long int (long int or long)
Unsigned integer unsigned int (or unsigned)
Unsigned short integer unsigned short int (or unsigned short)
Unsigned long integer unsigned long int (or unsigned long)
Floating point float
Double-precision floating point double
Extended double precision floating point long double
Example
main()
      {
      /*Declaration*/
      float x,y;
      int code;
      longint amount;
      unsigned n;
      char c;
      ………
      ………
      }

User Defined Type Declaration

'C' supports a feature known as “type definition” that allows users to define an identifier that would represent an existing data type. The user-defined data type identifier can later be used to declare variables. Its format is: typedef type identifier; Example
typedef int units;  
typedef float marks;
Here, units symbolizes int and marks symbolizes float. They can be used to declare variables as follows:
units batch1, batch2;
marks name1[50], name[50];
Another user-defined data type is enumerated data type. It is defined as follows: Syntax
enum identifier v1,v2, … vn;
Example
enum day {Monday, Tuesday, … Sunday};

Storage Classes Of 'C'

Variables in C can have not only data type but also storage class that provide information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized.
STORAGE CLASSES AND THEIR MEANINGS
Specifier Meaning
auto Variables declared with the storage class specifier auto have automatic storage duration. The specifier auto is applicable only to variables that are declared within a function. Because the automatic storage class is the default for such variables, the specifier auto is rarely used.
register register The storage class specifier register instructs the compiler to store the variable in a CPU register if possible. As a result, the address operator (&) cannot be used with a register variable. In all other respects, however, register variables are treated the same as auto variables.
static Variables declared as static always have static storage duration. The storage class specifier static is used to declare static variables with a limited scope.
extern The specifier extern is used to declare variables with static storage duration that can be used throughout the program.
Example
	auto int count;
	register char ch;
	static int x;
	extern long total;
Static and external (extern) variables are automatically initialized to zero. Automatic (auto) variables contain undefined values (known as ‘garbage’) unless they are initialized explicitly.

Assigning Values Of Variables In 'C'

Variables are created for use in the program. Values can be assigned to variables using the assignment statement by using the operator “=” as follows:
	variable_name = constant;
Example
initial_value=0;
balance=75.84;
During assignment operation, ‘C’ converts the type of value on RHS to the type of value on the LHS. This may involve truncation when real value is converted to an integer. It is also possible to assign a value to a variable at the time the variable is declared. The format is:
	data-type variable_name = constant;
'C' permits multiple assignments in one line. Example
initial_value=0; final_value=100;
The process of giving values to variables is called initialization. ‘C’ permits the initialization of more than one variables in one statement using multiple assignment operators. Example
p=s=q=0;
main()
      {
      /* …….. Declaration …………*/
       float x,p;
       double y,q;
       unsigned k;
      /*……… Declaration and assignments…….*/ 
        int m=54321;
        long int n=1234567890;
      /*……… Assignments………………*/
        x=1.2345678900000;
        k=54321;
       }

Defining Symbolic Constants In 'C'

We often use certain unique constants in a program. These constants may appear repeatedly in a number of places in the program. A constant is 3.142, for “pi” value. The “pi” value can be used for many purposes in a program. We face two problems in the subsequent use of such programs.
  • Problem in modification of the program
  • Problem in understanding the program
Assignment of such constants to a symbolic name frees us from these problems. The symbolic constant can be defined as follows:
#define symbolic_name value of constant
// Example
#define STRENGTH 100
#define PASS_MARK 50
Symbolic names are sometimes called constant identifiers. The following are the rules for declaring symbolic constants,
  • Symbolic names have the same form as variable names.
  • no blank space between ‘#’ and the word ‘define’
  • # must be the first character in the line
  • a blank space is must between the #define and the symbolic name
  • after definition, the symbolic name should not be assigned to any other value within the program
  • symbolic names are not declared for data types
  • #define must not end with semicolon (;)
  • #define may appear anywhere in the program but before it is referenced in the program

Declaring A Variable As A Constant

In some cases, we may be in need of certain values of variables to remain constant during the execution of a program. This can be done with by declaring the variable with the qualifier const at the time of initialization.
 const int class_size=40;
(value of the int variable class_size cannot be modified by the program).

Declaring A Variable As Volatile

There is a qualifier called “volatile” before a variable that could be used to tell compiler that a variable’s value may be changed at anytime by external sources.
volatile int date;
OVERFLOW AND UNDERFLOW OF DATA

Overflow And Underflow Of Data In 'C'

  • Problem of data overflow occurs when the value of a variable is either too big or too small for the data to hold
  • In floating point conversions, the values are rounded off to the number of significant digits
  • An overflow normally results in the largest possible value a machine can hold and underflow results in zero
  • The overflow problem may occur if the data type does not match the value of the constant
  • ‘C’ does not provide any warning or indication of integer overflow. It simply gives incorrect results. So care should be taken to define correct data types for handling I/O values