W
W
WorldEn2017-02-14 22:51:49
Programming
WorldEn, 2017-02-14 22:51:49

How to insert variable value after % in printf()?

Tell me if it's not difficult. Just started learning C.

int n = 3;
int s[10];
scanf("%s", &s); // допустим "aaa"
printf("%s", s); 
printf("%3d", n); // выведет ааа
                            //                 3
                            
// но мне надо, чтобы вместо 3 в printf было значение переменной n 
// типо printf("%dd", n, n) Это выведет просто 3n а надо "   3"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2017-02-14
@WorldEn

Hm. Well, let's say, like this:
char s[10]; //buffer for format string
sprintf(s, "%%%dd", n); //create a format string. if n=3, buffer s will be "%3d"
printf(s, n); //equivalent to printf("%3d", n);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question