Question

I have written the following code to multiply two numbers and I see the result to be incorrect. I assume the data type is not holding the value right. But the answer its printing is incorrect.

#include<stdio.h>
main()
{
    long int val1,val2;
    val1=val2=1235;
    char c = 'y';
    switch(c)
    {
        case 'y' : printf("%20d",val1*val2);
            break;
        default: printf("invalid");
            break;
    }
    return 0;
}

OUTPUT :
17897 //which is not the right answer
//it should actually be 1525225

Was it helpful?

Solution

Data type is holding the right value but you are using the wrong specifier. You should use %ld with long int.

printf("%20ld",val1*val2);

I double checked and it worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top