Questions tagged [printf]

printf is a common function for formatted output. C and many other languages have a whole family of related functions. Only use this tag if the question is directly concerned with printf or related functions.

For detailed information on how the printf function works in C, refer to Open Group Base Specifications or the C standard.

C99 knows these members of the printf()-family:

  printf     vprintf     wprintf     vwprintf
 fprintf    vfprintf    fwprintf    vfwprintf
 sprintf    vsprintf    swprintf    vswprintf
snprintf   vsnprintf   snwprintf   vsnwprintf

POSIX additionally knows these:

 dprintf    vdprintf

GNU adds these:

asprintf   vasprintf

The name is generally of the form:

  • optional v for va_list
  • output specifier: None for stdout, f for FILE*, s for supplied buffer, sn for supplied buffer of specified length, as for allocated buffer, d for supplied file descriptor
  • optional w for wide variant.
  • printf

Documentation for printf function is available on Linux/Unix in section 3 of the manual and can be accessed by using man 3 printf.

For a multi-lingual description of the history of printing formatted strings across many languages, refer to Wikipedia article printf format string

9422 questions
3
votes
2 answers

sizeof(function) always returns 1.Why?

size of a function using sizeof() always returns 1 I just tried to find the size of a function.While using sizeof() for finding the size it always returns 1.Even the function is a well defined function it just returns 1. I just wanna know why it…
3
votes
2 answers

What does $d do in printf?

I accidentaly wrote: printf "here: %s $d\n" "${line:0:32}" "${#line}" and got: here: /home/gsamaras/Bash/libs/goodLib here: 103 Why? Of course I meant to say %d, but I don't understand this behavior in the mistake I made. I would probably expect…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
3
votes
0 answers

Printf inconsistent results [The C Programming Language - Chapter 1.6 Arrays]

When running the program provided in chapter 1.6 Arrays of The C Programming Language I get some very inconsistent results from the final printf. Here is the program: #include int main() { int c, i, nwhite, nother; int ndigit[10]; …
htskll
  • 31
  • 3
3
votes
2 answers

Variadic Macro calling fprintf: how to add arguments to __VA_ARGS__?

I have two macros: #define LogFunction(str) fprintf(stdout, "%s: %s\n",__FUNCTION__,(str)) #define LogPrintf(f_, ...) fprintf(stdout, (f_), ##__VA_ARGS__) So i can use them this way: void MyFunction() { int N=4; …
Parduz
  • 662
  • 5
  • 22
3
votes
1 answer

C/C++ print custom EOL

I want to generate on Windows a file (script) for UNIX. So I need to output just LF characters, without outputting CR characters. When I do fprintf(fpout, "some text\n");, character \n is automatically replaced by \r\n in the file. Is there a way to…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
3
votes
3 answers

Why printf ("%d", ~a); displays -4 when a is equal to 3?

Running the following program display -4 while 252 is expected: unsigned char a=3; printf ("%d", ~a); Why this code doesn't display 252? I also tested the folowings according to the proposed answer: printf ("%u", ~a); displays: 4294967292 printf…
Mr Robot
  • 1,037
  • 9
  • 17
3
votes
1 answer

Printing multiple variables in single printf() statement

What I have so far: printf("Entered string is %c", string, "with length %d", i, "and ID number is %s", number, "with length %d", j); The result I'm getting is: Entered string is 4
user8565890
3
votes
6 answers

How to validate user-supplied printf format string against parameter list?

I have a list of numbers and want to give my users the option to enter a printf-style format string to specify how the numbers should be output. How can I validate the user-supplied format string against my parameter list? Malformed input should not…
tbleher
  • 1,077
  • 1
  • 9
  • 17
3
votes
2 answers

Why char specified as integer in printf gets printed correctly

Why does the following code work? char c = 'A'; printf("%d - size: %d", c, sizeof(c)); Prints out: 65 - size: 1 Why is the output not garbage, since an int would usually be 4 bytes long and we can clearly see that the char is 1 byte long. Does…
NightRain23
  • 143
  • 1
  • 5
3
votes
0 answers

Extra \1 Character In Printf

I'm working on a C program to solve the Dining Philosophers' problem. I loop through 500 philosopher state changes, and output each philosophers' status on each interation with the following statement: printf(" %d> |%s|%s|%s|%s|%s \n", i,…
Dakota B
  • 138
  • 9
3
votes
2 answers

bash echo environment variable containing escaped characters

I have an script that echo the input given, into a file as follows: echo $@ > file.txt When I pass a sting like "\"" I want it to exactly print "\"" to the file however it prints ". My question is how can I print all characters of a variable…
masec
  • 584
  • 5
  • 16
3
votes
2 answers

-NaN in printf in C

I am currently experiencing issues with a Raytracer "Engine" in some calculations. info->eyex = -1000.0; info->eyey = 0.0; printf("%f et %f et %f et %f et %f\n", info->eyex, info->vx, info->eyey, info->vy, info->vz); For example, in that…
Unlimited
  • 31
  • 1
  • 1
  • 2
3
votes
1 answer

A simple C program with unexpected output

I am trying to solve leetcode 506. (Relative Ranks problem). This is the c code not complete yet,and there's a puzzle that output is unexpected. #include #include int main() { int i=0,j,nums[]={5,4,3,2,1},tmp; char…
Howard
  • 143
  • 1
  • 1
  • 12
3
votes
1 answer

Can anyone give me some explanations about fprintf('How about single quote('')?\n') in MATLAB?

>> fprintf('How about single quote('')?\n') How about single quote(')? the output is the same as: >> fprintf("How about single quote(\')?\n") How about single quote(')? which is more general to comprehend (Escape character is composed of a…
livemyaerodream
  • 890
  • 6
  • 19
3
votes
2 answers

Format strings safely when vsnprintf is not available

I am writing code that needs to format a string, and I want to avoid buffer overruns. I know that if vsnprintf is available (C99 onwards) we can do: char* formatString(const char *format, ...) { char* result = NULL; va_list ap; …
user2891462
  • 3,033
  • 2
  • 32
  • 60
1 2 3
99
100