24 September 2013

Simple C – Chapter 3, Data Types



In the previous chapter, we learned about Variables, Constant and General Structure of the C program. We have also learned how to write a simple C program and execute it using the C compiler. Today in this chapter, I will discuss about Data Types in the C program. To be able to write a C program and assign a value to a variable, you have to know types of data that the C program handles. Let see what Data Types are in the C language.


Data Types

In C programming, Data Types define the nature of variables and how much space they occupy in the system memory. In other words, variable types depend on their data types and they can be in numeric or in character form. Data types vary in different C compilers but the five major categories are:

1. Primary or Standard data type

  • Integer
  • Float
  • Double
  • Character

2. Secondary or structured data type

  • Structure
  • Union
  • Array or String

3. User defined or Enumerated or Typedef data type

4. Void or Empty data type

5. Pointer data type 
 

Standard or Fundamental data type

This type of data only use for a single value in the C program and divided into four categories: Integer, Float, Double and Character.
 

Integer 

Integers don’t have any decimal points and they can be signed or unsigned. We have three types of integer data types in the C languages including Short int, int, and Long int. The range of signed short int is between -128 to +127 and having 8 bit size (integer values occupy 8 to 32 bits of the memory), for unsigned short int it is between 0 to 255 again with 8 bit size. The signed long int has 32 bit size and its range is from -2,147,483,648 to +2,147,483,647.


Float or Real

These types of data (Real numbers) can be signed or unsigned and have at least one digit with a decimal point. Their storage space is 32 bits with a range from 3.4E-38 to 3.4E+38.


Double

Double data types also called large real numbers as they have large floating data. They have 64 bits size and the range from 1.7E-308 to 1.7E+308.


Character

In the C language a combination of characters called String and like the integer data types they are signed or unsigned. They occupy 8 bits or 1 byte of the system memory and have the range between 0 to 255 for unsigned character and -128 to 127 for signed characters. 

Type
Size
Range
Signed Char
1 byte
-128 to 127
Unsigned Char
1 byte
0 to 255
Signed int
2 bytes
-32768 to 32767
Unsigned int
2 bytes
0 to 65535
Signed Short int
1 byte
-128 to 127
Unsigned Short int
1 byte
0 to 255
Signed Long int
4 bytes
-2,147,483,648 to 2,147,483,647
Unsigned Long int
4 bytes
0 to 4,294,967,295
Float
4 bytes
3.4E-38 to 3.4E+38
Double
8 bytes
1.7E-308 to 3.4E+308
Long Double
10 bytes
3.4E-4932 to 1.1E+4932

Structured data type

In the C programming, Structured data types or divided data types are used for representing multiple values although they can be used for representing the single value as well. These types of data are categorized into Array and Strings, Structures and Unions. They will discuss later in the next chapters.


User Defined data types or Enumerated data types

Using these types of data, a user can create or define a variable or identifier to represent some values. In the C programming, these sorts of data type are in two forms: Enumerated and typedef data type. The Syntax for defining an Enumerated data type is:



OR 



Where v1, v2, v3,……, vn are constant values. For example:

enum days {Monday, Tuesday, Wednesday,…., Sunday}; 

enum year {2000, 2001, 2003,……, 2014}y;

The next type of user defined data type is called typedef which is used to signify presented data type. In other words they are used to replace the old type of data in the C language.




In the syntax above, data-type can be int, float, double and char. The identifier is the new name that we assign for the data type. For example:

typedef int tax;

typedef flaot salary;

You do not need to concern about the Enumerated data type right now as in the next few chapters we are going to use the Standard data types only and after that Array and Pointer.


Void data type

The main role of the Void or Empty data type is in the user defined functions when a function returns no value or does not have any arguments.


Pointer data type

To assign a memory address we can use the Pointer data type to point the compiler to the address memory of the variable constant value instead of its actual value.


Variables declaration

Now that we know about all types of data that the C program can handle, let talk about the Variables declaration. To declare a variable, first we assign its data types at the beginning of the statement and then the variable name or names. The syntax is:




The data type can be either integer type or float or character type. For examples:

int a, b, c; 

float salary; 

char name;

If we declare the variable in the main() function, then it is called local variable, and if we declare it before the main() function then it is a global variable. Let write another simple program that calculates interest to see it in the action:




In the program above, we defined our variable in the first and second lines in the main() function (local variable declaration) and then asked the user to enter the values for these variables. The printf function is in charge of printing the output on the screen and the scanf function accepts input from the user. We will discuss the input/output function briefly in the next chapter when we started the actual programming. Go ahead and execute the program to see the result.