Simple C code keeps crashing -


so code did:

#include <stdio.h> #include <stdlib.h>  int main() { char playername; int playerage;  printf("what's name, , age?\nyour name: "); scanf("%s\n", playername); printf("your age: "); scanf("%d\n", &playerage); printf("okay %s, %d years old!", playername, playerage);  return 0; } 

and everytime run it, after input name crashes , don't know how fix it. these 3 things appear when closes:

format '%s' expects argument of type 'char *', argument 2 has type 'int' [-wformat]|  format '%s' expects argument of type 'char *', argument 2 has type 'int' [-wformat]|  'playername' used uninitialized in function [-wuninitialized]| 

what mistake?

scanf("%s\n", playername); wrong because %s call char* data playername here type char.

you have make playername array of characters , set max length of input avoid buffer overflow.

#include <stdio.h> #include <stdlib.h>  int main(void) {     char playername[1024];     int playerage;      printf("what's name, , age?\nyour name: ");     scanf("%1023s\n", playername); /* max length = # of elements - 1 terminating null character */     printf("your age: ");     scanf("%d\n", &playerage);     printf("okay %s, %d years old!", playername, playerage);      return 0; } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -