BIT102 ยท 2081 ยท 5 marks
What is constant? Explain basic data types in C programming along with their range. [1+4]
What is constant? Explain basic data types in C programming along with their range. [1+4]
C ProgrammingChapter 2: Data Types and ConstantsBasic data types and their ranges
Quick answer
A constant is a fixed value that does not change during the execution of a program. Once defined, its value remains the same throughout the program.
Constants and Basic Data Types in C Programming
What is a Constant? [1 Mark]
Example:
const int x = 10; // x is always 10
#define PI 3.14159 // PI is always 3.14159
Basic Data Types in C Programming [4 Marks]
C provides several fundamental (basic) data types to store different kinds of data. These are:
1. int (Integer Type)
- Used to store whole numbers (positive, negative, or zero).
- No decimal point.
| Type | Size | Range |
|---|---|---|
int | 2 or 4 bytes | -32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes) |
short int | 2 bytes | -32,768 to 32,767 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes) |
int age = 20;
2. float (Floating Point Type)
- Used to store real numbers (numbers with decimal points).
- Single precision.
| Type | Size | Range |
|---|---|---|
float | 4 bytes | 3.4 x 10^-38 to 3.4 x 10^+38 (6 decimal digits precision) |
float price = 99.99;
3. double (Double Precision Float)
- Used to store large real numbers with more precision than
float.
| Type | Size | Range |
|---|---|---|
double | 8 bytes | 1.7 x 10^-308 to 1.7 x 10^+308 (15 decimal digits precision) |
long double | 10 bytes | 3.4 x 10^-4932 to 1.1 x 10^+4932 |
double pi = 3.14159265358979;
4. char (Character Type)
- Used to store a single character (letter, digit, or symbol).
- Stored as its ASCII value internally.
| Type | Size | Range |
|---|---|---|
char | 1 byte | -128 to 127 (signed) |
unsigned char | 1 byte | 0 to 255 |
char grade = 'A';
Summary Table
| Data Type | Size | Range |
|---|---|---|
int | 2/4 bytes | -32,768 to 32,767 / -2,147,483,648 to 2,147,483,647 |
float | 4 bytes | 3.4e-38 to 3.4e+38 |
double | 8 bytes | 1.7e-308 to 1.7e+308 |
char | 1 byte | -128 to 127 |
Note: The exact size may vary depending on the compiler and system architecture (32-bit or 64-bit).
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.