GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jan-2008, 02:21
yogaac yogaac is offline
New Member
 
Join Date: Jan 2008
Posts: 2
yogaac is on a distinguished road

How to use C and C++ in same platform


Can 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  
Old 23-Jan-2008, 07:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to use C and C++ in same platform


Quote:
Originally Posted by yogaac
Can 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?

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:
#include <iostream>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    
    cout << "Hello, World! (from cout<<)";
    printf("Hello, World! (from printf())");
    getchar();
    return 0;
}



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:
  cout << "Hello, World! (from cout<<)" << flush;

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:
 cout << "Hello, World\n";
 cout << "Hello, World" << endl;

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 GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 03:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.