How to get a string in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to get a string in C

I want to know how to get a string in C, I'm using fgets to get a string, but I have one problem, when I use printf to display the string it doesn't display anything, but when I use puts(), I can display the string, why? how can I use printf() and display the string when I used gets() char name[50]; printf("enter your name"); fgets(name, 50, stdin); printf("%c", name); // it doesn't display the name puts(name); // now you can see the name

21st Aug 2018, 5:21 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
4 Answers
+ 3
Perhaps try: printf("%s", name); The "%s" is used for strings.
21st Aug 2018, 5:57 PM
Ipang
+ 2
You're welcome, glad if it helps : )
22nd Aug 2018, 3:49 PM
Ipang
+ 2
%s - string
22nd Aug 2018, 3:52 PM
Kunal Kumar
Kunal Kumar - avatar
+ 1
thank you so much! it works!
22nd Aug 2018, 3:47 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar