Topics
- 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?
Topic Ends from here
Programming languages are like a translator (bridge) between humans and computers.
-
Computers understand only machine language (0s and 1s).
-
Humans understand languages like Hindi, English, etc.
-
It is very difficult for humans to write instructions in only 0s and 1s.
-
Computers cannot understand human languages directly.
So, we need a programming language that:
-
Converts human instructions into machine language.
-
Helps the computer understand what we want it to do.
This translation is done by:
-
Compiler
-
Interpreter
So, programming languages help humans and computers communicate with each other.
We start with C language because:
-
It is a basic and powerful language.
-
It helps us understand:
-
Memory
-
How computers work internally
-
Logic building
-
-
Many modern languages are based on C.
-
It is widely used in:
-
Embedded Systems
-
System Programming
-
BCPL (Basic Combined Programming Language) was developed by
Martin Richards in 1967. -
From BCPL, a new language called B language was developed by
Ken Thompson. -
Later, C language was developed from B language by
Dennis Ritchie in 1972 at
Bell Labs.
So
C language is a modified and improved version of B language.
C language was created by:
-
Dennis Ritchie → Known as the Father of C language
-
Brian Kernighan → Helped in documenting and popularizing C
C language was developed at:
-
Bell Labs (USA)
Bell Labs was owned by AT&T
C language was developed in 1972.
C is still popular because:
a. Versatility (All-rounder Language)
C can be used to develop:
-
System Software (like Operating Systems)
-
Application Software
b. Fast and Efficient
-
C programs run very fast.
-
It gives direct access to memory.
c. Foundation Language
-
Many modern languages like:
-
Java
-
Python
are influenced by C.
d. Used in Important Systems
-
Embedded Systems
-
Device Drivers
-
Compilers
That’s why C is still called an “All-rounder” and “Mother language of many languages.”
#include <stdio.h> // Standard Input Output header file
#include <conio.h> // Console Input Output header file (for getch(), clrscr(), etc.)
void main() {
// This is the main function from where execution starts
printf("Hello User"); // Prints message on the screen
getch(); // Waits for user to press a key
}
A header file in C programming is a file that contains predefined functions, macros, constants, and type definitions that you can use in your program. Header files usually have the extension .h, for example:
stdio.h– for input/output functions likeprintf()andscanf().math.h– for mathematical functions likesqrt(),pow(),sin(), andcos().conio.h– for console input/output functions likegetch()andclrscr().
Think of a header file as a toolbox for C programmers. Instead of writing all the code from scratch, you can include these “tools” in your program to save time and effort.
Why Do We Use Header Files in C?
Including header files in C programming is essential for several reasons:
- Access Predefined Functions
Header files contain functions that are already written and tested. For example, without includingstdio.h, you cannot useprintf()orscanf()in your program. - Simplify Programming
Header files allow you to write code faster because you don’t need to reinvent the wheel. All standard functions are available at your fingertips. - Avoid Errors
When you include a header file, the compiler knows the function prototypes (what arguments they take and what value they return). This prevents compilation errors. - Code Reusability
Header files promote modular programming. You can write reusable functions in separate header files and include them in multiple programs. - Better Organization
Large programs can be split into multiple header files to keep the code clean, organized, and easier to maintain.
When Should You Include a Header File?
You should include a header file whenever your program uses functions, constants, or types that are defined outside your program. Some common examples include:
| Header File | Functionality Example |
|---|---|
stdio.h |
printf(), scanf() |
math.h |
sqrt(), pow(), sin(), cos() |
conio.h |
getch(), clrscr() |
string.h |
strcpy(), strlen(), strcmp() |
Syntax to include a header file:
Example: Simple C Program Using a Header File
int main() {
printf("Hello, User! Welcome to C Programming.\n");
return 0;
}
Here:
#include <stdio.h>allows the program to useprintf().- Without including this header, the compiler would not recognize
printf()and give an error.
In C programming, the stdio.h header file is one of the most important and widely used header files. The term stdio stands for Standard Input Output, which basically means it allows your program to take input from the user (keyboard) and display output on the screen (monitor).
Key Functions of stdio.h
The stdio.h header file provides functions that handle essential input and output operations in C. The most commonly used functions include:
-
printf()– This function is used for output, i.e., displaying text, variables, or results on the screen.printf("Hello, World!\n"); -
scanf()– This function is used for input, i.e., accepting data from the user via the keyboard.int age;
scanf("%d", &age);
Why is stdio.h Important?
- Essential for Almost Every Program:
Almost every C program involves input and output, makingstdio.hnecessary for nearly all projects. - Simplifies Programming:
Withoutstdio.h, programmers would have to write complex code to handle keyboard input and screen output manually. - Standardized Functions:
Functions likeprintf()andscanf()are predefined, tested, and optimized, making coding faster and error-free.
Example: Using stdio.h
int main() {
int age;
printf("Enter your age: "); // Output
scanf("%d", &age); // Input
printf("You are %d years old.\n", age); // Output
return 0;
}
Explanation:
#include <stdio.h>→ Includes standard input/output functions.printf()→ Displays a message on the screen.scanf()→ Reads input from the user.
In C programming, conio.h is a header file that stands for CONSOLE Input Output.
Here’s what that means:
- Console – The window where your program displays output and interacts with the user. This could be the command prompt or terminal on your operating system.
- Input/Output –
conio.hprovides functions to take input from the keyboard and control the console output, making your program more interactive and visually appealing.
Note: The console window itself is not created by
conio.h; it is generated by the operating system (like DOS or Windows).conio.hjust gives you the tools to control it.
Important Functions of conio.h
-
clrscr()– Clears the console window and removes all previous outputs.clrscr(); // Clear the screen -
textcolor(color)– Changes the font color of text in the console window.textcolor(RED); // Set text color to red -
getch()– Pauses the console window until a key is pressed.getch(); // Wait for user input -
gotoxy(x, y)– Moves the cursor to a specific position on the console.gotoxy(10, 5); // Move cursor to column 10, row 5
Why Use conio.h?
- Better Control of Console Output: Change text colors, move the cursor, or clear the screen for a cleaner interface.
- Pause Program Execution: Useful for letting the user see results before the console closes.
- Interactive Programming: Makes your program visually appealing and easy to follow, especially for beginner projects or small games.
Example Program Using conio.h
#include <stdio.h>
int main() {
clrscr(); // Clear the screen
textcolor(RED); // Change text color to red
printf("Hello User!\n"); // Display output
gotoxy(10, 5); // Move cursor to a specific position
printf("Welcome to C Programming\n");
getch(); // Wait for key press
return 0;
}
In the C programming language, the symbol # is called a pound sign (also known as hash).
Any line in a C program that begins with # is called a Preprocessor Directive.
What are Preprocessor Directives?
Preprocessor directives are special instructions that are processed before compilation.
a. They are handled by a special program called the C Preprocessor, not by the compiler.
b. The preprocessor runs before the compiler and prepares the code for compilation.
Role of the C Preprocessor
The preprocessor performs tasks like:
- Including files
- Defining macros
- Conditional compilation
- Removing comments
It only processes lines that start with #.
What is #include?
#include is a file inclusion directive.
It tells the preprocessor to include the content of another file into the program.
Example:
a. This line includes the standard input-output library
b. It allows functions like printf() and scanf() to work
Types of #include
- System File
Searches in standard library folders
- User-defined File
Searches in the current directory
Popular Preprocessor Directives in C
1. File Inclusion
Used to include header files
2. Macro Definition
Defines a macro (constant or expression)
3. Macro Undefinition
Removes a previously defined macro
4. Conditional Compilation
These directives control compilation based on conditions:
# else
# elif
# ifdef
# ifndef
# endif
Useful for:
- Debugging
- Platform-specific code
- Feature toggling
Why do we use Preprocessor Directives?
a. To make code reusable
b. To simplify complex code
c. To improve readability
d. To control compilation
A function in C language is a named block of code that performs a specific task.
It consists of:
- A function name
- A pair of parentheses
() - A set of instructions enclosed in curly braces
{}
Example:
printf("Hello!");
}
a. Functions help in breaking a program into smaller, manageable parts
b. They improve code readability and reusability
What is the main() Function in C?
Every C program must have a main() function.
a. It is the starting point of program execution
b. The Operating System (OS) always begins execution from main()
Importance of main() Function
- It acts like the entry point of the program
- Execution of the program always starts from
main() - Without
main(), the program cannot run
Think of main() as the engine of a car
Just like a car starts with an engine, a C program starts with main().
Structure of main() Function
// code statements
return 0;
}
int → return typereturn 0; → indicates successful execution
Can We Compile a C Program Without main()?
Yes, we can compile it, but…
a. Compilation is done by the compiler
b. The compiler checks syntax from top to bottom
c. It does not require main() to compile
Can We Run a Program Without main()?
No, we cannot run it β
a. Execution is handled by the Operating System (OS)
b. The OS always starts execution from the main() function
c. If main() is missing → program will not run
Key Difference (Important )
| Process | Needs main()? |
|---|---|
| Compilation | β No |
| Execution | β Yes |
Compilation is the process of converting source code (written in C language) into machine code (binary code).
a) Source code → Human-readable
b) Machine code → Computer-readable (0s and 1s)
Why is Compilation Important?
Computers (CPU) do not understand C language directly.
a) They only understand binary language (0 and 1)
b) So, we must convert our program into machine code
This conversion process is called Compilation
What is a Compiler?
A compiler is a software that performs compilation.
a> It converts the entire program at once
b> It checks for syntax errors
c> It generates machine code (object/executable file)
Compilation Process (Step by Step)
↓
Compiler
↓
Machine Code (Binary)
↓
Execution (by OS)
Example
Source Code:
int main() {
printf("Hello World");
return 0;
}
After Compilation:
Converted into binary like:
This binary code is what the computer executes
Key Points (Exam Ready )
- Compilation = Source Code → Machine Code
- Done by a Compiler
- Computers understand only binary (0 & 1)
- It checks for errors before execution
- It is required before running a program
Simple Definition (1 Line)
Compilation is the process of converting C source code into machine code using a compiler.
Integer Data Types
| Data Type | Size (Bytes) | Format Specifier | Range |
|---|---|---|---|
int / Signed int |
2 Bytes (TC) / 4 Bytes (Modern) | %d |
-32768 to 32767 (TC) |
unsigned int |
2 / 4 Bytes | %u |
0 to 65535 |
long int |
4 Bytes | %ld |
-2147483648 to 2147483647 |
unsigned long int |
4 Bytes | %lu |
0 to 4294967295 |
Character Data Types
| Data Type | Size (Bytes) | Format Specifier | Range |
|---|---|---|---|
char / Signed char |
1 Byte | %c |
-128 to 127 |
unsigned char |
1 Byte | %c |
0 to 255 |
How Values Are Stored in Memory?
Every value stored in RAM is converted into binary form (0s and 1s).
Example:
- Decimal value:
25 - Binary value:
11001
So internally:
Computers store and process only binary data
Integer Storage Example
Stored in memory as:
Character Storage & ASCII Value
Characters are stored using ASCII (American Standard Code for Information Interchange)
Example:
'A'→ ASCII = 65'B'→ ASCII = 66
So:
Important Points
- Every value in RAM is stored in binary form
- Characters are stored using ASCII codes
- ASCII converts characters → numbers → binary
Program: Display Character and its ASCII Value
Correct & Clean Code:
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("Character is: %c\n", ch);
printf("Its ASCII value is: %d", ch);
return 0;
}
Sample Output
Character is: B
Its ASCII value is: 66
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.
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.
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.