I'm a newbie in programming. It is confusing me when I taking input of a char array after scanning an integer. It is not working properly.
The code is following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
Output is following:
The problem is due to the white spaces.After scanf("%d",&x); the last entered '\n' character is taken and saved the string a of scanf("%[^\n]",a).
To avoid this give space in scanf() statement
scanf(" %[^\n]",a);//give a space
Why to give a space?
By giving a space,the compiler consumes the '\n' character or any
other white space ('\0','\t' or ' ' ) from the previous scanf()
your code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf(" %[^\n]",a);//give a space
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
fflush(stdin);
scanf("%[^\n]",a);
fflush(stdin);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
This also works. The same goes here, /0 at the end adds up to the character scan and interferes. Using fflush(stdin) will discard any unnecessary input data including the /0.
Correct me if I am wrong as I too am a newbie at coding. :p
Replace scanf("%[^\n]",a); with scanf(" %99[^\n]", a);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%s",a); // get char array without inputing space
scanf(" %99[^\n]", a); // get char array, allowing inputing space
scanf("%d",&y);
printf("%d\n%s\n%d\n",x,a,y);
return 0;
}
Instead of %d use %d\n to consume the newline, so that the following command will not just read nothing:
scanf("%d\n",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
Related
Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.
I'm supposed to write a simple C program to read in an integer and loop n times to work on the string, but the first loop automatically passes an empty string if I use the scanf integer, but if I use a constant number the loop works right. Somebody please explain to me what's going on.
#include <stdio.h>
#define MAX 80
int main(){
char sentence[MAX];
int n, i;
scanf("%d", &n);
for(i=0; i<3; i++){//it loops with empty string automatically if I replace 3 with n
gets(sentence);
printf("%s\n", sentence);
}
}
Try this code
#include<stdio.h>
#include<conio.h>
#define MAX 80
void main()
{
char sentence[MAX];
int n,i;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf(" %99[^\n]",sentence);
printf("\n%s\n",sentence);
getch();
}
Why won't my program print out a character in c? I can not seem to find any error in this. Below is a snippet of my program. When I tried to print firstBand character out, all i get is nothing.
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int bandOne;
int bandTwo;
double multiplierValue;
double resistanceValue;
double toleranceValue;
char firstBand;
char secondBand;
char multiplier;
char tolerance;
int main(void) {
//prompt user for band colours
printf("Colour of first band?\n");
firstBand = scanf(" %c",&firstBand);
printf("Colour of second band?\n");
secondBand = scanf(" %c",&secondBand);
printf("What is multiplier?\n");
multiplier = scanf(" %c",&multiplier);
printf("What is the tolerance?\n");
tolerance = scanf(" %c",&tolerance);
printf("%c\n",firstBand);
....
..
..
return 0;
}
please help.
firstBand = scanf(" %c",&firstBand);
firstband is a character read and scanf() returns number of elements successfully read.
So firstBand is no more a char which you have scanned and it is actually the value of number of elements read.
if(scanf(" %c",&firstBand) == 1)
printf("%c",firstBand);
Change all of your scanf statements from
firstBand = scanf(" %c",&firstBand);
to
scanf(" %c",&firstBand);
That should fix it.
#include <stdio.h>
#include <string.h>
void main()
{
int N,i,s;
char op;
scanf("%d",&N);
int a[N];
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
scanf("%c %d",&op,&s);
printf("%c %d",op,s);
}
If i include this for loop in my code and then execute this code, scanf doesn't read my input.
If i insert value of opand s is R and 5 then the output is garbage.
And if remove the for loop, then i printf print the correct answer.
You need to consume the trailing \n character. The easiest way to do it is to write:
scanf(" %c %d",&op,&s); //notice the space before %c
I am currently taking a basic C course and I was wondering why my code below doesn't run.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[4];
printf("Enter some text\n");
scanf(" %c %c %c", &string[0], &string[1], &string[2]);
printf("You Entered ");
int i;
for (i = 0; i < 4; i++){
printf("%c",string[i]);
}
return 0;
}
Xcode said there is an errr with my scanf line.
I was hoping to type in "a b c d" and expect
a "You entered abcd";
This code should run (albeit with a bug). I suspect you need to configure the Xcode build options correctly.
As for the bug, you have an array of four chars, but you are only scanning for three. Add another %c and &string[3] to your scanf line.
Here's an ideone snippet showing the modified code in action
#include <stdio.h>
int main()
{
char string[4];
int i;
printf("Enter some text\n");
scanf("%c %c %c %c", &string[0], &string[1], &string[2], &string[3]);
printf("You Entered ");
for (i = 0; i < 4; i++){
printf("%c", string[i]);
}
return 0;
}
This compiles just fine on the Mac command line (assuming the source is in "test.c")
$ cc -g -Wall -o test test.c
./test
Enter some text
a b c d
You Entered abcd
Also note that this particular snippet requires only stdio.h (man scanf and man printf will tell you which header to use).
How about executing this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char strin[10];
printf("Enter some text\n");
scanf("%s", strin);
printf("You Entered %s",strin);
return 0;
}
The following code gives you :
Enter some text
abcd
You Entered abcd