Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,968 people online right now. Registration is fast and FREE... Join Now!




code explanation

 
Reply to this topicStart new topic

code explanation, explanation for this code and how it works

titusraj
26 Jan, 2008 - 04:20 AM
Post #1

New D.I.C Head
*

Joined: 25 Dec, 2007
Posts: 13


My Contributions
CODE
#include<stdio.h>
#include<conio.h>
void main()
{
        clrscr();
        printf("%d",printf("%d"));
        getch();
}
//output...
//01

please explain how it works what is the concept behind this
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Code Explanation
26 Jan, 2008 - 04:55 AM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
I'm sure if this is the right explanation but what I think is this. After a little bit of meddling around I think your inner printf is outputting a 0 since there is no argument to tell what integer should be used for %d.
The outer printf then outputs the return value of the inner one. It seems to me that this return value is the number of digits outputted by the printf statement for if you try:
CODE

#include<stdio.h>
void main()
{
        printf("%d",printf("%d",9999));
}

The output is
QUOTE

99994

Notice that the last digit is the number of digits in 9999 i.e. 4.
I'm not sure if I'm right actually. This is something I noticed (I've used Visual C++ Express 2005).

Hope this helps smile.gif

P.S. I'd suggest not using getch() and clrscr(). They are non-standard functions. Secondly use:
CODE

int main()
{
/*code in main()*/
return 0;
}

instead of void main().
User is offlineProfile CardPM
+Quote Post

Bench
RE: Code Explanation
26 Jan, 2008 - 05:11 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 630



Thanked: 16 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
close. printf returns the number of characters sent to stdout.

it might be easier to see here
CODE
#include <stdio.h>

int main(void)
{
    int a,b;
    a = printf("Hello, World!\n");
    b = printf("%d", a);
    printf(" %d", b);

    return 0;
}


My output:
CODE
Hello, World!
14 2

14 characters for "Hello, World!" includes the '\n' newline character at the end

This post has been edited by Bench: 26 Jan, 2008 - 05:15 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month