- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Miscellanous Operators
![]() Share with a Friend |
C Programming - C Miscellanous Operators
C Miscellaneous Operators
In C programming, miscellaneous operators refer to those operators that don't necessarily fall into standard categories like arithmetic, relational, or logical operators. These include the sizeof, comma, pointer dereference, address-of, type-casting, and others.
- sizeof Operator
The sizeof operator is used to determine the size, in bytes, of a data type or an object.
- Syntax: sizeof(data_type) or sizeof(expression)
- Example:
C
int x = 10;
printf("Size of int: %zu bytes\n", sizeof(int)); // Output: Size of int: 4 bytes
printf("Size of x: %zu bytes\n", sizeof(x)); // Output: Size of x: 4 bytes
- Comma Operator (,)
The comma operator is used to separate multiple expressions where each expression is evaluated from left to right. It returns the value of the last expression.
- Syntax: expr1, expr2, expr3, ...
- Example:
C
int a = 5, b = 3, result;
result = (a = a + 2, b = b + 2, a + b); // Evaluates expressions from left to right
printf("Result: %d\n", result); // Output: Result: 11
- Explanation:
- a = a + 2 updates a to 7.
- b = b + 2 updates b to 5.
- The final value is a + b, which is 7 + 5 = 11.
- Address-of (&) Operator
The & operator is used to obtain the memory address of a variable. It returns a pointer to the variable.
- Syntax: &variable
- Example:
C
int x = 10;
int *ptr = &x; // &x gives the address of x
printf("Address of x: %p\n", ptr); // Output: Address of x: 0x7ffeeebc4e9c (depends on system)
- Pointer Dereference (*) Operator
The * operator is used to access the value at the memory address stored in a pointer. It is called the dereference operator.
- Syntax: *pointer
- Example:
C
int x = 10;
int *ptr = &x;
printf("Value pointed to by ptr: %d\n", *ptr); // Output: Value pointed to by ptr: 10
- Type-Casting Operator
Type-casting is used to explicitly convert one data type into another. The type-casting operator is a unary operator.
- Syntax: (type) expression
- Example:
C
float x = 10.5;
int y = (int) x; // Casting float to int
printf("Value of y: %d\n", y); // Output: Value of y: 10
- Conditional (Ternary) Operator (? :)
The conditional (ternary) operator is a shorthand for if-else. It evaluates a condition and returns one of two values based on whether the condition is true or false.
- Syntax: condition ? expression_if_true : expression_if_false
- Example:
C
int a = 10, b = 20;
int result = (a > b) ? a : b; // If a > b, result is a, else it is b
printf("Result: %d\n", result); // Output: Result: 20
- Type-Compatibility Operator (typeof) (Non-standard)
Some compilers (like GCC) support the typeof operator, which can be used to check the type of a variable or expression. This operator is not part of the standard C but is available in some compilers.
- Syntax: typeof(expression)
- Example (GCC-specific):
C
int x = 10;
typeof(x) y = 20; // y is of the same type as x (int)
- -> (Arrow) Operator
The -> operator is used to access members of a structure through a pointer to the structure.
- Syntax: pointer_to_structure->member
- Example:
C
struct Person {
char name[50];
int age;
};
struct Person *p;
p = (struct Person *)malloc(sizeof(struct Person));
p->age = 30; // Using the '->' operator to access 'age'
printf("Age: %d\n", p->age); // Output: Age: 30
- [] (Array Subscript) Operator
The [] operator is used to access elements of an array.
- Syntax: array[index]
- Example:
C
int arr[5] = {1, 2, 3, 4, 5};
printf("Element at index 2: %d\n", arr[2]); // Output: Element at index 2: 3
- , (Comma) Operator
The comma operator allows multiple expressions to be evaluated sequentially, from left to right, where the result of the last expression is returned.
- Syntax: expr1, expr2, ...
- Example:
C
int a = 10, b = 20, result;
result = (a = a + 2, b = b + 2, a + b); // The result will be a + b
printf("Result: %d\n", result); // Output: Result: 24
Summary of Miscellaneous Operators
| Operator | Description |
|---|---|
|
sizeof |
Returns the size of a data type or expression. |
|
, |
Allows multiple expressions to be evaluated from left to right, returning the last one. |
|
& |
Address-of operator, gives the memory address of a variable. |
|
* |
Dereference operator, accesses the value stored at the memory address of a pointer. |
|
? : |
Ternary conditional operator, shorthand for if-else. |
|
-> |
Used to access members of a structure through a pointer. |
|
[] |
Array subscript operator, used to access array elements. |
|
typeof |
(Non-standard) Used to get the type of a variable/expression (compiler-specific). |
|
, (Comma) |
Allows evaluating multiple expressions in sequence and returning the last value. |
These miscellaneous operators help in handling various memory-related, type-related, and control operations in C programming.
