Can you explain this 'Hello world' program?
c
Solution
I would suggest getting a different book, because it probably should have told you by now. However, lets get to it!
#include <stdio.h>
That part tells the "preproccesor" (part of the compiler that goes through and gets things ready to be compiled) to take the contents of the file "stdio.h" in some special part of your computer, and put it on top of your source code. This way, the compiler can know about the function `printf` below, what it takes for arguments, and what value it returns (gives back after it finishes).
void SayHello( void );
This part is declaring a function. By putting the definition up there, you can call the function before writing its implementation. This function "header" tells the compiler that there will be a function that returns void (i.e. nothing. After it finishes, no value is returned. For example, you could not write `int a = SayHello();`, because nothing is returned from `SayHello()`). It also takes no arguments. So, you could not write `SayHello(34)`, because it takes void arguments, meaning none.
int main (int argc, const char * argv[]) {
This part is the start of the "main" function. The `main` function is where your computer looks to start the program. This is the starting point. It takes two arguments; the number of arguments (`int argc`) passed in on the command line (for now) and an "array" of "strings (`char *`)". An array holds a list of the same type of data. So you can have an "array" of `char *`. If you start your program like this: `./myProgram fish`, `argv[1]` would hold the array of characters (`char *`) "fish". Don't worry about this part too much, your book will explain it later.
SayHello();
This part calls the function `SayHello()`. `SayHello`'s header is above, but the program calls the actual implementation of the function below. `SayHello` takes no arguments, and does not return any value (`void`). The computer temporarily jumps to the `SayHello` function. When its finished, it returns to the `main` function where it left off. We'll get to the actual `SayHello` definition later.
return 0;
The `main` function returns an integer. If everything went OK, `return 0`; meaning, the program exited gracefully.
} // end of the function
void SayHello( void ) { // We already covered this part above
printf( "Hello, world!\n" );
}
This part is the actual function we made. Its one goal is to print `Hello, world!` to the console. We accomplish this by calling another function defined in the `stdio.h` file we included. Its name is `printf`. It takes a varying number of strings (this will be covered much later in your book), and prints to the console. Your book will probably very very shortly cover printf fully. For now, all it does is print a single string (you can have it print with a format like this: `printf("My Num is: %d",34);` which substitutes `%d` with `34`. However, for now, just remember that it prints a string. So, it prints "Hello world!" followed by a newline (`\n`). `printf` returns an `int`; but its just for error recovery purposes. After `printf` finishes, it returns to the `SayHello` function, which then finishes and returns to `main`, which finishes and returns `0`.
I hope this explains everything nicely!
Problem
I just started learning C but I don't understand this part of the code. Can anyone explain each line of code and what it does. Thank you! Please just answer to what I said instead of saying (it should say in the book) Thank you. Hope you guys can help me. ``` #include <stdio.h> void SayHello( void ); int main (int argc, const char * argv[]) { SayHello(); return 0; } void SayHello( void ) { printf( "Hello, world!\n" ); } ```