Expression in C
An expression in C is a combination of variables, constants, operators, and functions that are evaluated to produce a result.
Types of Expressions:
- Arithmetic Expressions:
- Use arithmetic operators (
+
,-
,*
,/
,%
). - Example:
a + b - c
.
- Use arithmetic operators (
- Relational Expressions:
- Compare values using relational operators (
<
,>
,<=
,>=
,==
,!=
). - Example:
a > b
.
- Compare values using relational operators (
- Logical Expressions:
- Combine conditions using logical operators (
&&
,||
,!
). - Example:
(a > b) && (b > c)
.
- Combine conditions using logical operators (
- Bitwise Expressions:
- Perform bitwise operations (
&
,|
,^
,<<
,>>
). - Example:
a & b
.
- Perform bitwise operations (
- Conditional Expressions:
- Use the ternary operator (
? :
). - Example:
result = (a > b) ? a : b
.
- Use the ternary operator (
- Assignment Expressions:
- Assign a value to a variable.
- Example:
a = b + c
.
Type Conversion in C
Type conversion is the process of converting a variable from one data type to another.
Types of Type Conversion:
- Implicit Type Conversion (Type Promotion):
- Automatically performed by the compiler.
- Converts smaller data types to larger ones (e.g.,
int
tofloat
).
Example:
int a = 5;
float b = a; // ‘a’ is automatically converted to float
2. Explicit Type Conversion (Type Casting):
- Manually performed by the programmer using the cast operator
(type)
.
Example:
float a = 5.5;
int b = (int)a; // ‘a’ is explicitly cast to int
Preprocessor Directives in C
Preprocessor directives are instructions processed by the preprocessor before the compilation of the program. They begin with a #
symbol.
Types of Preprocessor Directives:
- File Inclusion (
#include
):- Includes header files in the program.
Example: