
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Modulus of Negative Numbers in C
Here we will see what will be the result if we use negative numbers to get the modulus. Let us see the following programs and their outputs to get the idea.
Example
#include<stdio.h> int main() { int a = 7, b = -10, c = 2; printf("Result: %d", a % b / c); }
Output
Result: 3
Here the precedence of % and / are same. So % is working at first, so a % b is generating 7, now after dividing it by c, it is generating 3. Here for a % b, the sign of left operand is appended to the result. Let us see it more clearly.
Example
#include<stdio.h> int main() { int a = 7, b = -10; printf("Result: %d", a % b); }
Output
Result: 7
If we interchange the sign of a and b, then it will be like below.
Example
#include<stdio.h> int main() { int a = -7, b = 10; printf("Result: %d", a % b); }
Output
Result: -7
Similarly if both are negative, then also the result will be negative.
Example
#include<stdio.h> int main() { int a = -7, b = -10; printf("Result: %d", a % b); }
Output
Result: -7
- Related Articles
- Modulus of two float or double numbers using C
- Modulus function in C++ STL
- How does modulus work with complex numbers in Python?
- Prevent negative numbers in MySQL?
- Find subarray with given sum - (Handles Negative Numbers) in C++
- Negative Binary Numbers\n
- What is negative numbers ?
- Compute modulus division by a power-of-2-number in C#
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Rearrange positive and negative numbers with constant extra space in C++
- Removal of negative numbers from an array in Java
- Make array numbers negative JavaScript
- Can negative numbers be prime?
- Distinguish positive and negative numbers.
- What are the negative numbers?

Advertisements