C/C++ Programming for Hackers: Part 3 (Examining the Code)

Part 3 (Examining the Code)

C/C++ Programming for Hackers: Part 3 (Examining the Code)

Welcome back, fellow hackers! as promised, in Part 3, we will be examining the code we just compiled in part 2. this will be a short how-to, but hopefully it will give you an understanding of how C/C++ programs run.

so, let's get started!

  • #include <stdio.h> this line tells the compiler to include a library file, in this case, stdio.h. this file is built into the libraries of GCC and holds all the basic input/output instructions, like printf().
  • int main() this is the main function of the program. all programs have this function. from embedded systems to computergames and operating systems. it is always the first function that is called (called=used in technical terms) when a program runs, much like the main.c file is the first file that is called when a program runs.
  • the { and } these are called "brackets". in between these, we put our code to be executed by that function. in this case, it is only the main() function. the code in between 2 brackets is called a block of code.
  • printf("Hello Null Byte!\n"); this is the instruction that prints text on the screen. the parameters of an instruction (parameters are values that an instructions requires to run) are placed in between (). in this case, printf just needs a string to run (yes, a string REALLY is the correct name for a word/sentence in a program). in almost every programming language, the value of a string variable (i'll talk about variables in a later how-to) is placed in between quotation marks, or "". so why wasn't \n printed out on the screen? because \n is an instruction inside printf to tell the program to go to a new line. you can try to edit the main.c and remove the \n, recompile it and see for yourselves what it will do.
  • return 0; this line instructs the program to exit with code 0 (code 0 stands for "the program completed successfuly"). take note that main() is an integer (int) and needs to return a value after it finished. that is for what the "return" instruction is made. have you ever wondered what these error codes mean when a program crashes? well now you know. they are simply waypoints for developers to find where something went wrong in their program when the user encountered the bug.

also take note: all instructions end with a ;. this ; basically means "go to the next instruction." it is kind of a dot on the end of a sentence, but for a program.

that's it folks! in the next tutorial, i will talk about the differences between programming and scripting languages and their advantages/disadvantages, as there still seems to be confusion about this.

if you have any problems or questions, feel free to comment your question down below!

-Phoenix750

12 Comments

"{" and "}" are called curly braces. Just so ya know.

thanks for the correction! i always called them brackets, but i guess you can call them however you want.

-Phoenix750

nice tuto, but i have a Q
i tried doing this: (But it only showed the 1. - Hello Null bute, what did i do wrong?)
{
printf("Hello Null Byte!\n");
printf("Bye Null Bute!\n");
return 0;
}

your code seems to work for me.

you sure you wrote both instructions in the main() function?

-Phoenix750

More technically "{ }" is known as Scope !

Agreed. But I never really took time to study the terms of C/C++ code, just how to write it.

-Phoenix750

yes i did write main(), here take a look by yourself:

-------Edit---------------------------------
nvm i found out the issue by myself, But i have another Q
why is it important to make "space" between the left side to the commands like
{
(some space) "i write my commands out here"
(some space again) "I write my command out here again, why?"
}

I find it quite wierd?

good job fixing it yourself!

honestly, it isn't really important, but it makes your program easier to read for other programmers. it's kind of a social rule for programmers to use that little amount of space.

if i may ask, how did you solve this issue?

-Phoenix750

I actually just tried putting the "space" in like you did. And then it worked, but didnt you just said that it didnt do anything?

strange...

well, in theory it shouldnt. bu apperantly it does.

-Phoenix750

Phoenix is it possible to put a unique
"while" command to every "if" command. As you can see I tried doing:

I wanted to make it +/- up to 0 if it not already was 0 and a negative or positive number. So i need to make the "if negetive" (+) up to 0, and the "if positive" (-) down to 0.

And i tried doing that putting a unique "while" in to both "if" and "else."

But I got this error?
main.cpp: In function 'int main()':
main.cpp:20:3: error: 'else' without a previous 'if'
main.cpp:26:3: error: 'else' without a previous 'if'

I hope you can spot the error, i dont understand else isnt without a previous if? it says "else if" ? or "if" before.

you forgot to put a { under your first if statement.

-Phoenix750

Share Your Thoughts

  • Hot
  • Latest