EzDevInfo.com

printf interview questions

Top printf frequently asked interview questions

How to escape the % sign in C's printf?

How do you escape the % sign when using printf in C?

printf("hello\%"); /* not like this */

Source: (StackOverflow)

How to pass variable number of arguments to printf/sprintf

I have a class that holds an "error" function that will format some text. I want to accept a variable number of arguments and then format them using printf.

Example:

class MyClass
{
public:
    void Error(const char* format, ...);
};

The Error method should take in the parameters, call printf/sprintf to format it and then do something with it. I don't want to write all the formatting myself so it makes sense to try and figure out how to use the existing formatting.


Source: (StackOverflow)

Advertisements

Why wasn't a specifier for `float` defined in `printf`?

It looks like it could have been, there are (at least in C99) length modifiers that can be applied to int: %hhd, %hd, %ld and %lld mean signed char, short, long and long long. There is even a length modifier applicable to double: %Lf means long double.

The question is why did they omit float? Following the pattern, it might have been %hf.


Source: (StackOverflow)

Why use sprintf function in PHP?

I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still confused, why would you want to use it?

Take a look at my example below.

Why use this:

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

When this does the same and is easier to write IMO:

$output = 'Here is the result: ' .$result. ' for this date ' .$date;

Am I missing something here?


Source: (StackOverflow)

Why does printf not flush after the call unless a newline is in the format string?

Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?


Source: (StackOverflow)

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 anybody help me?


Source: (StackOverflow)

JavaScript equivalent to printf/string.format

I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET).

My basic requirement is a thousand separator format for numbers for now, but something that handles lots of combinations (including dates) would be good.

I realize Microsoft's Ajax library provides a version of String.Format(), but we don't want the entire overhead of that framework.


Source: (StackOverflow)

printf vs cout in C++

What is the difference between printf() and cout in C++?


Source: (StackOverflow)

How does the below program output `C89` when compiled in C89 mode and `C99` when compiled in C99 mode?

I've found this C program from the web:

#include <stdio.h>

int main(){

    printf("C%d\n",(int)(90-(-4.5//**/
    -4.5)));

    return 0;
}

The interesting thing with this program is that when it is compiled and run in C89 mode, it prints C89 and when it is compiled and run in C99 mode, it prints C99. But I am not able to figure out how this program works.

Can you explain how the second argument of printf works in the above program?


Source: (StackOverflow)

Why is printf with a single argument (without conversion specifiers) deprecated?

In a book that I'm reading, it's written that printf with a single argument (without conversion specifiers) is deprecated. It recommends to substitute

printf("Hello World!");

with

puts("Hello World!");

or

printf("%s", "Hello World!");

Can someone tell me why printf("Hello World!"); is wrong? It is written in the book that it contains vulnerabilities. What are these vulnerabilities?


Source: (StackOverflow)

How does this program work?

#include <stdio.h>

int main() {
    float a = 1234.5f;
    printf("%d\n", a);
    return 0;
}

It displays a 0!! How is that possible? What is the reasoning?


I have deliberately put a %d in the printf statement to study the behaviour of printf.


Source: (StackOverflow)

Format number as fixed width, with leading zeros

this question is related to this one.

The following code

a <- seq(1,101,25)
b <- paste("name", 1:length(a), sep = "_")

produces this output:

"name_1"  "name_26"  "name_51"  "name_76"  "name_101"

I'd like to have the same width of all values which means for me to fill the values with zeros like this:

"name_001"  "name_026"  "name_051"  "name_076"  "name_101"

How to handle that?

Thanks in advance!
Arne


Source: (StackOverflow)

What is the use of the %n format specifier in C?

What is the use of the %n format specifier in C? Could anyone explain with an example?


Source: (StackOverflow)

What is the difference between printf() and puts() in C?

I know you can print with printf() and puts(). I can also see that printf() allows you to interpolate variables and do formatting.

Is puts() merely a primitive version of printf(). Should it be used for every possible printf() without string interpolation?


Source: (StackOverflow)

What is the printf format specifier for bool?

Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool?

I mean something like in that pseudo code:

bool x = true;
printf("%B\n", x);

which would print:

true

Source: (StackOverflow)