I am trying a c program with following input.
6
7.0
How are you
I am able to read and print the integer and double datatype variable but string variable is not accepting any input from user and printing the integer variable value by itself
Code for int
:
int j;
double e;
char str[100];
printf("enter the integer variable :");
scanf("%d", &j);
printf("enter the double variable :");
scanf("%lf", &e);
printf("enter the string :");
scanf("%[^
]s",str);
printf("integer variable is %d
",j);
printf("float variable is %0.1f
",e);
printf("string variable is %s
", str);
Output:
enter the integer :3
enter the double :4.0
enter the string :integer variable is 3 -> (automatically accepting printf of integer case and exiting the code)
float variable is 4.0
string variable is ??LX?
But If I am reading string value first (before reading integer and double) then code is working fine.
Code for string
:
printf("enter the string :");
scanf("%[^
]s",str);
printf("enter the integer variable :");
scanf("%d", &j);
printf("enter the double variable :");
scanf("%lf", &e);
printf("integer variable id %d
",j);
printf("float variable is %0.1f
",e);
printf("string variable is %s
", str);
Output:
enter the string :how are you
enter the integer :6
enter the double :7.0
integer variable is 3
float variable is 4.0
string variable is how are you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…