incorrect macro conversion to int when printing [duplicate] - c

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.

Related

How does the %(modulus) operator in c works? [duplicate]

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

Why sizeof() operator gives different value for 'a' and "a" in C? [duplicate]

This question already has answers here:
Sizeof(char[]) in C
(8 answers)
Why the sizeof character constant is 4 bytes? [duplicate]
(2 answers)
Sizeof string literal
(2 answers)
Closed 4 years ago.
As far as I know a character constant, viz, 'a', is stored in ASCII format which is internally treated as integer, 97 in case of 'a', that's why sizeof('a') returns 4 on executing but when I use sizeof("a") it returns 2. I have not found any explanation regarding that yet.
My code:
#include <stdio.h>
void main()
{
int x,y;
x = sizeof('a');
y = sizeof("a");
printf("%d\n",x);
printf("%d",y);
}
which gives the output:
4
2
'a' is an integer. It has a size of 4 on most computers. It may also be something else, however. 2 is also common on more specialized hardware.
"a" is a string literal. There are two characters: a and \0. They have a size of 1 each, for a total of size 2. However, when you try to assign that, what you get usually is a const char* and that may have a different size.

Trouble comparing value in C [duplicate]

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.

Convert dec to hex number in C [duplicate]

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)

int i=00100 error in TC++ for C programing Language [duplicate]

This question already has answers here:
Meaning of leading zero in integer literal
(3 answers)
Closed 8 years ago.
I am an amature C programer. I can only use C Programing language.
I have a following code containing a loop in TC++IDE.
It is simple code for printing consecutiveNo. till given value,
it contains something like this:
i = 00100
in the above line when i enter 00100 the colour of normal integer value changes.(It changes to dark blue/Navy blue)
And when i use this in my loop. instead of repeating 100 times it repeats only "64" times.
Same happens with any value which is like 023 instead 0f 23.
Please explain what kind of IDENTIFIER/Variable is 00100 or values similar to it are.
And Also explain Why does it happen so ? (64 instead of 100).
Regards and Thank You in advanced !
This happens because a numeric literal starting with a zero is interpreted as a number written in octal.
A numeric literal beginning with 0 is interpreted as octal number in C and as 100 in octal is 64 in decimal this explains what you observe.

Resources