Unit 2: Constant and Variable
Contents
1. Programming Language Levels
Programming languages are categorized based on their proximity to computer hardware.
- Machine Language: The lowest level, consisting of 0s and 1s that the CPU understands directly.
- Assembly Language: Uses mnemonics (symbolic names) instead of binary code; requires an Assembler to translate.
- High-Level Languages: Uses English-like commands (e.g., C, Java); easy for humans to read but requires a Compiler or Interpreter.
2. C Program Structure
Every C program follows a standard template to ensure the compiler can process the code correctly.
/* Documentation Section: Description of program */ #include <stdio.h> // Link Section: Preprocessor directives // Global Declaration Section (Optional) int main() // Main Function Section { // Declaration part // Execution part return 0; } 3. Character Set & Tokens
The Character Set includes all characters that the C compiler recognizes.
- Letters: A-Z and a-z.
- Digits: 0-9.
- Special Characters: Symbols like +, -, *, /, %, &, etc.
- White Spaces: Blank spaces, tabs, and newlines.
4. Identifiers and Keywords
Keywords
Keywords are reserved words that have a predefined meaning to the C compiler. They cannot be used as names for variables.
Rule: There are 32 standard keywords in C (e.g., int, float, if, else, while, return).
Identifiers
Identifiers are user-defined names given to various program elements like variables, functions, and arrays.
- Rules for Identifiers:
- Must start with a letter or underscore (_).
- Can contain letters, digits, and underscores.
- No special characters (except underscore) or spaces allowed.
- Cannot be a keyword.
5. Constants and Variables
Variables
A variable is a data name used to store a data value that can change during program execution.
Example: int age = 20; (Here, 'age' is a variable).
Constants
A constant is a value that does not change during the program execution.
| Constant Type | Examples |
|---|---|
| Integer Constants | 10, -50, 1000 |
| Real (Floating-point) | 3.14, -0.005, 1.5 |
| Character Constants | 'A', 'z', '5' |
| String Constants | "Hello", "Knowlet", "123" |
6. Exam Focus Enhancements
- Case Sensitivity: Remember that C is case-sensitive. 'Age' and 'age' are two different identifiers.
- Main Function: Every C program must have exactly one main() function. Execution starts here.
- Header Files: Always include #include <stdio.h> if you are using printf or scanf.
- Identifier Names: Starting a variable name with a number (e.g., 1number is wrong; number1 is correct).
- Keywords as Variables: Trying to name a variable "int" or "return".
- Semicolons: Forgetting the semicolon (;) at the end of statements.
Q: What is the difference between an identifier and a keyword?
A: A keyword is a reserved word with a fixed meaning; an identifier is a name created by the programmer for variables or functions.
Q: Why is C called a Middle-Level language?
A: Because it combines the features of low-level languages (direct memory access) with the ease of high-level languages.