Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Language Operators: Fundamentals and Usage

Tech 1

Arithmetic Operators

C language provides several arithmetic operators for performing mathematical operations on variables:

+   -   *   /   %
  1. All operators except % can work with both integers and floating-point numbers.
  2. For the / operator, if both operands are integers, integer division is performed. If at least one operand is a floating-point number, floating-point division is performed.
  3. The % operator requires both operands to be integers. It returns the remainder after division.

Assignment Operators

Assignment operators allow you to assign values to variables. They can be used for simple assignments or combined with other operations for concise code.

int weight = 120; // Initial weight value
weight = 89; // Reassigning a new value
double salary = 10000.0;
salary = 20000.0; // Using assignment operator

// Assignment operations can be chained:
int a = 10;
int x = 0;
int y = 20;
a = x = y + 1; // Chained assignment

// For better readability, consider this alternative:
x = y + 1;
a = x;

Compound Assignment Operators

These operators combine assignment with another operation for more concise code:

+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^=

Example:

int value = 10;
value = value + 10; // Standard assignment
value += 10; // Equivalent compound assignment

Relational Operators

Relational operators compare values and return a boolean result:

>
>=
<
<=
!= // Tests for "not equal"
== // Tests for "equal"

While these operators are straightforward, be cautious about a common pitfall: confusing == (equality comparison) with = (assignment).

Unary Operators

Overview

Unary operators operate on a single operand:

!  // Logical negation
-  // Unary minus
+  // Unary plus
&  // Address-of
sizeof // Size of operand in bytes
~  // Bitwise complement
-- // Pre/post decrement
++ // Pre/post increment
*  // Dereference operator
(type) // Type casting

Example code:

#include 

int main()
{
    int number = -10;
    int *pointer = NULL;
    printf("%d\n", !2);  // Output: 0 (false)
    printf("%d\n", !0);  // Output: 1 (true)
    number = -number;    // Change sign
    pointer = &number;   // Get address
    printf("%d\n", sizeof(number));
    printf("%d\n", sizeof(int));
    printf("%d\n", sizeof number); // Valid syntax
    printf("%d\n", sizeof int);    // Error: missing operand
    return 0;
}

sizeof with Arrays

#include 

void displayArraySize(int arr[])
{
    printf("%zu\n", sizeof(arr)); // Size of pointer, not array
}

void displayCharArraySize(char ch[])
{
    printf("%zu\n", sizeof(ch)); // Size of pointer, not array
}

int main()
{
    int numbers[10] = {0};
    char characters[10] = {0};
    printf("%zu\n", sizeof(numbers)); // Size of entire array
    printf("%zu\n", sizeof(characters)); // Size of entire array
    displayArraySize(numbers);
    displayCharArraySize(characters);
    return 0;
}

Increment and Decrement Operators

Pre-increment and Pre-decrement

#include 

int main()
{
    int counter = 10;
    int result = ++counter; // Increment first, then use
    // counter is now 11, result is 11
    
    int value = 10;
    int output = --value; // Decrement first, then use
    // value is now 9, output is 9
    return 0;
}

Post-increment and Post-decrement

#include 

int main()
{
    int counter = 10;
    int result = counter++; // Use first, then increment
    // counter is now 11, result is 10
    
    int value = 10;
    int output = value--; // Use first, then decrement
    // value is now 9, output is 10
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.