Beginner problem with printf and scanf in C - c

I have a problem with scanf() and printf() function in a snippet of code like this:
#include <stdio.h>
int main() {
int a;
int b;
int c;
scanf("%d %d", &a, &b);
while (c >= 2) {
c = a % b;
a = b;
b = c;
printf ("%d\n", c);
}
return 0;
}
What I expect to happen, and happens in my brother's Code::Block, is for the program to wait for input from stdin and then print to stdout the results, one per line, until it reaches the highest common divisor.
However, when I type it in vi and then compile it with gcc and run the program from my terminal, the program correctly takes the input but exit without returning anything to stdout.
If I comment out the scanf() line and hardcode any number to a and b variables, everything works as expected.
I'm trying to learn C and I've read basic documentation on the functions, but I can't help to understand this kind of behaviour.
I've tried to put a setbuf(stdout, NULL) before declaring variables but nothing changed.
Can somebody give me a clue?

There's nothing wrong with your scanf and printf calls but, as others have mentioned, one obvious problem is that you are testing the value of an uninitialised variable (c).
Maybe, what you want is a do { ... } while (...); loop, rather than a simple while loop.
The following code will guarantee to execute the loop at least once and then, at the end of each loop, check whether or not to repeat it:
#include <stdio.h>
int main() {
int a;
int b;
int c;
scanf ("%d %d", &a, &b);
do {
c = a % b;
a = b;
b = c;
printf ("%d\n", c);
} while (c >= 2);
return 0;
}
(Alternatively, initialise c with a value that is >= 2, i.e. use the declaration: int c = 3;.)
For further discussion of the do .. while loop, see here: 'do...while' vs. 'while'

Related

C programming- printf statement giving "expression expected" error

This is my first posted question, so I apologize in advance if it doesn't make perfect sense. I am having trouble debugging a snippet of code I wrote. When attempting to compile the code, I am getting a weird error that is pointing to my printf statements. Here is the code...
int IsItPrime (int number, int *ptr);
int main(void)
{
int userinput;
printf ("This program is designed to tell wether or not \na number you enter is prime or composite \nType your number, and press Enter: “);
scanf ("%i", &userinput);
int *ptr = &userinput;
if(IsItPrime(userinput,ptr) == 1)
{
printf("The number: %i is a prime number”, userinput);
exit(0);
}
printf("The number: %i is a composite number”, number);
return 0;
}
int IsItPrime (int number, int *ptr)
{
int ceiling, i = 0;
ceiling = number / 2;
for (i = 2; i <= ceiling; i++)
if (number % i == 0)
return 0;
return 1;
}
I've tried everything I could think of at this point. It is probably something blatantly obvious but I am not too experienced in C. Any suggestions would be greatly appreciated!
Thanks!
number is local to the function IsItPrime you cannot access variable number in main function.
syntax error and press Enter: “); in line 6 (printf statement) and other printf statements
Others already pointed out that numbers cannot be accessed in the main function.
It would be more helpful if you posted your error message.
Anyway, here are two things that might help:
Did you include stdio.h ?
Never seen that before so not sure, how that happened: But the closing double quotes of the strings are not the normal double quotes but some sort of German or French closing double quotes.

Scanf continuous input in C

#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.

Why this Ansi C program does not give result?

I am using ubuntu 12.04 lts with gcc. This ANSI C code has no error or warning when it is compiled, but when i try to execute a.out file, some junk values appear.
Can anyone tell me, what is wrong with this program?
#include <stdio.h>
int get_int(void);
int main (void)
{
int ret;
ret = get_int ;
putchar(ret);
printf("\n");
return 0 ;
}
int get_int(void)
{
int input;
char ch;
while ((scanf("%d", &input)) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}
You're missing parentheses in your function call. This:
ret = get_int ;
should be:
ret = get_int();
Also, both getchar() and putchar() deal in int, not char.
If your compiler isn't warning you about these things, particularly the first one, then either you need a new one, or you need to turn the warning levels on it up.
Also, as Gangadhar points out, right now you're reading in an integer and printing it out as a character, so entering 68 will output D, for instance, on a system that uses ASCII. This may be, but probably isn't, the behavior you want, so replace your putchar() with a call to printf() if it isn't.
These Below two statements are not correct
ret = get_int ;
putchar(ret);
correct them as below
ret = get_int () ;
printf("%d\n", ret);
in the above ret =get_int ; this says compiler to store the pointer into an integer(the function name it self points to function) And at that place you need to make call to function.here your function did not take require any arguments so your call should have Empty parenthesis preceded by function name. and the second one is you are using putchar function to print integer value.you need to use printf with %d specifier.

Why is sscanf acting this way?

I thought that I understood C but i am having a hard time just writing a simple addition code for practice. When I run this code, int a is 0 every time. However, int b works fine. The idea here is that the input to the program is 8 + 9. Why does sscanf not recognize variable a?
#include <stdio.h>
#include <stdlib.h>
int plus(int a, int b){
return (a + b);
}
int main()
{
int a, b;
char input[100], op;
printf("...I am ZOLO...\n");
printf("...The most vercatile calculator known to man...\n");
printf("...Please enter your query:");
fgets(input, sizeof(input), stdin);
sscanf(input, "%d %s %d", &a, &op, &b);
printf("%d + %d = %d...", a, b, plus(a, b));
return 0;
}
Jonathon Reinhart has the correct answer. In this case, it's not just the undefined behavior problem, it's the fact that the compiler managed to allocate op just before a (in internal memory order) and your machine uses little-endian byte order so that the '\0' character stored after op wipes out the value that was previously stored to a.

Reading Inputs using C

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();

Resources