C Programming - Constants, Variables, Data Types, Operators, Control Structures
Constants, Variables and Data Types
Constants:
- Fixed values that do not change during execution.
- Declared using `final` keyword.
Example: final int MAX = 100;
Variables:
- Used to store data values.
- Types: int, float, double, char, boolean, String, etc.
Declaration of Variables:
int age;
float salary;
char grade;
Giving Values to Variables:
age = 25;
salary = 50000.50f;
grade = 'A';
Symbolic Constants:
final double PI = 3.14;
Typecasting:
float x = (float) 10 / 3; // Result is 3.333...
Operators & Expressions
Types of Operators:
C Programming - Constants, Variables, Data Types, Operators, Control Structures
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, <, >, <=, >=
- Logical: &&, ||, !
- Assignment: =, +=, -=, etc.
- Increment/Decrement: ++, --
- Conditional (Ternary): (condition) ? true : false
- Bitwise: &, |, ^, ~, <<, >>
Arithmetic Expressions:
int result = (a + b) * c;
Evaluation of Expressions:
- Follows operator precedence and associativity rules.
Type Conversion in Expressions:
- Implicit: Widening (int to float)
- Explicit: Narrowing (float to int) using casting
Operator Precedence (High -> Low):
1. ()
2. ++, --, !
3. *, /, %
4. +, -
5. <, >, <=, >=
6. ==, !=
7. &&
8. ||
9. =, +=, -=, etc.
Decision Making, Branching & Looping
C Programming - Constants, Variables, Data Types, Operators, Control Structures
Control Statements:
- if, if-else, nested if, else-if ladder, switch-case
Example:
if (age > 18) { ... }
switch (choice) { case 1 -> ...; }
Looping Statements:
- for, while, do-while
Examples:
for (int i = 0; i < 10; i++) { ... }
while (i < 10) { ... }
do { ... } while (i < 10);
Jump in Loops:
- break: exits loop
- continue: skips current iteration
- return: exits method
Labelled Loops:
outer: for (...) {
for (...) {
if (condition) break outer;