Operators

operators


➕ Types of Operators in C

Operators are symbols that perform operations on variables and values. C supports the following operator types:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, <,>=, <=< /li>
  • Logical Operators: &&, ||, !
  • Assignment Operators: =, +=, -=, *=, /=
  • Increment/Decrement: ++, --
  • Bitwise Operators: &, |, ^, ~, <<,>>
  • Conditional (Ternary) Operator: condition ? expr1 : expr2
  • Special Operators: sizeof, comma operator (,), pointer (*), address-of (&)

⚖️ Precedence and Associativity

Precedence defines the order in which operators are evaluated when multiple operators are used in a single expression.

Associativity defines the direction (left-to-right or right-to-left) in which operators of the same precedence are evaluated.

Example: a + b * c → Multiplication (*) has higher precedence than addition (+), so b * c is evaluated first.

Associativity Example: a = b = c is evaluated right-to-left due to assignment associativity.


🧮 Expression in C

Expressions are combinations of variables, constants, and operators that produce a value.

Types of Expressions:

  • Arithmetic Expression: a + b - 2
  • Relational Expression: a > b
  • Logical Expression: a && b
  • Assignment Expression: a = 10

📜 Statement and Its Types

Statements are instructions that the C compiler executes. A C program is made up of one or more statements.

Types of Statements:

  • Expression Statement: Any valid expression followed by a semicolon. Example: a = b + 5;
  • Declaration Statement: Declares variables and data types. Example: int num;
  • Input/Output Statement: Used for user input and screen output. Example: scanf("%d", &num);, printf("%d", num);
  • Control Statement: Used to control the flow of execution. Includes: if, else, switch, while, for, break, continue
  • Compound Statement (Block): A group of statements enclosed in braces { }

🧮 Built-in Operators in C

Built-in operators in C are predefined symbols used to perform operations on variables and values. These operators are categorized into several types:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Assignment Operators: =, +=, -=, *=, /=
  • Increment/Decrement: ++, --
  • Bitwise Operators: &, |, ^, ~, <<, >>
  • Conditional Operator: ?: (ternary)
  • Sizeof Operator: sizeof() — returns memory size of data types or variables

Example: int a = 5, b = 3; int c = a + b; → Here, + is an arithmetic operator.


🧰 Built-in Functions in C

Built-in functions are standard library functions provided by C to perform common tasks like input/output, math operations, string handling, etc.

Types of Built-in Functions:

  • Input/Output: printf(), scanf(), getchar(), putchar()
  • Math Functions: Found in math.h Examples: sqrt(), pow(), abs(), floor(), ceil()
  • String Functions: Found in string.h Examples: strlen(), strcpy(), strcmp(), strcat()
  • Character Functions: Found in ctype.h Examples: isalpha(), isdigit(), toupper(), tolower()
  • Memory Functions: malloc(), calloc(), free() (from stdlib.h)

Example: printf("Sum = %d", a + b);printf() is a built-in output function.

Note: You must include proper header files (like #include <stdio.h>, #include <math.h>) to use these functions.


🖥️ Console Based I/O

Console Based Input/Output in C refers to performing input and output through the keyboard and monitor using standard functions provided by the C library.

Input: Data entered by the user using keyboard (e.g., using scanf())

Output: Displaying results on the screen (e.g., using printf())


🔧 Built-in I/O Functions

1. printf(): Used to display output on the screen.

printf("Value = %d", x);

2. scanf(): Used to take input from the user via keyboard.

scanf("%d", &x);

3. getch(): Waits for a character input from the user, does not echo it to the screen (found in conio.h).

char ch = getch();

4. getchar(): Reads a single character from the standard input.

char c = getchar();

5. putchar(): Displays a single character on the screen.

putchar(c);


📚 Concept of Header Files

Header files in C contain declarations of functions and macros used in programs. They are included at the top using the #include directive.

Examples:

  • stdio.h – for input/output functions like printf(), scanf()
  • conio.h – for getch(), clrscr()
  • math.h – for math functions like sqrt(), pow()

⚙️ Preprocessor Directives

Preprocessor directives are instructions processed before the actual compilation of code begins. They start with a # symbol.

1. #include: Tells the compiler to include contents of a header file.

#include <stdio.h> – includes standard I/O functions

2. #define: Used to define symbolic constants or macros.

#define PI 3.14 – replaces PI with 3.14 throughout the code

Comments