天天看点

c++初学者指南_2020年初学者终极C指南 结论 (Conclusion)

c++初学者指南

C is a high-level structured oriented programming language, used in general-purpose programming, developed by Dennis Ritchie. In the beginning, C was used for developing system applications including:

C是由Dennis Ritchie开发的,用于通用编程的高级面向结构化的编程语言。 最初,C用于开发系统应用程序,包括:

  • Database systems

    数据库系统

  • Language interpreters

    语言翻译

  • Compilers and assemblers

    编译器和汇编器

  • Operating systems

    操作系统

入门 (Getting Started)

To start learning C programming, you only have to install the C compiler in your system. Nowadays C and C++ both compilers come as a single integrated package that serves the purpose of C and C++ both program development.

要开始学习C编程,只需在系统中安装C编译器。 如今,C和C ++两种编译器都作为一个集成的软件包提供,可同时满足C和C ++两种程序开发的目的。

The best way to achieve an editor and compiler in one is Codeblocks. If you have installed Codeblocks, we can start writing code.

一个实现编辑器和编译器的最佳方法是Codeblocks 。 如果您已安装Codeblock,我们可以开始编写代码。

代币 (Tokens)

In C programs, each word and punctuation is referred to as a “token”. C Tokens are the smallest building block or smallest unit of a C program.

在C程序中,每个单词和标点符号都称为“令牌”。 C令牌是C程序的最小构件或最小单元。

Identifiers

身份标识

Identifiers are names given to different entities such as constants, variables, structures, functions, etc.

标识符是赋予不同实体的名称,例如常量,变量,结构,函数等。

int price;  
double totalprice;
           

Keywords

关键词

The C keywords must be in your information because you can not use them as a variable name.

C关键字必须包含在您的信息中,因为您不能将它们用作变量名。

#include<stdio.h> 
main() {float a, b; 
  printf("Showing how keywords are used."); 
  return 0;}
           

In the above program, “float” and “return” are keywords. The float is used to declare variables, and return is used to return an integer type value in this program.

在上面的程序中,“ float”和“ return”是关键字。 浮点数用于声明变量,而return则用于返回此程序中的整数类型值。

Constants

常数

Constants are like a variable, except that their value never changes during execution once defined.

常量就像一个变量,除了常量的值在定义后在执行期间不会改变。

const type constant_name;
           

For example:

例如:

#include<stdio.h>  main() {  
  const int SIDE = 10;   
  int area;   
  area = SIDE*SIDE;   
  printf("The area of the square with side: %d is: %d sq. units"   ,    SIDE, area); }
           

Basic Operators

基本运算符

  • + (Addition)

    +(加法)

  • - (Subtraction)

    -(减法)

  • * (Multiplication)

    *(乘法)

  • / (Division)

    /(部门)

  • % (Modulus)

    %(模量)

For example, adding two numbers:

例如,将两个数字相加:

#include <stdio.h>  void main() { 
  int i=3,j=7,k;  k=i+j;    printf("sum of two numbers is %d\n", k);
}
           

变数 (Variables)

Variables are memory locations (storage areas) in the C programming language. In C we have the following data types:

变量是C编程语言中的存储位置(存储区域)。 在C中,我们具有以下数据类型:

  • INT (stores integers)

    INT(存储整数)

  • DOUBLE (stores floating-point numbers)

    DOUBLE(存储浮点数)

  • CHAR (stores single characters)

    CHAR(存储单个字符)

  • STRING (stores text)

    STRING(存储文字)

  • BOOL (stores values with two states: true or false)

    BOOL(存储具有两种状态的值:true或false)

Variable Declaration

变量声明

char letter='A';
           

数组 (Arrays)

The array is a data structure in C programming that can store a fixed-size sequential collection of elements of the same data type.

数组是C编程中的数据结构,可以存储相同数据类型的元素的固定大小的顺序集合。

Defining an Array

定义数组

type arrayName [ size ];
           

Initialize an Array

初始化数组

int age[2]={16, 19};
           

Accessing Elements

访问元素

int myArray[3];
int n = 0;for(n=0;n<sizeof(myArray)/sizeof(myArray[0]);n++)
{
  myArray[n] = n;
}int a = myArray[3];
           

C程序的结构 (Structure of a C program)

An example of a C program:

一个C程序的例子:

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

The above example has been used to print Hello Medium Text on the screen.

上面的示例已用于在屏幕上打印Hello Medium Text。

条件语句 (Conditional Statements)

C has the following conditional statements:

C具有以下条件语句:

  • If

    如果

  • If Else

    如果别的

  • Switch

    开关

  • Goto

If Statement

如果声明

If statements are used to execute a block of code if a specific condition is true, for example:

如果满足特定条件,则使用if语句执行代码块,例如:

if(test_expression) {      statement 1;     
 statement 2;     
 ... }
           

If Else Statement

否则声明

If else statements are used to specify a new condition if the first condition is false. For example:

如果第一个条件为false,则使用else else语句指定新条件。 例如:

if(test_expression) {   
  //execute your code } else {  
  //execute your code }
           

Switch

开关

The switch statement is used to select one of many code blocks to be executed. For example:

switch语句用于选择要执行的许多代码块之一。 例如:

switch(variable)
{
case 1:
   //execute your code
break;case n:
   //execute your code
break;default:
   //execute your code
break;
}
           

Goto

C supports a unique form of a statement that is the ‘Goto’ Statement which is used to branch unconditionally within a program from one point to another. For example:

C支持一种独特的语句形式,即“ Goto”语句,该语句用于在程序中从一个点无条件地分支到另一个点。 例如:

goto label;- - -- -   -
 - - - - - - - -label:statement - X;
/* This the forward jump of goto statement */
           

循环 (Loops)

Loops can execute a block of code as long as a specified condition is reached.

只要达到指定条件,循环就可以执行代码块。

While Loop

While循环

The while loop loops through as long as a specific condition is true. For example:

只要满足特定条件,while循环就会循环通过。 例如:

While (condition)
{
   statement(s);
   Incrementation;
}
           

Do While Loop

循环执行

C do while loops are very similar to the while loops, but they always execute the code block at least once and furthermore as long as the condition remains true. For example:

C do while循环与while循环非常相似,但是只要条件保持为真,它们总是至少执行一次代码块。 例如:

do
{
   statement(s);} while( condition );
           

For Loop

对于循环

When you know exactly how many times you want to loop through a block of code, you can use the for loop. For example:

当您确切知道要遍历一段代码的次数时,可以使用for循环。 例如:

for ( init; condition; increment )
{
   statement(s);
}
           

结论 (Conclusion)

C is a very fast programming language to work with that is easy to learn. I hope that you have learned the fundamentals of C from this guide. With this knowledge, you should be able to explore the world of C yourself. Thanks for reading my article.

C是一种非常快速的编程语言,易于学习。 希望您从本指南中学到了C的基础知识。 有了这些知识,您应该能够自己探索C的世界。 感谢您阅读我的文章。

翻译自: https://codeburst.io/the-ultimate-c-guide-for-beginners-in-2020-ecf46e7a0a2a

c++初学者指南