This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 3 years ago.
I was trying the following code
printf("%d", 010 % 10);
I was expecting it output be 0, but it is 8.
Why? Is there any way to get the last digit of an integer which is taken as input.
Any numeric literal in c or c++ starting with a zero will be interpreted as octal
So your calculation is 8 modulo 10, which is 8
Related
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 11 months ago.
The following is the code
float lc = lcount/argc*100;
return lc;
I tried to get 362.5 but the result is 300
because initially it will computer division(because at the left of equation and having same precedence as multiplication )
lcount/argc(29/8=3)
and then multiplied by 100.
And at the last the computed result will be stored in floating format.
Your problem might be you are thinking initially it will compute 29/8=3.625
which wouldn't.
This question already has answers here:
In C storing values that start with zero get mutated, why?
(4 answers)
Closed 4 years ago.
So I have the following code:
#define x 010000
My intention is to define x as '10000' in decimal but when I print out the above code it gets printed as 4096.
I dont understand how is that being translated to 4096.
Could anyone please explain?
Numbers starting with a 0 are treated as octal in C. 010000 is an integer literal written in the octal notation. Its value is 8**4=4096. Remove the first 0.
This question already has answers here:
How to convert integer to char in C? [duplicate]
(6 answers)
Closed 5 years ago.
I am having problem comparing an int value and a char value in C. Lets say I variable int value1 that is 0 and a char that has value '0'. I know that that char's value is actually an ascii number and that '0' is 48, but how do I compare int's 0 value with chars '0' value, in an if statement as a example?
You can use the idiom c - '0' to convert a char c to its equivalent digit.
This is an expression of type int.
Note that it works in any character encoding supported by the C Standard, since such an encoding must order 0 to 9 consecutively.
This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I have the following code snippet:
#include <stdio.h>
int main(){
printf("%d\r\n", -1 % 7);
return 0;
}
When run, it prints -1. According to my own math and calculators like this one (http://www.miniwebtool.com/modulo-calculator/?number1=-1&number2=7), my answer should be 6. Im assuming there is some 'gotcha' in the C implementation of modulo that I am not grasping. Can someone explain why I am not getting the answer I expect?
The rule of C (after C99) is that the sign of the result of i%j is same as the sign of i. The answer you are getting is correct.
In C89 the result of i%j (if either i or j is negative) depends on the implementation. -1%7 could either be -1 or 6.
This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 8 years ago.
I tried to convert dec to hex. For example convert 255 to hex.
opa = 255.ToString("X");
gives me error:
error: invalid suffix "ToString" on floating constant
I spent lots of time to convert, but couldn't find right way.
You're asking about the wrong language. C does not support the dot operator on integers. To do this in C, you need to print it to a string like so.
char numstr[10];
sprintf(numstr, "%X", 255)