![]() |
|
#1
|
|||
|
|||
How to use C and C++ in same platformCan I use C and C++ in one platform (e.g is it possible to use printf and cout statements in same platform)? How to use it?
|
|
#2
|
|||
|
|||
Re: How to use C and C++ in same platformQuote:
Your title is a little misleading. You aren't using C and C++ in one platform. Almost all of the standard C library functions and functionality has been "grandfathered" into C++, so you can write a C++ program that uses C functions. Here's the scoop on C++ I/O: According to the C++ standard, output streams from C++ iostreams (the cout<< stuff) and stdio streams (the printf() stuff) are supposed to be synchronized by default. Here is an example that acts differently with different compilers. See Footnote. On my Windows XP machine, Borland C++ and GNU g++ compilers give programs that print the printf() statement, then wait for getchar() before printing the cout<< stuff. Microsoft Visual C++, version 6 (and also later versions) prints the cout stuff, then the printf stuff and waits for the keyboard input (just as if the sync_with_stdio(false) were not present). Same for current Borland compilers. CPP / C++ / C Code:
Now, if anyone is still interested, change the argument to sync_with_stdio to true. Now, they all act the same: print the cout stuff, print the printf stuff, wait for the char Now, comment out the sync_with_stdio statement completely. They should act the same as the function with the "true" argument, namely output in the expected sequence. Next, if you are still interested, change the cout statement to CPP / C++ / C Code:
and make the argument to sync_with_stdio false again. Now, it works the same as with sync_with_stdio(true) in all cases. In other words, if you flush your cout<< stuff every time (either with flush, or with endl) things are always in sync. By the way, this is the answer to the often-asked question, "What's the difference between the following two things? CPP / C++ / C Code:
The stream is not flushed by '\n', but it is flushed by endl; Sometimes it makes a difference. The GNU g++ compiler on my Linux box gives the same results as g++ on the Windows box. (sync_with_stdio(true) is the default). Well, you may be able to understand why people "in the know" generally just recommend that the two methods not be mixed. In fact mixing them is "OK" as long as the two streams are synchronized. Why did I put "OK" in quotation marks? Because some C++ implementations have been observed that apparently don't comply with the standard. In other words, Your Mileage May Vary. Regards, Dave Footnote: You might want to check the following: http://www.cplusplus.com/ref/iostrea...ith_stdio.html You can look up other references to sync_with_stdio() if you are really interested. Last edited by davekw7x : 23-Jan-2008 at 08:35.
|
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C-derived OO Prog Language, Cross Platform GUI Toolkit, 2D/3D Graphics Engine Project | jerstlouis | C Programming Language | 10 | 23-Aug-2007 07:34 |
| Reading Excel Sheets on Unix Platform | tanmay | C++ Forum | 2 | 24-Sep-2005 10:49 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 20:30 |
| adCenter - Microsoft new PPC platform | Div | Advertising & Affiliates Forum | 1 | 31-Mar-2005 14:36 |
| Platform SDK and MFC? Can any one help me out? | mingcosp | MS Visual C++ / MFC Forum | 4 | 14-Jan-2005 06:40 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The