C: Hello World

  • 2020-05-30 20:51:25
  • OfStack

First, you need a compiler for the C language. You can use an online compiler, or you can install a compiler locally. For example, Xcode can be installed on an Mac computer, and Dev C++ can be installed on an PC computer.

Write the first program: Hello World!


#include <stdio.h>

int main()
{
  /*  My first 1 a  C  The program  */
  printf("Hello, World! \n");
  
  return 0;
}

Operation results:

Hello, World!

Program analysis:

(1)
Here stdio.h is a header file. On behalf of stantard input & output. Program files in the C language are divided into header files (.h) and source files (.c), the specific concepts of which will be explained later.
Some methods are declared in stdio.h, for example, the printf function is declared in stdio.h.

#include <stdio.h>

This is to include the stdio.h file, otherwise the program will not recognize the printf function.

(2)
In C, the form is int main() {... The representation of} defines 1 function.
main is the name of the function, int stands for shaping, and int in front of main means that the function must return an integer. So this function is going to return the integer 0.
Between the curly braces is the body of the function. The body content of the function in this example is used to print Hello World and return 0. In programming languages, printing refers to printing the result in the console, rather than printing out the paper as a printer does.

(3)

/* my first C program */
In the C language, the content contained in slashes and asterisks is comment content. Annotation content is intended for human viewing and will not be compiled or run.
In the C language, there are two ways to comment. One is slash and asterisk, and the other is //
The difference between the two is:
Slashes and asterisks can be used to comment either 1 line (see program above) or multiple lines, such as:


/*  My first 1 a C The program 
   Try to print Hello World! */
// You can only comment 1 Line. If there are more lines to be used // Comments require more than one // . Such as: 

//  My first 1 a C The program 
//  Try to print Hello World! 

(4)
printf() has 1 pair of parentheses to indicate that it is also a function. There are no curly braces and no return type, because this is a function call. The main function calls the printf function.
The printf() function is declared in stdio.h, and the printf() function is defined in stdio.c. To call a function, simply include the header file in which the function is located, not the source file.
printf() is used to print out the contents of () in the console.

(5)
\n is an escape character. What is escape? For example, n originally means 1 character, and after adding a diagonal bar, it no longer means the character n, but a newline. The meaning has changed, so it's called an escape character.

(6)
return 0; Is the return value of the main() function. The main() function is a special function that only calls others and is not called by others. So it doesn't matter how much you return, you could have return 1; Or return 100;

Conclusion:

The first contact procedure, there are some things above, I don't think I can understand them all at once. But it doesn't matter, as long as you understand a half. So what we're going to do in this video is we're going to look at what the program looks like, what the results look like. If you don't understand something, you will continue to do so


Related articles: