Now Reading
C/C++ for Hackers4 min read
The short URL of the present article is: https://4hax.net/6xNjV

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 this is the equivalent to #include 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 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

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!


The short URL of the present article is: https://4hax.net/6xNjV
What's your reaction?
About The Author

Leave a Response