Skip to main content

Essential Guide to C Operators: Examples & Outputs Explained

Understanding operators in C is fundamental for anyone learning the language. This guide covers the various types of operators in C, complete with examples and their output, ensuring you have a solid grasp of their usage. 1. Arithmetic Operators in C Arithmetic operators perform basic mathematical operations. Addition ( + ) : Adds two operands. Subtraction ( - ) : Deducts the second operand from the first. Multiplication ( * ) : Multiplies two operands. Division ( / ) : Performs division, yielding the quotient of the operands. Modulus ( % ) : Computes the remainder after division of the numerator by the denominator. Example: # include <stdio.h> int main () { int a = 10; int b = 3 ; printf ( "Addition: %d\n" , a + b); printf ( "Subtraction: %d\n" , a - b); printf ( "Multiplication: %d\n" , a * b); printf ( "Division: %d\n" , a / b); printf ( "Modulus: %d\n" , a % b); return 0 ; } Output: Addi

Essential Guide to C Operators: Examples & Outputs Explained

Essential Guide to C Operators


Understanding operators in C is fundamental for anyone learning the language. This guide covers the various types of operators in C, complete with examples and their output, ensuring you have a solid grasp of their usage.

1. Arithmetic Operators in C

Arithmetic operators perform basic mathematical operations.

  • Addition (+): Adds two operands.
  • Subtraction (-): Deducts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Performs division, yielding the quotient of the operands.
  • Modulus (%): Computes the remainder after division of the numerator by the denominator.

Example:

#include <stdio.h> int main() { int a = 10;
int b = 3; printf("Addition: %d\n", a + b); printf("Subtraction: %d\n", a - b); printf("Multiplication: %d\n", a * b); printf("Division: %d\n", a / b); printf("Modulus: %d\n", a % b); return 0; }

Output:

Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1

2. Relational Operators in C

Relational operators compare two values.

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Example:

#include <stdio.h> int main() { int x = 10; int y = 20; printf("\n x == y: %d\n", x == y); printf("\n x != y: %d\n", x != y); printf("\n x > y: %d\n", x > y); printf("\n x < y: %d\n", x < y); printf("\n x >= y: %d\n", x >= y); printf("\n x <= y: %d\n", x <= y); return 0; }

Output:

x == y: 0 x != y: 1 x > y: 0 x < y: 1 x >= y: 0 x <= y: 1

3. Logical Operators in C

Logical operators are used for logical operations.

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Example:

#include <stdio.h> int main() { int a = 10;
int b = 20; printf("a && b: %d\n", a && b); printf("a || b: %d\n", a || b); printf("!a: %d\n", !a); return 0; }

Output:

a && b: 1 // 1 means true a || b: 1 !a: 0// 0 means false

4. Bitwise Operators in C

Bitwise operators operate on bits and perform bit-by-bit operations.

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left shift (<<)
  • Right shift (>>)

Example:

#include <stdio.h> int main() { int a = 5; // Binary: 0101 int b = 9; // Binary: 1001 printf("a & b: %d\n", a & b); printf("a | b: %d\n", a | b); printf("a ^ b: %d\n", a ^ b); printf("~a: %d\n", ~a); printf("a << 1: %d\n", a << 1); printf("a >> 1: %d\n", a >> 1); return 0; }

Output:

a & b: 1 a | b: 13 a ^ b: 12 ~a: -6 a << 1: 10 a >> 1: 2

5. Assignment Operators in C

Assignment operators assign values to variables.

  • Simple assignment (=)
  • Add and assign (+=)
  • Subtract and assign (-=)
  • Multiply and assign (*=)
  • Divide and assign (/=)
  • Modulus and assign (%=)

Example:

#include <stdio.h> int main() { int value = 10; value += 5; // value = value + 5; value is now 15 printf("Value after += 5: %d\n", value); value -= 3; // value = value - 3; value is now 12 printf("Value after -= 3: %d\n", value); value *= 2; // value = value * 2; value is now 24 printf("Value after *= 2: %d\n", value); value /= 4; // value = value / 4; value is now 6 printf("Value after /= 4: %d\n", value); value %= 5; // value = value % 5; value is now 1 printf("Value after %= 5: %d\n", value); return 0; }

Output:

Value after += 5: 15 Value after -= 3: 12 Value after *= 2: 24 Value after /= 4: 6 Value after %= 5: 1

6. Increment and Decrement Operators in C

These operators modify the value of a variable by incrementing or decrementing it by one.

  • Increment (++)
  • Decrement (--)

Example:

#include <stdio.h> int main() { int a = 10; a++; // a = a + 1; a = 11 printf("a after a++: %d\n", a); a--; // a = a - 1; a = 10 printf("a after a--: %d\n", a); return 0; }

Output:

a after a++: 11 a after a--: 10

7. Conditional (Ternary) Operator in C

The conditional operator evaluates a condition and returns one of two values accordingly.

  • Ternary Operator (? :)

Example:

#include <stdio.h> int main() { int a = 10;
int b = 20; int max = (a > b) ? a : b; printf("Maximum value: %d\n", max); return 0; }

Output:

Maximum value: 20

8. Sizeof Operator in C

The sizeof operator determines the memory size, in bytes, occupied by a data type or variable.

Example:

#include <stdio.h> int main() { int a = 10; printf("Size of int: %lu\n", sizeof(int)); printf("Size of a: %lu\n", sizeof(a)); return 0; }

Output:

Size of int: 4 Size of a: 4

9. Comma Operator in C

The comma operator enables the evaluation of several expressions within one statement, with the final expression's value being returned.

Example:

#include <stdio.h> int main() { int a, b; a = (b = 5, b + 5); // b is assigned 5, then a is assigned b + 5 printf("a: %d, b: %d\n", a, b); return 0; }

Output:

a: 10, b: 5

These examples provide a comprehensive overview of the various operators in C, helping you understand their usage and functionality. Whether you're a newcomer or wanting to brush up on your skills, this guide includes all the key operators you'll find in C programming.

Comments

Popular posts from this blog

Comprehensive Guide to C's Data Type Literals, Escape Sequences & Type Casting

In C programming, understanding data types is essential as they define the type of data that a variable can store. Here is an explanation of the primary data types in C: Basic Data Types Integer Types int : Represents integer numbers. Typically occupies 4 bytes. short : Smaller integer type, usually 2 bytes. long : Larger integer type, often 4 or 8 bytes. long long : Even larger integer type, usually 8 bytes. Floating-Point Types float : Single-precision floating-point number, usually 4 bytes. double : Double-precision floating-point number, typically 8 bytes. long double : Extended precision floating-point number, often 12 or 16 bytes. Character Type char : Used to store single characters, usually 1 byte. Derived Data Types Arrays An array is a collection where elements of the same type are stored sequentially in memory. Pointers Variables that hold the memory address of another variable. Structures ( struct ) A user-defined data type that combines variables of different types. Unions

Essential Guide to C Programming: Features, Structure, and Advantages

Introduction to C Programming C is a versatile and powerful general-purpose programming language created by Dennis Ritchie between 1969 and 1973 at Bell Labs. Renowned for its efficiency and control over system resources, C has become a foundational language in computer science and software engineering. Key Features of C Low-level Memory Access : C allows direct manipulation of memory using pointers, making it ideal for system-level programming. Portability : C programs can be compiled and run on various machine architectures with minimal modifications. Simplicity and Efficiency : C provides a straightforward syntax that maps efficiently to machine instructions. Modularity : Functions and separate files enable the creation of modular and maintainable code. Rich Standard Library : C includes a comprehensive set of libraries for various functions, enhancing its capabilities. Structure of a C Program A basic C program consists of the following components: Preprocessor Directives : Instru

Exploring the Diversity of Programming Languages: A Primer

Programming languages are the backbone of software development, allowing developers to communicate with computers and create innovative solutions. In this explanation, we'll delve into the world of programming languages, exploring their types, examples, and characteristics. What is a Programming Language? A programming language is a set of rules and instructions that a computer can understand, used to write software, apps, and websites. It's a way for humans to communicate with machines, creating a bridge between ideas and digital reality. Types of Programming Languages Programming languages can be categorized into several types, each with its strengths and weaknesses. Here are the main types of programming languages: 1. Procedural Programming Languages Example: C, C++, Java Characteristics : Focus on procedures and functions, sequential execution, and memory management. Description: Procedural languages follow a step-by-step approach, breaking down complex tasks into smaller