C language is a general-purpose, procedural programming language developed in the early 1970s by Dennis Ritchie at Bell Labs.
It
is known for its efficiency, speed, and low-level memory access, making it ideal for system-level programming like operating
systems, embedded systems, and compilers.
#include <stdio.h> // Preprocessor directive
int main() { // Main function - program entry point
printf("Hello, World!"); // Output function
return 0; // End of program
What is C Used For?
Operating Systems (e.g., parts of Linux, Windows)
Embedded Systems (like microcontrollers)
Game Development
Compilers and Interpreters
Network Drivers and Protocols
What is a Variable in C?
In C language, a variable is a named memory location used to store data that can be changed during program
execution. It's like a container that holds information your program can use and modify.
Basic Syntax Of Variable Declaration : - data_type variable_name;
Example:-
int age;
float salary;
char grade;
Rules for Naming Variables:
1. Must begin with a letter or underscore _.
2. Can contain letters, digits, and underscores.
3. Cannot use keywords (e.g., int, if, while).
4. Case-sensitive (Total ≠ total).
Example Code:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Variable Initialization:
int a = 10;
float b = 20.5;
Operators and Expressions in C
In C language, operators are symbols used to perform operations on variables and values. An expression is a
combination of variables, constants, and operators that produces a value
1. Types of Operators in C
1. Arithmetic Operators
Used to perform mathematical operations:
Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b (remainder)
2. Relational (Comparison) Operators
Used to compare values:
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b
3. Logical Operators
Used in decision making (conditions):
Operator Meaning Example
&& Logical AND a > 5 && b < 10
` `
! Logical NOT !(a == b)
4. Assignment Operators
Assign values to variables:
Operator Example Meaning
= a = 5; Assign 5 to a
+= a += 3; a = a + 3;
-= a -= 2; a = a - 2;
*= a *= 4; a = a * 4;
/= a /= 2; a = a / 2;
%= a %= 3; a = a % 3;
5. Increment & Decrement Operators
Change value by 1:
Operator Meaning Example
++ Increment by 1 a++ or ++a
-- Decrement by 1 a-- or --a
6. Bitwise Operators
Operate on binary bits:
Operator Meaning
& AND
` `
^ XOR
~ NOT
Operator Meaning
<< Left shift
>> Right shift
2. Expressions in C
An expression is a combination of variables, constants, and operators.
Examples:
int x = 10 + 5; // Arithmetic expression
if (x > 10 && x < 20) // Logical expression
printf("In range");
Example Program:
#include <stdio.h>
int main() {
int a = 10, b = 3;
int sum = a + b;
int isEqual = (a == b);
printf("Sum: %d\n", sum);
printf("Are they equal? %d\n", isEqual);
return 0;
}
1. Data Types in C
Data types define what kind of data a variable can hold. Each type also defines how much memory it uses.
Basic Data Types
Data Type Description Size* Format Specifier
int Integer numbers 2 or 4 bytes %d
float Decimal numbers (single precision) 4 bytes %f
double Decimal numbers (double precision) 8 bytes %lf
char Single character 1 byte %c
*Size may vary depending on system (32-bit vs 64-bit)
Example Declaration:
int age = 25;
float salary = 15000.50;
char grade = 'A';
double pi = 3.141592;
2. Input/Output Operators in C
In C, input/output operations are handled using scanf() and printf() functions from the <stdio.h> header file.
Input: scanf()
Used to take input from the user.
Syntax:
scanf("format_specifier", &variable);
Example:
int age;
scanf("%d", &age);
Output: printf()
Used to display output on the screen.
Syntax:
printf("text %format_specifier", variable);
Example:
int age = 25;
printf("Your age is: %d", age);
Common Format Specifiers
Specifier Used For Example
%d int 25
%f float 25.50
%lf double 3.14159
%c char 'A'
%s string (char array) "Hello"
Full Example:
#include <stdio.h>
int main() {
int age;
float height;
char grade;
// Taking input
printf("Enter your age, height, and grade:\n");
scanf("%d %f %c", &age, &height, &grade);
// Displaying output
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
return 0;
}