strange situation with scanf, char , etc...
hi everybody,
i am gonna give you some code blocks and some of them works and some does not, but all of them eventually same thing. if you can solve the situation i will be happy about that. thanks,
These is WORK fine #1
and these is my best solution but why????
Code:
#include<stdio.h>
int main(int argc, char *argv[])
{
int a,b;
char c;
scanf("%d", &a);
scanf("%d", &b);
fflush(stdin);
scanf("%c", &c);
printf("%d %c %d = %d\n", a, c, b, c=='+' ? a+b : a-b);
return 0;
}
These is WORK fine #2
Code:
#include<stdio.h>
int main(int argc, char *argv[])
{
char c;
int a,b;
scanf("%d", &a);
scanf("%d", &b);
scanf("%s", &c);
printf("%d %c %d = %d\n", a, c, b, c=='+' ? a+b : a-b);
return 0;
}
These is WORK fine #3
Code:
#include<stdio.h>
int main(int argc, char *argv[])
{
char c;
int a,b;
scanf("%d", &a);
scanf("%d", &b);
scanf(" %c", &c); // there is one space before %c. it is its difference then code 5
printf("%d %c %d = %d\n", a, c, b, c=='+' ? a+b : a-b);
return 0;
}
These is does NOT WORK #5
Code:
#include<stdio.h>
int main(int argc, char *argv[])
{
char c;
int a,b;
scanf("%d", &a);
scanf("%d", &b);
scanf("%c", &c);
printf("%d %c %d = %d\n", a, c, b, c=='+' ? a+b : a-b);
return 0;
}
These is does NOT WORK #6
Code:
#include<stdio.h>
int main(int argc, char *argv[])
{
int a,b;
char c; // char c is down from int a,b is difference then code 2
scanf("%d", &a);
scanf("%d", &b);
scanf("%s", &c);
printf("%d %c %d = %d\n", a, c, b, c=='+' ? a+b : a-b);
return 0;
}