Why is the printed result 5452853 [duplicate] - c

This question already has answers here:
Multi-character constant warnings
(6 answers)
Closed 11 months ago.
#include <stdio.h>
int main()
{
int a = '\12345';
printf("%d",a);
return 0;
}
Why is the printed result 5452853? I wonder what happened?

I combined phuclv's and Mark Dickinson's answers and I seem to understand that first of all '\12345' is equivalent to three characters '\123' = 1*8*8+2*8+3*1 = 83; '4' = 52 ; '5' = 53. Then the result is equal to 83*256*256+52*256+53 = 5452853
– HuangGuojun

Related

Power of 5 and its multiples do not work properly in VS code [duplicate]

This question already has answers here:
Why the result of pow(10,2) 99 instead of 100? [duplicate]
(1 answer)
Strange behaviour of the pow function
(5 answers)
Closed 28 days ago.
when i use pow() function in Vscode editior it works properly for all digits execpt 5 and its mulitples
#include<stdio.h>
#include<math.h>
int main(){
int a = 25;
int b = pow(a,2);
printf("%d",b);
return 0;
}

0 at the begining of value isnt shown in the output [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 2 years ago.
my question is short, if I have the following 2 lines of code:
int var = 01;
printf("%d", var);
the output is : 1
how do I get 01 rather than 1?
Use left padded format string.
Solution
int var = 1;
printf("%02d", var);

printing the char variable value using %d [duplicate]

This question already has answers here:
C/C++ unsigned integer overflow
(4 answers)
Closed 4 years ago.
I was trying to get into c programming, but in a question a get stuck, please explain this.
int main()
{
char c = 255;
c=c+10;
printf("%d",c);
return 0;
}
the output it gave is
> 9
kindly explain this to me.
The maximum value of a char is 255.
By adding 10 to that number you get 265.
Because that value is not a suitable value for a char it will do 265 % 256 resulting 9
That's why your result is 9

What happens when we convert string literal to integer in C? [duplicate]

This question already has answers here:
Multiple characters in a character constant
(3 answers)
Closed 6 years ago.
Consider a C program:
#include <stdio.h>
int main (void)
{
int x = 'a';
printf("%d", x);
}
Here the output is 97 as per the ASCII value table.
But in the example below:
#include <stdio.h>
int main(void)
{
int x ='aa';
printf("%d", x);
}
The output is 24929.
Can anyone please explain how the literal has been converted to this integer value?
int x ='aa';
This is valid but value of x is implementation defined. And btw, this is not a string literal. String literal would be this "aa".
You assigned a value to the int using octets : 'a' is 0x61.
So writing int x = 'aa' is like writing int x = 0x6161.
Edit: but do not write that. Just write int x = 0x6161 or int x = 24929.

Printf with unfixed length [duplicate]

This question already has answers here:
printf string, variable length item
(2 answers)
Closed 7 years ago.
I need to
printf(%?d)
Where '?' is some int. How can I do it?
I'm using pure c.
I've tried to work with const char* array. But there was no result.
See the printf man page:
int width = 16;
int value = 42;
printf("%*d\n", width, value);
Output:
42
LIVE DEMO

Resources