How do I input more than 1 character in C?

I’ve been doing the C course here and as far as I remember did not learn how to input multiple characters in one variable. For example:

#include <stdio.h>

int main() {
       int Response;

      printf("Do you like programming? (y/n): "); 
      Response = getchar();
      // I can only input one character: y/n.
      // But I want to input a string: yes/no.
}

Understanding the above I created a pseudo code to show what I want.

START

Ask       "Do you like Programing? " and "to enter yes / no."
Input      the words: yes / no.

STOP

Understanding the above I want to ask how can I input a string(word) in a single parameter(variable)?

My above question is what I have to do in many a competitive programming contests involving strings. It would be a big help if someone could answer my qustion! :smiley:

Below is a link to one such contest problem:
https://codedrills.io/contests/zco-practice-contest-1/problems/string-changes

You could use scanf() or fgets()
More info and examples can be found here: C User Input

1 Like

Hi! I did search about fgets() it was helpful but i can’t understand how do you input a string in scanf().
You can’t also use a loop in scanf() cause loop causes scanf() to read values from different lines rather than on the same line.

But fgets() solved my problem … thanks! If someone could help me understand the scanf() part, as how can we input a string using scanf() … that would be awesome!
Anyway, thanks for the introducing the fgets() function … it was great help! :slight_smile:

It’s honestly been several years since I’ve written any C code, but I believe you could use scanf() in a loop if you’re doing so with the understanding that each iteration of the loop will collect a single input… but I may be misunderstanding what you’re trying to do with it.

scanf() is going to accept only the input you specify in the first parameter (like %d for an int, %c for a single char, %s for a string, etc) and any whitespace that follows that entry will tell the program it’s done accepting the input. If you’re wanting to accept more than one input, that’s when you’ll want to use fgets().

I’m not certain how you were trying to use scanf(), but it seems like using scanf('%s', yourStringVarName) would do the trick.

I also notice in your original code sample you’ve declared Response as an int. If you’re looking to accept “yes” or “no”, you’d need to declare it as the proper data type. I also suggest making the variable lower-case response instead of capitalizing the first letter.

Anyway, hope this helps!

Hi as I earlier mentioned the fgets works great for me and that’s a plus!! :slight_smile:
But since you are unsure about how im trying to use the scanf() I also attached some code below.

#include <stdio.h>

int main() {
       char Response[20];
       printf("Your first name: ");
       for(int i = 0; i < sizeof(Response); i++) { // I want to iterate all the characters into the arrays. 
              scanf("%d", &Response[i]);             // But it does'nt do what I expected.
              if (Response[i] == "\n") {
                     break;
              }
       }
       
}

I hope this clarifies what im trying to do with scanf.

And also about Response … there is a function in C called getchar() as the name suggests, it gets the character value from int … i used this function to short code writing where scanf() is not much needed … the loop above could also be replaced with getchar() as i did in the code of my original problem, you can find it there.

Also replacing %c in scanf() to %s does allow you to input a string like in this case your first name but its not so durable … i mean as you said it won’t accept whitespace so you can’t type in your full name with space in between the first and sir names.

Please do correct me if im wrong!:slight_smile:

Hm - in the event that you want to accept multiple words like this it probably makes more sense to use fgets(). For the case of accepting a yes/no like the original question scanf() would probably be better. But I believe you can use scanf() to accept two separate words by including that in your first argument, like scanf("%s %s", firstName, lastName).

1 Like

hmm … it worked in this case so yeah its okay!! … but in other cases fgets would be more efficient to use.

thanks very much!