Im trying to read from stdin something like abc.com but a[0]='\0' after run scanf instead of 'a'. Im using %[^.] to read all character until find a '.'.
#include <stdio.h>
int main() {
char a[6];
char b[4];
int i = scanf("%[^.]%s", a, b);
printf("%d\n", i);
printf("a: %s\n", a);
printf("b: %s\n", b);
printf("%c\n", a[0]);
printf("%c\n", a[1]);
printf("%c\n", a[2]);
if (a[3]=='\0') puts("why?");
return 0;
}
Trying char b[5]; will get you an output of
2
a: abc
b: .com
a
b
c
It indicates that you have read 5 characters into b, the fifth being the '\0' which overwrites a[0]. The first one being the '.', which intentionally is the NOT read by the format for a. You hence write beyond b (which is undefined behaviour and I should stop explaining right now....).
One of the possible behaviours of this UB is that the fifth read character for b is written beyond and into the first entry of a.
Related
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);
}
For example:
#include<stdio.h>
int main()
{
char c;
c = getchar(); // Let's say you enter AA
printf("%c\n", c);
}
Does the second character get stuck in some reserve spot in memory like the stack? Because I know if I were to added another c = getchar(), the second character would be assigned to the variable c.
Does the second character get stuck in some reserve spot in memory like the stack?
It is most likely in a buffer associated with stdin.
If you enter AA for a single character the remaining input will still be in the stdin buffer, as this program demonstrates. Instead of printing the characters, it prints their ASCII value for clarity. I entered AA<Enter> just once.
#include<stdio.h>
int main()
{
char c;
c = getchar(); // Let's say you enter AA<Enter>
printf("%d\n", c);
c = getchar(); // still another A to come
printf("%d\n", c);
c = getchar(); // still a newline to come
printf("%d\n", c);
return 0;
}
Program session
AA
65
65
10
as you know getchar only take one variable.so the 2nd 'A' will not store any where.it will be on ROM for sometime but the moment you hit enter it will vanish.
#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.
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.
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();