Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
957 views
in Technique[技术] by (71.8m points)

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 
");
    
    fflush(stdin);
    scanf("%c", joueur.nom_joueur);
    
    printf("Choisir le prenom de votre joueur 
");
    
    fflush(stdin);
    scanf("%c", joueur.prenom_joueur);
    
    printf("Choisir un numero unique pour votre joueur 
");
    
    fflush(stdin);
    scanf("%u", &joueur.numero_joueur);
    
    printf("Choisir le nom de votre personnage
");
    
    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);
}
question from:https://stackoverflow.com/questions/65602773/warning-format-c-expects-argument-of-type-int-but-argument-2-has-type-cha

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %[^ ] 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 
");
    scanf(" %29[^
]", joueur.nom_joueur);
    
    printf("Choisir le prenom de votre joueur 
");
    scanf(" %19[^
]", joueur.prenom_joueur);
    
    printf("Choisir un numero unique pour votre joueur 
");
    scanf("%u", &joueur.numero_joueur);
    
    printf("Choisir le nom de votre personnage
");
    scanf(" %49[^
]", joueur.personnage);
    
    printf("Vous etes %s %s, votre numero est le %u et votre personnage est %s
", 
           joueur.prenom_joueur, joueur.nom_joueur, joueur.numero_joueur,
           joueur.personnage);
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...