tags:

views:

591

answers:

8

Hi, 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" );
}
+10  A: 

I hope the following commented code will be of help to you:

#include <stdio.h>  // Include the header file named `stdio.h`

void SayHello( void );  // Function named `SayHello` has been declared. It takes no (void) parametres.

int main (int argc, const char * argv[]) {  // `main()` function is part of all C/C++ programs. It accepts command line arguments and returns an exit code. In this case, it returns an integer exit code.
    SayHello();  // Call the function named `SayHello`

    return 0;  // Return from the `main()` function with the code 0 (success).
}

void SayHello( void ) {  // Definition of function `SayHello`.
    printf( "Hello, world!\n" );  // Use the `printf()` function in `stdio.h` header file to print the message.
}
Alan Haggai Alavi
Most of these source code comments do not explain the code's purpose, but merely translate the code into English, effectively saying what the code is doing, leaving the reader without any deeper understanding. micmoo's comments, on the other hand (spelling and grammar aside), give meaning to each line. micmoo explains whereas Alan translates.
Dave Jarvis
@Thangalin: You are right. I have rephrased and restructured micmoo's post.
Alan Haggai Alavi
+2  A: 

Explanation ...

void SayHello( void );

This line is a function prototype. The lack of code is intentional. It's a way for the C language to say that there will a function at some point later which matches this signature. C is a top down compiler so it must know the structure of a function before it can use it.

int main (int argc, const char * argv[]) {

This is the declaration of the main function. The command line arguments to the program are contained in the argv parameter. The argc parameter notes the number of arguments that will be in the argv parameter.

SayHello();

Calls the function SayHello passing no parameters

return 0;

Ends the program by returning 0 which is an indication the program succeeded.

void SayHello( void ) {

This is a function definition signature. It represents the start of a user defined function.

printf( "Hello, world!\n" );

This will print the text "Hello, world!" followed by a new line to the console.

JaredPar
+1  A: 

Filler so the next line is recognised as a code block

#include <stdio.h>

This says, "search in the compilers search path for include files for the file 'stdio.h', and insert the contents of the files here. Note that this happens before the compiler even sees it.

void SayHello( void );

This is a function prototype. It essentially says "there will be a function defined that matches this signature.

int main (int argc, const char * argv[]) {
    SayHello();

This calls the afore mentioned function. When this is reached, execution goes into the function's body, and returns when the function ends.

    return 0;

The value 0 is returned to the system (which means no error, by convention) and the program exits. }

void SayHello( void ) {
    printf( "Hello, world!\n" );

This calls the C standard library function printf, and prints the given string to stdout.

}
Bernard
+3  A: 
#include <stdio.h>

Include the standard input/output library.

void SayHello( void );

Define a function named SayHello, with no return value and no arguments. No body is defined here - it will be defined later.

int main (int argc, const char * argv[]) {

Define another function, named main. It is the entry point into your program. It takes 2 arguments - an integer named argc (the number of command-line arguments), and a const char* array named argv (the command-line arguments). Start the body.

SayHello();

Call the SayHello method we defined earlier (run the statements inside).

return 0;

Return 0 (success) back to the operating system.

}

End the main() body.

void SayHello( void ) {

Start the body of the SayHello function defined earlier.

printf( "Hello, world!\n" );

Print "Hello, world!" to the screen. The printf function comes from the standard input/output library we included earlier.

}

End the SayHello() body.

The output of this program would be:

Hello, World!
Isaac Waller
+32  A: 

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!

micmoo
You might mention that since neither argc nor argv is used, the alternative definition of main would start: int main(void). Otherwise, a good answer.
Jonathan Leffler
Ah, that would have been a good thing to mention! If I had enough Rep, I would go ahead and add that.
micmoo
You can always edit your own questions and answers. No rep required.
Michael Myers
woah, how did I miss this :/
micmoo
+6  A: 
#include <stdio.h>

This line tells the complier to include the header file stdio.h giving the program forward declarations for the standard input/output routines in the C Standard Library.

void SayHello(void);

This is a forward declaration or function prototype for a function. A forward declaration is a way to tell the compiler that later on a function will be defined but you want it to know about it now so that you can use it. This particular function is named SayHello, it accepts no parameters and it does not return a value.

int main(int argc, const char *argv[]) {

This line indicates that the definition of a function named main. The function main is where program execution begins. This function accepts two parameters: argc is of type int and argv is a const array of char *. The parameters argc and argv represent command-line arguments. The parameter argc represents the number of command-line arguments and argv represents the actual command-line arguments. The beginning of the line, int, specifies that the function main returns a value of type int. What distinguishes this line from the previous one is that it does not end in a semicolon (;) and is followed by an opening brace ({).

    SayHello();

This line of code calls a function named SayHello. This function was previously declared, but not defined.

    return 0;

This line of code causes the main function to return the int value 0. Any caller of main will receive a return value of 0.

}

This line of code tells the compiler that we are at the end of the definition of main.

void SayHello(void) {

With this line of code you are now telling the compiler that what follows is the definition of the function SayHello that was promised earlier in line two.

    printf("Hello, world!\n");

This line of code calls the standard input/output function printf that is part of the C Standard Library. The function printf causes output to be displayed on the terminal; it is a fairly flexible routing for formatting and displaying text on the terminal. Here, you are passing one parameter, the string "Hello, world!\n". This string ends with an escape character (\n) that is translated into a newline. Calling printf with the parameter "Hello, world!\n" will cause

Hello, world!

to be displayed on the terminal.

}

This line of code tells the compiler that we are at the end of the definition of SayHello.

In short, the lines of code tell the compiler to include the declarations of the methods in the standard input/output routines of the C standard library (the definitions will be linked in later by the linker), declares a function named SayHello, defines the function main that is called when the program is executed and defines a function named SayHello. The function main calls the function SayHello and returns the int value 0. The function SayHello causes the line

Hello, world!

to be displayed on the terminal.

When the program executes, execution will begin in the function main which calls the function SayHello which prints on the terminal the string "Hello, world!" followed by a newline and then returns to main which returns the int value 0 to the caller.

Jason
+1  A: 

#include "stdio.h" <-- inserts another c++ file into this file, from some unknown folder location. Requires the program to be linked with some stdio.lib-library, which is usually added to program linkage by default though.

MyType SayHello(void); <-- declaration of function that returns MyType-class-instance and has no arguments

MyType SayHello(); <-- same thing

MyType SayHello(0); <-- creation MyType-class-instance with integer 0 passed in to the class constructor.

void SayHello(); <-- declaration of function returning nothing (having no return-statements inside)

argc/argv <-- stupid abbreviations for "argument count" and "argument vector"

void SayHello() {...} <-- code compiled in program binary file, that can be called by anyone who knows the declaration of this function.

printf( "Hello, world!\n" ); <-- jump to memory address of printf-function, with one string argument.

AareP
+6  A: 

What does each line mean?

// Include the Standard Input/Output Header to use its declared functions, ...
#include <stdio.h>

// Declare a function that takes no parameters and doesn't return a value.
void SayHello(void);

// Declare the main function which takes the program's character string
// arguments and returns an integer error code.
int main (int argc, const char * argv[]) {
    // Call the above declared function with no parameters.
    SayHello();

    // Return the error code for OK to tell the OS everything went fine.
    return 0;
}

// Define a function that takes no parameters and doesn't return a value.
void SayHello( void ) {
    // Print a (constant) character string to the console that says Hello, world!
    // and places the cursor on the next line.
    printf( "Hello, world!\n" );
}

References

How is the code being executed?

  • Someone starts the application, either using command-line or a shortcut.
  • The main function is called and given the parameters of the command line or shortcut.
  • The main function calls the SayHello procedure.
  • The SayHello procedure prints the line Hello, world! and goes to the next line.
  • Back in the main procedure we end the application by returning that everything is OK.

How does this work under the hood?

You probably shouldn't look into this detail at this point, when you feel you are ready to get an even deeper understanding of what's happening under the rood you can read the "The stack in action" section of LearnCPP: The Stack and the Heap.

TomWij