天天看點

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++初學者指南