# Fundamental Data Types in C

## Chapter 4: Programming Basics

#### Unit 1

Chapter 1: Computer Basic*
*CS101 Computer Basic is a small separate course prerequisite for KCS101/KCS201 Programming for Problem Solving – Using C

Chapter 2: Terminology

Chapter 3: Idea of Algorithm

Chapter 4: Programming Basics

#### Unit 2

Chapter 5: Arithmetic Expressions and Precedence

Chapter 6: Conditional Branching

#### Unit 3

Chapter 7: Iteration and Loops

Chapter 8: Functions

#### Unit 4

Chapter 9: Arrays, Structure, Union, and Enumeration

Chapter 10: Basic Algorithms and their Complexity Coexistence

#### Unit 5

Chapter 11: Pointers

Chapter 12: File Handling

Chapter 13: Macros &amp; Command-Line Arguments

#### Appendix

KCS151/KCS251 PPS Lab Exercise

PPS Previous Year Question Papers

## Data Types in C

Each variable in C has a data type. It defines the memory size of the variable along with the type of values, the variable can hold. We can Classify C data types as below:

## Primary Data Type

Primary data types are also called basic or primitive data types. These data types are further classified into four categories:

### Integer Data Type

A variable of integer data type can hold an integer number as a value. Integers are the whole numbers that do not have any fractional or exponential part. These numbers may be positive or negative. Memory size and range of integer data types are machine-dependent. For example size of integer data type for a 16-bit machine is 2 bytes whereas, for a 32-bit machine size, it is 4 bytes. We use the keyword int to represent integer data type. We can declare integer variables as:

int num; or int num1, num2; or int num1 = 7, num2 = 5;

### Floating Point Data Type

Variables of the floating-point data type can hold the real numbers. These numbers also may be positive or negative. Unlike integers, these numbers have a decimal point and expressed either in decimal or exponential form. The exponent form is used, when the data value given to the variable is very small or very big. For example, the value 0.00000000000034 can be written as 3.4 x 10-13, and in exponent form, it is represented as 3.4E-13. Because of the shifting of a decimal point either to the left or to the right during the manipulation, these are also called floating points. The floating-point has two different flavors:
• float
• double
Keyword float is used to represent a single-precision floating-point whereas double is used to represent a double-precision floating-point. Variables of float data type can store the value accurately up to six decimal places but the variable of double data type can store up to 16 significant digits after the decimal point. We can declare floating-point variables as:

float g = 9.8; or double dis = 2.5E-52;

### Character Data Type

A variable of character data type can hold a single character. It actually stores the ASCII (American Standard Code for Information Interchange) value of character which is an integer. Moreover, it can be used as an integer in arithmetic expressions. Keyword char is used to represent character data type. We can declare character variables as:

char ch = ‘A’; or char in = 65;

### Void Data Type

The void is an empty data type that represents nothing or no value. It is used as a function parameter and as a function return type. It also used to define void pointers. We will study it in detail later.

### Data Type Qualifiers or Modifiers

Qualifiers are used to modify the properties of a primary data type to transform it into a new data type. Some keywords such as long, short, unsigned, etc are used for this purpose. These keywords prefixed with data types for the alteration. There are four types of qualifiers:
1. Size Qualifier
2. Sign Qualifier
3. Constant Qualifier
4. Volatile Qualifier

#### Size Qualifiers

Size qualifiers modify the size of primary data types. Two keywords are used as size qualifiers:
• long
• short
Keyword long increases the size of data type whereas short decreases. Example:

long double dis;

In this example size of the double data type variable is 8 bytes, but when we use long prefix variable size increased to 10 bytes.

#### Sign Qualifiers

Sign qualifiers decide whether a variable can store only the positive values or both positive and negative values. Following keywords are used as sign qualifiers:
• signed
• unsigned
The signed keyword is used with those variables that can store both positive and negative types of values whereas unsigned variables can store only positive values. Example:

signed int balance;
unsigned age;

In the above examples, the balance could be positive or negative, but age could not be negative.

Note: Variables without any sign qualifier are treated as signed variables.

#### Constant Qualifiers

The constant qualifier can transform any variable into a constant. Keyword const is used for it. The value of such type of variable is initialized at the time of declaration and it will not be changed during the execution of the program. Example:

const float pi = 3.1415;

#### Volatile Qualifiers

Volatile qualifiers are used to transform a variable such that it’s value can be modified anytime by some external sources outside the program. Keyword volatile is used to declare volatile variables. Example:

volatile int cost;

Following is a list of primary data types with their qualifiers, keywords, size, range, and format specifiers:

Note: Given sizes and ranges are for a 16-bit machine.

## Derived Data Type

Derived data types are derived from the primary data types. These are the following:
• Pointer
• Array
• Function

We will discuss Pointer, Array, and Function in detail later.

## User-Defined Data Type

In C, programmers can define their own data types as required. User-defined data types are the following:
• Structure
• Union
• Enumeration or Enum

We will discuss Structure, Union, and Enumeration in detail later.