[C++] does implicit type conversion happen during compile-time or run-time

  • Thread starter Thread starter Nyap
  • Start date Start date
  • Views Views 929
  • Replies Replies 3
The compiler basically adds the casts for you when needed. The following program:
Code:
#include <stdio.h>
int main() {
    int a = 2;
    char b = 'b';
    printf("%i", a + b);

    return 0;
}

will compile into the exact same executable when the cast is explicit:

Code:
#include <stdio.h>
int main() {
    int a = 2;
    char b = 'b';
    printf("%i", a + (int)b);

    return 0;
}
 
The compiler basically adds the casts for you when needed. The following program:
Code:
#include <stdio.h>
int main() {
    int a = 2;
    char b = 'b';
    printf("%i", a + b);

    return 0;
}

will compile into the exact same executable when the cast is explicit:

Code:
#include <stdio.h>
int main() {
    int a = 2;
    char b = 'b';
    printf("%i", a + (int)b);

    return 0;
}
ah ok thnx
 
  • Like
Reactions: 0x40

Site & Scene News

Popular threads in this forum