Table of contents |
|
Data Types in C |
|
Constants in C |
|
Good Practice |
|
Example Program Using Constants & Data Types |
|
Data types specify the type of data a variable can store. C supports several built-in data types categorized as basic, derived, and user-defined types.
a. Basic Data Types
Signed vs Unsigned:
Signed types store both positive and negative values.
Unsigned types store only non-negative values, doubling the positive range.
Floating-Point Representation:
IEEE-754 Standard: Floating-point numbers (float, double, long double) are stored using sign, exponent, and mantissa.
Derived from basic types:
Arrays – Collection of similar data types (int arr[5]).
Pointers – Stores the memory address of another variable (int *ptr).
Structures (struct) – Groups different types into one entity.
Unions (union) – Similar to struct, but shares memory.
Modifiers change the storage size and range of data types:
Signed (signed) – Default for int, allows negative values.
Unsigned (unsigned) – Only non-negative values.
Short (short) – Uses less memory (short int).
Long (long) – Expands storage (long int, long double).
Qualifiers modify variable behavior:
const – Declares a constant (const int x = 10;).
volatile – Prevents compiler optimizations (volatile int flag;).
restrict – Used for pointer optimization.
Constants are fixed values that cannot be modified during execution.
Integer Constants: Whole numbers (10, -5, 0xA in hex, 010 in octal).
Floating-point Constants: Decimal numbers (3.14, 2.5e3).
Character Constants: Single characters ('A', '1').
String Constants: Sequence of characters ("Hello").
Symbolic Constants: Defined using #define MAX 100.
When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only):
Example
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
You should always declare the variable as constant when you have values that are unlikely to change:
Example
const int minutesPerHour = 60;
const float PI = 3.14;
When you declare a constant variable, it must be assigned with a value:
Example
Like this:
const int minutesPerHour = 60;
This however, will not work:
const int minutesPerHour;
minutesPerHour = 60; // error
Another thing about constant variables, is that it is considered good practice to declare them with uppercase. It is not required, but useful for code readability and common for C programmers:
Example
const int BIRTHYEAR = 1980;
#include <stdio.h>
#define PI 3.14159 // Macro constant
int main() {
const int radius = 5; // Constant variable
float area = PI * radius * radius;
printf("Area of Circle: %.2f\n", area);
return 0;
}
#define PI 3.14159 → Preprocessor macro (no memory usage).
const int radius = 5; → Read-only variable stored in memory.
Computes circle area using the formula πr².
10 videos|13 docs|15 tests
|
1. What are constants in programming and why are they important? | ![]() |
2. How do data types differ from constants in programming? | ![]() |
3. Can you give examples of common data types used in programming? | ![]() |
4. What is the difference between a constant and a variable? | ![]() |
5. How can constants improve code readability and maintainability? | ![]() |