While you are giving like this, It will not ignore the white spaces.

scanf("%c",&ch);

When you are giving the input to the first scanf then you will give the enter('\n'). It is one character so it will take that as input to the second scanf. So second input will not get input from the user.

scanf(" %c",&ch);

If you give like this, then it will ignore that white space character, then it will ask for the input from the user.

Answer from Karthikeyan.R.S on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_user_input.php
C User Input
Read input typed by the user in C with the scanf() function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-input-or-read-a-character-word-and-a-sentence-from-user-in-c
How to input or read a Character, Word and a Sentence from user in C? - GeeksforGeeks
July 23, 2025 - Problem Statement#2: Write a C program to read words as input from the user. ... First, initialize the char array of size ( greater than are equal to the length of word). Then, use %s format specifier to take the string using the scanf() function.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-input-output
C Input/Output: printf() and scanf()
April 27, 2022 - #include <stdio.h> int main() { char chr = 'a'; printf("character = %c", chr); return 0; } ... To print char, we use %c format specifier. In C programming, scanf() is one of the commonly used function to take input from the user.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ getchar() function in c
Getchar() function in C - Scaler Topics
April 16, 2024 - The getchar in C is a non-standard function whose definition is already defined in the stdio.h header file and it is used to take the input of a single character(unsigned char) from the standard input(stdin).
๐ŸŒ
CodeChef
codechef.com โ€บ blogs โ€บ c-user-input
How to accept User Inputs in C (Examples and Practice)
August 6, 2024 - Learn how to take user input in C with this beginner-friendly guide. Discover string and integer inputs, handling multiple inputs, and practical examples to enhance your C programming skills
๐ŸŒ
EasyExamNotes
easyexamnotes.com โ€บ c-character-input
C Character input โ€“ EasyExamNotes.com
The scanf() function with the %c format specifier is used to read a character from the userโ€™s input. The & operator is used to pass the memory address of the input variable so that the value can be stored there. The printf() function displays the message โ€œYou entered: โ€ followed by the ...
Top answer
1 of 11
309

The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.

One way around the problem is to put a blank space before the conversion specifier in the format string:

scanf(" %c", &c);

The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.

2 of 11
39

First of all, avoid scanf(). Using it is not worth the pain.

See: Why does everyone say not to use scanf? What should I use instead?

Using a whitespace character in scanf() would ignore any number of whitespace characters left in the input stream, what if you need to read more inputs? Consider:

#include <stdio.h>

int main(void)
{
   char ch1, ch2;

   scanf("%c", &ch1);  /* Leaves the newline in the input */
   scanf(" %c", &ch2); /* The leading whitespace ensures it's the
                          previous newline is ignored */
   printf("ch1: %c, ch2: %c\n", ch1, ch2);

   /* All good so far */

   char ch3;
   scanf("%c", &ch3); /* Doesn't read input due to the same problem */
   printf("ch3: %c\n", ch3);

   return 0;
}

While the 3rd scanf() can be fixed in the same way using a leading whitespace, it's not always going to that simple as above. Another major problem is, scanf() will not discard any input in the input stream if it doesn't match the format. For example, if you input abc for an int such as: scanf("%d", &int_var); then abc will have to read and discarded. Consider:

#include <stdio.h>

int main(void)
{
    int i;

    while(1) {
        if (scanf("%d", &i) != 1) { /* Input "abc" */
            printf("Invalid input. Try again\n");
        } else {
            break;
        }
    }

    printf("Int read: %d\n", i);
    return 0;
}

Another common problem is mixing scanf() and fgets(). Consider:

#include <stdio.h>

int main(void)
{
    int age;
    char name[256];
    printf("Input your age:");
    scanf("%d", &age); /* Input 10 */
    printf("Input your full name [firstname lastname]");
    fgets(name, sizeof name, stdin); /* Doesn't read! */
    return 0;
}

The call to fgets() doesn't wait for input because the newline left by the previous scanf() call is read and fgets() terminates input reading when it encounters a newline.

There are many other similar problems associated with scanf(). That's why it's generally recommended to avoid it.

So, what's the alternative? Use fgets() function instead in the following fashion to read a single character:

#include <stdio.h>

int main(void)
{
    char line[256];
    char ch;

    if (fgets(line, sizeof line, stdin) == NULL) {
        printf("Input error.\n");
        exit(1);
    }

    ch = line[0];
    printf("Character read: %c\n", ch);
    return 0;
}

One detail to be aware of when using fgets() will read in the newline character if there's enough room in the inut buffer. If it's not desirable then you can remove it:

char line[256];

if (fgets(line, sizeof line, stdin) == NULL) {
    printf("Input error.\n");
    exit(1);
}

line[strcpsn(line, "\n")] = 0; /* removes the trailing newline, if present */
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ basic-input-and-output-in-c
Basic Input and Output in C - GeeksforGeeks
Functions such as scanf(), fgets(), and getchar() allow programs to read different types of input, including numbers, characters, and strings. Supports multiple data types such as integers, characters, and strings.
Published: June 20, 2026
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ strings input and output functions in c
Strings Input and Output functions in C
November 16, 2023 - Let us look at an example to understand the working of scanf() in C. ... The scanf() function is probably the easiest way to string input in C. We just need the access specifier %s and the pointer to the variable where we want the string to be stored, and it becomes very easy to input the string ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_input_output.htm
Input and Output Functions in C
The scanf() function reads the input from the standard input stream stdin and scans that input according to the format provided โˆ’ ... The printf() function writes the output to the standard output stream stdout and produces the output according ...
๐ŸŒ
CodeScracker
codescracker.com โ€บ c โ€บ program โ€บ c-program-receive-input.htm
C program to get input from the user
Simply use the format specifier and the variable that contains the value. Whereas, while scanning the input, the "address of" operator is used to put or store the entered value at the address of a given variable, say num. Now let's create another program that receives character-type input data ...
๐ŸŒ
HackerRank
hackerrank.com โ€บ challenges โ€บ playing-with-characters โ€บ problem
Playing With Characters | HackerRank
To take a single character as input, you can use scanf("%c", &ch ); and printf("%c", ch) writes a character specified by the argument char to stdout ... This piece of code prints the character .
๐ŸŒ
Medium
medium.com โ€บ @arashtad โ€บ user-input-scanf-and-getchar-in-c-12710a7eca89
User Input scanf and getchar in C | by Arashtad | Medium
August 15, 2025 - Here, %d tells scanf() to read an integer, and &age passes the address of the variable to store the input. getchar() reads a single character from the user. Unlike scanf(), it is more low-level and does not require format specifiers.
๐ŸŒ
CodePractice
codepractice.in โ€บ home โ€บ programming language โ€บ c user input
C User Input โ€“ Learn How to Read Data from Users in C
August 3, 2026 - The scanf() function is the most widely used function in C for reading formatted input. It allows the programmer to specify the type of data expected, such as integers, floating-point numbers, characters, or strings.
๐ŸŒ
Unibo
cs.unibo.it โ€บ ~renzo โ€บ doc โ€บ C โ€บ corso1 โ€บ introduttivo โ€บ sx6b.html
6.2 Character Input and Output
A companion function is putchar, which writes one character to the ``standard output.'' (The standard output is, again not surprisingly, usually the user's screen, although it, too, can be redirected. printf, like putchar, prints to the standard output; in fact, you can imagine that printf calls putchar to actually print each of the characters it formats.) Using these two functions, we can write a very basic program to copy the input, a character at a time, to the output:
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ input-individual-characters-using-scanf.aspx
Input individual characters using scanf() in C
Enter 3 character values: a b c x= 'a' y= ' ' z= 'b' Here, x contains 'a', y contains ' ' (space) and z contains 'b'. #include <stdio.h> int main(void) { char x; char y; char z; //input printf("Enter 3 character values: "); scanf ("%c%*c%c%*c%c", &x, &y, &z); //print printf("x= \'%c\' \n", x); printf("y= \'%c\' \n", y); printf("z= \'%c\' \n", z); return 0; }
Top answer
1 of 3
3

Your type for scores (int scores[row];) does not correspond to your attempt to read scores with scanf (scanf("%s", &scores);). The "%s" conversion specifier is for converting whitespace separated strings, not integers. The "%d" conversion specifier is provided for integer conversions.

Before looking at specifics. Any time you have a coding task of coordinating differing types of data as a single unit, (e.g. student each with a name (char*) and a score (int), you should be thinking about using a struct containing the name and score as members. That way there is only a single array of struct needed rather than trying to coordinate multiple arrays of differing types to contain the same information.

Also, don't skimp on buffer size for character arrays. You would rather be 10,000 characters too long than 1-character too short. If you think your maximum name is 10-16 character, use a 64-char (or larger) buffer to insure you can read the entire line of data - eliminating the chance that a few stray characters typed could result in characters remaining unread in stdin.

A simple stuct is all that is needed. You can add a typedef for convenience (to avoid having to type struct name_of_struct for each declaration or parameter), e.g.

 #include <stdio.h>

#define ROW 5       /* if you need a constant, #define one (or more) */
#define COL 64

typedef struct {        /* delcare a simple struct with name/score */
    char name[COL];     /* (add a typedef for convenience)         */
    int score;
} student_t;

Now you have a structure that contains your student name and score as a single unit rather than two arrays, one char and one int you have to deal with.[1]

All that remains is declaring an array of student_t for use in your code, e.g.

int main (void) {

    int n = 0;      /* declare counter */
    student_t student[ROW] = {{ .name = "" }};  /* array of struct */

    puts ("\n[note; press Enter alone to end input]\n");

With the array of struct declared, you can turn to your input handling. A robust way of handling input is to loop continually, validating that you receive the input you expects on each iteration, handling any errors that arise (gracefully so that your code continues), and keeping track of the number of inputs made so that you can protect your array bounds and avoid invoking Undefined Behavior by writing beyond the end of your array.

You could begin your input loop, prompting and reading your line of input with fgets as mentioned in my comment. That has multiple advantages over attempting each input with scanf. Most notably because what remains unread in the input buffer (stdin here) doesn't depend on the conversion specifier used. The entire line (up to and including the trailing '\n') is extracted from the input buffer and place in buffer you give fgets to fill. You can also check if the user simply presses Enter which you can use to conveniently indicate end of input, e.g.

    for (;;) {  /* loop until all input given or empty line entered */
        char buf[COL];              /* declare buffer to hold line */

        fputs ("Enter student name: ", stdout); /* prompt */
        if (!fgets (buf, sizeof buf, stdin))    /* read/validate line */
            break;
        if (*buf == '\n')   /* check for empty line */
            break;

(note you can (and should) additionally check the string length of the buffer filled to (1) check that the last character read is '\n' ensuring the complete line was read; and (2) if the last char isn't '\n' checking whether the length is equal to the maximum length (-1) indicating that characters may be left unread. (that is left to you)

Now that you know you have a line of input and it's not empty, you can call sscanf to parse the line into the name and score for each student while handling any failure in conversion gracefully, e.g.

        /* parse line into name and score - validate! */
        if (sscanf (buf, "%63s %d", student[n].name, &student[n].score) != 2)
        {   /* handle error */
            fputs ("  error: invalid input, conversion failed.\n", stderr);
            continue;
        }
        n++;                /* increment row count - after validating */
        if (n == ROW) {     /* check if array full (protect array bounds) */
            fputs ("\narray full - input complete.\n", stdout);
            break;
        }
    }

If you are paying attention, you can see one of the benefits of using the fgets and sscanf approach from a robustness standpoint. You get independent validations of (1) the read of user input; and (2) the separation (or parsing) of that input into the needed values. A failure in either case can be handled appropriately.

Putting all the pieces together into a short program, you could do the following:

#include <stdio.h>

#define ROW 5       /* if you need a constant, #define one (or more) */
#define COL 64

typedef struct {        /* delcare a simple struct with name/score */
    char name[COL];     /* (add a typedef for convenience)         */
    int score;
} student_t;

int main (void) {

    int n = 0;      /* declare counter */
    student_t student[ROW] = {{ .name = "" }};  /* array of struct */

    puts ("\n[note; press Enter alone to end input]\n");

    for (;;) {  /* loop until all input given or empty line entered */
        char buf[COL];              /* declare buffer to hold line */

        fputs ("Enter student name: ", stdout); /* prompt */
        if (!fgets (buf, sizeof buf, stdin))    /* read/validate line */
            break;
        if (*buf == '\n')   /* check for empty line */
            break;
        /* parse line into name and score - validate! */
        if (sscanf (buf, "%63s %d", student[n].name, &student[n].score) != 2)
        {   /* handle error */
            fputs ("  error: invalid input, conversion failed.\n", stderr);
            continue;
        }
        n++;                /* increment row count - after validating */
        if (n == ROW) {     /* check if array full (protect array bounds) */
            fputs ("\narray full - input complete.\n", stdout);
            break;
        }
    }

    for (int i = 0; i < n; i++) /* output stored names and values */
        printf ("%-16s %3d\n", student[i].name, student[i].score);
}

Example Use/Output

When ever you write an input routine -- Go Try and Break It!. Intentionally enter invalid data. If your input routine breaks -- Go Fix It!. In the code as noted the only check left for you to implement and handle is input greater than COL number of characters (e.g. the cat steps on the keyboard). Exercise your input:

$ ./bin/studentnamescore

[note; press Enter alone to end input]

Enter student name: zach 85
Enter student name: the dummy that didn't pass
  error: invalid input, conversion failed.
Enter student name: kevin 96
Enter student name: nick 56
Enter student name: martha88
  error: invalid input, conversion failed.
Enter student name: martha 88
Enter student name: tim 77

array full - input complete.
zach              85
kevin             96
nick              56
martha            88
tim               77

While you can use two separate arrays, a single array of struct is a much better approach. Look things over and let me know if you have further questions.

footnotes:

  1. Be aware that POSIX specifies that names ending with suffix _t are reserved for its use. (size_t, uint64_t, etc...) Also be aware you will see that suffix used in common practice. So check before you make up your own (but we no there is no POSIX student_t type).
2 of 3
1

You are almost there. However you have to make sure you do things cleanly Let's just do this step by step:

Step 1, get ONE name and ONE score

#include <stdio.h>

#define MAX_NAME_LENGTH 30

int main() {
      char name[MAX_NAME_LENGTH+1]; /* an array of characters making up ONE name (+1 for terminating NUL char in case of max-length name) */
      unsigned int score; /* a score */

      scanf("%30s", name); /* scan a name (assuming NO spaces in the name)*/
      /* also notice that name has no & in front of it because it already IS a pointer to the array name[MAX_NAME_LENGTH] */

      scanf("%u", &score);

      printf("%s scored %u in the test\n", name, score);
      return 0;
}

(See it run at http://tpcg.io/jS3woS)

STEP 2 -- Iterate to get multiple scores

Now let's read in 5 pairs and then print out 5 pairs.

#include <stdio.h>

#define MAX_NAME_LENGTH 30
/* i called rows iterations here just to provide contrast */
/* you can call them ROWS if you want but it then creates a confusion about name length */


#define ITERATIONS 5 

int main() {
      char name[ITERATIONS][MAX_NAME_LENGTH+1]; /* an array of names where each name is MAX_NAME_LENGTH long (notice the order) */
      unsigned int score[ITERATIONS]; /* score */

      int i; 

      for(i = 0; i < ITERATIONS; i++ ) {
           scanf("%30s", name[i]); /* scan a name (assuming NO spaces in the name)*/
           /* notice that name[i] has no & in front of it because name[i] is the pointer to the i-th row */

           scanf("%u", &score[i]);
      }

      for(i = 0; i < ITERATIONS; i++ ) {
           printf("%s scored %u in the test\n", name[i], score[i]);
      }

      return 0;
}

See it in action here (http://tpcg.io/iTj4ag)

๐ŸŒ
YouTube
youtube.com โ€บ watch
C user input โŒจ๏ธ - YouTube
C how to accept user input scanf fgets tutorial example explained#C #user #input char name[25]; //bytes int age; printf("\nWhat's your name?");
Published: August 24, 2021