🌟 C Language Tutorial for Beginners
Welcome! In this tutorial, we’ll learn the basics of C programming step by step with examples and outputs.
1. Introduction to C
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972 at Bell Labs. It is widely used for system software, operating systems, and embedded programming.
✨ Features of C
- Simple and powerful
- Portable (can run on different machines)
- Structured programming language
- Fast execution (close to machine language)
- Base for modern languages like C++, Java, and Python
📌 Structure of a C Program
A C program generally follows this structure:
#include <stdio.h> // Preprocessor directive
int main() { // Main function
// Statements
return 0; // Exit status
}
int main() { // Main function
// Statements
return 0; // Exit status
}
🔹 Example: Hello World Program
This is the first program for beginners.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
int main() {
printf("Hello, World!");
return 0;
}
Output:
Hello, World!
Hello, World!
2. Basics of C
📌 Tokens in C
Tokens are the smallest elements in a program. Example: keywords, identifiers, constants, operators.
📌 Data Types in C
- int → Stores integers (10, 200, -5)
- float → Stores decimal numbers (3.14, -2.5)
- char → Stores single characters ('a', 'Z')
- double → Stores large decimal values
🔹 Syntax for Variable Declaration
data_type variable_name = value;
🔹 Example: Variable Declaration
#include <stdio.h>
int main() {
int age = 18; // integer
float pi = 3.14; // float
char grade = 'A'; // character
printf("Age = %d\\n", age);
printf("Pi = %f\\n", pi);
printf("Grade = %c\\n", grade);
return 0;
}
int main() {
int age = 18; // integer
float pi = 3.14; // float
char grade = 'A'; // character
printf("Age = %d\\n", age);
printf("Pi = %f\\n", pi);
printf("Grade = %c\\n", grade);
return 0;
}
Output:
Age = 18
Pi = 3.140000
Grade = A
Age = 18
Pi = 3.140000
Grade = A
📌 Input and Output in C
- printf(): Used for output (printing).
- scanf(): Used for input (taking values from user).
- scanf(): Used for input (taking values from user).
🔹 Example: Taking Input
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Example Run:
Enter a number: 25
You entered: 25
Enter a number: 25
You entered: 25
Comments
Post a Comment