Home > Enterprise >  I was making a simple C program and got an error [duplicate]
I was making a simple C program and got an error [duplicate]

Time:09-21

I get this erorr message -

warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]

I looked it up and tried to fix it, with no avail. Below is my code. Could someone tell me what I'm doing wrong?

#include <stdio.h>
#include <conio.h>

void main() {
    int num;
    int amount;
    printf("\n 0. New Customer");
    printf("\n 1. Existing Customer");
    printf("\n Please enter your preference according to the numbers given above");
    scanf("%d", & num);

    printf("\n Enter your total bill amount");
    scanf("%d", & amount);
    if (amount > 10000) {
      printf("\nYou have got a 10% discount !");
    } else {
      printf("\n No discount");
    }
}

CodePudding user response:

As noted in the comments, % is a special character in C printf format specifiers. There are several, and working with C, you'll want to know them. Fortunately there are references, including this one.

The one you want in this case is %% which simply prints a % character.

CodePudding user response:

need to add %s so your program will be


#include <stdio.h>
#include <conio.h>
void main()
{
int num;
int amount;
printf("\n 0. New Customer");
printf("\n 1. Existing Customer");
printf("\n Please enter your preference according to the numbers given above");
scanf("%d", &num);

printf("\n Enter your total bill amount");
scanf("%d", &amount);
if (amount>10000)
{
    printf("%s","\nYou have got a 10% discount !");
}
else 
{
    printf("\n No discount");
}
}

CodePudding user response:

you can do this: printf("%s","\nYou have got a 10% discount !"); insted of doing this: printf("\nYou have got a 10% discount !");

because in printf % is a "spacial" cherecter.

CodePudding user response:

Try to add this line in the beggining:

#include <stdio.h>

All scanf examples on the web use only this lib to run scanf. You don't need conio.h for this one.

  •  Tags:  
  • c
  • Related