Whats wrong with my code?
It just scans a string and 1 character but not any integers.
char a,b;
long long int c,d;
char ch[10];
scanf("%s",&ch);
scanf("%c %c %lld %lld",&a,&b,&c,&d);
printf("%s %c %c %lld %lld",ch,a,b,c,d);
input: abcd p q 100 200
output: abcd p (some random crap)
Here is the working code.
‘ch’ has the address of the string. you dont need to provide ‘&ch’ as an argument to the scanf function. this will return the address of variable holding the address of first element of the array to which it is pointing.
Your statement scanf("%s",&ch)
is responsible for the problem which you are facing.
%s always expects argument of type (char *). In your case, &ch is the argument which is of a different type - pointer to a character array having ten characters.
If you have written it as scanf(%s",ch)
, your code would have worked perfectly.
Actually, they are wrong. The problem is with the character left after the string input. The real solution is to put a blank space before the first character input, or after the string input.
char a,b;
long long int c,d;
char ch[10];
scanf("%s",&ch);
scanf(" %c %c %lld %lld",&a,&b,&c,&d);
printf("%s %c %c %lld %lld",ch,a,b,c,d);
this also works and its best practice, but again, that was not the problem:
scanf("%s",ch);