C/C++ for Hackers: Part 6 (Our First Program in C++, Using C Code in C++, and Comment Lines)

Part 6 (Our First Program in C++, Using C Code in C++, and Comment Lines)

C/C++ for Hackers: Part 6 (Our First Program in C++, Using C Code in C++, and Comment Lines)

Welcome back, fellow hackers! in this tutorial, we will switch from C to C++, learn how to use C code in our C++ programs, and a quick introduction to comment lines and why they are so useful.

Why C++ and Not C?

simple: because C++ is more powerful. also, the syntax of C++ is almost identical to C. the only big difference are the instructions. in C++, the printf equivalent is named cout. we will mostly be focusing on C++ from now on, but we will do some C too. as i said before, if you know C++ syntax you know C syntax too.

alright, now, let's get started!

Step 1: Fire Up Ubuntu and Open a Text Editor

once you have Ubuntu up and running and have a text editor open, paste this code in it. then save it as main.cpp. MAKE SURE YOU NAME IT EXACTLY LIKE THIS!.

Step 2: Compile Using G++

compiling a C++ program is the same as compiling a C program, except one thing: we need to use the g++ command. this still belongs to GCC, but is the command to compile C++ programs. you can remember this by knowing the C's in "gcc" stand for C, and the pluses in "g++" stand for the pluses in C++. just run this command like we are compiling a C program using gcc.

g++ main.cpp

if everything goes alright, we will get a a.out file again.

Step 3: Run

to run our freshly compiled program, simply type this in your terminal:


Remember that you need to allow execution privileges first!

then our program will print out "Hello Null Byte!" on the screen, just like in our C program.

Explanation Time!

alright, as we can see, we have 3 new lines, the other lines we already saw in our C program. i will explain them one by one.

#include <iostream> this is the equivalent to #include <stdio.h> in our C program, except that the library is named differently here. you may recognize "io" in both, which stands for input/output. this means that the iostream library holds all the input/output instructions of C++. (also, take note that we don't have to provide a .h extension in this library).

using namespace std; this is something extra we need to use in C++. it tells us what namespace to use from our libraries. a namespace is basically something in a program that groups instructions and functions together. here, when we use the "using" instruction, we tell our compiler to use the namespace std (which stands for "standard"). this namespace holds the standard I/O instructions, like cout.

cout << "Hello Null Byte" << endl; this is the equivalent to printf in C programs. instead of using a parameter (we will talk about that later, it is the value that you place in between the ( and ) of a function), we need to use 2 << to then specify our string to print out. after that, we can specify even more strings or even a cout command, called endl. this will just end the line, and is the equivelant to \n in C programs (you can still use \n in the same way as in C programs, by the way).

Run C Code in C++ Programs

this is simpler than you would think. you just need to #include the C library you want to use! here is an example. go ahead and delete your previous a.out, open up main.cpp, delete everything and replace it with this code. then save it and recompile it again using g++. then run the new a.out. as you can see now, we get one line printed by the printf function from C and one line by the cout function from C++!

"Why Would You Even Run C Code in C++? I Thought C++ Was More Powerful!"

yes, it is. but it doesn't have all the instructions of C. for example, the System() instruction, which allows you to execute system shell commands (i'll talk about that later.). another example would be if you found an external library and it was only developed for C.

Comment Lines

comment lines are simply lines in your program that the compiler will completely ignore. these lines are great to explain what you are doing in your program to other programmers. to make a comment line, simply type 2 slashes (//) and after that your message. here are some examples:

//This is the instruction that will print out text on the screen
//This is the main function

when you type these lines in your code, the compiler will just ignore them. this is also great for debugging your code, as it allows you to temporarily disable an instruction without completely removing it.

that is it for now folks! in the next tutorial, we will learn about variables.

if you have any questions or problems, feel free to comment them down bellow or PM me!



I never understood why you use << to print something out... shouldn't it be >>?

"<<" for cout. Eg: cout<<"Hello";
">>" for cin Eg: Phoenix is going to tell us. (Next post maybe)

Not what I meant... I mean it just seems weird that C++ was written so << was output...

Fear nailed it. you can view of them as arrows pointing to the outside (left), which specifies the string is an output. when using cin (i will cover that later) the arrows are pointed right, which means "input".

i will cover cin after we covered variables, don't worry


Before reading this tutorial I wasn't aware of running C in C++ code , Thanks for this !

Very doable. I do it all the time. You just have to make sure you include the proper libraries that C requires.

Share Your Thoughts

  • Hot
  • Latest