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

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);

Related

Why is the printed result 5452853 [duplicate]

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

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

force length of int from a sensor [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 6 years ago.
I get data from a sensor in int type but the data range from 0 to 999.
I need a simple way for the var to always be the same length.
Like if I get 123 I don't need to change it but if it's 43 I need it to be 043.
Is there a simple way to do that?
Thanks.
Try with:
printf("%03d", var);

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

Text after % is not printing [duplicate]

This question already has answers here:
How do I print the percent sign(%) in C? [duplicate]
(4 answers)
Closed 7 years ago.
I was just completing my assignment when I noticed that the text after the % symbol in double quotes is not printing. Here is a very easy example to show this:
//program
#include<stdio.h>
int main()
{
printf("remainder of 5%2 is : %d",5%2);//here %2 is not printing
return 0;
}
output:
remainder of 5 is : 1
Only the %2 is not printed by printf() rest everything is fine.
Use %% to print %:
printf("remainder of 5%%2 is : %d",5%2);
You can also use ASCII code:
printf("remainder of 5%c2 is : %d",37,5%2");

Resources