C Notes
All Topics (20)
- 1. Why should we learn a programming language?
- 2. Why are we starting our programming journey with C language?
- 3. History of C Language
- 4. Who created C language?
- 5. Where was C language developed?
- 6. When was C language developed?
- 7. Why is C still popular even after many years?
- 8. First C Program Example
- 9. What is a Header File & Why Do We Use Header Files in C?
- 10. What is stdio.h Header File in C?
- 11. What is conio.h in C and Why Do We Use It?
- 12. Why is # symbol used in C language? What is #include?
- 13. What is a Function in C?
- 14. What is Compilation of Code in C?
- 15. Data Types in C (Size, Format Specifier & Range)
- 16. Understanding ASCII in C Programming
- 17. Understanding Float, Double, and Long Double in C
- 18. What Are Operators in C?
- 19. Type Casting vs Type Conversion in C/C++
- 20. Type Promotion in C Programming
16. Understanding ASCII in C Programming
1️⃣ What is ASCII?
ASCII stands for American Standard Code for Information Interchange.
It is a coding standard used to represent characters as numbers in computers.
- Every character (letters, digits, symbols) has a unique ASCII value.
- For example:
A→ 65a→ 970→ 48
2️⃣ ASCII Ranges
There are 256 ASCII characters divided into two main ranges:
| Range | ASCII Values | Description |
|---|---|---|
| Standard ASCII | 0–127 | Characters found on a normal keyboard, like letters, digits, punctuation. |
| Extended ASCII | 128–255 | Extra symbols, special characters, and accented letters not on a standard keyboard. |
So, in total, there are 256 possible characters in the extended ASCII table.
3️⃣ Characters You Can See vs Special Ones
- Characters on a normal keyboard:
A, B, C, a, b, c, 0, 1, 2, !, @, # - Characters not usually visible: Special symbols like
☻, ♦, ║or accented letters.
These are important when you want to display symbols directly using ASCII values in your C program.
4️⃣ Writing a C Program to Display ASCII Characters
Here’s a corrected and clean version of your program. I’ll explain each line for beginners.
#include <conio.h> // Console Input/Output (for clrscr() and getch())
void main() {
unsigned char ch; // Declare a variable to store ASCII character
clrscr(); // Clear the console screen
ch = 251; // Assign an ASCII value (Extended ASCII)
// Display symbol and its ASCII value
printf("Symbol: %c\nASCII Value: %d", ch, ch);
getch(); // Wait for a key press before closing
}
5️⃣ Step-by-Step Explanation
unsigned char ch;charstores characters.unsignedallows it to store values from 0–255 (good for extended ASCII).
clrscr();- Clears the screen (optional, but makes output neat).
ch = 251;- Assigns an ASCII value.
- Value 251 in extended ASCII represents
√in some systems (depends on your console font).
printf("Symbol: %c\nASCII Value: %d", ch, ch);%cprints the character itself.%dprints its numeric ASCII value.
getch();- Pauses the program so you can see the output.
17. Understanding Float, Double, and Long Double in C
In C programming, floating-point numbers are used to store decimal numbers like 3.14 or -0.0002.
There are three main types:
| Data Type | Size (Bytes) | Format Specifier | Range |
|---|---|---|---|
float |
4 | %f |
~3.4×10^-38 to 3.4×10^38 |
double |
8 | %lf |
~1.7×10^-308 to 1.7×10^308 |
long double |
10 (varies) | %Lf |
~3.4×10^-4932 to 1.1×10^4932 |
1️⃣ How Floating-Point Numbers are Stored
- Float numbers are not stored exactly in memory.
- C does not handle floats directly in the CPU. Instead, it uses FPE (Floating Point Emulator).
FPE is developed based on IEEE standards by the Institute of Electrical and Electronics Engineers (IEEE).
The number is stored as:
- Mantissa (M) – the significant digits of the number
- Exponent (E) – the scale or magnitude
Because of this approximation and compression, floats can store huge numbers in just 4 bytes, but their values might not be exact.
2️⃣ Important Points About Float
- Approximation
-
Example:
float x = 45.8;
printf("%f", x);Output might be something like:
45.79999924- Not exactly
45.8due to internal representation.
- Not exactly
-
- Cannot use wrong format specifiers
-
Example:
float x = 65.0;
printf("%d", x); // ❌ Will print garbage✅ Correct usage:
printf("%f", x); // Will print 65.000000
-
- Integers and characters can be printed as
%d-
Example:
char ch = 'A';
printf("%d", ch); // 65 (ASCII value of 'A')
int n = 65;
printf("%c", n); // 'A' (ASCII character) - But the reverse does not work for floats.
-
- No
signedorunsignedmodifier for float- Floats can be negative, but you cannot declare
unsigned float.
- Floats can be negative, but you cannot declare
3️⃣ Why Floats are Approximate
- Computers store floats in binary, not decimal.
- Some decimal numbers cannot be represented exactly in binary.
- That’s why
0.1 + 0.2may not equal exactly 0.3 in C.
Always remember: Float ≈ Approximation, Not Exact
4️⃣ Best Practices
-
Use float for small precision numbers
float pi = 3.14; -
Use double for higher precision calculations
double pi = 3.141592653589793; - Use
%f,%lf,%Lfcorrectly%f→ float%lf→ double%Lf→ long double
5️⃣ Example Program
int main() {
float f = 45.8;
double d = 123456.7890123;
long double ld = 1.234567890123456789L;
printf("Float: %f\n", f);
printf("Double: %lf\n", d);
printf("Long Double: %Lf\n", ld);
return 0;
}
Output:
Double: 123456.789012
Long Double: 1.234568
Notice how
floatis approximate,doubleis more precise, andlong doublecan store even bigger numbers.
18. What Are Operators in C?
Operators are special symbols in C used to perform operations on variables or values.
- Example:
a = 25; // Here '=' is an operator that assigns the value 25 to variable a
Think of operators as action symbols. They tell the computer: “Do this calculation, comparison, or check for me.”
Types of Operators in C
C language has many types of operators, but the most common ones beginners use are:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
We’ll discuss each in detail.
1️⃣ Arithmetic Operators
Used for mathematical calculations:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
Example:
printf("a + b = %d\n", a + b); // 13
printf("a %% b = %d\n", a % b); // 1
2️⃣ Relational Operators
Used to compare values. Result is 1 (true) or 0 (false).
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater or equal | a >= b |
<= |
Less or equal | a <= b |
Example:
printf("%d\n", a < b); // 1 (true)
3️⃣ Logical Operators
Used to combine or modify conditions.
| Operator | Meaning | Example |
|---|---|---|
&& |
AND | a && b |
| ` | ` | |
! |
NOT | !a |
Example:
printf("%d\n", a && b); // 0 (false)
printf("%d\n", a || b); // 1 (true)
printf("%d\n", !a); // 0 (false)
Logical operators are used a lot in if statements, loops, and decision making.
19. Type Casting vs Type Conversion in C/C++
What is Type Casting?
Type casting is a technique in which a programmer temporarily changes the data type of a variable into another data type during program execution.
Important points:
- It is done manually by the programmer
- It is also called explicit conversion
- The change is temporary (only for that expression)
Example:
Here, 5 is explicitly converted to float, so result becomes 2.5.
What is Type Conversion?
Type conversion is the process in which the compiler automatically converts one data type into another during assignment or expression evaluation.
Important points:
- It is done automatically by the compiler
- It is also called implicit conversion
- It may happen during assignments or expressions
Example:
Here:
1.7is a float- But variable
ais int - So compiler automatically converts
1.7 → 1
Final value: a = 1
Difference Between Type Casting and Type Conversion
| Feature | Type Casting | Type Conversion |
|---|---|---|
| Done by | Programmer | Compiler |
| Type | Explicit | Implicit |
| Control | Manual | Automatic |
| Effect | Temporary (in expression) | Automatic during assignment |
| Syntax | (type) value |
No special syntax |
Examples of Type Conversion
Example 1:
a = 10/4;
Output: 2 (integer division)
Example 2:
a = 10/4;
Output: 2.0 (still integer division first, then converted)
Example 3:
a = 10/4.0;
Output: 2.5 (float division occurs)
Where Type Conversion is happening?
int a; a = 10/4; → Yes (implicit conversion happens after integer division)
float a; a = 10/4.0; → Yes (10 is converted to float automatically)
float a; a = (float)10/4; → This is type casting (explicit)
20. Type Promotion in C Programming
In C programming, when we perform arithmetic operations on variables of different data types, the compiler automatically converts the smaller data type into a larger data type before performing the operation.
This automatic upgrading of data type is called Type Promotion.
Definition:
Type promotion is the automatic conversion of a smaller data type into a larger data type by the compiler during arithmetic operations.
Why Type Promotion is needed?
- To avoid data loss
- To ensure accurate calculation
- To make operations compatible between different data types
Example of Type Promotion
float b = 5.5;
float result = a + b;
Here:
a (int)is automatically promoted tofloat- Then addition happens:
10.0 + 5.5 = 15.5
Order of Type Promotion (Hierarchy)
In C, data types are promoted in this order:
↓
short int
↓
int
↓
unsigned int
↓
long int
↓
unsigned long int
↓
float
↓
double
↓
long double
Key Rule:
The smaller data type is always promoted to the larger data type during expression evaluation.
Examples
Example 1:
int b = 20;
int result = a + b;
char → int (type promotion happens)
Example 2:
float b = 2.5;
float result = a + b;
int → float (type promotion happens)
Type Casting vs Type Promotion
| Feature | Type Casting | Type Promotion |
|---|---|---|
| Done by | Programmer | Compiler |
| Type | Explicit | Implicit |
| Control | Manual | Automatic |
| Purpose | Force conversion | Safe calculation |