IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Error: format specifies type ‘char’ but the argument has type ‘string’
Tagged: char type, correct format code, get_string(), return value, scanf, storing return value, string type
- This topic is empty.
-
AuthorPosts
-
July 26, 2021 at 8:24 am #87
This is the code written:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int main(void) { string name = get_string ("what is your name\n"); scanf("%s", &name); printf("%s",name); }
[/dm_code_snippet]
On typing help50 make hello command, I get this suggestion:
format specifies type 'char *' but the argument has type 'string *' (aka 'char **') [-Werror,-Wformat] scanf("%s", &name); ~~ ^~~~~
Be sure to use the correct format code (e.g., %i for integers, %f for floating-point values, %s for strings, etc.) in your format string on line 6 of hello.c. ~/pset1/hello/ $
Do I need to remove string type and instead use char type. If so, why?
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int main (void) { int number; scanf ("enter a number %i", &number); printf ("the number is %i", number); }
[/dm_code_snippet]
Here is the result as I enter 44:
~/pset1/ $ ./Mario 44 the number is 0~/pset1/ $
<
p style=”text-align: center;”>
Reply
https://cs50.stackexchange.com/questions/41713/error-format-specifies-type-char-but-the-argument-has-type-string[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.