Output not printing expected variable. Scanf() issue [duplicate] - c

This question already has answers here:
C scanf() problem
(2 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
The integer and character code work seperately, but don't work together in the same code.
#include<stdio.h>
int main() {
int base;
char num1,num2,num3,num4;
scanf("%i",&base);
printf("%i\n",base);
scanf("%c",&num1);
scanf("%c",&num2);
scanf("%c",&num3);
scanf("%c",&num4);
printf("%c",num1);
return 0;
}
Input:
8
m n o p
Expected output:
8
m
Real output:
8

My understanding is that when you are reading in the characters, it is taking whitespace instead of the actual number. To fix this, you can add a whitespace character in front of the %c like so:
#include<stdio.h>
int main() {
int base;
char num1,num2,num3,num4;
scanf("%i",&base);
printf("%i\n",base);
scanf(" %c",&num1);
scanf(" %c",&num2);
scanf(" %c",&num3);
scanf(" %c",&num4);
printf("%c",num1);
return 0;
}
Hope this helps!

Related

printf is not showing before scanf [duplicate]

This question already has answers here:
C/C++ printf() before scanf() issue
(2 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 6 months ago.
I'm using Eclipse IDE
I have the following simple code in C language:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a;
printf("Enter a number");
scanf("%d",&a);
printf("You have entered %d",a);
return EXIT_SUCCESS;
}
So as you can see, there are two printf's.
but if i run the code on console My scanf is running before my first printf displays.
And all output printed together after the execution of scanf.
Then the result will be like this (This is what I'm getting in my console):
6
Enter a numberYou have entered 6
What i really wanted is:
Enter a number
6
You have entered 6
I tried adding fflush(stdout) but the broblem still exists.
is there any working solution to fix this?

Why would a C program need two scanf with same definition.Can somebody explain it? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
This is the code.Check Line 5 in the code below.
#include<stdio.h>
float occurence(char array[],char searched);
int main(){
/*char new[] = "NITTE"; */
char new[100];
char *point = & new[0];
char query;
printf("String:\t");
scanf("%s",point); //THIS CAUSES ERROR BUG HERE!!!.ALONG WITH "char new[100]" IN THE LINE ABOVE.
/*printf("Search:\t");*/
scanf("%c",&query);
printf("Search:\t");
scanf("%c",&query);
printf("String =\"%s\"\nQuery =\"%c\" ",point,query);
//printf("%c",*(point+1));
for(int i = 0 ; *(point+i)!='\0';i++ ){
if(*(point+i)==query){
static char * add;
add=&new[i];
printf("Position:%d\nAddress:%p \n", i+1,add);
}
}
printf("Occurance in String = %f %%\n",occurence(new,query));
return 0;
}
float occurence(char array[],char searched){
float appearance = 0,occurance=0;int i=0;
for(i=0;array[i]!='\0';i++){
if(array[i]==searched){
appearance++;
}
}
float length = i;
appearance=appearance+0.0;length=length+0.0;//THIS IS THE ONLY WAY I COULD THINK OF CONVERTING INT TO FLOAT SINCE
// APPEARENCE WOULD RETURN 2 AND 2.O AND SAME GOES FOR THE LENGTH.
occurance = (appearance/length)*100;
return occurance;
}
HERE two scanf s are needed but it has the same definitions.
Explain with details No short answer please.
in your first scanf() at this line
/*printf("Search:\t");*/ scanf("%c",&query);
cathes newline char \n, and again your second scanf() at this line
printf("Search:\t");
scanf("%c",&query);
catches your input.
You should leave a space for get rid of first scanf() like this
scanf(" %c",&query);
thanks to the white space, it skips newline char \n and gets correct input.

Why is fgets() not taking input in C program? [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 3 years ago.
I wrote a code to implement Julias Caesar Cryptography in C.
The problem is that my fgets() is not waiting for input and skipping to fputs().
#include<stdio.h>
int main(){
int size;
printf("Enter the size of the string: ");
scanf("%d",&size);
int key;
printf("Enter the key: ");
scanf("%d",&key);
char a[size];
printf("Enter the string: ");
fgets(a,size,stdin);
for(int i=0;i<size;i++){
a[i]+=key;
}
fputs(a,stdout);
return 0;
}
I used CodeBlocks 17.12 to compile this C program.
My OS is Windows 7 (32-bit)
Welcome to stackoverflow.
When you enter your answer, there is a newline char at the end (\n) in the buffer. When fgets() reads your input it reads the newline to. You can remove the newline, or use a regex to skip it, or fgets() once on the line so that you can use scanf() once more as suggested in this other answer that may help you.
And, please, remember to search in stackoverflow before posting a question!

Can't understand function output (C) [duplicate]

This question already has answers here:
C - trying to read a single char from stdin (and failing) w/ scanf / getchar
(4 answers)
C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf [duplicate]
(7 answers)
Closed 5 years ago.
trying to learn C i got stuck with this: I'm trying to write a function squared which should print on screen a filled square with size and filling character choosen by the user. So i wrote this:
#include <stdio.h>
int squared (int side, char fillCharacter);
int main(){
int side;
char fillCharacter;
printf("Enter the side ");
scanf("%d", &side);
printf("Enter the character ");
scanf("%c", &fillCharacter);
printf("%d%c",squared(side, fillCharacter));
}
int squared (int side, char fillCharacter){
for (int row=1 ; row <= side; row++){
for (int i =1; i <= side; i++){
printf("%c", fillCharacter);
}
puts("");
}
}
But sadly the output of this doesn't even scan for the character and just print a blank space
Enter the side 2
Enter the character
I tried change things but i made it worse so if someone could give me a hint i really would appreciate it. Thanks.
This is linked to this problem. This answer provides a fix but you could just fix it by reversing the calls to scanf: first the character and then the number.

Cannot get char array based on the given integer [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 6 years ago.
#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
char vote[n];
for(i=0;i<n;i++)
{
scanf("%c",&vote[i]);
}
for(i=0;i<n;i++)
{
printf("%c",vote[i]);
}
return 0;
}
It doesn't get second value, after getting first value it prints the first value .
If I give 3 for n it have to get three char values and it have to print that three char values , but code does not work properly for that.
Buffer problem in scanf. Adding a space in the scanf because %c does not skip white space and terminate.
scanf(" %c",&vote[i]); instead of scanf("%c",&vote[i]);

Resources