0

sololearn1. If I give the input 1234 5.7 elephant will give the resulting output 12 34 eleph I know that * means it ignores the input field, %2d is up to 2 character will be considered. but why 34 is applied to the second conversion character %d it belongs to the first %d?
2. Can I include space, tab, newline in the scanf? what happens? scanf("% d % d%s",var1, var2, var3);//is this fine?

/* input: 1234  5.7  elephant */
/* output: 12  34  eleph */
#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;
} 

result expected: 12 elephant

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
vijay_ky
  • 41
  • 6
  • @user3121023 It is invalid for scanf. It is valid for printf. – Vlad from Moscow Jul 31 '19 at 09:49
  • @KamilCuk Yes, '*' is an assignment suppressing character. I did not see it.:) – Vlad from Moscow Jul 31 '19 at 09:52
  • 1
    The `34` input does not belong to the first input, becuase it was halted at two characters. That's the point of the restriction `%2d` - so that the 3rd and 4th characters do not belong with it. No input is **forgotten**: if it is in the input buffer, it remains there until it is read. – Weather Vane Jul 31 '19 at 09:53
  • 1
    vijay_ky Tip: post text as text, not as a picture. Text as pictures attracts down-votes. – chux - Reinstate Monica Jul 31 '19 at 10:17
  • @chux yes %2d upto 2 characters and thanks for information on not to post text as image – vijay_ky Jul 31 '19 at 10:17
  • Note (1) [Trailing white space in a format string](https://stackoverflow.com/questions/19499060/what-is-difference-between-scanfd-and-scanfd) and (2) [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). – Jonathan Leffler Jul 31 '19 at 15:24

2 Answers2

1

spacing in %d for inputs for scanf

In scanf() format a " " never fails. It scans for optional white-space.

why 34 is applied to the second conversion character ?

"%2d" consumes the 12 of input. The 2 in the format limits input to 2 characters: 1 and 2. @Weather Vane
" " scans for spaces and sees none in the input. Scanning continues. "%d" scans in 34.

can I include space, tab, newline in the scanf? what happens?

Yes. A whites-space in the scan format matches 0 or more input white-space.1

scanf("% d % d%s",var1, var2, var3); is this fine?

No. "% d" is not a valid scanf() conversion specifier.

result expected: 12 elephant

This is curious. printf("%d %d %s", x, y, text); would print something out for y. This is no nothing value for an int.


Tip: good code checks the return value from scanf().

if (scanf("%2d %d %*f %5s", &x, &y, text) == 3) {
  printf("%d  %d  %s", x, y, text);
} else {
  puts("Bad input");
} 

Better code does not use scanf(), but fgets(). Far easier to handle errant input.


1White-space in a "%[]" specifier is handled different though. It needs an exact match.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

%2d will get 2 lengths decimal when you in put.

If you try this...

int x,y;
/* type  : 1234 */
scanf("%2d %d", &x &y);
/* var x : 12   */
printf("%d %d", x,y);
/* input : 12 34  */

Or this?

int x,y;
/* type  : 1 234 */
scanf("%2d %d", &x &y);
/* var x : 1   */
printf("%d %d", x,y);
/* input : 1 234   */

Then if you get limited length It will record as next conversion character

and it depent on your space to!

I'm Limit
  • 889
  • 5
  • 18