C program won't compile despite using proper specifier? [closed] - c

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 7 years ago.
Improve this question
For reasons that I don't understand at the moment, this code will not compile despite having everything it needs in order to do the task.
#include <stdio.h> /* printf */
#include <math.h>
double resistance;
double voltage;
double current;
double wattage;
int main()
{
printf("type in resistance\n");
scanf("%f",resistance);
printf("type in current");
scanf("%f",current);
//voltage = resistance*resistance*current;
printf("%f Volts",resistance*resistance*current);
// return voltage;
}
The thing I don't understand is why won't it compile? I keep getting "wrong specifier" message when compiling. I tried both %lf and %f but neither of them works.

Change the lines
scanf("%f",resistance);
scanf("%f",current);
to
scanf("%lf", &resistance);
scanf("%lf", &current);
The %f conversion specifier expects the corresponding argument to have type float *, but both expressions resistance and current have type double. The expressions &resistance and &current have type double *, so you need to use the %lf conversion specifier (which expects arguments of type double *).

You want to assign a floating value to your double vars by using scanf. The arguments need to be pointer :
int main() {
printf("\ntype in resistance\n");
scanf("%lf",&resistance);
printf("\ntype in current");
scanf("%lf",&current);
//voltage = resistance*resistance*current;
printf("\n%f Volts",resistance*resistance*current);
// return voltage;
}

It is not the printf, it is the scanf:
scanf("%f", resistance);
This should be:
scanf("%lf", &resistance);
%lf because it is a double, %f is for floats. And &resistance because scanf is expecting a pointer to whatever, so it could write there. When you write %f, printf expects just a float, scanf a pointer to float. %lf and it's double/double*.

As per the standard of C99, there is no exclusive format specifier for float. Whether you use a float or double, it doesn't count. What matters is that when you use a float variable printf automatically promotes it to a double and displays it. So %f indicates a double or a floating-point value and %Lf is used for a long double value.
Reference: http://www.cplusplus.com/reference/cstdio/printf/
int main() {
printf("Type in Resistance: ");
scanf("%lf", &resistance);
printf("Type in Current: ");
scanf("%lf", &current);
printf("%f Volts",resistance*resistance*current);
}

Related

The function log(x) in C is always giving the same value [duplicate]

This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 1 year ago.
Basically, I am trying to find the logarithm of a number using the log(x) function in the math.h library of C. But, for any and every value that I enter for the variable x, I end up with the same answer: -722.259365. Where is the problem with my code below?
I am kind of new to C, so please don't mind if this is too silly a mistake.
#include<stdlib.h>
#include<math.h>
void PrintLogarithm(double x){
if(x<=0){
printf("Positive numbers only please.\n");
return;
}
double Result = log(x);
printf("%f\n", Result);
}
int main(){
double x;
scanf("%f", &x);
PrintLogarithm(x);
return 0;
}
I am kind of new to C, so please don't mind if this is too silly a mistake.
First of all, you are missing an include
#include <stdio.h>
Secondly, you are not using the correct format specifier. You are using doubles, so you should use %lf. If you compile with the -Wformat flag, your compiler will warn you about this by telling you something like this:
/cplayground/code.cpp:16:17: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%f", &x);
~~ ^~
%lf
If you fix these 2 problems, your program should finally work as expected.
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
void PrintLogarithm(double x) {
if(x<=0) {
printf("Positive numbers only please.\n");
return;
}
double Result = log(x);
printf("%lf\n", Result);
}
int main() {
double x;
scanf("%lf", &x);
PrintLogarithm(x);
return 0;
}
Edit : As commenters pointed out, printf() works fine with either %lf or %f as the format specifier.

I am passing float value to function square but the value getting printed is 0 [closed]

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 5 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
float square(float);
main()
{
float a,b;
printf("enter the number=");
scanf("%d",&a);
b=square(a);
printf("the square is=%d",b);
}
float square(float x)
{
float y;
y=x*x;
return(y);
}
scanf("%d",&a);
needs to be
scanf("%f",&a);
%d is meant for reading ints while %f is meant for reading floats.
If you increase the warning level on your compiler you might be able to detect that as a problem at compile time. For example, using gcc -Wall, I get the following output:
soc.c:5:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^
soc.c: In function ‘main’:
soc.c:9:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
scanf("%d",&a);
^
soc.c:11:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
printf("the square is=%d",b);
If you are working with float numbers, you have to use "%f" specifier in printf and scanf calls. Now you have "%d", but this is for integer values (you can read here about this).
So, try this:
...
scanf("%f", &a);
b=square(a);
printf("the square is=%f", b);
...
In your scanf and printf in main function use %f instead of %d
The format specifier for a float is "%f" while "%d" is used in case of an integer.
#include<stdio.h>
#include<conio.h>
float square(float);
main() {
float a,b;
printf("Enter the number=");
scanf("%f",&a);
b=square(a);
printf("The square is=%f",b);
}
float square(float x) {
float y;
y=x*x;
return(y);
}
This code should work.
Although I would suggest you use integers as the code would show decimal points with the square output.
Enter the number=2
The square is=4.000000

Can printf take a floating point variable as an argument for a %d format specifier? [duplicate]

This question already has answers here:
What happens to a float variable when %d is used in a printf?
(4 answers)
Closed 5 years ago.
So, my question is, when I try to use %d as a format specifier and print 'a', which is a floating value, I get the answer as 0. I want to know why that is so.
#include<stdio.h>
#include<conio.h>
void main()
{
float a = 3.5;
clrscr();
printf("The value of a is:%d",a);
getch();
}
The %d specifier only takes integers. To format floating point numbers use %f.
See the printf manpage on how to choose which format specifier you should use for which input.

Why does casting this double to an int give me 0? [closed]

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 7 years ago.
Improve this question
Sorry this has been confusing me for hours and nothing comes up on google about this. I'm programming in C, trying to convert decimal to binary, and I'm confused as to why this keeps giving me the right amount as input but the integer value is always 0.
even if I properly cast input as an int, it doesn't work (ie: int integer = (int) input;
main() {
double input;
scanf("%d", &input);
printf("input: %d \n", input); // this will print fine
int integer = input; // stores nums before decimal
printf("integer:%i\n", integer); // this always prints 0
Because you are using the wrong specifier to read -> scanf() or print -> printf() the double, do this
if (scanf("%lf", &input) == 1)
{
printf("input: %f\n", input);
printf("integer: %d\n", (int) input);
}
You need To Correct Format Specifiers.
main(){
double input;
scanf("%lf",&input);
printf("input: %lf \n", input);
int integer = input;
printf("integer:%i\n", integer); }
Some Format Specifiers
Or you can read from here

Incorrect output and warning message ''Makes pointer from int '' [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
I was writing a c program whose job was to convert a Celsius temperate to Fahrenheit, and vice-versa. I wanted my user to provide input in the form
double, char
Where the character would be either a 'F' for Fahrenheit to Celsius conversion, or 'C' for Celsius to Fahrenheit conversions.
I wrote this program to do it.
The Program
#include <stdio.h>
int main( )
{
char in[10];
int i;
printf("Welcome to the Tempurate Conversion Enter a ");
scanf("%s %d", i, &in);
printf(i);
printf(in);
return 0;
}
When I compile this program I get this warning:
The Warning
CtoF.c: In function 'main':
CtoF.c:8:4: warning: passing argument 1 of 'printf' makes pointer from integer w
ithout a cast [enabled by default]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include/stdio.h:294:37: note:
expected 'const char *' but argument is of type 'int'
I when ran to the program and gave this input:
The Input
3 C
and got this output:
The Output
3#
I want to know the meaning of the warning message and what I can to do to fix it.
Problem is with format specifiers in scanf and printf.
scanf("%s %d", i, &in);
should be
scanf("%d %s", &i, in);
And
printf(i);
printf(in);
should be
printf("%d", i);
printf("%s", in);
And a side note:
I wanted my user to provide input in the form double, char
For this you should have to change int i to
double i;
and conversion specifier %d to %lf in scanf:
scanf("%lf %s", &i, in);
Your printf statement is wrong.
printf expects a char array like char in[10]. However you are passing an integer. int i.
The warnings informs you that because printf expects a pointer parameter and you are passing an integer, this integer gets cast to a pointer.
So if your integer has the value of 100 it now tries to read from memory address located at 100 and keeps dumping the memory untill a \0 character/value is found
printf works in the following way:
the first argument to printf tells how many arguments are expected
printf( "%d", n );
tells that one argument is expected (an int pointer)
printf( "%d %d", n, m );
tells that there are two arguments so your syntax is wrong

Resources