国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Table of contents
Basics and data types of C language
Core concept:
User input
Conditional expression abbreviation
Switch statement
C language array
Nested loops
C language functions
Structure
pointer
Home Backend Development C++ C language starts from 0

C language starts from 0

Apr 03, 2025 pm 08:24 PM
c language ai switch string array c language programming 2025

C language starts from 0

It 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

  1. Basics and data types of C language
  2. User input
  3. Abbreviation of conditional expressions
  4. Switch statement
  5. C language array
  6. Nested loops
  7. C language functions
  8. Structure
  9. 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 and double : 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 or false , 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語句檢查'grade'的值case 'a': // 如果'grade'是'a' printf("優(yōu)秀!\n"); // 打印"優(yōu)秀!" break; // 退出switch語句case 'b': // 如果'grade'是'b' printf("良好!\n"); // 打印"良好!" break; // 退出switch語句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è)字符串最大長度為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語言編程。\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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The popularity of the currency circle has returned, why do smart people have begun to quietly increase their positions? Look at the trend from the on-chain data and grasp the next round of wealth password! The popularity of the currency circle has returned, why do smart people have begun to quietly increase their positions? Look at the trend from the on-chain data and grasp the next round of wealth password! Jul 09, 2025 pm 08:30 PM

As the market conditions pick up, more and more smart investors have begun to quietly increase their positions in the currency circle. Many people are wondering what makes them take decisively when most people wait and see? This article will analyze current trends through on-chain data to help readers understand the logic of smart funds, so as to better grasp the next round of potential wealth growth opportunities.

Bitcoin breaks new highs, Dogecoin rebounds strongly, will Ethereum keep up with the pace Bitcoin breaks new highs, Dogecoin rebounds strongly, will Ethereum keep up with the pace Jul 09, 2025 pm 08:24 PM

Recently, Bitcoin hit a new high, Dogecoin ushered in a strong rebound and the market was hot. Next, we will analyze the market drivers and technical aspects to determine whether Ethereum still has opportunities to follow the rise.

What are the mainstream public chains of cryptocurrencies? The top ten rankings of cryptocurrency mainstream public chains in 2025 What are the mainstream public chains of cryptocurrencies? The top ten rankings of cryptocurrency mainstream public chains in 2025 Jul 10, 2025 pm 08:21 PM

The pattern in the public chain field shows a trend of "one super, many strong ones, and a hundred flowers blooming". Ethereum is still leading with its ecological moat, while Solana, Avalanche and others are challenging performance. Meanwhile, Polkadot, Cosmos, which focuses on interoperability, and Chainlink, which is a critical infrastructure, form a future picture of multiple chains coexisting. For users and developers, choosing which platform is no longer a single choice, but requires a trade-off between performance, cost, security and ecological maturity based on specific needs.

Cardano's smart contract evolution: The impact of Alonzo upgrades on 2025 Cardano's smart contract evolution: The impact of Alonzo upgrades on 2025 Jul 10, 2025 pm 07:36 PM

Cardano's Alonzo hard fork upgrade has successfully transformed Cardano from a value transfer network to a fully functional smart contract platform by introducing the Plutus smart contract platform. 1. Plutus is based on Haskell language, with powerful functionality, enhanced security and predictable cost model; 2. After the upgrade, dApps deployment is accelerated, the developer community is expanded, and the DeFi and NFT ecosystems are developing rapidly; 3. Looking ahead to 2025, the Cardano ecosystem will be more mature and diverse. Combined with the improvement of scalability in the Basho era, the enhancement of cross-chain interoperability, the evolution of decentralized governance in the Voltaire era, and the promotion of mainstream adoption by enterprise-level applications, Cardano has

There are too many slanderous stories in the currency circle? Understand the key logic and risk control secrets in one article! There are too many slanderous stories in the currency circle? Understand the key logic and risk control secrets in one article! Jul 09, 2025 pm 08:33 PM

The currency circle seems to have a low threshold, but in fact it hides a lot of terms and complex logic. Many novices "rush into the market" in confusion and end up losing money. This article will give a comprehensive explanation of common terms in the currency circle, the operating logic of real money makers, and practical risk control strategies to help readers clarify their ideas and reduce investment risks.

Who issues stablecoins? What are the stablecoins? Who issues stablecoins? What are the stablecoins? Jul 09, 2025 pm 06:24 PM

Stablecoins are crypto assets that maintain price stability by anchoring fiat currencies such as the US dollar. They are mainly divided into three categories: fiat currency collateral, crypto asset collateral and algorithmic stablecoins. 1. USDT is issued by Tether and is the stablecoin with the largest market value and the highest liquidity. 2. USDC is released by the Centre alliance launched by Circle and Coinbase, and is known for its transparency and compliance. 3. DAI is generated by MakerDAO through over-collateralization of crypto assets and is the core currency in the DeFi field. 4. BUSD was launched in partnership with Paxos, and is regulated by the United States but has been discontinued. 5. TUSD achieves high transparency reserve verification through third-party escrow accounts. Users can use centralized exchanges such as Binance, Ouyi, and Huobi

Which chain does Dogecoin DOGE belong to? Does Dogecoin belong to the Binance Chain? Which chain does Dogecoin DOGE belong to? Does Dogecoin belong to the Binance Chain? Jul 10, 2025 pm 08:39 PM

Recently, the discussion in the digital asset field has remained hot. Dogecoin DOGE, as one of the most popular focus, has become a question that many people have explored. Where does it "settling down"? What is the relationship with the current leading trading platform, Binance? To answer these questions, we need to conduct in-depth analysis from the two dimensions of the underlying technical logic of digital assets and the platform ecology, rather than just staying in appearance.

Leading the top 20 token rankings in the 2025 crypto market (Latest update) Leading the top 20 token rankings in the 2025 crypto market (Latest update) Jul 10, 2025 pm 08:48 PM

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

See all articles