2

I want to use variables to specify format of printf in C programming.

I am quite a C programming newbie, having practiced bash shell scritps though.

In bash scripts, I can use a variable to specify format of printf as one below.

#!/bin/bash

### Variable
format="%4s%4d %8s\n"

### Main
printf "$format" $1 $2 $3

Then, Is there similar way like the above in C programming?

Is it possible in C?

The strings for the printf format include characters and numbers.

I have heard C programming uses different declaration for each of them; i.e. int or char.

oguz ismail
  • 1
  • 16
  • 47
  • 69
TADASUKE
  • 95
  • 8
  • 2
    You can even `printf` to a format string that you use in another `printf`. – Paul Ogilvie Aug 12 '20 at 13:40
  • Yes, you can, but it is rarely useful to do so. – John Bollinger Aug 12 '20 at 13:54
  • Thank you everyone! Although I've understood there are many ways, each line's meaning in code is difficult to understand as of now, because today is still the second from C programming learning start. I will choose the best answer, when I am able to understand sample lines. I appriciate all support from the community. – TADASUKE Aug 13 '20 at 04:36

3 Answers3

3

Is there similar way like the above [format="%4s%4d %8s\n"] in C programming?
Is it possible in C?

Yes, several ways to both questions.
Among them, you can use sprintf() to prepare the buffer:

char format[80] = {0};//create editable char array
int a = 12;
char buf1[] = {"this is string 1"};
char buf2[] = {"this is string 2"};

sprintf(format, "%s", "%4s%4d %8s\n");
printf(format, buf1, val, buf2);

Even closer to what you have done in your example ( format="%4s%4d %8s\n" ), you can simply define format as a string literal:

char *format = "%4s%4d %8s\n";//string literal not editable

Or, create an initialized, but editable char array

char format[] = {"%4s%4d %8s\n"};//editable, but only up to 
                                 //strlen("%4s%4d %8s\n"); 
                                 //(+ NULL, which is implied when using "...".)

Note that C also provides a built in feature to enable run-time setting of precision when outputting floating point numbers:

double val = 22.123445677890;
int precision = 3;

printf("%.*f", precision ,  val);

Will output 22.123

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Thank you for yor useful advice. Could I ask one additional question? As my own post for an answer, I used "" double quotations like "char format[] = "%s\n"; ". Then your sample code is using {} like "char format[80] = {0};". Do {} braces have special meaning? Should they be used in such case? Since I am a newbie, I would want to know how they are different. – TADASUKE Aug 13 '20 at 09:50
  • 1
    @CaptainCookie - Yes, `{0}` is the `C` way to ensure that _all_ elements of an `array` are zeroed when using as an initializer. There is a great discussion [here](https://stackoverflow.com/a/201116/645128) about initializations in general, including this one. – ryyker Aug 13 '20 at 12:45
  • I will check suggested page! Today I could understand the difference single and double quotation in other question. I also want to understand about {}. Thank you! – TADASUKE Aug 14 '20 at 07:49
2

Yes, you can build a format string dynamically:

char fmt[20];
int b;
char a[SOME_SIZE];
char c[SOME_SIZE];

...
sprintf( fmt, "%%4s%%4d %%8s\n" ); // need %% to print a literal %, so fmt will contain "%4s%4d %8s\n"
printf( fmt, a, b, c );

There are other ways of building a format string dynamically, this is just the most straightforward.

I have heard C programming uses different declaration for each of them; i.e. int or char.

Yes, each format specifier expects its corresponding argument to have the correct type. %d expects its corresponding argument to have type int, %s expects its argument to have type char *, etc. If they don't match, then the behavior is undefined and you'll likely get weird-looking output.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

I could achieve my first purpose thanks to all your support.

I wrote such code below, using a Symbolic Constant or "char anyname[] = string".

#include <stdio.h>

#define FORMAT "%30s\n" 

int main()
{
    char c_string[] = "You Suceed!";

    printf(FORMAT, c_string);

    return 0;
}

/*
int main()
{
    char format[] = "%s\n"; 
    char c_string[] = "You Suceed!";

    printf(format, c_string);

    return 0;
}
*/

I appreciate all your suggestion!

TADASUKE
  • 95
  • 8