第一个C程序–打印Hello World消息

We have gained lots of theoretical knowledge. Now its time to move on and write our first C program to print hello world message and understand it. It is the very first program that most of the people write when start learning any language.
我们已经获得了很多理论知识。 现在该继续前进并编写我们的第一个C程序以打印问候世界消息并理解它了。 这是大多数人开始学习任何语言时编写的第一个程序。

Hello World C程序 (Hello World C Program)

This is a program to print “Hello World” message on screen.

这是一个在屏幕上打印“ Hello World”消息的程序。

#include<stdio.h>
 
void main()
{
 printf("Hello World");
}
Output 输出量

Hello World

你好,世界

Explanation

说明

Now try to understand this program step by step.

现在尝试逐步了解此程序。

1. #include<stdio.h>: First statement started with #, it is called pre-processor directive. We will learn about them thoroughly in later tutorials. #include<stdio.h> is used to include the stdio.h header file in our program. Header files contains the functions that we use in our program. Here we have used printf() function which is present in stdio.h header file.

1. #include <stdio.h>:第一个以#开头的语句,称为预处理器指令。 我们将在以后的教程中全面了解它们。 #include <stdio.h>用于在程序中包含stdio.h头文件。 头文件包含我们在程序中使用的功能。 在这里,我们使用了stdio.h头文件中存在的printf()函数。

2. void main(): Here main() is the function. A program always starts with the main() function and every program must have main(). Here void is the return type of this function. It means main() function will not return anything. The opening curly braces ({) and closing curly braces (}) shows the body of the function.

2. void main(): main()是函数。 程序始终以main()函数开头,并且每个程序都必须具有main()。 这里void是此函数的返回类型。 这意味着main()函数将不返回任何内容。 大括号({)和大括号(})显示了函数的主体。

main() can also be called as a collection of statements.

main()也可以称为语句的集合。

3. printf(): Here printf() is another function. This is used to print the values on the screen. Its general form is

3. printf():这里的printf()是另一个函数。 这用于在屏幕上打印值。 它的一般形式是

printf(“Statement you want to print on screen”);

printf(“您要在屏幕上打印的声明”);

4. Semicolon (;) is used for denoting the termination of statement. It is also called statement terminator in C Language.

4.分号(;)用于表示语句的终止。 在C语言中,它也称为语句终止符。

如何编译和执行? (How to compile and execute?)

Here we are using  Codeblocks IDE that comes with GCC compiler. If you have not downloaded it yet then follow below link to know how to download and install it.

在这里,我们使用GCC编译器随附的Codeblocks IDE。 如果尚未下载,请点击以下链接了解如何下载和安装。

Also Read: How to Install CodeBlocks IDE on Windows

另请参阅: 如何在Windows上安装CodeBlocks IDE

Watch below video know how to compile and run this hello world C program in Codeblocks.

观看下面的视频,了解如何在Codeblocks中编译和运行此Hello World C程序。

Well this is the easiest C program. Now lets move on and try slightly complicated program of multiplication of two numbers.

好吧,这是最简单的C程序。 现在让我们继续尝试两个数字相乘的稍微复杂的程序。

C程序将两个数字相乘 (C Program to Multiply two Numbers)

#include<stdio.h>
  
 void main()
 {
  int a, b, c;
  a=3;
  b=4;
  c=a*b;
  printf("Answer is %d",c);
 }
Output 输出量

Answer is 12

答案是12

Explanation

说明

Now lets try to understand this program.

现在让我们尝试了解此程序。

1. First two statements are same as above program. In the third statement I have written

1.前两个语句与上述程序相同。 在第三句话中,我写了

int a, b, c;

int a,b,c;

Here int is the keyword for integer and a, b and c are the integer variables. So they can only store integer values.

这里的int是整数的关键字,而a,b和c是整数变量。 因此它们只能存储整数值。

2. In the next two statements I have written

2.在接下来的两个陈述中,我写了

a=3; b=4;

a = 3; b = 4;

In these statements we are storing the values 3 and 4 in a and b variables respectively.

在这些语句中,我们分别将值3和4存储在a和b变量中。

3. In the next statement

3.在下一条语句中

c=a*b;

c = a * b;

We are multiplying the values in a and b, and storing them in the variable c.

我们将a和b中的值相乘,并将它们存储在变量c中。

4. Now in the last statement we are printing the value which is in c. A new thing %d which we have used in this program. It is called format specifier. It usually tell the compiler that it has to print the integer value on screen which is present in a variable.

4.现在,在最后一条语句中,我们将打印c中的值。 我们在此程序中使用了新事物%d。 它称为格式说明符。 它通常告诉编译器必须在屏幕上打印变量中存在的整数值。

Some common format specifiers are given below

下面是一些常见的格式说明符

a. %d for integers b. %c for characters c. %f for floating point numbers or real numbers

一个。 %d代表整数b。 %c代表字符c。 %f用于浮点数或实数

A bit elaborated form of printf() is given below

下面给出了printf()的详细阐述形式

printf(“string you want to print “, variable);

printf(“您要打印的字符串”,变量);

Suppose we have to print the value in b so we will use the function.

假设我们必须打印b中的值,所以我们将使用该函数。

printf(“%d”,b);

printf(“%d”,b);

注意事项 (Things to keep in mind)

1. Use semicolon at the end of each statement. 2. Type the statements in main program as the way you want them to be executed. 3. Never try to memorise any program. Just try to understand it. 4. Learning programming is all about practical. So start doing practical from now.

1.在每个语句的末尾使用分号。 2.在主程序中键入所需的语句作为执行它们的方式。 3.切勿记住任何程序。 只是尝试了解它。 4.学习编程完全是实用的。 因此,从现在开始做实践。

Comment below if you are facing any difficulty to understand your first C program to print hello world message.

如果您在理解第一个C语言程序来打印问候世界消息时遇到任何困难,请在下面发表评论。

翻译自: https://www.thecrazyprogrammer.com/2014/12/first-c-program-hello-world.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值