I am developing a simple compiler in the cygwin environment using flex and bison generating C code as output and I have generated a sequence of code intended to read two integers followed by a char.
Whereas I thought I knew basic c code I am suffering a problem with the code below where I input two integers, but it never asks for the character after reading the integers!
What is the best way to handle generating code like this, should I always clear the character buffer before performing a scan or a getchar() or have i just made a mistake somewhere!!!
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int a = 0;
int b = 0;
char f = '\0';
scanf("%d",&a);
scanf("%d",&b);
f = getchar();
fflush( stdin );
return EXIT_SUCCESS;
}
If you want to read each part of the input on a new line, change your scanf format string so that it consumes the '\n', since at the moment f = '\n'. ie. use "%d\n" instead of "%d".
And then you can print the values to make sure (printf("a: %d, b: %d, f: %c\n", a, b, f);)
$ ./test
2
4
b
a: 2, b: 4, f: b
An alternative is also to replace the two scanf statements and the getchar with a single scanf statement: scanf("%d\n%d\n%c", &a, &b, &f);.
You are not consuming the newlines left in the input buffers. The changes are the following:
scanf("%d\n",&a);
scanf("%d\n",&b);
f = getchar();
Related
I want to input valors for a and b, being a an int and b a str. When I run my program I can input a valor, but then it ingnores printf() and gets() for b.
#include<stdio.h>>
int main()
{
int a;
char b[5];
printf("Write a:\n");
scanf("%i", &a);
printf("Write b:\n");
gets(b);
printf("a = %i, b = %s", a, b);
return 0;
}
In the end, it just prints:
a = (valor written), b =
I don't know what's wrong with this, neither if it's a different way to get this working. I'm pretty new with C. Thank you in advance. ;)
The function gets is unsafe and is not supported by the C Standard. Instead use either scanf or fgets.
As for your problem then after this call of scanf
scanf("%i", &a);
the input buffer contains the new line character '\n' that corresponds to the pressed key Enter. And the following call of gets reads an empty string by encountering the new line character.
Instead of using gets write
scanf( " %4[^\n]", b );
Pay attention to the leading space in the format string. It allows to skip white space characters as for example the new line character '\n'. And the call of scanf can read a string with maximum length equal to 4. If you want to read a larger string then enlarge the array b and the field width specifier in the format string.
I was shown this program to read an entire string:
#include <stdio.h>
int main(int argc, char *argv[]){
int something;
printf("Enter something \n");
while (scanf("%c", &something)==1) {
printf("%c", something);
}
return 0;
}
When I enter hello world it outputs hello world.
Can someone please explain why it doesn't output:
h
e
l
l
...
because I thought loops go over one letter at a time I'm confused as to why it didn't output like that. Now, I tried to write a program that does the same thing using getchar instead:
#include <stdio.h>
int main()
{
char c;
printf("Enter character: ");
c = getchar();
printf("Character entered: ");
putchar(c);
return(0);
}
When I enter hello world this program just outputs h. How can I use getchar to do the same thing as the first program? Also, what are the differences between getchar and scanf?
getchar() takes one character input at a time. If you want it to read a whole string or a character array, you may use a while loop or a for loop to store each character one by one. Eg : c[i]=getchar() for i = 0 to string length, or while c[i]!='\n'
In the first program, the problem is the line
printf("%c", something);
It's missing a newline character, it should be
printf("%c\n", something);
if you want to print one character per line.
The problem with the second one is that you are reading only one character with getchar() function. You can't read in the whole string on the same line with getchar(), you can try gets(char* s) instead. And you need to loop over putc() to dump all the characters.
#include <stdio.h>
int main ()
{
double a=0;
char b=0;
scanf ("%d%c",&a,&b);
printf ("%d,%c", a, b);
return 0;
}
This is my code for a quick test program I wrote to play around with the scanf function in C. I am trying to have the user input something like 78X + 5 = 19 (then hit enter) and then parse that into variables a, b, and c where in this case a=78, b=5, c=19. In the sample code, when I type in 78X, c doesn't store a value to b and only prints "78, " and then terminates. Why won't it store a value to b?
If your input is 75x then below is the code which reads the value and stores it in a(75) and b(x) respectively
#include <stdio.h>
int main ()
{
int a=0;
char b=0;
scanf ("%d%c",&a,&b);
printf ("%d%c", a, b);
return 0;
}
The , in your format string is significant. The string %d,%c would match the input 78,x but it would not match 78x .
Also you need to use %f to scan and print a double. Using %d causes undefined behaviour (which may manifest itself as b seeming to not appear). Either change to %f, or change your double to an int.
This question already has answers here:
take char input and store it in an array
(3 answers)
Closed 8 years ago.
what I want to do is input characters in character array in loop .I am new to c programming and don't know uses of pointers very well.my input is something like this :
INPUT:
a b
c d
e f upto n times
I have declared two arrays char a[n],b[n]; and I want to input a in a[0], b in b[1], c in a[2]...
Here is my attempt
char a[n],b[n];
for(i=0;i<n;i++)
{
scanf("%c %c",&a[i],&b[i]);
printf("%c %c",*(a+i),*(b+i));
}
but this doesn't work!!if I input: a b the output is a a
`
If I run your code I get
a b
a bc d
c d
which means, that the enter you press after each line, will be interpreted as an input character by scanf(), since you told her to read characters. Also, I had n = 3, but the loop run twice, which also supports this idea.
You should change your scanf() to this:
scanf(" %c %c",&a[i],&b[i]);
which will eat the newline.
Here is a minimal example:
int main(void) {
int n = 3;
int i;
char a[n],b[n];
for(i=0;i<n;i++) {
scanf(" %c %c",&a[i],&b[i]);
printf("%c %c\n",*(a+i),*(b+i));
}
return 0;
}
Output:
a b
a b
c d
c d
The newline in printf() is just for formatting elegance, not something more, like a comment had wrongly suggested.
If you want, you can read my page:
Caution when reading char with scanf (C)
%c does not skip newlines. So your second scanf will first read the newline at the end of the first line instead of the 'c'.
You could change the scanf to scanf("%1s %1s", &a[i], &b[i]); but then you'll have to increase the size of the arrays by one.
For my C program, the user enters in "aY + b = c" where a, b, and c are int values and Y is a "symbolic constant."
How does one make it so that "aY+b=c" works as well as "aY + b = C" works? Basically, I am unsure of how to utilize scanf() so that I can grab my variables a,b, and c from the user input no matter how many spaces the user decides to input.
Thanks!
Consider the following:
Code
#include <stdio.h>
#include <ctype.h>
#define MAX_EQUATION_LEN (1000)
int main(void)
{
char equation[MAX_EQUATION_LEN];
int num, i;
printf("Enter a equation: ");
fgets(equation, MAX_EQUATION_LEN, stdin);
sscanf(equation, "%d", &num);
i = 0;
while(equation[i])
{
if(!isdigit(equation[i]))
break;
else
i++;
}
printf("You entered: %d\n", num);
printf("Unhandled string data: %s\n", &equation[i]);
return 0;
}
Example Run
Enter a equation: 204Y + 52 = 9
You entered: 204
Unhandled string data: Y + 52 = 9
Logic
Take the full equation in at one time as a string.
Parse the string accordingly. Here, I'm looking for the first int.
Keep track of where you are in the string parsing process.
Keep parsing the string until there are no more characters.
I am unsure of how to utilize scanf() so that I can grab my variables a,b, and c from the user input no matter how many spaces the user decides to input.
Don't use scanf in the first place. You need to write a lexer and a parser, so write a lexer and a parser.