Format specifier in c [duplicate] - c

This question already has answers here:
What does "%.*s" mean in printf?
(4 answers)
Closed 6 years ago.
Why the following program work ? & what is difference b/w %d vs %*d ?
#include<stdio.h>
int main()
{
int n=5;
printf("n=%*d\n", n, n);
return 0;
}
What does width meant here by ?

The first n belongs to the * in %*d, so for your example it is %5d. %5d means to print an integer of width 5. Example: 234 is printed as __234 (2 spaces)

Related

Getting incorrect output for pow function typecasted in int for 5,11,15... and so on raised to 2 [duplicate]

This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
other number number raised to 2 are giving correct answer but 5,11,15... are showing 1 number less than the correct answer.
#include<stdio.h>
#include<math.h>
int main(){
int side;
printf("Enter side value \n");
scanf("%d", &side);
printf("The area is %d", (int) pow(side,2.0));
return 0;
}

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

Why am I getting 0 as the output? Shouldn't it be 40? [duplicate]

This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.

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

What does "-->" mean in C? [duplicate]

This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 8 years ago.
I have a question, what mean "-->" in C? For example:
int a, b, c, x;
a=2001;
b=1000;
c=2;
x=a-b*c;
printf("First: %i", x-->0);
It will print "1".
But:
printf("Second: %i", x-->0);
will print "0". Why when I use it second time, it print "0"?
x --> 0 is to be read (x--) > 0.
x-->0 is parsed as (x--) > 0.

Resources