当没有 space 时,在 scanf 中区分字符串和 int

distinguishing between a string and an int in scanf when there is no space

我正在尝试区分字符串和 int,并使用 scanf() 将两者分开。

例如:Ron Burgundy41

是否可以使用scanf("%s %s %i", name, last_name, &number)

并获得

姓名 = 罗恩

last_name = 勃艮第

人数=41

是的,很有可能。你可以这样写你的 scanf -

if(scanf("%19s %19[^0-9]%d",name,last_name,&number)==3){   //assuming both array of size 20
/*                  ^ this will read and store in array until a numbers is encountered */
   // print them 
}

Click on link to see working code