Cant understand why I'm getting this warning - c

double S = 0;
double *pSum = &S;
double P = 0;
double *pAverage = &P;
printf("The average and sum of the variables: %lf %lf", &S, &P);
I'm working with pointers and functions but for some reason I can't understand why I'm getting 2 warning specifically
non-float passed as argument 3 when float is required in call to printf actual type: double *.
non-float passed as argument 2 when float is required in call to printf actual type: double *.
To be completely honest I don't know what to try to get rid of the warning.

It seems you have mixed arguments for scanf (where you need to pass pointers to the variables) and printf (which want values, not pointers).
The correct solution is to not use the pointer-to operator &:
printf("The average and sum of the variables: %lf %lf", S, P);

You can use
printf("The average and sum of the variables: %lf %lf", S, P);
to get the values from the varriables directly or to use:
printf("The average and sum of the variables: %lf %lf", *pSum, *pAverage);
to get the value from the pointers= * before a pointer means "Give me the value of that pointer"

Related

How many ways are there to printf() and scanf() a float* variable?

I'm preparing for my graduation test and I have some questions regarding the c programming language. I will combine two of these questions which I have answers for, but I don't really understand them. The first question is select the correct expression that outputs the value of the i-th element of the array and these are the possible answers:
(note that the code for both questions is the same)
printf("%f",B[i]);
printf("%f",&B[i]);
printf("%f",B+i);
printf("%p",*(B+i));
printf("%f",*(B+i));
The second question is select the correct expression that inputs the value of the i-th element of the array
scanf("%f", B[i]);
scanf("%f", B+i);
scanf("%p", B+i);
scanf("%f", &B[i]);
scanf("%f", *(B+i));
I tried to compile and run a program that simulates these questions. The scanf part works for the answers 2-5 and all of the printfs print out a zero except the 4th one (of course after input).
float *B;
int n;
B=calloc(n,sizeof(float));
Your understanding of which answers are correct are flawed.
To print a value of type float using printf, you need to use the %f format specifier and pass a value of type float. To read a value of type float with scanf, you also use the %f format specifier but pass a value of type float *.
Regarding array indexing, the notation A[N] is exactly equivalent to *(A + N) and has the type of the array element, and &A[N] is exactly equivalent to A + N and has type pointer to array element.
Based on that, we have the following:
printf("%f",B[i]); // Correct
printf("%f",&B[i]); // Incorrect, passing a float *, expected float
printf("%f",B+i); // Incorrect, passing a float *, expected float (same as prior)
printf("%p",*(B+i)); // Incorrect, %p is used to print a pointer (expects a void *)
printf("%f",*(B+i)); // Correct
scanf("%f", B[i]); // Incorrect, passing a float, expected float *
scanf("%f", B+i); // Correct
scanf("%p", B+i); // Incorrect, %p is used to read a pointer (expects a void *)
scanf("%f", &B[i]); // Correct
scanf("%f", *(B+i)); // Incorrect, passing a float, expected a float *
float
There are 2 ways to printf a float and 1 way to scanf them.
printf("%f", my_float);
printf("%lf", my_float); // works fine but bad style
scanf("%f", &my_float);
(In the oldest C standard, printf didn't allow %lf)
float*
There is one way to printf the pointer itself:
printf("%p", ptr);
It doesn't make sense to read a pointer with scanf, but you can read to the memory pointed-at by the pointer ptr as:
scanf("%f", ptr); // no & needed, it is already a pointer
Everything else from your examples doesn't make much sense. Printing an address is not printing a float. Applying various forms of arithmetic on the pointer such as &B[i] versus *(B+i) have nothing whatsoever to do with the the printing.

Why is my C program to do simple depreciation not working?

I am trying to write a program in C to calculate a salvage value after depreciation of something. However, in when I run this:
int main(int argc, char* argv){
printf("enter the purchase price, years of service, annual depreciation:\n");
double purchasePrice, yearsOfService, annualDep;
scanf("%d %d %d", &purchasePrice, &yearsOfService, &annualDep);
printf("purchase price is %d dollars\n", purchasePrice);
printf("years of service is %d years\n", yearsOfService);
printf("annual depreciation is %d dollars\n", annualDep);
double salvageValue = purchasePrice - (yearsOfService * annualDep);
printf("The salvage value of the item is %d", salvageValue);
return 0;
}
it prints out the purchasePrice instead of what the salvageValue should be. What is going on?
Replace your scanf call with:
scanf("%lf %lf %lf", &purchasePrice, &yearsOfService, &annualDep);
scanf requires that all passed pointers match the types of the conversion specifiers (and vice versa). You're passing pointers to doubles, and the correct conversion specifier is %lf.
From the scanf manpage:
l indicates either that the conversion will be one of d, i, o, u, x, X, or n and the next pointer is a pointer to a long int or unsigned long int (rather than int), or that the conversion will be one of e, f, or g and the next pointer is a pointer to double (rather than float). Specifying two l characters is equivalent to L. If used with %c or %s, the corresponding parameter is considered as a pointer to a wide character or wide-character string respectively.
(emphasis mine)

Double, Integer, Long double

I apologies in advance if there is a stupid error in this code, but I can't seem to trouble shoot it. My problem is this, I compile with GCC-8 (installed on Mac via home-brew), then execute in the terminal. When using int do define variables s & a, I get zeros as output using the print statements below. If I declare s & a variables as double I still get zeros for the first two print statements, and 1024 for the last print statement. I'm just lost as to what is going on. Appreciate any help!
/* square code */
#include <stdio.h>
int main() {
int s, a;
printf("enter the length of your square \n");
scanf("%f", &s);
a= s * s;
printf("the area of your square is %f cm using f placeholder \n", a);
printf("the area of your square is %lf cm uning fl placeholder\n", a);
printf("the area of your square is %d cm using d placeholder \n", a);
return(0);
}
int s;
scanf("%f", &s);
If s is an int then you need to scan it in like an int using "%d" instead of "%f". What you are doing is undefined behavior, both in scanf() and printf(). An integer needs to be scanned and printed as an integer.
I think the misunderstanding is that you treat the conversion specifiers %d and %f as defining only the output formatting, regardless of the type of value passed.
Actually each conversion specifier is tightly coupled with a particular type of argument it will accept; For example, %d is defined for integral types only, and all other types like floating point or string used together with %d will yield undefined behaviour (cf, for example, cpp-reference of printf for a more detailed explanation).
So double f=1.2;printf("%d",f) will not format a floating point value 1.2 as integral (someone might expect 1 being the output); It rather yields undefined behaviour, having any output (even no output), a crash, a... whatever.
Same applies to scanf-format specifiers, of course.
So if data type is int, use %d; if data type is double, use %f.
In your code you use s as an int, and when you use scanf you are using %f instead of %d. That's the reason why you are getting the first two outputs as a zero.
As for the third output, you will get the expected value, because in that case you are correctly using %d. See the code below:
int s;
printf("enter the value");
scanf("%d", &s);
printf("the area of square is=", s*s);
Hey guys thanks so much for the help. Before checking back this morning I tried one more thing, and alas it was a stupid error.
When declaring "double s,a", I realized my scanf() function was using scanf("%f",s) as opposed to the correct scanf("%lf", s)".
Also thanks for the help for use of %d when using "int s,a".

Scanf not reading in double

I am trying to read in a double value continuously from the user using scanf.
Code:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
The output is
INPUT: 0.000
You lied to the compiler: when scanning, %f says you supply a pointer to float. But you provided a pointer to double.
To fix, either use %lf or declare input as float.
Note that there is an asymmetry with printf formats, which uses %f for both float and double arguments. This works because printf arguments are promoted to double (and are not pointers).
I am trying to read in a double value continuously from the user using scanf.
To do so you need a loop, like the following:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
Note that, as the primitive type of input is double, you need to use %lf instead of %f (%fused for float).

Why is my C program printing 0.000000 here?

I just started to learn C programming.
In my book there is this piece of code:
/*Code Start*/
/*This code is use to find the simple interest*/
main ()
{
int p, n;
float r, si;
p = 1000;
n = 3;
r = 8.5;
si= p*n*r/100;
printf("%f", si);
}
/*Code end*/
The output i got was " 255.000000 "
I though i'll modify it with scanf function so i wrote this:
/*Code Start*/
main ()
{
int p, n;
float r, si;
printf("Enter value for p: \n");
scanf("%d", &p);
printf("Enter value for n: \n\n");
scanf("%d", &n);
printf("Enter valuse for r: \n\n");
scanf("%d", &r);
si= p*n*r/100;
printf("\nYour Simple Interest is %f\n\n", si);
}
/*Code End*/
No matter what values i give to p,n,r the answer i get is always 0.000000..
I also tried giving the values, p=1000, n=3, r=8.5 but still i get 0.000000..
Change the specifier in scanf. You're using %d instead of %f:
scanf("%f", &r);
^
First side note: the code looks kind of bad (no return type for main ?!). Are you sure it's a good book ?
Second side note: using floats today is kind of pointless. Maybe you
should use doubles ?
Firstly, your main problem: The %d specifier is only for integers, not floats or doubles. Use %f for floats.
In addition, the main should return an int, this will do:
int main() {
/* your code */
return 0;
}
Finally, I would recommend you make better use of white-space as it will vastly help with readability once you start making larger programs.
Use %f conversion specification to read a float:
scanf("%f", &r);
%d means it reads a decimal integer and not a float.
r is a float, but you're reading it in using %d as a scanf specifier, which expects an int.
The real culprit in your code is the line scanf("**%d**", &r).
%d is the format specifier for integer value, as you declared r as float then use %f instead of %d.
i.e. scanf("%f", &r)
Change Either
int p, n;
float r, si;
to
int p, n,r;
float si;
or change formate specifier in scanf("%d", &r); %d to %f.
when you declare r as an integer r=8 will be considered, in that case scanf("%d", &r); will be accepted. and your program get compiled and executed.
both declaration and formate specifier should be same.
my suggestion is to use %.2f when dealing with money. which will give like 10.00 which is the correct formate.

Resources