Data Types


In C programming, variables or memory locations should be declared before it can be used. Similarly, a function also needs to be declared before use.

Data types simply refers to the type and size of data associated with variables and functions.


Type Of Data types

  1. Fundamental Data Types
    • Integer types
    • Floating type
    • Character type
  2. Derived Data Types




Integer data types

Integers are whole numbers that can have both positive and negative values, but no decimal values. Example: 0, -5, 10

In C programming, keyword int is used for declaring integer variable. For example:

int id;

Here, id is a variable of type integer.

You can declare multiple variable at once in C programming. For example:

int id, age;

The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1

Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648, program will not run correctly.





Floating types

Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. For example:

float accountBalance;
double bookPrice;

Here, both accountBalance and bookPrice are floating type variables.

In C, floating values can be represented in exponential form as well. For example:

float normalizationFactor = 22.442e2;

Difference between float and double

The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the the precision of double is 14 digits.





Character types

Keyword char is used for declaring character type variables. For example:

char test = 'h';

Here, test is a character variable. The value of test is 'h'.

The size of character variable is 1 byte.





Qualifiers alters the meaning of base data types to yield a new data type.


Size qualifiers

Size qualifiers alters the size of a basic type. There are two size qualifiers, long and short. For example:

long double i;

The size of float is 8 bytes. However, when long keyword is used, that variable becomes 10 bytes.


Sign qualifiers

Integers and floating point variables can hold both negative and positive values. However, if a variable needs to hold positive value only, unsigned data types are used. For example:


// unsigned variables cannot hold negative value 
unsigned int positiveInteger;

There is another qualifier signed which can hold both negative and positive only. However, it is not necessary to define variable signed since a variable is signed by default.

An integer variable of 4 bytes can hold data from -231 to 231-1. However, if the variable is defined as unsigned, it can hold data from 0 to 232-1.

It is important to note that, sign qualifiers can be applied to int and char types only.


Constant qualifiers

An identifier can be declared as a constant. To do so const keyword is used.

const int cost = 20;

The value of cost cannot be changed in the program.


Volatile qualifiers

A variable should be declared volatile whenever its value can be changed by some external sources outside the program. Keyword volatile is used for creating volatile variables.



Variables and Constants



Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:

int playerScore = 95;

Here, playerScore is a variable of integer type. The variable is holding integer 95 in above program.

The value of an variable can be changed, hence the name 'variable'.



Rules for writing variable name in C :

  1. A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with a system name and may cause errot.
  3. There is no rule on how long a variable can be. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.

In C programming, you have to declare a variable before you can use. It is a common practice in C programming to declare all the variables at the beginning of the program.





Constants/Literals

A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy" etc.

As mentioned, an identifier also can be defined as a constant.

const double PI = 3.14;

Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.



Integer constants

A integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:/p>

  • Decimal constant(base 10)
  • Octal constant(base 8)
  • Hexadecimal constant(base 16)

For example:
Decimal constants: 0, -9, 22 etc Octal constants: 021, 077, 033 etc Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.


Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:

-2.0
0.0000234
-0.22E-5
Note :

E-5 = 10-5



Character constants

A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'



Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.

For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.

Escape Sequences
Escape SequencesCharacter
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null character


String constants

String constants are the constants which are enclosed in a pair of double-quote marks. For example:

"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline

Enumeration constants

Keyword enum is used to define enumeration types. For example:

enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.


Keywords and Identifiers


Character set

Character set is a set of alphabets, letters and some special characters that are valid in C language.


Alphabets

Uppercase: A B C ................................... X Y Z

Lowercase: a b c ...................................... x y z


Digits

0 1 2 3 4 5 6 7 8 9


Special Characters


Special Characters in C Programming
,<>._
();$:
%[]#?
'&{}"
^!*/|
-\~+ 

White space Characters :

blank space, new line, horizontal tab, carriage return and form feed





Keywords

Keywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier. For example:

int money;

Here, int is a keyword that indicates 'money' is a variable of type integer.



As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.


Keywords in C Language
autodoubleintstruct
breakelselongswitch
caseenumregister typedef
charexternreturnunion
continueforsignedvoid
doifstatic while
defaultgotosizeofvolatile
constfloatshortunsigned

Along with these keywords, C supports other numerous keywords depending upon the compiler.






Identifiers

Identifiers are the names you can give to entities such as variables, functions, structures etc

Identifier names must be unique. They are created to give unique name to a C entity to identify it during the execution of a program. For example:


int money;
double accountBalance;

Here, money and accountBalance are identifiers.

Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.


Rules for writing an identifier

  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of an identifier should be either a letter or an underscore. However, it is discouraged to start an identifier name with an underscore. It is because identifier that starts with an underscore can conflict with system names.
    In such cases, compiler will complain about it. Some system names that start with underscore are _fileno, _iob, _wfopen etc.
  3. There is no rule on the length of an identifier. However, the first 31 characters of identifiers are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.

Good Programming Practice

You can choose any name for an identifier. However, if the programmer choose meaningful name for an identifier, it will be easy to understand and work on.