Super!
Here are your C programming notes from the very beginning up to type casting, all
explained clearly:
---
1. Introduction to C Programming
C is a powerful general-purpose programming language developed in the early 1970s.
Used to develop operating systems, embedded systems, and compilers.
Code is structured and compiled (converted into machine code before execution).
---
2. Data Types and Variables
Basic Data Types:
Declaration:
int age;
float height;
char grade;
Initialization:
age = 18;
height = 5.9;
grade = 'A';
---
3. Input and Output
Include Header File
#include <stdio.h>
Input using scanf()
int age;
scanf("%d", &age);
Output using printf()
printf("Age is: %d", age);
Example Code:
#include <stdio.h>
int main() {
char name[50];
char initial;
int age;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your initial: ");
scanf(" %c", &initial);
printf("Enter your age: ");
scanf("%d", &age);
printf("Your name: %s\nYour initial: %c\nYour age: %d\n", name, initial, age);
return 0;
}
---
4. Arithmetic Operators
#include <stdio.h>
int main() {
int a = 4, b = 80;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
}
---
5. Relational Operators
Compare values and return 1 (true) or 0 (false)
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
---
6. Logical Operators
Example:
int age = 20, mark = 80;
if (age > 18 && mark > 70)
printf("Both are positive.\n");
else
printf("Not both are positive.\n");
if (age > 23 || mark > 70)
printf("May Be eligible.\n");
else
printf("Not eligible.\n");
Incorrect usage of NOT:
// if (age > 23 ! mark > 90) Not valid
Correct usage:
if (!(age > 23 && mark > 90))
printf("Not positive.\n");
---
7. Increment and Decrement Operators
int a = 5;
printf("%d\n", ++a); // 6
printf("%d\n", a++); // 6, then becomes 7
printf("%d\n", a); // 7
---
8. Type Casting
Implicit Type Casting (done automatically)
int a = 5;
float b = a; // b = 5.0
Explicit Type Casting (done manually)
float a = 5.7;
int b = (int)a; // b = 5
Important Use Case: Division
int x = 7, y = 2;
float result = (float)x / y; // 3.5
Without casting, it would give:
int x = 7, y = 2;
float result = x / y; // 3.0 (wrong)