Analog5:000 Article 009
From Hak5
Intro to C and C++
By: TomB
Published: January 20th, 2007
Many of you will be reading this because you wish to learn how to code or you know how to code and want to learn something new. I will generally be using C and C++. I will assume anyone reading this has little to no knowledge of C or C++.
To start off with I'll show you simple console input and output in C and C++. To follow these examples you will need a C/C++ compiler with a standard C library, and standard C++ library I will provide a list of good compilers at the end of the section. It is a good idea to read your compilers help files to understand how to use it.
Console output
Console output is basically display any type of output on a console (terminal). Generally this is usually text. For the first example we are going to output 'Trust Your Technolust' onto the console. After I will example what the lines of code do.
[code]
#include <stdio.h>
int main()
{
printf("Trust Your Technolust!\n");
return 0;
}
[/code]
Compile this with your compiler and run the executable. If everything went well it should display 'Trust Your Technolust!' on the console. Now I will explain what each of these lines is doing.
1. #include <stdio.h>
#include is a preprocessor directive which tells the preprocessor
to include the file 'stdio.h'. The file stdio.h contains all
the function prototypes for standard input/output functions. Which
means we can use them in our project.
2. int main()
This is the main function of the program, and should always exist
in your code. The 'int' part is the type that the function
returns. Sometimes you will see 'void main()' or maybe even see
'int main(int argc, char** argv)', I will explain these another
time.
3. {
Functions require you to use curly braces. You will get used
to using these in your code.
4. printf("Trust Your Technolust!\n");
This is a function that prints text to the console screen. We
give it the argument "Trust Your Technolust!\n". This is a
string. The '\n' you see at the end is an escape character.
The one we are using means insert a newline. Also remember
to put the semicolon after function calls.
5. return 0
This tells the program to return 0 when it finished. Returning
0 means that the program didn't have any errors when executing.
6. }
We must remember to close our curly braces. This is all the code we need. it should look like the following code:
[code]
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Trust Your Technolust!\n");
return 0;
}
[/code]
When you compile this code, and run the executable. It should display the output 'Trust Your Technolust!', and exit.
Now we will look at the exact same application, but written in C++ instead.
[code]
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "Trust Your Technolust!" << endl;
return 0;
}
[/code]
As you can see this code is pretty different from it's C counter part. Now we will look at each line of code, and see what it does.
1. #include <iostream>
This does the same thing as in the C example, except it is
including the header file "iostream". You will see that
many C++ headers don't have the .h extension.
2. using namespae std;
This allows use to use the functions in the std namespace
without having to prefix them with 'std::'. This isn't
that important at the minute, but remember to include it.
3. int main(int argc, char** argv)
This is the same as explained in the C example.
4. {
The usual open bracket for the main function.
5. cout << "Trust Your Technolust!" << endl;
This is line that outputs the string to the console. As you
can see it is very different. 'cout' is the standard output
defined as a steam object. The stream object is used in
connection with the insertion operator, which is two
less-than symbols '<<'. 'endl' is a manipulator that
adds a newline. 'endl' also flushes buffered streams.
Generally 'cout' will be an unbuffered stream.
6. return 0
This is the same as explained in the C example.
7. }
This is the same as the C example. Always remember to close your opened brackets.
When you compile and run this application, it should do exactly the same as the above C example. Now we have looked at standard output, I am going to show you standard input. Which is basically getting input from the keyboard.
I will start off by showing you the example, and then explaining each line.
[code]
#include <stdio.h>
int main(int argc, char** argv)
{
char name[20];
printf("Please enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
[/code]
Please understand I am not including any error checking, because it is only the basics. When you become more experience you will be able to incorporate the needed error checking.
1. #include <stdio.h>
This is needed for the standard input/output functions in the standard
C library.
2. int main(int argc, char** argv)
This is the main function as usual.
3. {
The open bracket, get used to it you will be seeing lots of them ;-).
4. char name[20];
This is our first use of variables. To store a name we need to store
a number of characters in an array. A string is basically an array
of characters in C and C++. So we declared an array called 'name'
that can store 20 characters, enough for the average name.
5. printf("Please enter your name: ");
This is just displaying the question we are asking the user, using
the same function we used in the C example for output.
6. scanf("%s", name);
The function 'scanf' scans the standard input for the intput required
and stores them in the specified variables. So '%s' means we are taking
a string from the standard input, and we're storing it in our variable
called 'name'.
7. printf("Hello, %s!\n", name);
This shows us how we can use the 'printf' function is display output
that includes variables as well. The string we want to display is
'Hello, %s!\n', so we are displaying a string variable, and it's passed
as the second argument to the 'printf' function.
8. return 0;
This is the same as explained above.
9. }
Remember to close those open brackets ;-).
When you compile and execute this application, it'll prompt you to enter your name, when you do and press enter, it'll display 'Hello, name!', where name is your name, then exit.
Now we will look at the same example in C++.
[code]
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
char name[20];
cout << "Please enter your name: ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
return 0;
}
[/code]
1. #include <iostream>
Include the <iostream> header as usual for C++ programs.
2. using namespace std;
Allows us to use the functions without prefixing 'std::'.
3. int main(int argc, char** argv)
The main function.
4. {
The infamous open bracket.
5. char name[20];
This is the same as the variable declared in the C example.
6. cout << "Please enter your name: ";
This is the output for our question to the user.
7. cin >> name;
Now we are introduced to the standard input stream object 'cin'.
We use the extraction operator, which is two greater-than
symbols to get the string from standard input, and store it in
'name'.
8. cout << "Hello, " << name << "!" << endl;
This is outputting the message 'Hello, name!' to the user.
9. return 0;
This is the same as all the above examples.
10. }
Remember to close the open brackets.
When you run this program, it should do exactly the same as the C program. There is one thing you have to remember when using 'cin' with strings. It will only get the string up to the first blank character. This means we can only get single words. I will expand on this in the next issue.
Next issue I will expand on the different data types, and some selection and iteration.
Appendix
C/C++ Compilers:
For Windows:
- Dev-C++ - http://www.bloodshed.net/devcpp.html
- Visual C++ 2005 Express - http://msdn2.microsoft.com/en-gb/visualc/default.aspx
For Linux:
- Ubuntu and derivatives - sudo apt-get install build-essential
- (Most distributions have gcc and g++ installed.)
- TomB (Tom Bell)
- tomb@nubuntu.org
- http://ownthebox.net


