A lot of pretty basic questions seem to come up over and over. Let's cover those answers here.
Question: What is a good free compiler and/or IDE?
On linux, you'll generally see a separation between IDE and compiler. By far the most popular compiler for anything on linux is gcc. g++ is the gcc c++ compiler, which can be installed however your distribution installs software.
IDEs include kdevelop, Code::Blocks, Anjuta, or pretty much any text editor such as emacs or vi(m).
Question: When I run my console program, the console window doesn't stay open, so I can't see the output. What am I doing wrong?
Answer: Nothing. When a command line program is finished, it's supposed to return you back to the command line, not hang until you "press any key..." Of course, this behavior is really troublesome when you are debugging your program. There are a couple different things you can do:
- Most recommended: Use an IDE that keeps the console window open for you. Visual C++ Express, Visual Studio, and Code::Blocks all fall under this category.
- Equally as good: Run your program from the command line. (IE, use "cmd.exe")
- Least recommended: Insert a line of code that keeps your program running until you press a certain key.
If you are hell-bent on the last option, a nice solution (using C++) is as follows:
using namespace std;
// ...program is over.
<< "Press enter to exit."
Question: What is the difference between #include <foo.h> and #include "foo.h" ?
Answer: Strictly speaking, none: in both cases it is completely up to the compiler where it searches for the files (if it does at all).
However, most compilers behave the same way: #include <name> will search the compiler's "include path" for the file 'name'. #include "name" will first search the directory the including file is in, and then the include path.
Based on this, the general convention in usage is that #include "name" is used for headers that are part of the current project, and #include <name> for all others, such as standard library headers or 3rd party library headers.
Question: What is the difference between #include <iostream.h> and #include <iostream> ?
Answer: The first is wrong. It may work with some older compilers, but most modern compilers will either issue a warning or completely fail to find the file. Suffice to say that all STL headers should not have a ".h" appended to them.
Question: How do I work with strings in C/C++?
Answer: The answer depends greatly on whether or not you are using C or C++.
With C++, all you need to do is include the <string> header. You can then create and use "std::string"s which provide a ton of useful functions and simplify working with strings.
If you are using plain C, you will need to learn about character arrays and the associated functions such as strcpy(), strcat(), and so on. String handling is C is much more complicated than in C++.
using namespace std;
string s = "hello world";
<< s << endl; // print "hello world"
<< "that's "
<< " characters."
<< "'world' starts at the index: "
, s.find(" "))
<< " is what is before the first space"