C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples.
1. Basic syntax
1.1 Comments
In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments.
// This is a single line comment /* This is Multi-line comments */
1.2 Keywords
There are some keywords in C language that are used to express specific meanings and cannot be used as variable names. Common keywords includeint
,char
,if
,else
, etc.
1.3 Variable declaration
In C language, variables need to be declared first and then used. When declaring a variable, you need to specify the variable type and variable name.
int num; // Declare an integer variable num
1.4 Function definition
In C language, a function is the execution unit of code, used to encapsulate specific functions . Function definition includes function return type, function name, parameter list and function body.
int add(int a, int b) { return a b; }
1.5 Conditional statements
Conditional statements are used to execute different code blocks based on conditions. Common conditional statements includeif
statements andif-else
statements.
int x = 5; if (x > 0) { printf("x is positive"); } else { printf("x is non-positive"); }
1.6 Loop statement
Loop statement is used to repeatedly execute a specific block of code. Common loop statements includefor
loop,while
loop anddo-while
loop.
for (int i = 0; i < 5; i ) { printf("%d ", i); }
2. Data types
2.1 Basic data types
C language provides some basic data types for storing different types of data. Common basic data types includeint
,char
,float
, anddouble
, etc.
int num = 10; char ch = 'A'; float f = 3.14; double d = 3.1415926;
2.2 Array
An array is a collection that stores data of the same type. Array elements are accessed through subscripts. The declaration of an array requires specifying the array type and array size.
int arr[5] = {1, 2, 3, 4, 5};
2.3 Pointer
Pointer is a variable that stores the address of a variable and is used for indirect access data in memory. Pointer variables need to specify the pointer type and the variable type pointed to.
int *ptr; int num = 10; ptr = #
2.4 Structure
Structure is a custom data type that can store multiple different types of data. The declaration of a structure requires specifying the structure name and member variables.
struct Student { char name[20]; int age; }; struct Student stu;
Through the above introduction, we understand the basic syntax and data types of C language. To master the C language proficiently, you need to practice more and continue to study in depth. Hope this article helps you!
The above is the detailed content of Introduction to basic syntax and data types of C language. For more information, please follow other related articles on the PHP Chinese website!