Search code examples
cformatwarnings

warning :format 'c' expects argument of type 'int', but argument 2 has type 'char '


I'm new on stack overflow and I need some help, excuse me but I don't understand where is the problem, someone can help me?

I have the message:

warning: format 'c' expects argument of type 'int', but argument 2 has type 'char '

and I don't know how to resolve it.

Code:

typedef struct Ajouter_un_joueur Joueurdef;
struct Ajouter_un_joueur {
    char nom_joueur[30];
    char prenom_joueur[20];
    unsigned int numero_joueur;
    char personnage[50];
};

int main() {
    Joueurdef joueur;
    
    printf("Choisir le nom de votre joueur \n");
    
    fflush(stdin);
    scanf("%c", joueur.nom_joueur);
    
    printf("Choisir le prenom de votre joueur \n");
    
    fflush(stdin);
    scanf("%c", joueur.prenom_joueur);
    
    printf("Choisir un numero unique pour votre joueur \n");
    
    fflush(stdin);
    scanf("%u", &joueur.numero_joueur);
    
    printf("Choisir le nom de votre personnage\n");
    
    fflush(stdin);
    scanf("%c", joueur.personnage);
    
    printf("Vous etes %c %c, votre numero est le %u et votre personnage est %c", 
           joueur.prenom_joueur, joueur.nom_joueur, joueur.numero_joueur,
           joueur.personnage);
}

Solution

  • There are multiple problems in your code:

    • fflush(stdin); is not a proper way to get rid of pending input. It actually has undefined behavior.

    • %c in a scanf() conversion specification expects to read a single character, not a string. If the input for nom, prenom and personnage are single words, you can use %s instead of %c. If white space is allowed, use %[^\n] to read characters until the end of line. An initial space in the format string will skip the pending newline and an initial whitespace.

    • to output a string in printf, use %s instead of %c. The error message is a bit misleading and probably has a star * at the end: warning: format 'c' expects argument of type 'int', but argument 2 has type 'char *'

    Here is a modified version:

    #include <stdio.h>
    
    typedef struct Ajouter_un_joueur Joueurdef;
    struct Ajouter_un_joueur {
        char nom_joueur[30];
        char prenom_joueur[20];
        unsigned int numero_joueur;
        char personnage[50];
    };
    
    int main() {
        Joueurdef joueur = { 0 };
        
        printf("Choisir le nom de votre joueur \n");
        scanf(" %29[^\n]", joueur.nom_joueur);
        
        printf("Choisir le prenom de votre joueur \n");
        scanf(" %19[^\n]", joueur.prenom_joueur);
        
        printf("Choisir un numero unique pour votre joueur \n");
        scanf("%u", &joueur.numero_joueur);
        
        printf("Choisir le nom de votre personnage\n");
        scanf(" %49[^\n]", joueur.personnage);
        
        printf("Vous etes %s %s, votre numero est le %u et votre personnage est %s\n", 
               joueur.prenom_joueur, joueur.nom_joueur, joueur.numero_joueur,
               joueur.personnage);
        return 0;
    }