I cannot understand about scanf("%2d %d %*f %5s", &x, &y, text); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I cannot understand about scanf("%2d %d %*f %5s", &x, &y, text);

#include <stdio.h> int main() { int x, y; char text[20]; scanf("%2d %d %*f %5s", &x, &y, text); /* input: 1234 5.7 elephant */ printf("%d %d %s", x, y, text); /* output: 12 34 eleph */ return 0; } I cannot understand why resault come out 12 34 I understand about 12, but not 34. how output can show 34?? (5.7 is ignored by %*7. is it??)

13th Mar 2019, 3:10 AM
sollex
sollex - avatar
1 Answer
+ 1
Example 1 : /* Meaning : %2d of variable x * it will reserve the output console with 2 character and * if the number of digits of x are larger than the specifier * then it will print with no spaces. * If x is less than 2 digits then one space (padding to left) will be added */ scanf("%2d", &x); //input : 1234 printf("%d", x); //output : 12 scanf("%2d", &a); //input : 1 printf("%d", a); //output : 1 // ================================= Example 2 : /* Meaning : %2d %d of variable x * If there are remaining digits .. will be assigned to the next variable */ scanf("%2d %d", &x, &y); //input : 1234 printf("%d %d", x, y); //output : 12 34 // ================================= Example 3 : /* Meaning : %2d %d %*f * The same as example 2 but one float will be read from the console * but it wouldn’t be assigned to any variable */ scanf("%2d %d %*f", &x, &y); //input : 1234 5.7 printf("%d %d", x, y); //output : 12 34 // ================================= Example 4 : /* Meaning : %2d %d %*f %5s * The same as example 3 but keep only 5 characters from the string variable */ scanf("%2d %d %*f %5s", &x, &y, text); //input : 1234 5.7 elephant printf("%d %d %s", x, y, text); //output : 12 34 eleph
13th Mar 2019, 8:18 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar