Can I use scanf to capture a directive with a width specified by a variable?

后端 未结 3 808
梦毁少年i
梦毁少年i 2020-12-20 01:24

I have the following code:

scanf(\" %Xs %Ys\", buf1, buf2);

Where X and Y should be integers. The problem is that the values for X and Y ar

3条回答
  •  没有蜡笔的小新
    2020-12-20 01:52

    Your question is not clear. If you don't know the values, then they are probably run-time constants, not compile-time constants.

    If that's the case (i.e. they are run-time consants), then no, there's no such feature in 'scanf'. The only thing you can do is to produce the complete format string (with the concrete values embedded in it) at run-time by using 'sprintf' and then pass that format string to your 'scanf'.

提交回复
热议问题