#include <stdio.h>
int main(){
int l,m,q,j;
char option;
scanf("%d",&q);
printf("%d\n",q);
for(j=0;j<q;j++){
scanf("%c %d %d",&option,&l,&m);
printf("%c %d %d",option,l,m);
}
return 0;
}
Output:
3(Input)
3
C 1 4(Input)
0 -374066224C 1 4
What is wrong with the above code? It is not giving the expected output.
There is still a newline character in the input stream from the initial
scanf()(and subsequentscanf()s): this will be assigned tooptionand the subsequentintassignments will fail asCis not anint, meaninglandmare uninitialisedintvariables.To skip it add a leading space character to the format specifier of the
scanf()within theforloop:The return value of
scanf()is the number of successful assignments made: check it to ensure3assignments are made.