-5

My Code:

#include <stdio.h>

int main()
{
    int l, b, a;
    printf("Enter the length of the rectangle: ");
    scanf("%f", &l);

    printf("Enter the breadth of the rectangle: ");
    scanf("%f", &b);

    printf("Area of rectangle is %f", l * b);
    return 0;
}

When I give any input it doesn't show me its product, but 0.000000 instead:

As I gave input 2 and 3, it should print Area of rectangle is 6

Kundan
  • 1,754
  • 16
  • 37
  • 7
    Please read the [scanf](https://www.man7.org/linux/man-pages/man3/scanf.3.html) and [printf](https://www.man7.org/linux/man-pages/man3/printf.3.html) manuals. `%f` is not for integers. The manual will tell you what it should be instead. – kaylum Sep 27 '21 at 11:25
  • You're mismatching the conversion specifier (the `"%f"`) and the variable type (type `int`). scanf wants `"%d"` for `int`s; `"%f"` for `float`s and, **better** if you want to use floating-point, `"%lf"` for `double`s – pmg Sep 27 '21 at 11:25
  • 2
    And please turn on all warnings for your compiler, it should have told you already – Mat Sep 27 '21 at 11:25
  • 1
    The `%f` format expects a pointer to `float`; you are passing a pointer to `int`. Make the format match the variable type or vice versa. – Jonathan Leffler Sep 27 '21 at 11:27
  • @Mat How to turn on warnings? – adityanithariya Sep 27 '21 at 11:27
  • Thanks Everyone for solving my problem. – adityanithariya Sep 27 '21 at 11:31
  • 2
    If you are learning C, you should avoid scanf: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – William Pursell Sep 27 '21 at 11:32
  • @Mat I had turned on Intellisense but don't know how to turn on warnings? – adityanithariya Sep 27 '21 at 11:32
  • @WilliamPursell Then what should we use instead of scanf? – adityanithariya Sep 27 '21 at 11:34
  • 1
    @AdityaSoni There are many options, and several are outlined on the page I provided. `fgetc`, `fgets`, etc. – William Pursell Sep 27 '21 at 11:35
  • @WilliamPursell Thanks for your suggestion, but would you like to give a illustration of using ```fgetc``` and ```fgets```. – adityanithariya Sep 27 '21 at 11:39
  • 1
    You'll need to read the first chapter of a beginner level C programming book before you start coding. Format specifiers to printf/scanf are typically mentioned in that first chapter. – Lundin Sep 27 '21 at 11:40
  • @Lundin I am learning ```c``` from ```codewithharry``` on youtube. He is teaching from the very beginning. – adityanithariya Sep 27 '21 at 11:42
  • 1
    Not very successfully, it would seem. Any random person can post a youtube video. But publishers almost only let experts write C books. Or as someone wisely said, "He who searches for wisdom on youtube is a-..." – Lundin Sep 27 '21 at 11:45
  • @Lundin Then which book should I read? As I learnt python and flask from him. I started learning ```c``` from him. – adityanithariya Sep 27 '21 at 11:48
  • Please anyone tell me why my question is downvoted? – adityanithariya Sep 27 '21 at 11:52
  • I don't know which books there are that's actually newbie-friendly. [Modern C/Gustedt](https://gustedt.gitlabpages.inria.fr/modern-c/) is probably the most up to date one (and free as pdf), though I suspect it might be a bit too advanced to keep up with for someone just learning the language. – Lundin Sep 27 '21 at 11:52
  • 1
    As for down votes (I didn't down vote, but close vote), likely lack of research. This kind of question with a trivial mismatch of format specifiers has already been asked thousands of times before on SO. – Lundin Sep 27 '21 at 11:53
  • Thanks @Lundin I will start learning from it. – adityanithariya Sep 27 '21 at 11:53
  • @AdityaSoni You can at least use it for reference when you need to look something up. – Lundin Sep 27 '21 at 11:54
  • I thought that I installed compiler the wrong way and searching for that. – adityanithariya Sep 27 '21 at 11:56
  • I can recommend https://www.eskimo.com/~scs/cclass/cclass.html (although I'm admittedly biased). – Steve Summit Sep 27 '21 at 12:19
  • Using `scanf` to get numbers into your programs is not the end of the world, and is fine for, say, your first three weeks of C programming. You've got a lot to learn at first, and a few other things might come first. Soon enough, though, you'll do yourself a big favor by weaning yourself off of `scanf` and abandoning it forever. See [What can I use for input conversion instead of scanf?](https://stackoverflow.com/questions/58403537) – Steve Summit Sep 27 '21 at 12:21
  • Regarding scanf, I'd just say that learning console input/output with `stdio.h` is largely obsolete practice. Console I/O is only used in niche system level programming nowadays, so don't invest a lot of time and energy into learning all the dirty details of it. Most desktop programs/engines etc have a GUI and C is just a back-end for performance-critical or hardware-related programming. So stdio.h is just one lib among many that you could learn after learning the language itself, I regard it as a quick & dirty debug library for testing, not something to use in production code. – Lundin Sep 27 '21 at 12:38

1 Answers1

4

%f expects its corresponding argument to have type float and you are passing int to it, so changing it to %d would fix the issue, as %d expects its corresponding argument to have type int.

#include <stdio.h>
 
int main() {
   int length, breadth, area;
 
   printf("\nEnter the Length of Rectangle: ");
   scanf("%d", &length);
 
   printf("\nEnter the Breadth of Rectangle: ");
   scanf("%d", &breadth);
 
   area = length * breadth;
   printf("\nArea of Rectangle: %d\n", area);
 
   return 0;
}
Kundan
  • 1,754
  • 16
  • 37
  • ```getch()``` is for? Actually, I started learning today and stuck in error. – adityanithariya Sep 27 '21 at 11:30
  • 2
    This code exhibits undefined behavior on certain inputs. Code provided as an example for beginners should not do this. You *must* check the value returned by scanf. Always. – William Pursell Sep 27 '21 at 11:31
  • Actually in old turbo c compilers the output screen won't stay so getch() method pauses the Output Console until a key is pressed – Kundan Sep 27 '21 at 11:32
  • @Kundan Yes, if I run file outside the vscode it closes as I it reaches EOF – adityanithariya Sep 27 '21 at 11:38
  • @WilliamPursell Understood your point, but I was fixing the issue which was there in the code which was mentioned in the question. – Kundan Sep 27 '21 at 11:38
  • @AdityaSoni I hope you understood the points and the answer helped you. – Kundan Sep 27 '21 at 11:42
  • @AdityaSoni Forget that you ever saw conio.h and getch. It's from an ancient non-standard library used for MS DOS programming that everyone in the world except a certain large country in central Asia stopped using 20 years ago. – Lundin Sep 27 '21 at 11:42
  • @Lundin Thanks for your suggestions. As I use VS Code, I never need to pause file at last. – adityanithariya Sep 27 '21 at 11:45
  • @AdityaSoni yeah, while using new compilers, you don't have to use that. – Kundan Sep 27 '21 at 11:46