C Notes

C
DSA
Software Engineering
Software Architecture
Operating System
Big Data
Data Mining and Warehousing
TOC
Ada
CPP
DBMS

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 → 65
    • a → 97
    • 0 → 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 <stdio.h> // Standard Input/Output library
#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

  1. unsigned char ch;
    • char stores characters.
    • unsigned allows it to store values from 0–255 (good for extended ASCII).
  2. clrscr();
    • Clears the screen (optional, but makes output neat).
  3. ch = 251;
    • Assigns an ASCII value.
    • Value 251 in extended ASCII represents in some systems (depends on your console font).
  4. printf("Symbol: %c\nASCII Value: %d", ch, ch);
    • %c prints the character itself.
    • %d prints its numeric ASCII value.
  5. 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:

  1. Mantissa (M) – the significant digits of the number
  2. 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

  1. Approximation
    • Example:

       
      float x = 45.8;
      printf("%f", x);

      Output might be something like: 45.79999924

      • Not exactly 45.8 due to internal representation.
  2. 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
       
  3. 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.
  4. No signed or unsigned modifier for float
    • Floats can be negative, but you cannot declare unsigned float.

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.2 may not equal exactly 0.3 in C.

Always remember: Float ≈ Approximation, Not Exact

4️⃣ Best Practices

  1. Use float for small precision numbers

     
    float pi = 3.14;
  2. Use double for higher precision calculations

     
    double pi = 3.141592653589793;
     
  3. Use %f, %lf, %Lf correctly
    • %f → float
    • %lf → double
    • %Lf → long double

5️⃣ Example Program

#include <stdio.h>
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:

Float: 45.799999
Double: 123456.789012
Long Double: 1.234568
 

Notice how float is approximate, double is more precise, and long double can 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:
int a;
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:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. 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:

int a = 10, b = 3;
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:

int a = 5, b = 10;
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:

int a = 1, b = 0;
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:

float a = (float)5/2;

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:

int a = 1.7;

 Here:

  • 1.7 is a float
  • But variable a is 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:

int a;
a = 10/4;
 

 Output: 2 (integer division)

Example 2:

float a;
a = 10/4;
 

 Output: 2.0 (still integer division first, then converted)

Example 3:

float a;
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

int a = 10;
float b = 5.5;

float result = a + b;

 Here:

  • a (int) is automatically promoted to float
  • Then addition happens: 10.0 + 5.5 = 15.5

 Order of Type Promotion (Hierarchy)

In C, data types are promoted in this order:

 
char / signed char / unsigned char
  ↓
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:

char a = 10;
int b = 20;

int result = a + b;

char → int (type promotion happens)

Example 2:

int a = 5;
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

 

Page 4 of 4