views
Module 2: Tokens, Operators and Expressions
a. Tokens in C
i. Variables
ii. Keywords
iii. Constants
iv. Operators
v. Special Symbols
b. Operators and Expressions
i. Arithmetic Operators
ii. Relational Operators
iii. Logical Operators
iv. Assignment Operators
v. Expression and their evaluation / Order of evaluation, Parentheses, and Precedence
c. Lvalues and Rvalues
d. Type Conversion in C
i. Automatic conversion by Compiler
ii. Explicit Type Conversion
In the world of C programming, understanding tokens, operators, and expressions is essential. These fundamental building blocks form the basis of any C program and play a crucial role in how the program functions. In this module, we will explore each of these elements in detail, providing you with a solid foundation to write efficient and effective C code.
a. Tokens in C
Tokens are the smallest individual units of a C program. They can be classified into different categories, including variables, keywords, constants, operators, and special symbols.
i. Variables
In C, variables are used to store data values that can be manipulated by the program. They have a specific data type, such as int, float, or char, which determines the kind of data they can hold. For example:
int age = 25;
float pi = 3.14;
char grade = 'A';
ii. Keywords
Keywords are reserved words in C that have predefined meanings and cannot be used as variable names. They are an integral part of the language and serve specific purposes. Some common keywords in C include if, else, for, while, and return.
iii. Constants
Constants are fixed values that do not change during the execution of a program. They can be of different types, such as integer constants, floating-point constants, character constants, and string constants. For example:
#define PI 3.14159
const int MAX_SIZE = 100;
iv. Operators
Operators in C are symbols that perform specific operations on operands. They can be classified into various categories, such as arithmetic operators, relational operators, logical operators, and assignment operators. We will explore each of these categories in detail.
v. Special Symbols
In addition to keywords, constants, and operators, C also uses special symbols for various purposes. These symbols include parentheses, brackets, commas, semicolons, and more. They help in structuring the code and making it more readable.
b. Operators and Expressions
Operators and expressions are at the heart of C programming. They allow you to perform calculations, make decisions, and control the flow of your program. Let's dive deeper into each category of operators.
i. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and division. They include symbols like +, -, *, /, and %.
int sum = 10 + 5;
int difference = 10 - 5;
int product = 10 * 5;
int quotient = 10 / 5;
int remainder = 10 % 5;
ii. Relational Operators
Relational operators are used to compare values and determine the relationship between them. They include symbols like ==, !=, >, <, >=, and <=. The result of a relational operation is either true (1) or false (0).
int a = 10;
int b = 5;
int result = a > b; // result will be 1 (true)
iii. Logical Operators
Logical operators are used to combine multiple conditions and perform logical operations. They include symbols like && (logical AND), || (logical OR), and ! (logical NOT).
int age = 25;
int grade = 80;
if (age > 18 && grade >= 60) {
printf("You are eligible for admission.");
}
iv. Assignment Operators
Assignment operators are used to assign values to variables. They include symbols like =, +=, -=, *=, /=, and %=.
int x = 10;
x += 5; // equivalent to x = x + 5;
v. Expression and their evaluation / Order of evaluation, Parentheses, and Precedence
Expressions in C are combinations of variables, constants, and operators that produce a single value. The order of evaluation, parentheses, and precedence of operators play a crucial role in determining the result of an expression.
For example:
int result = 10 + 5 * 2; // result will be 20, not 30
In this case, the multiplication is performed first due to the higher precedence of the * operator.
c. Lvalues and Rvalues
In C, lvalues and rvalues are important concepts related to the assignment of values. An lvalue refers to a memory location that can be assigned a value, while an rvalue refers to a value that can be assigned to an lvalue.
int x = 10; // x is an lvalue
int y = x + 5; // (x + 5) is an rvalue
d. Type Conversion in C
Type conversion, also known as type casting, is the process of converting one data type to another. C provides automatic type conversion by the compiler, as well as explicit type conversion by the programmer.
i. Automatic conversion by Compiler
Automatic type conversion occurs when the compiler automatically converts one data type to another based on certain rules. For example, when performing arithmetic operations involving different data types, the compiler will automatically convert them to a common type.
ii. Explicit Type Conversion
Explicit type conversion allows the programmer to manually convert one data type to another using type casting. This can be useful in situations where the automatic conversion may not produce the desired result.
int x = 10;
double y = (double)x; // explicit type conversion
This statement converts the integer value of x to a double value.
Understanding tokens, operators, and expressions is crucial for any C programmer. By mastering these concepts, you will be able to write efficient and powerful C programs. Remember to consider the order of evaluation, use parentheses to control precedence, and be mindful of type conversions. Happy coding!
Comments
0 comment