Given the following
#include <stdio.h>
int main(void)
{
char line[BUFSIZ];
long arg1, arg2;
while(fgets(lin e, BUFSIZ, stdin) != NULL){
if(sscanf(line, "%ld%ld",&arg1, &arg2) == 2){
snprintf(line, sizeof(line), "%ld\n", arg1 + arg2);
if(fputs(line, stdout) == EOF){
fprintf(stderr, "output error\n");
}
}
else{
fprintf(stderr, "invalid input\n");
}
}
return 0;
}
When I run it, I get the following
[cdalten@localho st oakland]$ ./add
4 5
9
65
invalid input
Now, here's the question. How come there has to be a space between the
numbers? sscanf() in this case doesn't even have a space in the format
args. Ie, it is like the following
(sscanf(line,"% ld%ld",&arg1,&a rg2)
More to the point. How come when I enter 65, the computer won't spit
back 11?
#include <stdio.h>
int main(void)
{
char line[BUFSIZ];
long arg1, arg2;
while(fgets(lin e, BUFSIZ, stdin) != NULL){
if(sscanf(line, "%ld%ld",&arg1, &arg2) == 2){
snprintf(line, sizeof(line), "%ld\n", arg1 + arg2);
if(fputs(line, stdout) == EOF){
fprintf(stderr, "output error\n");
}
}
else{
fprintf(stderr, "invalid input\n");
}
}
return 0;
}
When I run it, I get the following
[cdalten@localho st oakland]$ ./add
4 5
9
65
invalid input
Now, here's the question. How come there has to be a space between the
numbers? sscanf() in this case doesn't even have a space in the format
args. Ie, it is like the following
(sscanf(line,"% ld%ld",&arg1,&a rg2)
More to the point. How come when I enter 65, the computer won't spit
back 11?
Comment