Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Operators

Operators

Operators enable operations to be performed on variables and values.

In the following example, we utilize the + operator to add together two values:

Example

int myNum = 100 + 50;

While the + operator commonly adds together two values, as illustrated above, it can also combine a variable with a value or another variable.

Example

int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400) 

 

In C, operators are categorized into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Operator

Name

Description

Example

+

Addition

Performs addition on two values.

x + y

Subtraction

Performs subtraction by subtracting one value from another.

x – y

*

Multiplication

Multiplies two values together.

x * y

/

Division

Performs division by dividing one value by another.

x / y

%

Modulus

Returns the remainder of the division.

x % y

++

Increment

Increments the value of a variable by 1.

++x

Decrement

Decrements the value of variable by 1.

–x

Assignment Operators

Assignment operators are utilized to assign values to variables.

In the following example, we employ the assignment operator (=) to assign the value 10 to a variable named x:

Example

int x = 10

The addition assignment operator (+=) adds a value to a variable:

Example

int x = 10;
x += 5

Here’s a list of all assignment operators:

 

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x – 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3

 

Comparison Operators

Comparison operators are employed to compare two values or variables. This is crucial in programming as it facilitates decision-making and finding solutions.

The result of a comparison operation is either 1 or 0, representing true (1) or false (0). These are known as Boolean values, and you’ll delve deeper into them in the Booleans and If..Else chapter.

In the subsequent example, we utilize the greater than operator (>) to determine if 5 is greater than 3:

Example

int x = 5;
int y = 3;
printf(“%d”, x > y); // returns 1 (true) because 5 is greater than 3 

Here’s a list of all comparison operators:

Operator

Name

Example

Description

== Less than x==y Returns 1 if the values are equal.

!=

Not equal

x!=y

Returns 1 if the values are not equal.

> Greater than x>y Returns 1 if the first value is greater than the second value.

< 

Less than

x<y

Returns 1 if the first value is less than the second value.

>= Greater than or equal to x>=y Returns if the first value is greater than or equal to the same value.

<=

Less than or equal to

x<=y

Returns 1 if the first value is less than or equal to the second value.

 

Logical Operators

You can utilize logical operators to evaluate true or false conditions between variables or values.

Logical operators are employed to establish the relationship between variables or values in terms of logic.

Operator

Name

Example

Description

&& 

Logical and

x < 5 &&  x < 10

Returns 1 when both statements are true.

||

Logical or

x < 5 || x < 4

Returns 1 if at least one of the statements is true.

!

Logical not

!(x < 5 && x < 10)

Inverts the outcome, yielding 0 if the result is 1.