Thread: Newbie Question <iostream> library

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Question Newbie Question <iostream> library

    Hi folks.

    I'm completely new to C programming.

    I installed the DEV C++ IDE compiler (by Bloodshed) on a Windows-XP machine.

    (Since I'm new, for now I want to use an IDE environment.)


    As a newbie, I noticed that when I click "compile&run" the program-execution-window instantly vanishes after the program runs, which is annoying.

    This is only my first day of learning C programming, so I'm sure soon enough I will learn how to make the program execution pause, until the users presses any key.

    (I also I know that I can run the .exe file in command line, to stop the output from vanishing, but it's not fun to be using the command line all the time!)


    Anyways, in short, to solve this "problem" I "cheated" and found a sample C++ program (not C) in which they use the "<iostream>" library and send a "pause" command to Windows-XP, to stop the window from vanishing.

    I tried it with my C program, to see if it also works in C, and it does!

    But... I'm wondering 3 things about this:


    1) Am I "allowed" to use the <iostream> library in C programs, or was that "iostream" library really just intended for C++?

    2) Since it compiles and runs, then I guess it's ok to use that library for now, and it must have been made for C usage as well?


    Using the <iostream> library to solve this problem kinda feels like "cheating" since I'm using the operating system to keep the window open, rather than figuring out how to do that within C itself.

    But again, today is only my first day... so I'll figure it out soon enough. So until then, this is how my C code looks when I use that library:


    #include <stdio.h>
    #include <iostream>

    int main()
    {
    printf("Hello.\n");

    system("PAUSE");
    }


    So it seems like the <iostream> library allows me to send commands to windows, such as "pause".

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You don't really need to use <iostream> to get your program doing what you want it to do. Have a look at this and this. Finally, Dev C++ is pretty well dead. It seems to me that people are recommending Code::Blocks an awful lot these days. Why not give that one a try. It is pretty decent, and is under active development.
    Last edited by kermit; 01-06-2011 at 06:36 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by intrinsic_value View Post
    1) Am I "allowed" to use the <iostream> library in C programs, or was that "iostream" library really just intended for C++?
    Sorry, C++ only.

    2) Since it compiles and runs, then I guess it's ok to use that library for now, and it must have been made for C usage as well?
    Nope. Look at your compiler: DEV C++ IDE. That's a C++ compiler. The reason this works is that most valid C code will compile and run as C++, so DEV C++ is treating this as C++ with some C code in it. There may be a setting somewhere to change this behavior, or you need to find a different compiler that will support C programs (sorry, I'm not familiar with DEV C++).

    Using the <iostream> library to solve this problem kinda feels like "cheating" since I'm using the operating system to keep the window open, rather than figuring out how to do that within C itself.
    The "PAUSE" command you are giving to system() is Windows specific, so if you use this, realize it's not portable to Linux, etc. You could do something like "Press ENTER to continue", then run a getchar() loop looking for a '\n' if you want to be more generic about it.
    So it seems like the <iostream> library allows me to send commands to windows, such as "pause".
    That's not the iostream library that's allowing system, that's stdlib.h (which you don't include).

  4. #4
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I think everyone did a great job answering your questions. It may help you would if you start off with something less confusing than an IDE, and in particular an IDE that was built for C++. It may help you to begin with a plain text editor and compile on your own within Windows with mingw/gcc.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    FYI, <iostream> is a header file, it is not a library. Libraries consist of pre-compiled code that gets linked to your applications compiled code typically to produce an executable as a final result. Most header files consist of function prototypes and definitions that are added to your source file during compilation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    That's true. Headers usually consist of prototypes, globals, and #defined stuff. Usually the definitions will end up in their own .c file, and everything is compiled to object code before being compiled all together. But that isn't true all the time.

    Programming is confusing!

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks everyone for these great tips.

    I've visited all the links you referred me to, and ended up doing some late night reading, and learnt a lot for my first day! (also visited 007's blog).

  8. #8
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    ^ I hope it helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. VC++ 6 question: browsing into a library
    By maxhavoc in forum Tech Board
    Replies: 4
    Last Post: 09-15-2006, 06:57 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 09:58 PM