If you are trying to run modulo / remainder operator like below, there are higher chances you might get an error as “error: invalid operands to binary” The solution for this error is as mentioned below. $ vim using_mod.c #include int main(void) float num = 11.00; int remainder = num % 3.0; if (remainder == 0) printf("number is divisible "); else printf("number is not divisible: Remainder = %d ", remainder); return 0; $ gcc using_mod.c using_mod.c: In function ‘main’:using_mod.c:5:22: error: invalid operands to binary % (have ‘float’ and ‘double’) int remainder = num % 3.0;

Solution :

The remainder operator (otherwise known as the modulo operator) % is a binary operator (i.e., takes exactly 2 operands) and operates only on integer types (e.g., short, int, long, long long, etc).


You are watching: Invalid operands to binary expression c++

Hence, we either need to change float to int, or typecast both the values before and after % operator. like
int remainder = (int)num % (int)3.0;The complete working program will look like as below, $ vim using_mod.c #include int main(void) float num = 6.00; int remainder = (int)num % (int)3.0; if (remainder == 0) printf("number is divisible "); else printf("number is not divisible: Remainder = %d ", remainder); return 0;
*
*




See more: What Has 3 Feet But Cannot Walk ? What Has 3 Feet But Cannot Walk

Enter Email Address for Free SubscriptionSubscribe

Select Category

Select CategorySelect CategoryAmazon Affiliate (1)Amazon Alexa (1)Amazon Web Services ( AWS ) (6)Android (145)Android Applications (72)Android Build system (11)Android Commands (43)Android Errors and Solutions (2)Android Framework (6)Application Libraries (20)Application Programming Interface ( API ) (3)Application Stack / User Interface (11)Arduino (1)Assembly Language (3)Audio driver (1)Bash / Shell Scripts (19)Battery Life Improvements (1)Beagleboard (1)Bluetooth driver (12)Browsers (1)Business Development (5)C Programs (65)Career & Jobs (5)CentOS (1)Cloud Technologies (15)Code Review / Code Coverage (1)Commands and Packages (55)Compilation (20)Content Management System (13)Core Kernel (33)Database (3)Development & Build (74)Development Environment Setup (52)Development, Debugging and Performance Tools (14)Django & REST Api (38)E-Commerce (1)E-commerce (1)Editors (9)Email-Marketing (5)Embedded Linux (3)Entertainment (5)Errors & Failures (57)Ethernet Driver (5)External Peripherals (3)Facebook (1)File Systems (5)Flutter (8)Framebuffer / Display Driver (4)GDB (2)Git (23)Git Errors (1)Go Language Programs (8)Google AdSense (9)Google Analytics (1)Google News (1)Hardware Platforms (13)HTML (32)Internet of Things ( IoT ) (3)JAVA Programs (15)Javascript (3)JavaScript Object Notation ( JSON ) (8)JFFS2 Filesystem (2)Kernel & Device Drivers (1)Kernel Booting and Porting (7)Kotlin (6)LinkedIn (1)Linux Commands (1)Linux Device Drivers (29)Linux Host, Ubuntu, SysAdmin (77)Linux Kernel (42)Linux Networking (33)Linux, OS Concepts and Networking (60)Middleware Libraries, HAL (31)Moodle (4)Movies (3)MTD/Nand Driver (2)Multimedia Audio, Video, Images (25)NDK / Middleware / HAL (17)Optimizations (1)OS Concepts (34)PHP (20)Presentations (7)Procfs Filesystem (2)Programming Languages (71)Protocols (1)Python (6)Quora (1)RaspberryPi (9)Reddit (1)Remote Debugging and Monitoring (2)RFID and NFC (1)RTC driver (1)Scripting and Automation (19)Search Engine Optimisation ( SEO ) (45)Security Testing (1)Social Media (17)Source Code Management ( SCM ) (15)Sysfs filesystem (1)System Administration, Security (36)Technology (1)Testing and Debugging (26)Twitter (1)UART/Serial driver (2)Uncategorized (69)USB Driver (5)User Interface (6)Userspace Utilities (7)Virtual Machines (1)Virtualization (1)Web design and development (104)Website Hosting (12)WiFi Driver (9)Windows Softwares (2)Wordpress (34)Yocto / Bitbake / Openembedded (34)Youtube (1)Ezoicreport this ad

Top Pages

Embedded, Linux, Android, Opensource, Web Tech