Trying to understand the basics

+3 Artem Tarasov · February 15, 2015
/*Hello guys, I am trying to get a full understanding of what is going on inside of computer whenever I type execute a code, so please help me to figure it out, thank you*/



#include <stdio.h>  //that is the library that I the code is going to use

int main (void)  //main function, though I am not really sure about the purpose of "void" here


{
if (0) // I did not even input any value, with what is this "if" statement is  going to compare 0?
printf ("1");
else
printf ("2");
return 0; /* every code have to return something.... okay, but why would you need to put it everywhere, for example here program would just print 2.*/
}

/*I will be happy with any explanation, of how this running thank you.*/


Post a Reply

Replies

Oldest  Newest  Rating
+1 Artem Tarasov · February 15, 2015
Really clear explanation,thank you, that helps.
+2 Lazar Bulic · February 15, 2015
Ok lets start:

#include <stdio.h> 

This line program uses to find necessary information. Its like a dictionary. If you find a word you don't know you will look it in a dictionary. So this does the same thing. When program comes across something he doesn't know he looks in dictionary stdio.h

For int main(void) you can find the answer hear:
http://www.geeksforgeeks.org/difference-int-main-int-mainvoid/

if(0)
If statement works like,  well, I will put it like this. The state of things inside of () in if can only be 0 or 1. If the state is 0 then code inside if won't be initialized but if state is 1 it will be initialized.
So in your example if(0){/*code inside if*/} the code inside if will never be initialized.
So how does this work with  equations. Here are some examples:
if(32==5) {} // 32 != 5 so the state is 0 and the code won't be initialized.
if(3+5 == 8){} // this equations is correct so the code inside if will be initialized.

return 0;
So I guess that you are just beginning to learn C. So until you learn functions and more stuff I can't explain to you the usage of return. So remember to write return 0; at the end of every int main(){}
  • 1

C

107,203 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator