w3resource

C Programming: Special Operators with examples

Overview of Special Operators in C Programming

Overview

In C programming, aside from the standard arithmetic and logical operators, the language also supports special operators for specific tasks. These include: Comma Operator ( , ), Sizeof Operator (sizeof), Pointer Operators (& and *), and Member Selection Operators (. and ->)

The special operators in C provide essential tools for tasks like memory management, pointer operations, and working with structures. By using these operators effectively, you can write more powerful and optimized code in C.

Key Topics:

Comma Operator (,)

The comma operator is used to separate multiple expressions and ensures that each expression is evaluated. The result of the entire expression is the result of the last expression.

Why and When to use the Operator:

  • Why: Used to evaluate multiple expressions in a single statement, particularly in situations where multiple assignments or operations need to be performed.
  • When: Typically used in loops or to execute multiple expressions where only the last expression's value is needed.

Example: Using the Comma Operator

The comma operator allows multiple expressions to be evaluated, and the result of the last expression is returned.

Code:

#include <stdio.h>
int main() {
    int x = 10, y = 20;
    
    // Using comma operator to evaluate multiple expressions
    int result = (x += 5, y += 10, x + y);  // x becomes 15, y becomes 30, result is 45
    
    printf("Result: %d\n", result);  // Output will be 45
    return 0;
}

Output:

Result: 45.

Explanation:

  • The comma operator evaluates x += 5, then y += 10, and finally x + y.
  • The result of the expression x + y (which is 45) is assigned to result.

Sizeof Operator (sizeof)

The sizeof operator returns the size (in bytes) of a variable or data type. It is commonly used to determine the size of data types or structures at runtime.

Why and When to use the Operator:

  • Why: Determines the size, in bytes, of a data type or a variable, which is essential for memory allocation and system-level programming.
  • When: Used when you need to allocate memory dynamically, optimize memory usage, or ensure compatibility across different systems.

Example: Using the sizeof Operator

The sizeof operator is used to determine the memory size of a data type or variable.

Code:

#include <stdio.h>
int main() {
    int a = 10;    
    // Using sizeof to find the size of an integer
    printf("Size of int: %zu bytes\n", sizeof(a));  // Output will be 4 bytes (or system-dependent)    
    // Using sizeof to find the size of a float
    printf("Size of float: %zu bytes\n", sizeof(float));  // Output will be 4 bytes (or system-dependent)
    
    return 0;
}

Output:

Size of int: 4 bytes
Size of float: 4 bytes.

Explanation:

  • The sizeof operator is used to find the size of the integer a and the float data type.
  • The result is system-dependent, but typically an int is 4 bytes and a float is also 4 bytes.

Pointer Operators (& and *)

The & (address-of) operator is used to get the address of a variable, and the * (dereference) operator is used to access the value stored at a memory location.

Why and When to use the Operator:

  • Why: The & operator is used to get the address of a variable, and the * operator is used to dereference a pointer and access the value stored at that address.
  • When: Used when working with pointers, dynamic memory allocation, and functions that require reference passing to modify values directly.

Example: Using Pointer Operators

The & operator retrieves the address of a variable, and the * operator accesses the value stored at that address.

Code:

#include <stdio.h>
int main() {
    int a = 10;
    int *ptr;  // Pointer declaration
    
    ptr = &a;  // Assign the address of 'a' to the pointer 'ptr'
    
    printf("Address of a: %p\n", ptr);  // Output will be the memory address of 'a'
    printf("Value of a: %d\n", *ptr);   // Dereferencing pointer to get the value of 'a'
    
    return 0;
}

Output:

Address of a: 0x7fffc85bae94
Value of a: 10

Explanation:

  • The & operator is used to get the address of the variable a, which is assigned to the pointer ptr.
  • The * operator dereferences the pointer, meaning it accesses the value stored at the memory address held by ptr (which is the value of a).

Member Selection Operators (. and ->)

The dot ( . ) operator is used to access members of a structure or union, and the arrow ( -> ) operator is used to access members via a pointer to a structure.

Why and When to use the Operator:

  • Why: The dot operator (.) is used to access members of a structure or union, and the arrow operator (->) is used to access members through a pointer to a structure.
  • When: The dot operator is used when you have a direct structure variable, and the arrow operator is used when working with pointers to structures.

Example: Using the Dot Operator

The dot operator is used to access the members of a structure directly.

Code:

#include <stdio.h>
struct Point {
    int x, y;
};

int main() {
    struct Point p1;
    
    // Accessing members using the dot operator
    p1.x = 10;
    p1.y = 20;
    
    printf("Point p1: (%d, %d)\n", p1.x, p1.y);  // Output will be Point p1: (10, 20)
    return 0;
}

Output:

Point p1: (10, 20).

Explanation:

  • The dot operator is used to access the members x and y of the structure Point.
  • The values 10 and 20 are assigned to x and y, respectively.

Example: Using the Arrow Operator

The arrow operator is used to access members of a structure through a pointer.

Code:

#include <stdio.h>
struct Point {
    int x, y;
};
int main() {
    struct Point p1 = {10, 20};
    struct Point *ptr = &p1;  // Pointer to structure
    
    // Accessing members using the arrow operator
    printf("Point p1: (%d, %d)\n", ptr->x, ptr->y);  // Output will be Point p1: (10, 20)
    return 0;
}

Output:

Point p1: (10, 20).

Explanation:

  • The arrow operator -> is used to access the members x and y of the structure Point via the pointer ptr.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/c-special-operator.php