printf

Decoding printf statements in C (Printf Primer)

I'm working on bringing some old code from 1998 up to the 21st century. One of the first steps in the process is converting the printf statements to QString variables. No matter how many times I look back at printf though, I always end up forgetting one thing or the other. So, for fun, let's decode it together, for ole' times sake and...

C++: how to get fprintf results as a std::string w/o sprintf

I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred. ...

Is there a printf converter to print in binary format?

I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base? I am running gcc. printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n" print("%b\n", 10); // prints "%b\n" ...

Java printf functionality for collections or arrays

In python you can use a tuple in a formatted print statement and the tuple values are used at the indicated positions in the formatted string. For example: >>> a = (1,"Hello",7.2) >>> print "these are the values %d, %s, %f" % a these are the values 1, Hello, 7.200000 Is there some way to use any array or collection in a java printf s...

Printing leading 0's in C?

I'm trying to find a good way to print leading 0's, such as 01001 for a zipcode. While the number would be stored as 1001, what is a good way to do it? I thought of using either case statements/if then to figure out how many digits the number is and then convert it to an char array with extra 0's for printing but I can't help but think...

printf + uint_64 on Solaris 9?

I have some c(++) code that uses sprintf to convert a uint_64 to a string. This needs to be portable to both linux and Solaris. On linux we use %ju, but there does not appear to be any equivalent on Solaris. The closest I can find is %lu, but this produces incorrect output. Some sample code: #include <stdio.h> #include <sys/types.h>...

Cross platform format string for variables of type size_t?

On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is there an elegant way to handle this? ...

How do I emulate Python's named printf parameters in Ruby?

In Python, you can do this: print "Hi! I'm %(name)s, and I'm %(age)d years old." % ({"name":"Brian","age":30}) What's the closest, simplest Ruby idiom to replicate this behavior? (No monkeypatching the String class, please.) EDIT: One of the really excellent benefits of this is that you can store the pre-processed string in a varia...

Print spinning cursor in a terminal running application using C

How would I print a spinning curser in a utility that runs in a terminal using standard C? I'm looking for something that prints: \ | / - over and over in the same position on the screen? Thanks ...

how to go back to some position already printed in C?

let us have a situation in which the following program prints some 10 lines of # for(i=0;i<10;i++) prinf("\n#"); now how to go back to 5 th line and edit that # and change the color of it without clearing the screen or clearing the below 5 lines? I have tried window(5,0,20,20); textcolor(GREEN); cprintf("#"); but it is not editing...

How to get equivalent of printf_l on Linux?

This function exists on OS X and allows you to pass custom local to the function. setlocale is not thread-safe, and passing locale as parameter is. If there is no equivalent, any way of locale-independent printf, or printf just for doubles (%g) will be ok. ...

Java: Print a 2D String array as a right-justified table

What is the best way to print the cells of a String[][] array as a right-justified table? For example, the input { { "x", "xxx" }, { "yyy", "y" }, { "zz", "zz" } } should yield the output x xxx yyy y zz zz This seems like something that one should be able to accomplish using java.util.Formatter, but it doesn't seem to allow n...

Avoid trailing zeroes in printf()

I keep stumbling on the format specifiers for the printf() family of functions. What I want is to be able to print a double (or float) with a maximum given number of digits after the decimal point. If I use: printf("%1.3f", 359.01335); printf("%1.3f", 359.00999); I get 359.013 359.010 Instead of the desired 359.013 359.01 Can ...

Left-pad printf with spaces

How can I pad a string with spaces on the left when using printf? For example, I want to print "Hello" with 40 spaces preceding it. Also, the string I want to print consists of multiple lines. Do I need to print each line separately? EDIT: Just to be clear, I want exactly 40 spaces printed before every line. ...

printing floating point numbers in D

It's been quite a while since I last used D Programming Language, and now I'm using it for some project that involves scientific calculations. I have a bunch of floating point data, but when I print them using writefln, I get results like: 4.62593E-172 which is a zero! How do I use string formatting % stuff to print such things as 0? R...

Why #include <stdio.h> is *not* required to use printf()?

Session transcript: >type lookma.c int main() { printf("%s", "no stdio.h"); } >cl lookma.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. lookma.c Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. ...

Warnings using format strings with sprintf() in C++

Compiling this lines long int sz; char tmpret[128]; //take substring of c, translate in c string, convert to int, //and multiply with 1024 sz=atoi(c.substr(0,pos).c_str())*1024; snprintf(tmpret,128,"%l",sz); I read two warning on snprintf line: warning: conversion lacks type at end of format warning: too ...

How do I print one bit?

Please tell me how do I print a bit, like printf("%d",bit); Thanks a lot ...

Are there any practical applications for the format %n in printf/scanf family?

int x; printf("hello %n World\n", &x); printf("%d\n", x); ...

sprintf() without trailing null space in C

Is there a way to use the C sprintf() function without it adding a '\0' character at the end of its output? I need to write formatted text in the middle of a fixed width string. ...