What will be the result of executing the loop with int8_t type?

#include <iostream>
#include <stdint.h>

using namespace std;

int main() {
    int a = 0;
    for (int8_t i = 1; i > 0; i <<= 1)
    cout << a;
    return 0;
int8_t is an 8 bit signed type, the most significant bit of which is responsible for the sign of a number. The loop will end after seven iterations, as the most significant bit is set to one. Therefore, a will be 7.
int8_t is defined in stdint.h

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes