Thread: <iostream> <stdio.h>

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    18

    Question <iostream> <stdio.h>

    What is the difference between #include <iostrream> and <stdio.h>??

    we have began lessons on c++ programming in school and we are being taught in #include<stdio.h> and w/o "using namespace std;" and there are different codes like printf which is also cout and others..
    is one better than the other?

    and well, i don't even know what #include<****> are for..

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    stdio.h is a C header file for basic C functions like printf(). iostream is a C++ header file for basic I/O, like cout. (To use stdio.h in C++, use cstdio.) iostream.h is an older version of iostream. Standard C++ header files don't have a .h extension.

    [edit]
    and well, i don't even know what #include<****> are for..
    They have prototypes so the compiler can tell you're passing the right arguments to functions; classes so you can create instances of them; #defines so you can use constants; etc.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    18
    thank you..

    but is one of them better than the other?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    First, what the pre-compiler command #include does is simple: it pastes the content of the file specified either between " " or < >. Second, you should use cstdio over stdio.h because the latter is not standard. Third, the difference is simple, cstdio provides C-style input/output functions/structs and others while iostream provide C++ input/output functions and classes. I would generally stick to iostream when programming in C++ but I have found lately that sometimes it's better to use the good old C functions. Sometimes they are more convenient and sometimes not. It's up to you.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    I like the iostream stuff better, because the syntax is simpler.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Plus it's more extensible. (You can write your own operator<<() and cout your objects.) If you program in C++ you should use the iostream library.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Second, you should use cstdio over stdio.h because the latter is not standard.
    This isn't accurate. Both stdio.h and cstdio are standard in C++, it is just that stdio.h is deprecated in favor of cstdio.

    Besides, iostream should be preferred because it works with classes in C++.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    In some cases, using streams is just more work for nothing:

    Code:
    std::cout << "Hi " << userName << " it is now " << hours << ':' << mins << ':' << secs << " and you are the " << rank << " user." << std::endl;
    printf("Hi %s, it is now %d:%d:%d and you are the %d user", userName, hours, mins, secs, rank);

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In some cases, using streams is just more work for nothing:
    You mean replacing a conversion checked at compile time (C++) for an unchecked conversion at run time (C)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Look on the bright side: in my MinGW compiler, an <iostream> "Hello World" program is 250 KB, a <stdio.h> one is only six.

    Too much inheritance can be a bad thing
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    These days, 250kb is a negligible amount of space - especially for a Hello World program. The power of OOP is mainly for larger programs, and is generally aimed at making program easier, not more space-efficient. If space and performance is of major concern, I can see your point, but for mainstream programming, iostream makes the job A LOT easier.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look on the bright side: in my MinGW compiler, an <iostream> "Hello World" program is 250 KB, a <stdio.h> one is only six.
    Perhaps you should read what the MinGW FAQ and Stroustrup's FAQ has to say about that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    18
    Quote:
    Look on the bright side: in my MinGW compiler, an <iostream> "Hello World" program is 250 KB, a <stdio.h> one is only six.

    How is that?

  14. #14
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>How is that?<<

    If by 'how' you mean 'why' then it's for two reasons. Firstly the respective parts of the stl are statically linked and secondly because a lot of symbolic debug information is included in the built executable (this may be explained in the MinGW faq referred to by laserlight). In the first case, the size hit is only for the first inclusion; subsequent uses will not significantly add to bloat. For the second case, link with the -s (strip) option.

    You can also build and use stlport for MinGW and dynamically link with that implementation of the standard library; you'll probably want to use something like that if your require wide character support anyway; beyond std::wstring, MinGW's stl implementation has little wide character support. Executables dynamically linked with stlport's standard library give sizes comparable to those built using cstdio/stdio.h and for pretty much the same reason - dynamic linkage of the relevant libraries. An '<iostream> hello world' program is ~20kb with stlport(v5.0) with MinGW (stripped, ie, -s linker switch).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. include <iostream> appearing red
    By Tachimazu in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2009, 10:59 PM
  2. <iostream.h> or <iostream>?
    By {MaX} in forum C++ Programming
    Replies: 18
    Last Post: 06-05-2006, 12:52 AM
  3. <iostream.h> or <iostream>?
    By Yumin in forum C++ Programming
    Replies: 30
    Last Post: 01-31-2006, 02:00 AM
  4. <iostream>
    By Ivan! in forum C++ Programming
    Replies: 10
    Last Post: 05-24-2003, 08:04 PM
  5. #include <iostream> ?
    By Stan100 in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2003, 06:30 PM