c printf %lf %d different result [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
MyCode
#include<stdio.h>
int main(){
printf("result1 : %lf %d\n", (1 - (double)((int)1)), (1 - (double)((int)1)));
return 1;
}
Result
result1 : 0.000000, 1
I dont't understand this result.
I think when printf("%d"), this result must be zero!

Refer to the printf reference to find that the "%d" format specifier expects an int as parameter. Yet, you pass it a double. This is undefined behavior, meaning anything can happen, including the result you get (for more details on what's likely happening, refer to eg. What happens to a float variable when %d is used in a printf?).
Instead, try adding a cast to int :
printf("result1 : %lf %d\n", (1 - (double)((int)1)), (int) (1 - (double)((int)1)));

The type of the arguments passed to printf have nothing inherently to do with the format string. It is your responsibility to make sure the types match up. In this case, you are passing two double values. However, the format string is attempting to interpret the second one as an integer. This is undefined behavior.
While the behavior is undefined in the general case, it is likely that you are seeing the sign bit of the IEEE 754 double in a little-endian interpretation of an integer.

Related

How do you print double * in c? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i am trying to print a double and have been using %lf throughout my code, but when i compile the program two of the printf doubles come up with a warning saying:
format specifies type 'long double' but the argument has type
'double *' [-Wformat]
what format should i use to print this?
You should pass the double rather than a pointer to it.
You apparently have something similar to
double d = ...;
double *p = &d;
printf("%lf\n", p);
Replace the last line with
printf("%f\n", *p);
Also note the removed l, which is for use with integer conversion specificers.

Warnings Data/Format specifies [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I got some warning in my code. Is there someone who can help me?
I tried to change %d to %f but the program will then not work as I want to, like with the warnings.
In the line
printf("%d |%.2f \n", MOVE_FORWARD * i, i, MOVE_FORWARD);
You are doing three things wrong:
i) You used more arguments than format specifiers, you have 2 format specifiers and used 3 arguments namely (MOVE_FORWARD *i), (i) and MOVE_FORWARD
, you used printf to print only 2.
ii) You used wrong specifiers, MOVE_FORWARD * i is of type double, not integer.
iii) i is of type integer not double.
The correct way should be:
printf("%f |%d \n", MOVE_FORWARD * i, i);
if you want to print those two.

Code output explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Printing after typecasting with %d or %i gives unexpected outputs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am rounding off some values and then printing them. When I use %f option, they are printed correctly, but using the %d or %i option (even after casting the rounded values to int) is giving a weird output, and I am not able to figure the why of it out.
Any help is much appreciated!
When I use %f:
i = 0;
while(i < n_shapes)
{
ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
printf("%f,%f,%f,%f\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
i++;
}
Output:
115.000000,94.000000,115.000000,101.000000
116.000000,51.000000,117.000000,58.000000
116.000000,60.000000,117.000000,67.000000
116.000000,69.000000,117.000000,75.000000
116.000000,77.000000,117.000000,84.000000
116.000000,86.000000,117.000000,93.000000
116.000000,94.000000,117.000000,101.000000
Now, with %d (or %i):
i = 0;
while(i < n_shapes)
{
ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
printf("%d,%d,%d,%d\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
i++;
}
Output:
1079590912,0,6,-1
1078788096,0,5,-1
1079033856,0,6,-1
1079164928,0,6,-1
1079312384,0,6,-1
1079459840,0,6,-1
1079590912,0,6,-1
Thank you!
Edit: Yes, I realize that using (int) in the printf gives me the right output. I was curious about the values I got when I didn't do so. What does my output when I use %d without casting inside the printf mean?
This is undefined behavior. You need to use the correct type specifier.
printf cannot verify that the types of parameters that you pass to it for printing match their corresponding format specifiers. The compiler performs type-specific conversions before passing these parameters, so printf expects that for each %f if would find a double (float gets converted to double as well) and for each %d it would find an int. Your code passes a double-converted value for that %d specifier, which causes undefined behavior.
Note that casting a float or a double expression to int before assigning to a float or double variable does not change the representation of the number. All it does is truncating the fractional part. The representation remains the same. In other words, if you do
double x = 12.345;
double y = (int)x;
it is the same as
double x = 12.345;
double y = (double)((int)x);
because in this case the compiler knows the type of variable y, and inserts the missing cast for you.
The first thing to learn about gcc is that you need to turn warnings on explicitly and make them errors using -Wall -Wextra -Werror. These are going to warn you about many useful things if you do not do them exactly right.
Including the format string and wrong argument types as you have here.
I guess the reason why these warning options are not enabled by default is that good-old-perfectly-working-K&R-code would now produce warnings and upset some venerable hackers.

Format specifier in C not clear [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
Why the output is;
6.412
What is the effect of .* here ?
The . means that the next characters indicate the precision to use. The * means to read the value from the argument list; in your case, it will read a. The value is 3, so the next argument is printed to 3 decimal places.
In printf function, the format %[flags][width][.precision][length]specifier of this question is .precision, it has two choices number or *.
When *, it means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
For more details, see http://www.cplusplus.com/reference/cstdio/printf/
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
It substiutes the value of a to the *,implying a precision.

Resources