Unit 3: Constants, Variables, Data Types, and Operators
Table of Contents
1. C Fundamentals
Character Set
The C Character Set is the set of all valid characters you can use in a C program. This includes:
- Alphabets: A-Z, a-z
- Digits: 0-9
- Special Characters:
+ - * / % & # ! ? , ; : . ( ) { } [ ] _etc. - Whitespace: Space, Newline (
\n), Tab (\t)
C Tokens
A token is the smallest individual unit in a C program. A line of code is made up of multiple tokens. The main tokens are:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
- Special Symbols (e.g.,
;,,)
Keywords and Identifiers
- Keywords: These are reserved words that have a special meaning to the compiler (e.g.,
int,if,else,while,return). You cannot use them as variable names. - Identifiers: These are the names you give to variables, functions, and arrays (e.g.,
sum,myVariable,main).- Rules: Must start with a letter (a-z, A-Z) or an underscore (
_). Can be followed by letters, underscores, or digits. Case-sensitive (ageandAgeare different).
- Rules: Must start with a letter (a-z, A-Z) or an underscore (
2. Constants
Constants are values that do not change during program execution.
- Integer Constants:
10,-5,0 - Floating-point Constants:
3.14,-0.005 - Character Constants:
'a','5',''(single quotes) - String Constants:
"Hello"(double quotes)
Symbolic Constants
You can give a name to a constant using the #define preprocessor directive. This makes code more readable and easier to maintain.
#include// This is a symbolic constant #define PI 3.14159 int main() { float area = PI * 5 * 5; // Compiler replaces PI with 3.14159 return 0; }
3. Variables
A variable is a named location in memory used to store data. Its value can be changed during program execution.
Declaration of Variables
You must declare a variable before using it. This tells the compiler the variable's type and name.
int age; // Declares a variable 'age' of type int float marks; // Declares a variable 'marks' of type float char grade; // Declares a variable 'grade' of type char
Assigning Values to Variables
You assign a value using the assignment operator (=). This can be done at declaration (initialization) or later.
// Initialization int age = 20; // Assignment float marks; marks = 95.5;
4. Data Types
Data types define the type and size of data a variable can store.
| Data Type | Keyword | Typical Size (bytes) | Description |
|---|---|---|---|
| Integer | int | 2 or 4 | Stores whole numbers (e.g., 5, -10). |
| Character | char | 1 | Stores a single character (e.g., 'a', ''). |
| Floating Point | float |
4 | Stores single-precision numbers with decimals (e.g., 3.14). |
| Double | double |
8 | Stores double-precision numbers with decimals. |
| No Type | void |
N/A | Used for functions that return nothing. |
5. Operators and Expressions
An expression is a combination of variables, constants, and operators that evaluates to a single value (e.g., a + 5 * b).
Arithmetic Operators
Used for mathematical calculations.
+(Addition),-(Subtraction),*(Multiplication),/(Division)%(Modulus): Gives the remainder of a division. (e.g.,10 % 3is 1).
Relational Operators
Used to compare two values. They return 1 (true) or 0 (false).
==(Equal to)!=(Not equal to)>(Greater than),<(Less than)>=(Greater than or equal to),<=(Less than or equal to)
Logical Operators
Used to combine two or more conditions.
&&(Logical AND): True only if both conditions are true.||(Logical OR): True if at least one condition is true.!(Logical NOT): Reverses the truth value (true becomes false, false becomes true).
Assignment Operators
Used to assign values to variables.
=(Simple assignment):a = 10;- Shorthand operators:
a += 5(same asa = a + 5). Also-=,*=,/=.
Increment and Decrement Operators
++(Increment): Increases value by 1.--(Decrement): Decreases value by 1.- Pre-increment (
++x): Increments first, then uses the value. - Post-increment (
x++): Uses the value first, then increments.
Conditional Operator (Ternary Operator)
A shorthand for an if-else statement.
(condition) ? value_if_true : value_if_false;
int a = 10, b = 20; int max = (a > b) ? a : b; // 'max' will be 20
Arithmetic Expressions
These are combinations of variables, constants, and arithmetic operators. C follows operator precedence (like BODMAS) to evaluate them. (e.g., * and / have higher precedence than + and -).