#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.
Related
I'm new to C language. Here's the code I used to get input for a and b and print them. But I did not get the values I entered via the terminal can you explain why I got different values for a and b?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a,b;
scanf("%d, %d", &a, &b);
printf("%d, %d", a, b);
return 0;
}
I entered 5 9 as inputs, but I got 5 and 16 as outputs.
You need to check the value of scanf() otherwise some or all the values you attempt to use are undefined. In your case the input you provided did not match the scanf() format string. I changed the format string to match the input you provided, removed headers that are not used and added a newline to your printf() statement:
#include <stdio.h>
int main(void) {
int a, b;
if(scanf("%d%d", &a, &b) != 2) {
printf("scanf failed\n");
return 1;
}
printf("%d, %d\n", a, b);
}
You don't have to use comma (,) to separate 2 inputs in scanf() function. This function automatically separates your inputs by number of occurrences of format specifiers i.e. %d in your case. However you have to separate variable addresses using comma.
Just use it like scanf("%d %d", &a, &b).
I executed following code in ubuntu with gcc compiler.
As a=0, the second printf() prints some garbage value.
What kind of behavior is it by printf()?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 0;
printf("\nThe Value of %ns : %d\n", &a, a);
printf("\n%d\n", a);
printf("\n\n");
return 0;
}
If you read e.g. this printf reference you will see that the %n format specifier will:
returns the number of characters written so far by this call to the function.
So it will overwrite the contents of a with the number of characters it has written so far, which should be 14 if I count correctly.
The output of the first printf is 0
Because the second argument of printf passes a by the value and it gets printed afterwards.
The output of the second printf is 14
Because at this time the value of a was replaced by the number of characters that was printed before %n by the first printf
As a=0, the second printf() prints some garbage value.
You can't believe that statement until you verify it!
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 0;
printf("\nThe Value of %ns : %d\n", &a, a);
assert(a == 0);
printf("\n%d\n", a);
printf("\n\n");
}
Any time you want to say "I'm sure that (some invariant) is true at place x", use assert to state that fact and let it be checked at runtime.
Output:
output.s: ./example.c:8: main: Assertion `a == 0' failed.
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'
When you declare two variables char a,b; and then you use first 'a' and then 'b',it prints only b, but if you declare it 'b' then 'a', it has no problem printing both in ASCII,the point of the program is to read 121 and 120 and to print yx. the problem - https://prnt.sc/pr5nww
and if you swap them -https://prnt.sc/pr5mt5
#include <stdio.h>
#include <stdlib.h>
int main(){
char a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("%c",a);
printf("%c",b);
}
This is kind of a confusing situation. When it comes to mixing char and int values (as you might do when investigating the numeric values of characters in a character set), it turns out the rules for scanf and printf are almost completely different.
First let's look at the scanf lines:
char a,b;
scanf("%d",&a);
scanf("%d",&b);
This is, in a word, wrong. The %d format in scanf is for scanning int values only. You cannot use %d to input a value of type char. If you want to input a character, the format for that is %c (although it'll input it as a character, not a number).
So you'd need to change this to
char a,b;
scanf("%c",&a);
scanf("%c",&b);
Now you can type characters like A and $ and 3 and have them read into your char variables a and b. (Actually, you're going to have additional problems if you hit the Return key between typing the characters for a and b, but that's a different story.)
When it comes to printing the characters out, you have a little more freedom. Your lines
printf("%c",a);
printf("%c",b);
are fine. And if you wanted to see the integer character-set values associated with the characters, you could have typed
printf("%d",a);
printf("%d",b);
and that would have worked, too. This is because when you call printf (and other functions ike it), there are some automatic conversions that take place: types char and short int are automatically promoted to (passed as) int, and type float is promoted to double. But these automatic conversions happen only for values of those types (as when calling printf). There a=is no such conversion when you're passing pointers to these types, as when calling scanf.
What if you wanted to read numbers, not characters? That is, what if you wanted to input the number 65 and see it get printed as capital A? There are several possible ways to do that.
The first way would be to continue to use %d in your scanf call, but change the type of your variables to int:
int a,b;
scanf("%d",&a);
scanf("%d",&b);
Now you can print a and b out using either %c or %d, and it'll work fine.
You could also use a temporary int variable, before reassigning to char, like this:
char a,b;
int tmp
scanf("%d",&tmp);
a = tmp;
scanf("%d",&tmp);
b = tmp;
The final, lesser-known and somewhat more obscure way, is to use the h modifier. If you say
char a,b;
scanf("%hhd",&a);
scanf("%hhd",&b);
now you're telling scanf, "I want to read decimal digits, but the target variable is a char, not an int."
And, again, you can print a and b out using either %c or %d, and it'll work fine.
the point of the program is to read 121 and 120 and to print yx
Do
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a, b;
/* Scan into the half of the half of an int (the leading blank
makes scanf() eat whitespaces): */
scanf(" %hhd", &a);
scanf(" %hhd", &b);
/* Print the half of the half of an int: */
printf("%hhd", a);
printf("%hhd", b);
}
To print the characters literally do the printing part like this:
...
printf("%c", a);
printf("%c", b);
}
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();