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:

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:

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Operators
  6. Special Symbols (e.g., ;, ,)

Keywords and Identifiers

2. Constants

Constants are values that do not change during program execution.

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.

Relational Operators

Used to compare two values. They return 1 (true) or 0 (false).

Logical Operators

Used to combine two or more conditions.

Assignment Operators

Used to assign values to variables.

Increment and Decrement Operators

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 -).