Thursday, January 3, 2019

Data Types in C

                                      Data Types in C

A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in his program. These are data types. There are many data types in C language. A C programmer has to use appropriate data type as per his requirement in the program he is going to do.


Primary data type 
All C Compilers accept the following fundamental data types


 Integer Type
Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. The long and unsigned integers are used to declare a longer range of values.

Floating Point Types 
Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of memory space.

 Void Type
Using void data type, we can specify the type of a function. It is a good practice to avoid functions that does not return any values to the calling function.

 Character Type
A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

 Size and Range of Data Types on 16 bit machine;

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; 


User defined type declaration 
In C language a user can define an identifier that represents an existing data type. The user defined datatype identifier can later be used to declare variables. The general syntax is

 typedef type identifier;

here type represents existing data type and „identifier‟ refers to the „row‟ name given to the data type.

Example:
typedef int salary;

typedef float average;

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

Units dept1, dept2;
 Average section1, section2; 

Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 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 …. Value n};

 The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed 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 1:
enum day {Monday, Tuesday, …. Sunday};
 enum day week_st, week end;
 week_st = Monday;
 week_end = Friday;
 if (wk_st == Tuesday) week_en = Saturday; 


 Example 2:

#include int main() 
{
 enum {RED=5, YELLOW, GREEN=4, BLUE}; 
 printf("RED = %d\n", RED); 
 printf("YELLOW = %d\n", YELLOW); 
 printf("GREEN = %d\n", GREEN);
 printf("BLUE = %d\n", BLUE); return 0; 


This will produce following results
RED = 5 
YELLOW = 6
GREEN = 4 
BLUE = 5



No comments:
Write comments

Popular Posts

Translate

TOPICS