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.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Alright, so basically recently I started studying C as a hobby, and I wanted to create a small program.Everything works fine, but when I make the variable "Age" like this:
int myAge = "14";
printf("My age is%d\n", myAge);
It prints out 1642.
I have tried switching to
printf('My age is%i\n', age);
But it did the same.
I also tested changing the number to a string but it obviously failed, because this isn't Python.
Anybody can help?
Save time, enable all compiler warnings.
Perhaps receive a warning like:
warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
int myAge = "14"; sets myAge to the address of the string literal "14".
Instead, initialize with an int.
int myAge = 14;
This is a fixed version of your program. Just declaring the age variable as an integer is enough to fix it.
#include <stdio.h>
int main() {
int age = 14;
printf("My age is %d", age);
}
Friendly note:
C is not like Python. Arrays don't work the same, strings don't exist, and programming in C is fundamentally different in every way. I would advise that you definitely take a course rather than trying to teach yourself from scratch.
u should write int myAge = 14 , writting 14 in double quotes means its a string (thx HAL)
We use double quotes when we have string and single quote when we have character but 14 is an integer we should write int myAge=14; or if u want "14" u should write char myAge[2]="14"; then printf("%s",myAge);
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.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I don't know why cannot compile simple project below by dev.
error: Project1.exe has stopped working.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n,d;
while(1){
printf("enter number");
scanf("%d",n);
d=n%10;
while(d!=0){
n=n/10;
printf("%d",d);
d=n%10;
}
}
return 0;
}
For one, your project does compile, since you get a run time error.
The run time error occurs because you are not using scanf correctly. The arguments to scanf after the format string should be pointers to the variables.
I don't know which compiler you are using, but any fairly modern compiler would have given you compiler warnings about this, e.g. here's Clang's output:
apa.c:9:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",n);
~~ ^
Changing this into scanf("%d",&n); makes your program work.
https://linux.die.net/man/3/scanf
First, the error that you got is not a compile, it's a run time error.
Second, it's happen when the run arrive to the line of scanf. This function required the variavbles address to put the value in.
Replace this:
scanf("%d", n);
With this:
scanf("%d", &n);
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.