C Programs Tutorials | IT Developer
IT Developer

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.

  1. 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

  1. 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.
  1. 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)

  1. 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

  1. 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

  1. 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

  1. 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)

  1. -> (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

  1. [] (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

  1. , (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.