The result does not happen

I did this code and my result always shows me 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double c;
double f;

int main(){

	printf("Entre com a temperatura em Celsius \n");
	scanf("%d",&c);

	f=9/5*c+32;
	

	printf("%d º celsius ",c);
	printf("em farehnheit e %d \n",f);


}


Where is the problem?
bar.c: In function ‘main’:
bar.c:7:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat=]
bar.c:12:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
bar.c:13:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
bar.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]


Also integer division returns an integer, so
9/5 = 1
Actually your code isn't used to be C++ code, but C language code.
closed account (3hM2Nwbp)
@ne555 - is the warning of not returning a value from main legit? If so, what compiler? The standard allows the return statement to be omitted.

To expand on ne555's answer, the correct specifier for a double is "%f". Even when floats are passed to printf, they are promoted to doubles and are handled under the "%f" specifier. You can refer to this page for a list of specifiers.

http://www.cplusplus.com/reference/cstdio/printf/

* Upon reading the reference for scanf again, it operates a little differently than printf. You must provide the sub-specifier "l" to scanf, so the correct specifier for your call to scanf would be "%lf".

http://www.cplusplus.com/reference/cstdio/scanf/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	// Note - these variables were moved into the main function body
	double c;
	double f;
	printf("Entre com a temperatura em Celsius \n");
	// "%lf" - length sub-specifier "l" applied to specifier "f"
	scanf("%lf",&c);
	// 9.0 and 5.0 are subject to floating point division, instead of integral division
	f = 9.0 / 5.0 * c + 32;
	// "%f" - specifier "f" handles both floats and doubles
	printf("%f º celsius ",c);
	printf("em farehnheit e %f \n",f);
}
Last edited on
@Luc Lieber
Its a GCC compiler warning, and it isn't specifically for main. Its just a warning that you have declared a function that the program expects to have something returning from (i.e. non void / non [[noreturn]]) and at least one path has the possibility of not returning anything at all.
closed account (3hM2Nwbp)
I had my suspicions. A bit odd there isn't a special case for the main function. Even the standard has a special case for the main function. Perhaps I'll file a bug about the non-compliance.

Standard wrote:

§ 3.6.1
A return statement in main has the effect of leaving the main function (destroying any objects with automatic
storage duration) and calling std::exit with the return value as the argument. If control reaches the end
of main without encountering a return statement, the effect is that of executing
return 0;
Last edited on
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
was added by C99

gcc does the right thing if we ask it to conform. c89 would give the warning; c99 and c1x won't.

The default for gcc (when -std is omitted) is ‘gnu89’ - an amorphous mixture of mostly C89, bits and pieces of C99, and a bunch of non-standard linuxisms.

http://coliru.stacked-crooked.com/a/25edc3e5f76aaca1
Topic archived. No new replies allowed.