C TUTORIALS

LEARN C PROGRAMMING

To learn about the data types in C Programming language, click here.

In this post, we will learn about the declaration of C Variables with the different types of datatypes, in C programming Language.

Declaration of variables

Every variable used in the program should be declared to the compiler.The declaration does two things:

  1. Tells the compiler the variables name.
  2. Specifies what type of data the variable will hold.

The general format of any declaration:

datatype V1, V2, V3,………. VN;

Where V1, V2, V3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon.

Example:
int sum;
int number, salary:
Double average, mean;

 

DATATYPE KEYWORD EQUIVALENT
Character char
Unsigned Character unsigned char
Signed Character signed char
Signed Integer signed int (or) int
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) 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

User defined type declaration

In C language a user can define an identifier that represents an existing data type. The user defined data type identifier can later be used by the programmer to declare variables. The general Syntax is:

typedef type identifier;

here ‘type’ represents existing C data type and ‘identifier’ refers to the ‘new’ name given to the data type.

Example:

typedef int sal;

typedef float avg;

Here sal symbolizes int and avg symbolizes float. They can be later used to declare variables as follows:

sal dep1, dep2;

avg sec1, sec2;

Therefore dep1 and dep2 are indirectly declared as integer datatype and sec1 and sec2 are indirectly float data type.

The second type of user defined datatype is enumerated data type which is defined as follows.

enum identifier { value1, value2, …….valuen };

This identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values within the braces. After the definition we can declare variables to be of this ‘new’ type as below:

enum identifier v1, v2, v3, … vn.

The enumerated variables v1, v2,… vn can have only one of the values value1, value2 ….value n.

Example:

enum month { January, February…………… December};

enum month year_start, year_end;

year_start = January;

year_end = December;

Declaration of storage class

Variables in C have not only the data type but also storage class that provides information about their location and visibility. The storage class divides the portion of the program in which the variables are recognized.
auto: It is a local variable known only to the function in which it is declared. Auto is the default storage class.
static: It is a local variable which exists and retains its value even after the control is transferred to the calling function.
extern: It is a global variable known to all functions in the file.
register: The social variables which are stored in the register.

Defining symbolic constants

A symbolic constant value can be defined as a preprocessor statement and can be used in the program as any other constant value. The general form of a symbolic constant is

# define symbolic_name value of constant

Valid examples of constant definition are:

# define marks 100

# define total 50

# define Pi 3.14159

These values may appear anywhere in the program, but must come before it is referenced in the program.

It is a standard practice to place them at the beginning of the program.

Declaring variable as a constant

The values of some variable may be required to remain constant throughout the program. We can do this by using the qualifier const at the time of initialization.

Example:

Const int salary = 10000;
The const data type qualifier tells the compiler that the value of the int variable salary may not be modified in the program.

Format specifiers

The most common format specifiers are:

%d print an int type of argument in decimal

%ld print a long int type of argument in decimal

%c print a character

%s print a string

%f print a float or double type of  argument

%e same as %f, but use exponential notation

%o print an int type of argument in octal (base 8)

%x prints an int type of  argument in hexadecimal (base 16)

%% print a single percent sign (%).

%d bloggers like this: