English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

printf("%d", printf("tim"));
This statement provides the output as : ??
The options are:
a) syntax error b) Outputs tim 3 c) outputs garbage d) prints tim and terminates abruptly

Guyz.. I also need the reason as why is the answer so.. !!
thx

2007-04-30 04:03:41 · 3 answers · asked by missoz 1 in Computers & Internet Programming & Design

3 answers

To evaluate the first printf, we need to know the value of the second printf. Here is the prototype for printf:

int printf(const char *format, ...)

Notice that it returns an int, which is exactly what the first printf specifies to print. So the second printf is evaluated (prints "tim"), then returns an integer, and the first printf prints that integer, and returns an integer which is ignored.

The first answerer is incorrect; there is no syntax error in this code (try compiling and running it).

For what it's worth, my attempt outputted "michael 7" on stdout.

2007-04-30 04:16:57 · answer #1 · answered by Michael 5 · 4 0

Syntax error big time.

You have a printf statement inside a printf statement.

The printf statetement takes two arguments. The first is the format string, and the second is the actual value to print. You don't need the second "printf" or the paranthesis.

2007-04-30 11:14:31 · answer #2 · answered by tkquestion 7 · 1 2

The answer is b) since printf returns an int and the inner printf will be evaluated first.

2007-04-30 12:00:12 · answer #3 · answered by icnintel 4 · 1 0

fedest.com, questions and answers