By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,813 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,813 IT Pros & Developers. It's quick & easy.

cin/cout vs. scanf/printf

P: n/a
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
Share this Question
Share on Google+
25 Replies


P: n/a
Podrzut_z_Laweczki 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

P: n/a
Podrzut_z_Laweczki 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

P: n/a
On 3 Aug 2006 12:54:00 -0700, "Podrzut_z_Laweczki" <em*****@o2.pl>
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 <cstdiofunctions 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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 3 '06 #4

P: n/a
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<void*>(&c) << '\n';

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

P: n/a

Podrzut_z_Laweczki 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

P: n/a
mlimber wrote:
Podrzut_z_Laweczki 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

P: n/a
Hi

Podrzut_z_Laweczki 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

P: n/a
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

P: n/a
Sjouke Burry wrote:
mlimber wrote:
>Podrzut_z_Laweczki 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

P: n/a

Jack Klein wrote:
What program? Who's hint? How long does the program take when you
use the cin and cout streams?
First of all thank you all for replying. It's nothing big, just a
starter for online problems solving (Fibonacci numbers). Sry for this
-people send source files to a server (files are compiled by an
automate) and get reply with some stats - whether it passed time limit,
memory limit, if there were runtime or compilation errors etc.
On the server it is compiled with GNU C++ 4.0.2, hint about
printf/scanf is in faq. The task is here:
http://opss.safo.biz/?menu=comp&sub=...mp=0&prob=1000
I made sth like this (more or less the same as the spoiler). Are there
any free progs for Windows and Linux to measure this speed and memory
limit?

#include <cstdio>
int main()
{
double t[46] = {1,1};
for (int i = 2; i < 46; ++i)
t[i] = t[i-1] + t[i-2];
int d;
scanf("%d",&d);
if ((d>=1)&&(d<=1000)){
while (d--){
int j;
scanf("%d",&j);
printf("%.0lf\n",t[j]);
}
}
}

Aug 3 '06 #11

P: n/a
Thomas J. Gritzan wrote:

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

printf("%u", mysize);

size_t is unsigned int, isn't it?
No, it is an unsigned integral type. Usually unsigned long.
Or what is the correct modifier for
size_t?
There isn't one in C++. The new C standard added one. The usual
practice is to use ld and cast the value to unsigned long.


Brian
Aug 3 '06 #12

P: n/a
Default User wrote:
Thomas J. Gritzan wrote:

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

printf("%u", mysize);

size_t is unsigned int, isn't it?

No, it is an unsigned integral type. Usually unsigned long.
>Or what is the correct modifier for
size_t?

There isn't one in C++. The new C standard added one. The usual
practice is to use ld and cast the value to unsigned long.
Since the size of 'size_t' (which is its own type, not a typedef for
anything) is implementation-defined, you should consider looking for
an implemenation-specific solution. For example, in Win64 casting
to unsigned long is not going to work very well. Microsoft did add
the I64 specifier (IIRC) for printing out types that are longer than
unsinged long.

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 #13

P: n/a
On Thu, 03 Aug 2006 15:41:07 -0700, Podrzut_z_Laweczki wrote:
>
Jack Klein wrote:
>What program? Who's hint? How long does the program take when you
use the cin and cout streams?

First of all thank you all for replying. It's nothing big, just a
starter for online problems solving (Fibonacci numbers). Sry for this
-people send source files to a server (files are compiled by an
automate) and get reply with some stats - whether it passed time limit,
memory limit, if there were runtime or compilation errors etc.
On the server it is compiled with GNU C++ 4.0.2, hint about
printf/scanf is in faq. The task is here:
http://opss.safo.biz/?menu=comp&sub=...mp=0&prob=1000
I made sth like this (more or less the same as the spoiler). Are there
any free progs for Windows and Linux to measure this speed and memory
limit?

#include <cstdio>
int main()
{
double t[46] = {1,1};
for (int i = 2; i < 46; ++i)
t[i] = t[i-1] + t[i-2];
int d;
scanf("%d",&d);
if ((d>=1)&&(d<=1000)){
while (d--){
int j;
scanf("%d",&j);
printf("%.0lf\n",t[j]);
}
}
}

return 0;

at the end of main, to keep the compiler happy.

To see if it passes the 1 second time limit, try it yourself.
Build an input file with 1000 datasets, and time it.
On a unix system (probably windows too), you can use the
rand() or random() function for that.

Incidentally, except for cstdio, your code is all C.

Aug 3 '06 #14

P: n/a
Amadeus W. M. wrote:
On Thu, 03 Aug 2006 15:41:07 -0700, Podrzut_z_Laweczki wrote:
>>
Jack Klein wrote:
>>What program? Who's hint? How long does the program take when you
use the cin and cout streams?

First of all thank you all for replying. It's nothing big, just a
starter for online problems solving (Fibonacci numbers). Sry for this
-people send source files to a server (files are compiled by an
automate) and get reply with some stats - whether it passed time
limit, memory limit, if there were runtime or compilation errors etc.
On the server it is compiled with GNU C++ 4.0.2, hint about
printf/scanf is in faq. The task is here:
http://opss.safo.biz/?menu=comp&sub=...mp=0&prob=1000
I made sth like this (more or less the same as the spoiler). Are
there any free progs for Windows and Linux to measure this speed and
memory limit?

#include <cstdio>
int main()
{
double t[46] = {1,1};
for (int i = 2; i < 46; ++i)
t[i] = t[i-1] + t[i-2];
int d;
scanf("%d",&d);
if ((d>=1)&&(d<=1000)){
while (d--){
int j;
scanf("%d",&j);
printf("%.0lf\n",t[j]);
}
}
}


return 0;

at the end of main, to keep the compiler happy.
If your compiler is unhappy about the absence of 'return 0;',
throw away that compiler and get yourself a happier one.
To see if it passes the 1 second time limit, try it yourself.
Build an input file with 1000 datasets, and time it.
On a unix system (probably windows too), you can use the
rand() or random() function for that.

Incidentally, except for cstdio, your code is all C.
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 4 '06 #15

P: n/a
Amadeus W. M. wrote:
[...]
int main()
{
[...]
}

return 0;

at the end of main, to keep the compiler happy.
If your C++ compiler needs that, throw it away, it's not fully
compliant.
To see if it passes the 1 second time limit, try it yourself.
Build an input file with 1000 datasets, and time it.
On a unix system (probably windows too), you can use the
rand() or random() function for that.

Incidentally, except for cstdio, your code is all C.
Avtually, that's not true. C requres 'int main(void)'. The 'void'
is necessary.

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

P: n/a

Amadeus W. M. wrote:
To see if it passes the 1 second time limit, try it yourself.
Build an input file with 1000 datasets, and time it.
On a unix system (probably windows too), you can use the
rand() or random() function for that.
So there are no free programs to measure that? Or plugins or modules or
whatever that does it - freezes and shows how much time and memory does
it take? As I understand rand() would be only for creating these sets,
so I still don't get it how to "time it" - with a hand watch, or am I
writing nonsense?. Sry to bother that much.

Aug 4 '06 #17

P: n/a
Podrzut_z_Laweczki <em*****@o2.plwrote:
[..] Are there
any free progs for Windows and Linux to measure this speed and memory
limit?
There is a 'clock' function and a 'time' function which you can use
at the beginning and end of your main() function to time its execution
time. 'clock' probably has a higher resultion than 'time', tho.

hth
--
jb

(reply address in rot13, unscramble first)
Aug 4 '06 #18

P: n/a
On Thu, 03 Aug 2006 21:14:58 -0700, Podrzut_z_Laweczki wrote:
>
Amadeus W. M. wrote:
>To see if it passes the 1 second time limit, try it yourself.
Build an input file with 1000 datasets, and time it.
On a unix system (probably windows too), you can use the
rand() or random() function for that.

So there are no free programs to measure that? Or plugins or modules or
whatever that does it - freezes and shows how much time and memory does
it take? As I understand rand() would be only for creating these sets,
so I still don't get it how to "time it" - with a hand watch, or am I
writing nonsense?. Sry to bother that much.

Oh, you time it with the "time" command. Like so:

time your_progam < dataset

at a command prompt, on any unix machine. It will be a few
milliseconds. The more precise way is to use gettimeofday
(man gettimeofday), from within your program.

The cpu and memory usage (as well as time too),
you can view with the "top" command, but because your program
will finish so quickly, top won't catch it. If you want to get
serious about it, try valgrind. That's the mother of all
memory profiling tools. Will save you a lot of headaches in the
future.

You're worrying too much about a 5-line program.
Aug 4 '06 #19

P: n/a

Victor Bazarov wrote:
Avtually, that's not true. C requres 'int main(void)'. The 'void'
is necessary.
Not according to the FAQ.

http://c-faq.com/ansi/maindecl.html

I know why you believe it is, and maybe the faq is wrong...it's the
only authority I could find. A common C definition of main is:

main()
{
return 0;
}

I was trying to find something that speaks to the unspecified nature of
the accepted parameters in that declaration but couldn't coax google
into giving me any. The FAQ would lead one to believe it is ok.

Aug 4 '06 #20

P: n/a
Victor Bazarov wrote:
Default User wrote:
Thomas J. Gritzan wrote:

<beginner's mode>
size_t mysize = some_container.size();
>
printf("%u", mysize);
>
size_t is unsigned int, isn't it?
No, it is an unsigned integral type. Usually unsigned long.
Or what is the correct modifier for
size_t?
There isn't one in C++. The new C standard added one. The usual
practice is to use ld and cast the value to unsigned long.

Since the size of 'size_t' (which is its own type, not a typedef for
anything) is implementation-defined, you should consider looking for
an implemenation-specific solution. For example, in Win64 casting
to unsigned long is not going to work very well. Microsoft did add
the I64 specifier (IIRC) for printing out types that are longer than
unsinged long.
That's a way, but in my mind needless unportability. When used for its
intended purpose, holding a size of an object, size_t is very rarely
going to have a value larger than ULONG_MAX.


Brian
Aug 4 '06 #21

P: n/a
Noah Roberts posted:
A common C definition of main is:

main()
{
return 0;
}

That's valid in C89.

In C99, the implicit return value of "int" no longer applies.

Furthermore, in C, empty parentheses only specify a variable length
argument list if it's a declaration:

void Func(); /* Might be variable length */

void Func() /* Takes no arguments */
{

}

The most "proper" way of defining main in C is:

int main(void) {...

Although there's nothing wrong with:

int main()

(But they'd don't like empty parentheses.)

--

Frederick Gotham
Aug 4 '06 #22

P: n/a
Noah Roberts wrote:
>
Victor Bazarov wrote:
Avtually, that's not true. C requres 'int main(void)'. The 'void'
is necessary.

Not according to the FAQ.

http://c-faq.com/ansi/maindecl.html
Actually, the FAQ supports what Victor said.

The C99 Standard says:

5.1.2.2.1 Program startup

[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined
manner.

I know why you believe it is, and maybe the faq is wrong...it's the
only authority I could find. A common C definition of main is:

main()
{
return 0;
}
Common doesn't make it correct.
I was trying to find something that speaks to the unspecified nature
of the accepted parameters in that declaration but couldn't coax
google into giving me any. The FAQ would lead one to believe it is
ok.
That what is ok? Leaving out the void? It doesn't say that.

Brian
Aug 4 '06 #23

P: n/a
Default User wrote:
Noah Roberts wrote:

Victor Bazarov wrote:
Avtually, that's not true. C requres 'int main(void)'. The
'void' is necessary.
Not according to the FAQ.

http://c-faq.com/ansi/maindecl.html

Actually, the FAQ supports what Victor said.
From your follow-up question on comp.lang.c, I understand now what you
were getting at. Disregard this.

Brian
Aug 4 '06 #24

P: n/a

Default User wrote:
The C99 Standard says:

5.1.2.2.1 Program startup

[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined
manner.
Ok, this is getting a little OT so I'm labeling it such...

That last statement is something I find interesting. Assuming that
"implementation-defined" is the same in C as it is in C++ then by that
statement "void main()" would be perfectly /valid/ (even if not
portable) in some implementation that defines it as being such.

Aug 4 '06 #25

P: n/a
Noah Roberts wrote:
>
Default User wrote:
The C99 Standard says:

5.1.2.2.1 Program startup

[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined
manner.

Ok, this is getting a little OT so I'm labeling it such...

That last statement is something I find interesting. Assuming that
"implementation-defined" is the same in C as it is in C++ then by that
statement "void main()" would be perfectly valid (even if not
portable) in some implementation that defines it as being such.

I assume so. The C++ standard is a bit firmer in that regard, mandating
the return type but allowing the rest of the type to be
implementation-defined. This is, I assume, to support legacy stuff like
the env parameter. I don't know that C was trying specifically to allow
void return types, but frankly I don't see it as precluding them either.


Brian
Aug 4 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.