Bla*_*iev 15
printf("Label-one: %.*s", 13, str);
printf("Label-two: %.*s", 2, str + 13);
Run Code Online (Sandbox Code Playgroud)
如果这些长度是常数,@ Bob的答案也是可以接受的,但是如果在运行时确定长度,这是最好的方法,因为它参数化了它们.
printf( "%.13s", labelOne ); // stops after thirteen characters.
printf( "%.3s", &labelOne[ 13 ] ); // prints three characters of the string that starts at offset 13
Run Code Online (Sandbox Code Playgroud)
根据您的观点,我注意到您的问题或答案中可能出现的fencepost错误/不一致.第二个例子的正确答案可能是:
printf( "%.3s", &labelOne[ 12 ] );
Run Code Online (Sandbox Code Playgroud)