Expression, Type Conversion and Preprocessor Directives

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:

  1. Arithmetic Expressions:
    • Use arithmetic operators (+, -, *, /, %).
    • Example: a + b - c.
  2. Relational Expressions:
    • Compare values using relational operators (<, >, <=, >=, ==, !=).
    • Example: a > b.
  3. Logical Expressions:
    • Combine conditions using logical operators (&&, ||, !).
    • Example: (a > b) && (b > c).
  4. Bitwise Expressions:
    • Perform bitwise operations (&, |, ^, <<, >>).
    • Example: a & b.
  5. Conditional Expressions:
    • Use the ternary operator (? :).
    • Example: result = (a > b) ? a : b.
  6. 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:

  1. Implicit Type Conversion (Type Promotion):
    • Automatically performed by the compiler.
    • Converts smaller data types to larger ones (e.g., int to float).
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:

  1. File Inclusion (#include):
    • Includes header files in the program.

Example:

#include <stdio.h> // Includes the standard input-output library

        2. Macro Definitions (#define):

  •   Defines constants or macros.
Example:

 

#define PI 3.14159
#define AREA(r) (PI * r * r) // Macro with parameter

Example of Preprocessor Directives:

#include <stdio.h>
#define PI 3.14159
#define CIRCLE_AREA(r) (PI * r * r)

int main() {
float radius = 5.0;

// Using macro for area calculation
printf(“Area of Circle: %.2f\n”, CIRCLE_AREA(radius));

return 0;
}

Output:

Area of Circle: 78.54

Summary

Concept Description
Expression A combination of variables, constants, and operators that produce a value.
Type Conversion Converts a variable from one data type to another (implicit or explicit).
Preprocessor Directives Instructions processed before compilation, e.g., #include, #define, #ifdef.

Leave a Comment

Your email address will not be published. Required fields are marked *