473,659 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's this

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

main()
{
int a=2;
printf("%d",pri ntf("\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 1591
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",pri ntf("\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...@gmai l.comwrote:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",pri ntf("\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",pri ntf("\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*******@gmai l.comwrote:
>why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",pri ntf("\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",pri ntf("\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
3882
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 the example refered to Times New Roman - I didn't have this, but I did have Times-Roman which worked. My question - How do I know what fonts I *do* have available? I've run phpinfo() and gd_info() which I hope is enough to tell some wise person...
2
3078
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 burns. Here is the code: $frank = "burns";
220
19000
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
699
33868
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
9
2671
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 concrete slurry, and others I have mercifully forgotten about, I stumbled across pygame. (maybe not for the first time; I think I set it aside earlier because it was described as being aimed at a different sort of game, and besides, I had hoped wx might...
92
6445
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? cheers, reed
3
2422
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 usefull. Interesting in any case. Making decorators with this class is a snap!
12
11149
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 { public: string desc;
30
15429
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 to get both some understanding of how wide candidates knowledge is, and how much DB2 specifics they know. Of the questions below, how many do you think are useful in determining if you've got somebody capable of keeping a DB2 instance up,...
2
1631
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) - 'A')+10 : (what - '0'))); return(digit); }
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.