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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Modulus of two float or double numbers
- C program to invert (making negative) an image content in PGM format
- C program to Check Whether a Number is Positive or Negative or Zero
- How will you print numbers from 1 to 100 without using loop?
- Write a one line C function to round floating point numbers
- How to Count Variable Numbers of Arguments in C?
- To find sum of two numbers without using any operator
- Converting Strings to Numbers in C/C++
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- C Program to print numbers from 1 to N without using semicolon?
- Program to print a pattern of numbers
- Add two numbers using ++ and/or --
- Octal numbers in c
- Why variable name does not start with numbers in C ?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- LEX program to add line numbers to a given file
- C Program to Multiply two Floating Point Numbers
- C program to Find the Largest Number Among Three Numbers
- Problem in comparing Floating point numbers and how to compare them correctly?
- Average of Cubes of first N natural numbers
Improved By : sushantgundla

