C language starts from 0
Apr 03, 2025 pm 08:24 PMIt may be a bit difficult to get started with C language learning, but after mastering the correct method, you will quickly master the basics and gradually master them. This guide will guide you step by step to learn the core concepts of C language, from basics to advanced topics.
Table of contents
- Basics and data types of C language
- User input
- Abbreviation of conditional expressions
- Switch statement
- C language array
- Nested loops
- C language functions
- Structure
- pointer
Basics and data types of C language
The C program follows a standard structure and defines variables using multiple data types. The basic program structure is as follows:
<code class="c">#include <stdio.h> int main() { printf("hello, world!"); return 0; }</stdio.h></code>
Core concept:
- Data type:
-
int
: integer (e.g.int x = 10;
). -
float
anddouble
: single-precision and double-precision floating point numbers (e.g.float pi = 3.14;
). -
char
: a single character or ASCII code (e.g.,char letter = 'a';
). -
bool
: Boolean value (true
orfalse
, it must include the<stdbool.h></stdbool.h>
header file).
-
<code class="c">// 數(shù)據(jù)類型示例: int a = 40; // 整數(shù)(4字節(jié)) short int b = 32767; // 短整型(2字節(jié),范圍:-32768到32767) unsigned int c = 4294967295; // 無符號(hào)整數(shù)(4字節(jié),范圍:0到4294967295) float d = 9.81; // 單精度浮點(diǎn)數(shù)(4字節(jié),精度6-7位,格式:%f) double e = 3.141592653589793; // 雙精度浮點(diǎn)數(shù)(8字節(jié),精度15-16位,格式:%lf) bool f = true; // 布爾值(1字節(jié),true/false,格式:%d,其中1=true,0=false) char g = 'e'; // 字符(1字節(jié),可用于字符或數(shù)字) char h = 100; // 字符(1字節(jié),格式:%d表示數(shù)字,%c表示ASCII碼,范圍:-128到127) char name[] = "example"; // 字符串// 變量聲明和初始化int age; // 聲明age = 5; // 初始化char language = 'c'; // 聲明和初始化// 顯示變量printf("你%d歲了", age); // 整數(shù)printf("你好%s", name); // 字符串printf("你現(xiàn)在正在學(xué)習(xí)%c", language); // 字符// 格式說明符:%d -> int, %s -> string, %c -> char, %f -> float, %.(numberofdecimals)f -> 帶指定小數(shù)位的浮點(diǎn)數(shù)</code>
- Operator:
<code class="c">/* = 加法- = 減法* = 乘法/ = 除法% = 取模= 自增1 -- = 自減1 */ // 結(jié)果需要存儲(chǔ)在與結(jié)果類型匹配的變量中// 數(shù)據(jù)類型轉(zhuǎn)換: int x = 5; int y = 2; float z = 5/2; // 錯(cuò)誤結(jié)果,因?yàn)閤和y是整數(shù)float z = 5 / (float)2; // 正確方法// 單變量自增: int x = 4; x = 2; // x = 6 x -= 2; // x = 4 x *= 2; // x = 8 x /= 2; // x = 4</code>
User input
In VS Code, you need to switch from "Output" to "Terminal" window to run the program because the terminal receives user input.
<code class="c">int age; char name[25]; // 用戶輸入整數(shù): printf("你幾歲了?\n"); // 顯示提示信息scanf("%d", &age); // 指定數(shù)據(jù)類型和變量名printf("你%d歲了", age); // 用戶輸入字符串(字符數(shù)組): printf("你的名字是?"); scanf("%s", name); printf("你好%s,你好嗎?", name); /* scanf() 不讀取空格,如果需要輸入姓名和姓氏,可以使用fgets函數(shù):結(jié)構(gòu): fgets(變量名, 大小, stdin) */ fgets(name, 25, stdin); // fgets 也包含結(jié)尾的'\n'</code>
C language is case sensitive If capitalization values ??are required, you can modify the user input to get the correct value. For example:
<code class="c">#include <ctype.h> // 我們要求用戶輸入大寫F或大寫C char unit; printf("溫度是攝氏度(c)還是華氏度(f)?"); scanf(" %c", &unit); // 注意%c前的空格,用于去除前導(dǎo)空格// 修改用戶輸入: unit = toupper(unit); // 現(xiàn)在,即使用戶輸入小寫c或f,我們也保存大寫值到unit if(unit == 'C'){ printf("溫度目前是攝氏度。"); } else if (unit == 'F'){ printf("溫度目前是華氏度。"); } else{ printf("%c 不是正確的值", unit); }</ctype.h></code>
Conditional expression abbreviation
C language uses ternary operators to simplify if-else conditional statements:
<code class="c">int max = (a > b) ? a : b;</code>
Equivalent to:
<code class="c">if (a > b) { max = a; } else { max = b; }</code>
This is a simple and efficient way to write simple conditional logic.
Switch statement
The switch statement allows processing of multiple possible values ??of a variable:
<code class="c">char grade = 'a'; // 聲明變量'grade'并初始化為'a' switch (grade) { // 開始switch語(yǔ)句檢查'grade'的值case 'a': // 如果'grade'是'a' printf("優(yōu)秀!\n"); // 打印"優(yōu)秀!" break; // 退出switch語(yǔ)句case 'b': // 如果'grade'是'b' printf("良好!\n"); // 打印"良好!" break; // 退出switch語(yǔ)句default: // 如果'grade'不是'a'或'b' printf("下次加油。\n"); // 打印"下次加油。" }</code>
Always include default case handling unexpected values.
C language array
An array is a collection of variables of the same type stored in memory in order. For example:
<code class="c">int numbers[5] = {10, 20, 30, 40, 50};</code>
Core concept:
- Access elements: Use array index, starting from 0:
<code class="c">printf("%d", numbers[0]); // 打印10</code>
- Two-dimensional array: similar to matrix or grid:
<code class="c">int matrix[2][3] = { // 聲明一個(gè)2行3列的二維數(shù)組'matrix' {1, 2, 3}, // 初始化第一行{4, 5, 6} // 初始化第二行};</code>
- String array: Arrays can also store strings:
<code class="c">// 聲明一個(gè)字符串?dāng)?shù)組'cars',每個(gè)字符串最大長(zhǎng)度為10個(gè)字符char cars[][10] = {"bmw", "tesla", "toyota"};</code>
Arrays are widely used to process data lists, grids, or tables.
Nested loops
A nested loop is when one loop contains another loop, which is usually used to deal with grids or repetitive patterns:
<code class="c">for (int i = 0; i </code>
This is great for handling multi-dimensional arrays or creating complex output.
C language functions
Functions allow code reuse. For example:
<code class="c">void greet() { printf("hello, world!\n"); printf("歡迎學(xué)習(xí)C語(yǔ)言編程。\n"); printf("讓我們開始編碼吧!\n"); } int main() { greet(); return 0; }</code>
Functions can accept parameters to make them more flexible:
<code class="c">void greet(char name[]) { printf("你好,%s!\n", name); } int main() { greet("Alice"); return 0; }</code>
Using functions helps keep code organized and reusable.
Structure
The structure ( struct
) combines the relevant variables under one name:
<code class="c">// 定義一個(gè)名為'player'的結(jié)構(gòu)體,包含兩個(gè)成員struct player { char name[50]; // 字符數(shù)組'name'存儲(chǔ)玩家姓名(最多50個(gè)字符) int score; // 整數(shù)'score'存儲(chǔ)玩家分?jǐn)?shù)}; // 創(chuàng)建一個(gè)'player'結(jié)構(gòu)體的實(shí)例并初始化struct player player1 = {"Alice", 100}; // 初始化'player1',姓名為"Alice",分?jǐn)?shù)為100 // 打印玩家姓名和分?jǐn)?shù)printf("姓名:%s,分?jǐn)?shù):%d", player1.name, player1.score); // 輸出:姓名:Alice,分?jǐn)?shù):100</code>
Structures are often used to create complex data models, such as records or objects.
pointer
Pointers are variables that store memory addresses, which can achieve efficient data processing:
<code class="c">int value = 42; // 聲明一個(gè)整數(shù)變量'value'并初始化為42 int *ptr = &value; // 聲明一個(gè)指向整數(shù)的指針變量'ptr'并將其初始化為'value'的地址printf("值:%d,地址:%p", *ptr, ptr); // 打印'ptr'指向的值和'ptr'存儲(chǔ)的地址</code>
It is crucial to target dynamic memory allocation and underlying operations in C language.
Learn C language and accumulate this practical information. Mastering these concepts will lay a solid foundation for your C programming. Take this guide as a reference and practice regularly and you will soon grow from a beginner to a C language expert. Have a happy programming!
The above is the detailed content of C language starts from 0. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The top 20 most promising crypto assets in 2025 include BTC, ETH, SOL, etc., mainly covering multiple tracks such as public chains, Layer 2, AI, DeFi and gaming. 1.BTC continues to lead the market with its digital yellow metallicity and popularization of ETFs; 2.ETH consolidates the ecosystem due to its position and upgrade of smart contract platforms; 3.SOL stands out with high-performance public chains and developer communities; 4.LINK is the leader in oracle connecting real data; 5.RNDR builds decentralized GPU network service AI needs; 6.IMX focuses on Web3 games to provide a zero-gas-free environment; 7.ARB leads with mature Layer 2 technology and huge DeFi ecosystem; 8.MATIC has become the value layer of Ethereum through multi-chain evolution

The core difference between USDC, DAI and TUSD lies in the issuance mechanism, collateral assets and risk characteristics. 1. USDC is a centralized stablecoin issued by Circle and is collateralized by cash and short-term treasury bonds. Its advantages are compliance and transparent, strong liquidity, and high stability, but there is a risk of centralized review and single point failure; 2. DAI is a decentralized stablecoin, generated through the MakerDAO protocol, and the collateral is a crypto asset. It has the advantages of anti-censorship, transparency on chain, and permission-free, but it also faces systemic risks, dependence on centralized assets and complexity issues; 3. TUSD is a centralized stablecoin, emphasizing real-time on-chain reserve proof, providing higher frequency transparency verification, but has a small market share and weak liquidity. The three are collateral types and decentralization

DAI is suitable for users who attach importance to the concept of decentralization, actively participate in the DeFi ecosystem, need cross-chain asset liquidity, and pursue asset transparency and autonomy. 1. Supporters of the decentralization concept trust smart contracts and community governance; 2. DeFi users can be used for lending, pledge, and liquidity mining; 3. Cross-chain users can achieve flexible transfer of multi-chain assets; 4. Governance participants can influence system decisions through voting. Its main scenarios include decentralized lending, asset hedging, liquidity mining, cross-border payments and community governance. At the same time, it is necessary to pay attention to system risks, mortgage fluctuations risks and technical threshold issues.

Ordinary investors can discover potential tokens by tracking "smart money", which are high-profit addresses, and paying attention to their trends can provide leading indicators. 1. Use tools such as Nansen and Arkham Intelligence to analyze the data on the chain to view the buying and holdings of smart money; 2. Use Dune Analytics to obtain community-created dashboards to monitor the flow of funds; 3. Follow platforms such as Lookonchain to obtain real-time intelligence. Recently, Cangming Money is planning to re-polize LRT track, DePIN project, modular ecosystem and RWA protocol. For example, a certain LRT protocol has obtained a large amount of early deposits, a certain DePIN project has been accumulated continuously, a certain game public chain has been supported by the industry treasury, and a certain RWA protocol has attracted institutions to enter.

In the global wave of digital asset trading, it is crucial to choose a safe, efficient and trading platform that meets your own needs. Especially for users in Asia, facing many platforms, complex functions and changing regulatory environments, how to find the best cryptocurrency platform for you and find the best cryptocurrency platform for you has become a problem for many people. This article aims to provide you with an in-depth analysis of the TOP5 cryptocurrency platforms that are most suitable for Asian users in 2025, helping you move forward steadily in the world of digital assets.

The latest version of Ouyi is v6.1271, and the update tutorial is: 1. Uninstall the old version or unofficial APK; 2. Click the official download address provided by the article to download the latest APK; 3. Enable the mobile phone installation permission; 4. Install and log in to the verification function. It is recommended to use v6.127.0 or higher. If the current version is lower than v6.124 or comes from a third-party platform, you should immediately switch to the official channel to ensure transaction security and stable functions.

The price potential of major crypto assets from 2025 to 2030 is driven by technological development, market cycles and macroeconomics. 1. Bitcoin (BTC) is expected to break through the historical high in 2025 due to the halving event and the launch of ETFs, and may reach a new order of magnitude in 2030; 2. Ethereum (ETH) benefits from network upgrades and ecological expansion, and its long-term value is bullish; 3. Projects such as Solana, BNB, and Chainlink rely on ecological development and technological stability, and the overall market will mature but be accompanied by high risks.

Ouyi okex is a professional digital asset trading and management tool, providing users with safe, stable and reliable trading services. It supports the transaction of a variety of mainstream digital assets and provides a wealth of financial tools and products to help users easily manage and configure their own digital assets.
