The first C language program, C Language Program

Source: Internet
Author: User

The first C language program, C Language Program

1/* The first C Language Program 2 prints Hello, world */3 # include <stdio. h> // contains the header file stdio. h4 int main () {// main function 5 printf ("Hello, word \ n"); // print string 6 return 0; // return 0, indicates that the program runs correctly. 7}

Although this program is very simple, it may still become a major obstacle for beginners, because to achieve this goal, you must first compile the code, compile, link and run it, and finally see the output result. After mastering these operation details, it will be easier for other tasks.

The compilation and link will be described in the next section. Here we will explain the program:

  • Line 3 contains the standard library file. include is called the file inclusion command, and a file with the extension. h is called the header file.
  • Row 4th defines a function named main, which does not accept parameter values. The statements of the main function are enclosed in curly brackets. int Is the return value type of the main function.
  • Line 3 prints "Hello, world". The main function calls the library function printf to display the character sequence.
  • Row 6th indicates that the return value of the main function is 0, and return indicates that the function returns a value.
  • Line 3 ends the main function, and the Arc must appear in pairs.


The content located after "/**/" and "//" is a comment to describe the program. The comment is automatically ignored during compilation.

A c program, regardless of its size, is composed of functions and variables.

A function has certain functions and can execute specific operations. A function contains some statements to describe the operation process. The variable is used to store the values used in the calculation process.

In this example, the function name is main. Normally, there is no restriction on the function name, but main is a special function name. Every program is executed from the start point of the main function, this means that each program must contain a main function at a certain position.

The main function usually calls other functions to help complete some work. The called functions can be compiled by ourselves or from the function library. The first line of the statement # include <stdio. h> in the above section is used to tell the compiler to include the standard input/output library in this program. Many C language source programs contain this line of statements at the beginning. We will introduce the standard library in detail in subsequent chapters.

One way to exchange data between functions is to call a function to provide a list of values (called parameters) to the called function. A pair of parentheses after the function name enclose the parameter list. In this example, the main function does not require any parameters, so it is expressed by an empty parameter table.

The statements in the function are enclosed in braces. The main function in this example contains the following two statements:

printf("Hello, word\n");return 0;

When calling a function, you only need to use the function name and the parameter table enclosed by parentheses. The preceding statement uses "hello, world \ n" as the parameter to call the printf function. Printf is a library function used to print the output, where it prints the string in the middle of double quotation marks.

Character sequences enclosed in double quotation marks are called strings or string constants. For example, "hello, world \ n" is a string. Currently, only strings are used as parameters of printf and other functions.

In C, the character sequence \ n indicates a line break. When it is printed, the output print will wrap the line, starting from the beginning of the Left end line of the next line. If \ n is removed from the string (this is an exercise worth doing), it will not wrap the line even after the output is printed. In the parameters of the printf function, \ n can only be used to represent line breaks. If it is replaced by a program line break, for example:
Printf ("Hello, word
");
The C compiler will generate an error message.

The printf function will never automatically wrap the line, so that we can call this function multiple times to get a long output line in stages. The first program shown above can also be rewritten to the following form:

#include<stdio.h>int main(){    printf("Hello, ");    printf("word");    printf("\n");    return 0;}

This program is the same as the output of the previous program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.