While loop repeating function twice in one loop [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}

Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.

The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

Related

This C program keeps skipping the line without taking the value from user [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

second block of code doesn't output properly/cannot receive input [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

struct/fscanf not getting correct values [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

Scanf does not work with multiple inputs on different lines [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

In while loop char doesn't show output as i want [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
the program doesnt stop on scanf("%c", &ch) line. why does it happens sombody can please explain this to me
#include<stdlib.h>
#include<stdio.h>
struct list {
char val;
struct list * next;
};
typedef struct list item;
void main()
{
char ch;
int num;
printf("Enter [1] if you want to use linked list or [2] for realloc\n");
scanf("%d", &num);
if(num == 2)
{
scanf("%c", &ch);
printf("%c", ch);
}
}
Let's say you input 2 when you're reading for num. The actual input stream will be 2\n (\n is the newline character). 2 goes into the num, and there remains \n, which goes into ch. To avoid this, add a whitespace in format specifier.
scanf(" %c", &ch);
This will ignore any whitespaces, newlines or tabs.
The reason behind this is the newline \n character left behind by previous scanf, when pressing Enter key, for the next read of scanf. When the statement
scanf("%c", &ch);
executed then it reads that \n left behind by the previous scanf.
To eat up this \n you can use a space before %c specifier. A space before the %c specifier is able to eat up any number of white-space characters.
scanf(" %c", &ch);
^ a space

Resources