Because the type of the result in C is int
(and 4 bytes is a typical size) and in C++ it's bool
(and 1 is the typical size for that).
These values are implementation dependent.
Here's a C11 program that demonstrates that using _Generic
(typical output int 4
):
#include <stdio.h>
void what_int(){
printf("int %lu",sizeof(int));
}
void what_other(){
printf("other ?");
}
#define what(x) _Generic((x), \
int : what_int(),\
default: what_other()\
)
int main(void) {
what(1==1);
return 0;
}
Here's a C++ program that demonstrates that using template specializaton (typical output bool 1
):
#include <iostream>
template<typename T>
void what(T x){
std::cout<<"other "<<sizeof(T);
}
template<>
void what(bool x){
std::cout<<"bool "<<sizeof(bool);
}
int main(){
what(1==1);
return 0;
}
I can't readily think of any code that is both C and C++ that would have a different outcome. Please accept that as a challenge.