C Programming Tutorial

C Data Types Variables and Constants

C Data Types

C supports different types of data. A data type determines the type of data that a variable will hold. Storage representation of these data types is different in memory. There are four fundamental datatypes in C, which are int, char, float and double. ‘char’ is used to store any single character, ‘int’ is used to store integer value, ‘float‘ is used for storing single precision floating point number and ‘double’ is used for storing double precision floating point number.

We can use type qualifiers with these basic types to get some more types.

There are two types of type qualifiers-

  1. Size qualifiers short, long
  2. Sign qualifiers signed, unsigned

When the qualifier unsigned is used the number is always positive, and when signed is used number may be positive or negative. If the sign qualifier is not mentioned, then by default signed qualifier is assumed. The range of values for signed data types is less than that of unsigned type. This is because in signed type, the leftmost bit is used to represent the sign, while in unsigned type this bit is also used to represent the value.

The size and range of different data types on a 16-bit machine is given in the following table. The size and range may vary on machines with different word sizes.

Data Types with Type Qualifiers Storage size Value range
1 byte
-128 to 127 or 0 to 255
Unsigned Char
1 byte
0 to 255
Signed Char
1 byte
-128 to 127
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
Unsigned Int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,967,295
2 bytes
-32,768 to 32,767
Unsigned Short
2 bytes
0 to 65,535
8 bytes
-9223372036854775808 to 9223372036854775807
unsigned long
8 bytes
0 to 18446744073709551615

C Variables

Variable is a name that can be -used to store values. Variables can take different values but one at a time. These values can be changed during execution of the program. A data type is associated with each variable. The data type of the variable decides what values it can take. The rules for naming variables are same as that for naming identifiers.

Declaration of Variables

It is must to declare a variable before it is used in the program. Declaration of a variable specifies its name and datatype. The type and range of values that a variable can store depends upon its data type. The syntax of declaration of a variable is-

Data-type variable_name;

Here datatype may be int, float, char, double etc. Some examples of declaration of variables are­

int x;

float salary;

char grade;

Here x is a variable of type int, salary is a variable of type float, and grade is a variable of type char. We can also declare more than one variable in a single declaration. For example-

Int  x, y, z, total;

Here x, y, z, total are all variables of type int.

Initialization of Variables

When a variable is declared it contains undefined value commonly known as garbage value. If we want, we can assign some initial value to the variable during the declaration itself, this is called initialization of the variable. For example-

int a = 5;

float x = 8.9, Y = 10.5;

char ch = ‘y’;

double num= 0.15197e-7;

int 1, m, n, total = 0;

In the last declaration only variable total has been initialized.

C Constants

Constant is a value that cannot be changed during execution of the program. There are three types of constants-
  1. Numeric Constants
  2. Character Constants
  3. String Constants

Numeric Constants-

numeric constants consist of numeric digits, they may or may not have decimal point (.). These are rules for defining numeric constants-

  • Numeric constant should have at least one digit.
  • No comma or space is allowed within the numeric constant.
  • Numeric constants can either be positive or negative, but default sign is always positive.

there are two types of numeric constants-

  1. Integer constant
  2. Real (floating point) Constant

Integer constant

integer constants are whole numbers which have no decimal point ( . ). There are three types of integer constants based on different number systems. The permissible characters that can be used in these constants are-
Decimal constants –  0,1,2,3,4,5,6,7,8,9                                                                    (base 10)
Octal “constants – 0,1,2,3,4,5,6,7                                                                              (base 8)
Hex decimal constants – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, a, b, c, d, e, f.        (base 16)
some valid decimal integer constants are-
Some invalid decimal integer constants are-
Invalid                                    Remark
2.5                                          illegal character (.)
3#5                                        illegal character (#)
98 5                                       No blank. space allowed
0925                                       First digit cannot be zero
8,354                                     Comma is not allowed’

C Floating-Point Constants

A “floating-point constant” is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent.

Some valid floating point constants are-


Character Constants

A character constant is a single character, enclosed in apostrophe’s i.e. single quotation marks. Character constants have integer values that are determined by computers. Those integer values are called ASCII values. ASCII stands for American Standard Code for Information Interchange.
Example of character constants are-

‘A’, ‘b’, ‘@’, ‘9’, ‘?’
Some invalid character constants are-

Invalid·                              Remark
‘four’                                  There should be only one character within quotes
“d”                                     Double quotes are not allowed
‘ ‘                                       No character between single quotes
y                                        Single quotes missing

String Constants

A string constant has zero, one or more than one character. A string constant is enclosed within double quotes (” “). At the end of the string, \0 is ‘automatically placed by the compiler.
some examples of string constants are-


Note that “A” and ‘A’ are different, the first one is a string constant which consists of character A and \0 while the second one is a character constant which represents integer value 65.

Symbolic Constants

If we want to use a constant several times, then we can provide it a name. For example, if we have to use the constant 3.14159265 at many places in our program, then we can give it a name PI and use this name instead’ of writing the constant value everywhere. These types of constants are called symbolic constants or named constants.

A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a numeric constant, a character constant, or a string constant. These constants are generally defined at the beginning of the program as-

#define name value

Here ‘name’ is the symbolic name for the constant and is generally written in uppercase letters. ‘value’ can be numeric, character, or string constant.

Some examples of symbolic constants are as-

#define MAX 100

#define PI 3.14159625

#define CH ‘y’

#define NAME “Jhon”

In the program, these names will be replaced by the corresponding values. These symbolic constants improve the readability and modifiability of the program.