char Input in C by scanf

Md. Arif

Help me Please. I want to know why it happen.

This code is not give right answer:

#include < stdio.h>

int main()

{

  char c,ch;

  int i;

    printf("Welcome buddy!\n\nPlease input first character of your name: ");
    scanf("%c",&c);

    printf("\nPlease input first character of your lovers name: ");
    scanf("%c",&ch);

    printf("\nHow many children do you want? ");
    scanf("%d",&i);

    printf("\n\n%c loves %c and %c want %d children",c,ch,c,i);

  return 0;
}

but this code give right answer.

#include < stdio.h>

int main()
{
  char c,ch;
  int i;

    printf("Welcome buddy!\n\nPlease input first character of your name: ");
    scanf(" %c",&c);

    printf("\nPlease input first character of your lovers name: ");
    scanf(" %c",&ch);

    printf("\nHow many children do you want? ");
    scanf("%d",&i);

    printf("\n\n%c loves %c and %c want %d children",c,ch,c,i);

  return 0;
}

Why? and How?

Please help me anyone who know this why it happend.

Karthikeyan.R.S

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Simple C Menu Building, Scanf not haulting for input

From Dev

C scanf char array

From Dev

Scanf a char in C with an example

From Dev

Matrix of Char in C - Using Scanf or getchar

From Dev

How to get integer input in an array using scanf in C?

From Dev

C Programming: Scanf in while loop only reads input once and then terminates

From Dev

C scanf in loop continues automaticly without input

From Dev

Can you assign a char a default name without scanf in c?

From Dev

C Convert decimal scanf input to binary array

From Dev

How to use scanf for a char array input?

From Dev

C: scanf for char not working as expected

From Dev

How to call scanf_s in assembly to input a char?

From Dev

C input of char returns a different char

From Dev

How to create optional input for scanf in C?

From Dev

scanf a string in a char pointer

From Dev

Difference between scanf for char * and char [ ]?

From Dev

C scanf() doesn't parse my input

From Dev

scanf by passing char

From Dev

C: Using scanf to accept a predefined input length char[]

From Dev

Input not terminating in scanf in C++

From Dev

Char not appearing after scanf

From Dev

Can you assign a char a default name without scanf in c?

From Dev

How to call scanf_s in assembly to input a char?

From Dev

Using scanf() on *char[]

From Dev

C input of char returns a different char

From Dev

OCaml Reading a char with Scanf

From Dev

linked list char pointer scanf input

From Dev

C: scanf input single character and validation

From Dev

scanf won't designate char in c, and strcmp is messing with me