Data types of Variables
Data types specify the type of data getting stored in the variable. They restrict other types of data to be stored in the variable. In this tutorial we will discuss about some basic data types and their implementation.
For Numbers
The basic data types which we commonly used to store numbers are given below.
- int - stores integer value.
- float - it stores decimal values.
- double - it also stores decimal values but its precision is double than float.
Char Data Type
Char data type is used to store any single character in a variable.
Boolean Data Type
Boolean data type stores only true / False. For using booleans in C, you have to import
Booleans can store:
- true / false
- 1 / 0
- Yes / No
String Data Type
String data type is use to store sequence of characters/text/anything inside double quotation marks.
We will learn about string data type in detail in later tutorial.
Format Specifiers
Format specifiers in C are used with functions like printf and scanf to specify the expected format of the data to be printed or read.
| Data Type | Format specifier |
|---|---|
| int | %d |
| float | %f |
| double | %lf |
| char | %c |
| string | %s |
Size of Data Types
Size of variable depends upon the data type of variable.
| Data Type | Size |
|---|---|
| char | 1 byte |
| bool | 1 byte |
| int | 2 or 4 bytes |
| float | 4 bytes |
| double | 8 bytes |
Note: It is not important to remember the size of all data types.
sizeof() operator
We can find the size of any data type or variable using sizeof() operator. Let's see an example.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP