Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need help I keep getting this error I am new to c

I keep getting the same error when I try to compile it

#include <stdio.h>

int main(){

    char name[25];
    int age;

    printf("\nwhat is your name: ");
    scanf("%s", &name);

    printf("what is your age: ");
    scanf("%d", &age);

    printf("\nyour name is %s", name);
    printf("\nand your age is %d", age);

    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I assume this is the "error" you are receiving:

main.c: In function 'main':
main.c:9:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[25]' [-Wformat=]
 scanf("%s", &name);

You should try to read and understand it, as compiler warnings usually are quite clear. This one tells you that the %s in the line scanf("%s", &name); expects a char *, that is, a pointer to a char, whereas you have supplied a char (*)[25] which is a pointer to an array of 25 chars.

Basically, the argument &name has the wrong type. The name of the array, in this case the name is a pointer to the first element of the array, and you should not put an ampersand (&) (the address of …) operator when giving it as an argument to scanf.

The correct call would be: scanf("%s", name);.

I also strongly suggest you to avoid using scanf as a user-interface function. See this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading