[ACCEPTED]-Can you explain this 'Hello world' program?-c
I would suggest getting a different book, because 59 it probably should have told you by now. However, lets 58 get to it!
#include <stdio.h>
That part tells the "preproccesor" (part 57 of the compiler that goes through and gets 56 things ready to be compiled) to take the 55 contents of the file "stdio.h" in some special 54 part of your computer, and put it on top 53 of your source code. This way, the compiler 52 can know about the function printf
below, what 51 it takes for arguments, and what value it 50 returns (gives back after it finishes).
void SayHello( void );
This 49 part is declaring a function. By putting 48 the definition up there, you can call the 47 function before writing its implementation. This 46 function "header" tells the compiler that 45 there will be a function that returns void 44 (i.e. nothing. After it finishes, no value 43 is returned. For example, you could not write int a = SayHello();
, because 42 nothing is returned from SayHello()
). It also takes 41 no arguments. So, you could not write SayHello(34)
, because 40 it takes void arguments, meaning none.
int main (int argc, const char * argv[]) {
This 39 part is the start of the "main" function. The 38 main
function is where your computer looks to 37 start the program. This is the starting 36 point. It takes two arguments; the number 35 of arguments (int argc
) passed in on the command 34 line (for now) and an "array" of "strings 33 (char *
)". An array holds a list of the same type 32 of data. So you can have an "array" of char *
. If 31 you start your program like this: ./myProgram fish
, argv[1]
would 30 hold the array of characters (char *
) "fish". Don't 29 worry about this part too much, your book 28 will explain it later.
SayHello();
This part calls the 27 function SayHello()
. SayHello
's header is above, but the program 26 calls the actual implementation of the function 25 below. SayHello
takes no arguments, and does not 24 return any value (void
). The computer temporarily 23 jumps to the SayHello
function. When its finished, it 22 returns to the main
function where it left off. We'll 21 get to the actual SayHello
definition later.
return 0;
The 20 main
function returns an integer. If everything 19 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 18 part is the actual function we made. Its 17 one goal is to print Hello, world!
to the console. We 16 accomplish this by calling another function defined 15 in the stdio.h
file we included. Its name is printf
. It 14 takes a varying number of strings (this 13 will be covered much later in your book), and 12 prints to the console. Your book will probably 11 very very shortly cover printf fully. For 10 now, all it does is print a single string 9 (you can have it print with a format like 8 this: printf("My Num is: %d",34);
which substitutes %d
with 34
. However, for 7 now, just remember that it prints a string. So, it 6 prints "Hello world!" followed by a newline 5 (\n
). printf
returns an int
; but its just for error 4 recovery purposes. After printf
finishes, it returns 3 to the SayHello
function, which then finishes and 2 returns to main
, which finishes and returns 1 0
.
I hope this explains everything nicely!
I hope the following commented code will 1 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.
}
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
- The Standard Input/Output Header to do basic I/O operations..
- Why does main return a value and what is the best way?
- The printf function to print a line to the console.
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 5 at this point, when you feel you are ready 4 to get an even deeper understanding of what's 3 happening under the rood you can read the 2 "The stack in action" section 1 of LearnCPP: The Stack and the Heap.
#include <stdio.h>
This line tells the complier to include 58 the header file stdio.h
giving the program forward declarations 57 for the standard input/output routines in 56 the C Standard Library.
void SayHello(void);
This is a forward declaration or function prototype for a function. A forward 55 declaration is a way to tell the compiler 54 that later on a function will be defined 53 but you want it to know about it now so 52 that you can use it. This particular function 51 is named SayHello
, it accepts no parameters and 50 it does not return a value.
int main(int argc, const char *argv[]) {
This line indicates 49 that the definition of a function named 48 main
. The function main
is where program execution 47 begins. This function accepts two parameters: argc
is 46 of type int
and argv
is a const
array of char *
. The parameters 45 argc
and argv
represent command-line arguments. The parameter argc
represents 44 the number of command-line arguments and 43 argv
represents the actual command-line arguments. The 42 beginning of the line, int
, specifies that 41 the function main
returns a value of type int
. What 40 distinguishes this line from the previous 39 one is that it does not end in a semicolon 38 (;
) and is followed by an opening brace ({
).
SayHello();
This 37 line of code calls a function named SayHello
. This 36 function was previously declared, but not 35 defined.
return 0;
This line of code causes the main
function 34 to return the int
value 0
. Any caller of main
will 33 receive a return value of 0
.
}
This line of 32 code tells the compiler that we are at the 31 end of the definition of main
.
void SayHello(void) {
With this line 30 of code you are now telling the compiler 29 that what follows is the definition of the 28 function SayHello
that was promised earlier in line 27 two.
printf("Hello, world!\n");
This line of code calls the standard 26 input/output function printf
that is part of the 25 C Standard Library. The function printf
causes output to be displayed 24 on the terminal; it is a fairly flexible 23 routing for formatting and displaying text 22 on the terminal. Here, you are passing one 21 parameter, the string "Hello, world!\n"
. This string ends 20 with an escape character (\n
) that is translated into a newline. Calling 19 printf
with the parameter "Hello, world!\n"
will cause
Hello, world!
to be displayed 18 on the terminal.
}
This line of code tells 17 the compiler that we are at the end of the 16 definition of SayHello
.
In short, the lines of code 15 tell the compiler to include the declarations 14 of the methods in the standard input/output 13 routines of the C standard library (the 12 definitions will be linked in later by the 11 linker), declares a function named SayHello
, defines the 10 function main
that is called when the program 9 is executed and defines a function named 8 SayHello
. The function main
calls the function SayHello
and 7 returns the int
value 0
. The function SayHello
causes 6 the line
Hello, world!
to be displayed on the terminal.
When 5 the program executes, execution will begin 4 in the function main
which calls the function 3 SayHello
which prints on the terminal the string 2 "Hello, world!"
followed by a newline and then returns 1 to main
which returns the int
value 0 to the caller.
Explanation ...
void SayHello( void );
This line is a function prototype. The 18 lack of code is intentional. It's a way 17 for the C language to say that there will 16 a function at some point later which matches 15 this signature. C is a top down compiler 14 so it must know the structure of a function 13 before it can use it.
int main (int argc, const char * argv[]) {
This is the declaration 12 of the main function. The command line 11 arguments to the program are contained in 10 the argv parameter. The argc parameter 9 notes the number of arguments that will 8 be in the argv parameter.
SayHello();
Calls the function 7 SayHello passing no parameters
return 0;
Ends the program 6 by returning 0 which is an indication the 5 program succeeded.
void SayHello( void ) {
This is a function definition 4 signature. It represents the start of a 3 user defined function.
printf( "Hello, world!\n" );
This will print the 2 text "Hello, world!" followed by a new line 1 to the console.
#include <stdio.h>
Include the standard input/output library.
void SayHello( void );
Define 17 a function named SayHello, with no return 16 value and no arguments. No body is defined 15 here - it will be defined later.
int main (int argc, const char * argv[]) {
Define another 14 function, named main. It is the entry point 13 into your program. It takes 2 arguments 12 - an integer named argc (the number of command-line 11 arguments), and a const char* array named 10 argv (the command-line arguments). Start 9 the body.
SayHello();
Call the SayHello method we defined 8 earlier (run the statements inside).
return 0;
Return 7 0 (success) back to the operating system.
}
End 6 the main() body.
void SayHello( void ) {
Start the body of the SayHello 5 function defined earlier.
printf( "Hello, world!\n" );
Print "Hello, world!" to 4 the screen. The printf function comes from 3 the standard input/output library we included 2 earlier.
}
End the SayHello() body.
The output 1 of this program would be:
Hello, World!
#include "stdio.h" <-- inserts another 19 c++ file into this file, from some unknown 18 folder location. Requires the program to 17 be linked with some stdio.lib-library, which 16 is usually added to program linkage by default 15 though.
MyType SayHello(void); <-- declaration 14 of function that returns MyType-class-instance 13 and has no arguments
MyType SayHello(); <-- same 12 thing
MyType SayHello(0); <-- creation 11 MyType-class-instance with integer 0 passed 10 in to the class constructor.
void SayHello(); <-- declaration 9 of function returning nothing (having no 8 return-statements inside)
argc/argv <-- stupid 7 abbreviations for "argument count" and "argument 6 vector"
void SayHello() {...} <-- code 5 compiled in program binary file, that can 4 be called by anyone who knows the declaration 3 of this function.
printf( "Hello, world!\n" ); <-- jump 2 to memory address of printf-function, with 1 one string argument.
Filler so the next line is recognised as 15 a code block
#include <stdio.h>
This says, "search in the compilers 14 search path for include files for the file 13 'stdio.h', and insert the contents of the 12 files here. Note that this happens before 11 the compiler even sees it.
void SayHello( void );
This is a function 10 prototype. It essentially says "there will 9 be a function defined that matches this 8 signature.
int main (int argc, const char * argv[]) {
SayHello();
This calls the afore mentioned 7 function. When this is reached, execution 6 goes into the function's body, and returns 5 when the function ends.
return 0;
The value 0 is returned 4 to the system (which means no error, by 3 convention) and the program exits. }
void SayHello( void ) {
printf( "Hello, world!\n" );
This 2 calls the C standard library function printf, and 1 prints the given string to stdout.
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.