473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what's this

why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me

Feb 24 '07 #1
5 1578
On Sat, 24 Feb 2007 06:53:46 -0800, vicky wrote:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me

Some mistakes there. This is what you mean:

#include <stdio.h>

int main(){
int a = 2;
printf("%d\n", printf("%d %d ", a, a));
return 0;
}
...and that will print 2 2 4. Now for the reason for that see `man 3
printf' ( http://www.die.net/doc/linux/man/man3/printf.3.html ) which
states:

int printf(const char *format, ...);
...which means printf is a function call that returns a value of type int.
It further states:

Return value
Upon successful return, these functions return the number of characters
printed (not including the trailing ’\0’ used to end output to
strings). ... If an output error is encountered, a negative value is
returned.
Now take a look:

#include <stdio.h>

int a(int x){
return x * x;
}

int main(){
printf("%d\n", a(a(a(2))));
return 0;
}
...that will print 256 just as this will print 256:

#include <stdio.h>

int a(int x){
return x * x;
}

int main(){
int tmp1 = a(2);
int tmp2 = a(tmp1);
int tmp3 = a(tmp2);
printf("%d\n", tmp3);
return 0;
}
...get it?

--
Lars Rune Nøstdal
http://nostdal.org/

Feb 24 '07 #2
On Feb 24, 2:53 pm, "vicky" <goelvi...@gmail.comwrote:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();

}

i did not understand what these two printf in a command signifies.
please help me

Perhaps it will be clearer if you see the similarity
between your code and this:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int a=2;
int b;

b = printf( "\n%d %d", a, a );
printf("%d", b);

return EXIT_SUCCESS;
}

--
Bill Pursell

Feb 24 '07 #3
vicky wrote, On 24/02/07 14:53:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
It isn't on my system. On my system the output is a compilation error.
Perhaps if you posted your actual code (copy and past, do not retype)
then a better suggestion could be given. Although I suspect even then
the answer will be "because it is since you invoked undefined behaviour".

A few general comment though, main returns an int and it is better to be
explicit about that (it is required in the latest C standard) and it is
also better to be explicit about not taking any parameters. Having
realised main returns an int you should then return one! Calling printf
requires a prototype, so you should include <stdio.h>. Standard C does
not have a getch function, it has a getchar function (prototype in
stdio.h) which probably does what you want, although learning to drive
your IDE would be better so you do not have to make the program pause.
So a slightly improved form would be:

#include <stdio.h>

int main(void)
{
int a=2;
/* Some printf calls, but I'm not sure what you intended */
getchar(); /* better to remove this, but I'll leave it in for you */
return 0;
}
--
Flash Gordon
Feb 24 '07 #4
On 24 Feb 2007 06:53:46 -0800, in comp.lang.c , "vicky"
<go*******@gmail.comwrote:
>why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
This is essentially FAQs 3.1 & 3.2 & 3.3
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 24 '07 #5
vicky wrote:
>
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
Perfectly legitimate. Any behaviour whatsoever is allowed when you
invoke undefined behavior. On another system you might invoke
WWIII, or find demons in your snot.

Lack of #include <stdio.h>
Failure to include a '\n' in the output, or a fflush(stdout).
getch undefined
main returns int. Say so and do so.

In addition, blanks are no longer on allocation, so you can use
them without fouling the supply lines. They are quite cheap too.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Randell D. | last post by:
I have just recompiled, upgraded to PHP 4.3.4. As an exercise (and curiosity) I've decided to test out PDF functions and got the test in the PHP online manual working. I had one problem whereby...
2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
9
by: Martin Maney | last post by:
In my copious spare time I've been dabbling at getting a computerized version of a board game working. After deciding that tk just made me want to vomit, and wx was like swimming through...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
30
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
2
by: yogesh | last post by:
char TCGI::x2c(char *what) { register char digit; digit = (char) ((what >= 'A' ? ((what & 0xdf) - 'A')+10 : (what - '0'))); digit *= (char) 16; digit += (char) ((what >= 'A' ? ((what & 0xdf) -...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.