473,899 Members | 2,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cin/cout vs. scanf/printf

Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster? And if it is, is it legal to mix scanf/printf with C++ code?
Program should execute below 1 sec and the hint is to use scanf/printf.

Aug 3 '06 #1
25 9753
Podrzut_z_Lawec zki wrote:
Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?
All generalizations are false.
And if it is, is it legal to mix scanf/printf with C++ code?
Yes, it is legal. scanf/printf are parts of C++.
Program should execute below 1 sec and the hint is to use
scanf/printf.
The hint to whom?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '06 #2
Podrzut_z_Lawec zki wrote:
Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?
Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.
And if it is, is it legal to mix scanf/printf with C++ code?
Look into ios_base::sync_ with_stdio() (e.g., at
http://www.cplusplus.com/ref/iostrea...th_stdio.html).
Program should execute below 1 sec and the hint is to use scanf/printf.
It depends on your compiler and optimizer, your computer, your data
source, your programming skill, etc. etc. etc., not just C vs. C++
style I/O.

Cheers! --M

Aug 3 '06 #3
On 3 Aug 2006 12:54:00 -0700, "Podrzut_z_Lawe czki" <em*****@o2.p l>
wrote in comp.lang.c++:
Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?
Yes, it's true. No, it's not true. Neither the C nor C++ standard
specify the absolutely or relative speed of any operator or function.
The C++ standard does specify the order of some operations, but order
alone does not define speed.
And if it is, is it legal to mix scanf/printf with C++ code?
It is legal to use <cstdiofunction s in a C++ program is you use them
correctly. It is rarely a good idea to use scanf() in either C or
C++, as it is very difficult and complex to use correctly.
Program should execute below 1 sec and the hint is to use scanf/printf.
What program? Who's hint? How long does the program take when you
use the cin and cout streams?

First write a program that runs correctly and meets all its
requirements. Then test it thoroughly, especially for corner cases.
Then, and only then, if it runs correctly but too slowly, you can
start thinking about ways to make it faster.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 3 '06 #4
mlimber <ml*****@gmail. comwrote:
Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.
Almost, except when you have a pointer to char, at which point it tries
to treat it as a C-style string. For example:
#include <iostream>

int main()
{
int i = 42;
std::cout << "&i = " << &i << '\n';

char c = 'a';
std::cout << "&c = " << &c << '\n';
}
Output I got:

&i = 0012FEE4
&c = a*
In order to work around this, one method is to perform a cast:

std::cout << "&c = " << static_cast<voi d*>(&c) << '\n';

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 3 '06 #5

Podrzut_z_Lawec zki wrote:
Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster? And if it is, is it legal to mix scanf/printf with C++ code?
Program should execute below 1 sec and the hint is to use scanf/printf.
I tested this once, I think, and the result was negative...c++ streams
where faster. At any rate, it is usually rather insignificant in the
larger scheme...string and stream operations are not usually your
bottlenecks.

Aug 3 '06 #6
mlimber wrote:
Podrzut_z_Lawec zki wrote:
>>Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?


Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.
Did you make the stupid error in the printf line
on purpose or dont you know any better?
Any body used to using printf and its cousins,
knows that it is stupid to specify a char instead
of a string,and I am sure you can make just as
many stupid errors in the cout line.The cout
line might do the correct thing with an input
error,but it is stil an error.
If you want to prove a point, dont use false
arguments.
Aug 3 '06 #7
Hi

Podrzut_z_Lawec zki wrote:
Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?
Could be faster. Could as well be slower.
Program should execute below 1 sec and the hint is to use scanf/printf.
Wild guess: you're doing some contest problems with huge inputs. Use
scanf/printf.

Markus

Aug 3 '06 #8
Sjouke Burry schrieb:
mlimber wrote:
>Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.

Did you make the stupid error in the printf line
on purpose or dont you know any better?
Any body used to using printf and its cousins,
knows that it is stupid to specify a char instead
of a string,and I am sure you can make just as
many stupid errors in the cout line.The cout
line might do the correct thing with an input
error,but it is stil an error.
If you want to prove a point, dont use false
arguments.
Another arguments from another person:

1)
<beginner's mode>
size_t mysize = some_container. size();

printf("%u", mysize);

size_t is unsigned int, isn't it? Or what is the correct modifier for
size_t?
</>

2)
some_typedef myvar;
printf("???", myvar);

How to print unknown types, or types that may change/be redefined?

3)
myclass myobject;
printf("???", myobject);

How to print custom classes?
With stream, the compiler handles it for you, and you can implement
operator<< for custom types.

--
Thomas
Aug 3 '06 #9
Sjouke Burry wrote:
mlimber wrote:
>Podrzut_z_Lawe czki wrote:
>>Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?


Sometimes, but the C-style I/O functions are also not typesafe, so
your development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line
always gets it right.

Did you make the stupid error in the printf line
on purpose or dont you know any better?
Any body used to using printf and its cousins,
knows that it is stupid to specify a char instead
of a string,and I am sure you can make just as
many stupid errors in the cout line.The cout
line might do the correct thing with an input
error,but it is stil an error.
If you want to prove a point, dont use false
arguments.
I suggest checking in your attitude at the door. The is no "false argument"
here. The simple demostration is supposed to show that 'printf' has no type
safety. And that's true. Besides, if you want to output an object, printf
is of no help.

Now, about the errors in the "cout line". *I* am sure that *you* can make
just as many stupid mistakes, but I would doubt very much that 'mlimber'
can (even if he tried). Now if you didn't intend to insult 'mlimber', then
you could probalby show us what kind of "stupid mistake" one can make on
a "cout line"... I am sure everybody would appreciate it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '06 #10

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

Similar topics

2
3050
by: nrhayyal | last post by:
Hello experts, are cout and printf and other ostream and stdio functions/libraries are threadsafe? i am using gnu gcc3.3.2 on AIX5.2. and my applications are multithreaded applications. i am using -lpthread as one of the compiler option. i am using cout and printf for debugging purpose. thanks for ur replies in advance. Thanks and Regards
15
7076
by: pereges | last post by:
have i written this program correctly ? it is giving me correct output but i am little suspicious in the following two statements - scanf("%d", &ptr->data) and printf("%d\n", ptr->data). /********** LINK LIST **********/ #include <stdio.h>
0
9997
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...
1
10976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10497
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
9671
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
8043
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
7204
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
5891
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
4721
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
3
3320
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.