0

// program for verifing profit or loss

#include <stdio.h>

main()
{
    int a,b,c;
    
    printf("Enter Cost Price: ");              
    scanf("%i", &a);                        
    
    printf("Enter Selling Price: ");  
    scanf("%i", &b);  
    
    c = b - a;           // out is wrong constantly 
    printf("%i ", &c);                    
 
    if(a < b)
    {
        printf("Profit");
    }
    else if(b < a)
    {
        printf("loss");
    }
    else
    {
        printf("You are Nil");
    }
}

The output is constantly same for any intput by scanf overall program works fine.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 2
    Typo: `printf("%i ", &c);` should be `printf("%i ", c);` Please enable compiler warnings. Also, use `%d` format specifer for decimal. Only use `%i` if you accept input in other number bases. – Weather Vane Aug 03 '22 at 18:09
  • superb it worked. – Abhijeet Paturkar Aug 03 '22 at 18:13
  • can you please tell why use only c insted of &c – Abhijeet Paturkar Aug 03 '22 at 18:14
  • Since you compute `c`, why not use that? `if (c < 0)` for example. – tadman Aug 03 '22 at 18:14
  • 1
    `c` is the integer value `c`, while `&c` is a *pointer to* an integer value. You need to use pointer form when saying "save value to this thing" as opposed to the immediate value. In a lot of cases in C, passing a pointer to a value means "You can save the result here if you like." – tadman Aug 03 '22 at 18:15
  • Because `printf` wants the actual value, not a pointer to the value (unlike `scanf`). – Weather Vane Aug 03 '22 at 18:15
  • 2
    Of course, if you are being taught Turbo C by a last-century teacher you won't get the warnings for `printf` that an up-to-date compiler gives. And no modern textbook will recommend `main()` which is an obsolete definition. It should be `int main(void)` – Weather Vane Aug 03 '22 at 18:21
  • Why don't you need `&` when calling `printf`? You rarely need `&` when calling anything. `scanf` is special. Don't use it as a model for anything else. See https://stackoverflow.com/questions/50074855 for some more explanations. – Steve Summit Aug 03 '22 at 18:30
  • In general, you should aim to terminate `printf()` formats with a newline character. You get away with not doing so on some systems (Windows), but on Unix, the output likely won't appear until the program terminates. That matters when you are debugging, in particular — so it is a good habit to get into. So is coding to a more recent standard than C90 — none of C99, C11, C18 allow you to write your `main()` function the way you did. – Jonathan Leffler Aug 03 '22 at 19:14

0 Answers0