C Miscellaneous Operators: Overview and Examples

Explore lesser-known but essential operators in C that offer powerful functionalities, such as determining the size of data types, accessing memory addresses, and more. This guide provides an overview of these miscellaneous operators with practical examples to enhance your understanding.



C - Miscellaneous Operators

Besides the commonly used arithmetic, logical, and assignment operators, C provides other essential operators for various functionalities. Let’s explore these operators with brief descriptions and examples:

Overview of Miscellaneous Operators

Operator Description Example
sizeof() Returns the size of a variable or data type. sizeof(a) where a is an integer returns 4.
& Returns the memory address of a variable. &a gives the memory address of variable a.
* Pointer dereference operator. *ptr accesses the value of the pointer ptr.
?: Conditional expression (Ternary operator). Condition ? X : Y
. Access struct or union members. var.member
-> Access struct members via a pointer. ptr->member

The sizeof Operator

The sizeof operator calculates the size (in bytes) of its operand, which can be a variable or a data type. Here's how it works:


#include 

int main() {
    int a = 16;
    printf("Size of variable a: %d\n", sizeof(a));
    printf("Size of int data type: %d\n", sizeof(int));
    printf("Size of char data type: %d\n", sizeof(char));
    printf("Size of float data type: %d\n", sizeof(float));
    printf("Size of double data type: %d\n", sizeof(double));
    return 0;
}

Output:

Size of variable a: 4
Size of int data type: 4
Size of char data type: 1
Size of float data type: 4
Size of double data type: 8

The Address-of Operator (&)

The & operator returns the memory address of a variable.


#include 

int main() {
    int var = 100;
    printf("var: %d address: %p", var, (void*)&var);
    return 0;
}

Output:

var: 100 address: 0x7ffec1d8a2a4

The Dereference Operator (*)

The * operator is used to declare a pointer and to dereference (access the value stored at) that pointer.


#include 

int main() {
    float var1 = 10.55;
    float *floatptr = &var1;

    printf("var1: %f\naddress of var1: %p\n", var1, (void*)&var1);
    printf("floatptr: %p\naddress of floatptr: %p\n", (void*)floatptr, (void*)&floatptr);
    printf("Value at floatptr: %f", *floatptr);

    return 0;
}

Output:

var1: 10.550000
address of var1: 0x7ffed3d7a3f4
floatptr: 0x7ffed3d7a3f4
address of floatptr: 0x7ffed3d7a3f0
Value at floatptr: 10.550000

The Ternary Operator (?:)

The ternary operator simplifies conditional statements.


#include 

int main() {
    int a = 10;
    (a % 2 == 0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);
    return 0;
}

Output:

10 is Even

The Dot (.) Operator

The . operator accesses members of a struct or union.


#include 

struct book {
    char title[10];
    double price;
    int pages;
};

int main() {
    struct book b1 = {"Learn C", 675.50, 325};
    printf("Title: %s\n", b1.title);
    printf("Price: %lf\n", b1.price);
    printf("No of Pages: %d\n", b1.pages);
    printf("Size of book struct: %lu\n", sizeof(struct book));
    return 0;
}

Output:

Title: Learn C
Price: 675.500000
No of Pages: 325
Size of book struct: 32