C Programming ยท Unit 2
Data Types and Constants
Exam-focused notes for Data Types and Constants (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Data Types and Constants in short
This chapter explains the kinds of data a C program can store and how to write fixed values. Choosing the right type saves memory and avoids wrong results.
What this unit covers
- Basic data types and their ranges
- Constants and constant declaration
- Type conversion and casting
- Sizeof operator
- Conversion specifiers
Conversion specifiers
2081 ยท 5 marks
What are conversion specifiers? Explain the compilation of C program in brief. [2+3]
2080 ยท 5 marks
What are conversion specifiers? Explain the basic structure of a C program. [5]
Basic data types and their ranges
2081 ยท 5 marks
What is constant? Explain basic data types in C programming along with their range. [1+4]
Show solved answerHide answer
Constants and Basic Data Types in C Programming
What is a Constant? [1 Mark]
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.
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).
2079 ยท 5 marks
How keyword is different from Variables? Discuss Basic data types with its ranges. [5]
2078 ยท 10 marks
What different data types available in C along with their respective range?Write a program to check whether a given integer is palindrome or not.[5+5]
Show solved answerHide answer
Data Types in C with Their Ranges
(a) Data Types in C
A data type defines the type of data a variable can hold, the amount of memory allocated, and the operations that can be performed on it.
Primary (Fundamental) Data Types in C
| Data Type | Size | Range |
|---|---|---|
char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
int | 2 or 4 bytes | -32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes) |
unsigned int | 2 or 4 bytes | 0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (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 long int | 4 bytes | 0 to 4,294,967,295 |
float | 4 bytes | 3.4E-38 to 3.4E+38 (6 decimal digits precision) |
double | 8 bytes | 1.7E-308 to 1.7E+308 (15 decimal digits precision) |
long double | 10 bytes | 3.4E-4932 to 1.1E+4932 |
void | 0 bytes | No value |
Classification of Data Types
Data Types in C
โโโ Primary (Basic)
โ โโโ int
โ โโโ char
โ โโโ float
โ โโโ double
โ โโโ void
โโโ Derived
โ โโโ Array
โ โโโ Pointer
โ โโโ Function
โโโ User Defined
โโโ struct
โโโ union
โโโ enum
(b) Program to Check Palindrome Integer
Concept
A number is called a palindrome if it reads the same forwards and backwards.
- Example: 121, 1331, 12321 are palindromes
- Example: 123, 456 are NOT palindromes
Algorithm
- Read the integer
n - Store original number in
original - Reverse the digits of
nto getreversed - If
original == reversed, it is a palindrome - Otherwise, it is not a palindrome
C Program
#include <stdio.h>
int main() {
int n, original, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
/* Handle negative numbers - negative numbers are not palindromes */
if (n < 0) {
printf("%d is NOT a palindrome.\n", n);
return 0;
}
/* Reverse the digits of the number */
while (n != 0) {
remainder = n % 10; /* Extract last digit */
reversed = reversed * 10 + remainder; /* Build reversed number */
n = n / 10; /* Remove last digit */
}
/* Check if original and reversed are equal */
if (original == reversed) {
printf("%d is a PALINDROME.\n", original);
} else {
printf("%d is NOT a PALINDROME.\n", original);
}
return 0;
}
Dry Run / Trace (for n = 121)
| Iteration | n | remainder (n%10) | reversed |
|---|---|---|---|
| Start | 121 | - | 0 |
| 1st | 12 | 1 | 0*10+1 = 1 |
| 2nd | 1 | 2 | 1*10+2 = 12 |
| 3rd | 0 | 1 | 12*10+1 = 121 |
original = 121,reversed = 121- Since
121 == 121, it is a PALINDROME
Sample Output
Enter an integer: 121
121 is a PALINDROME.
Enter an integer: 123
123 is NOT a PALINDROME.
Type conversion and casting
2081.2 ยท 5 marks
What is the task of sizeof and comma operator? Discuss about type conversion. [5]
Make Unit 2 stick
Practice BIT102 with flashcards & quizzes