printf interview questions
Top printf frequently asked interview questions
How do you escape the % sign when using printf
in C?
printf("hello\%"); /* not like this */
Source: (StackOverflow)
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)
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)
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? Is this POSIX behavior? How might I have printf
immediately flush every time?
Source: (StackOverflow)
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)
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)
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)
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)
#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)
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)
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)
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)