-2

So I am still training with C and noticed an unusal result while practicing functions.

#include <stdio.h>
#include <math.h>
 void main()
 {


 printf("Input any number for square : ");
  double X;
  scanf("%f", &X);
  double square(double X);
  double n=square(X);
  printf("The square of %f %f:", X, n);
}
double square(double X)
{
return (pow(X,2));
}

Here's the output:

Input any number for square : 21 The square of 0.000000 0.000000:

So, I am not understanding why is it returning zeros while the compilation is totally fine and the semantic looks coherent. I'll appreciate it if you don't go in depth because I think it can be explained simply (I'm still quite new ^^' )

Ghassen
  • 1
  • 3
  • 4
    No images please. Post the code/output as text. – Eugene Sh. Feb 11 '21 at 21:39
  • 1
    Use `%lf` instead of `%d`. – alex01011 Feb 11 '21 at 21:41
  • @EugeneSh. isn't the hypertext working ? – Ghassen Feb 11 '21 at 21:42
  • 1
    @Ghassen: That's not the point. [Images of code are not permitted](https://meta.stackoverflow.com/q/285551/10077). – Fred Larson Feb 11 '21 at 21:43
  • @alex01011 I replaced the %d in scanf by %f. Still the same output – Ghassen Feb 11 '21 at 21:43
  • @EugeneSh. thanks for clarification I'll change it – Ghassen Feb 11 '21 at 21:44
  • 1
    For `double` you must use `%lf`, not `%f`. – isrnick Feb 11 '21 at 21:47
  • Why is the function prototype for `square()` placed inside `main()`? – ad absurdum Feb 11 '21 at 21:48
  • @isrnick I see now thanks for explaining – Ghassen Feb 11 '21 at 21:50
  • @adabsurdum because the definition is under main() – Ghassen Feb 11 '21 at 21:50
  • @Ghassen the prototype by convention typically goes before main(), in the global scope (not inside any other function). – isrnick Feb 11 '21 at 21:53
  • ⟼This code could benefit greatly by adopting an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. – tadman Feb 11 '21 at 21:54
  • @isrnick thanks for the info too.. It all started by asking myself what if I placed the defiintion under the main function – Ghassen Feb 11 '21 at 21:55
  • 2
    Turn up your compile warnings and your compiler will likely tell you about the format specifier error, as well as [`void main()`](https://stackoverflow.com/q/9356510/10077). – Fred Larson Feb 11 '21 at 21:55
  • @Ghassen -- usually function prototypes come after the `#include`s; [see this](https://stackoverflow.com/questions/940402/function-prototype-declared-inside-main-best-practice) – ad absurdum Feb 11 '21 at 21:55
  • @tadman isn't it indented ? – Ghassen Feb 11 '21 at 21:57
  • @FredLarson Well, there is no error. the syntax is correct so the compiler doesn't find any problems. It's the output that was unexpected but thankfully I got my answer – Ghassen Feb 11 '21 at 21:57
  • It's honestly just jumbled up, it's not indented in any way that's recognizable or consistent. Stuff is just thrown about. The first `printf` isn't aligned with the rest of the code, and your `square()` function body isn't indented at all. `void main()` is indented for no reason. It's just chaos. I linked to the indentation style page as there's a number of popular styles to choose from, they all have their merits, but it's important to embrace one and use it *consistently*. – tadman Feb 11 '21 at 21:58
  • 1
    @Ghassen it is very badly indented. You can use an automatic formatter such as https://www.tutorialspoint.com/online_c_formatter.htm if you don't want or don't know how to do it properly yet. – isrnick Feb 11 '21 at 22:00
  • 1
    @isrnick I see how important this is for the readers. I will use that website from now on and consider the indentation's importance more – Ghassen Feb 11 '21 at 22:04
  • @Ghassen Good. Also compile with `-Wall -Wextra` and pay attention to warnings. – klutt Feb 11 '21 at 22:18

1 Answers1

0

When I try and compile this code I get warnings:

square.c:3:2: warning: return type of 'main' is not 'int' [-Wmain-return-type]
 void main()
 ^
square.c:3:2: note: change return type to 'int'
 void main()
 ^~~~
 int
square.c:9:15: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
  scanf("%f", &X);
         ~~   ^~
         %lf
2 warnings generated.

These are both very relevant to the problem. These are generated with clang using -Wall, but GCC and other compilers have similar methods. clang is very helpful, specific and very good about explaining potential fixes.

Correcting those issues and cleaning up the code results in this:

#include <stdio.h>
#include <math.h>

// Declare functions before they are used whenever possible
double square(double X)
{
  // No reason for the extra brackets, just return ...
  return pow(X,2);
}

// main() is supposed to at least return int
int main()
{
  printf("Input any number for square : ");
  double x; // Variables typically lower-case, #define macros are upper-case
  scanf("%lf", &x);

  double n = square(x);

  printf("The square of %lf %lf:", x, n);

  // Formally indicate everything's good (no error = 0)
  return 0;
}

Where now everything works out as expected.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • thank you so much for the detailed explanation. Looks like I'll stop using my Atom extension. Just a brief question, what's the difference between void main and int main ? Theoratically, I found it working and also my Atom executed it normally after changing the %f to %lf (while keeping void main) – Ghassen Feb 11 '21 at 22:08
  • 1
    @Ghassen the main() function must return an int value, it returns a number to your system, and by convention returning 0 indicates that the program exited correctly without any errors (and a non-zero value indicates that something anomalous occurred, such as an error, and that the program was not finished as originally intended). And void main() is simply wrong despite working. https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – isrnick Feb 11 '21 at 22:17
  • If you're committed to learning C the good news is the language itself isn't all that complex, but it does have important nuances and caveats as it depends on the programmer paying attention to what they're doing. Just like a car will drive off a cliff if you're not operating it correctly, C will do all kinds of things that will horribly crash the program if you inadvertently ask it to. As such, pay close attention to any and all compiler warnings, or even engage the flag that says "treat warnings as errors" if that's available. – tadman Feb 11 '21 at 22:31