Modulus on Negative Numbers
What will be the output of the following C program?
#include <stdio.h> int main() { int a = 3, b = -8, c = 2; printf("%d", a % b / c); return 0; } |
chevron_right
filter_none
Output
1
% and / have same precedence and left to right associativity. So % is performed first which results in 3 and / is performed next resulting in 1. The emphasis is, sign of left operand is appended to result in case of modulus operator in C.
#include <stdio.h> int main() { // a positive and b negative. int a = 3, b = -8; printf("%d", a % b); return 0; } |
chevron_right
filter_none
Output
3
#include <stdio.h> int main() { // a negative and b positive int a = -3, b = 8; printf("%d", a % b); return 0; } |
chevron_right
filter_none
Output
-3
#include <stdio.h> int main() { // a and b both negative int a = -3, b = -8; printf("%d", a % b); return 0; } |
chevron_right
filter_none
Output
-3
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Modulus of two float or double numbers
- C program to Check Whether a Number is Positive or Negative or Zero
- C program to invert (making negative) an image content in PGM format
- Add two numbers using ++ and/or --
- Octal numbers in c
- Converting Strings to Numbers in C/C++
- Why variable name does not start with numbers in C ?
- How will you print numbers from 1 to 100 without using loop?
- To find sum of two numbers without using any operator
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Program to print a pattern of numbers
- C Program to print numbers from 1 to N without using semicolon?
- LEX program to add line numbers to a given file
- How to Count Variable Numbers of Arguments in C?
- Print numbers in sequence using thread synchronization
Improved By : sushantgundla



