天天看點

C語言(二十二):習題練習-記憶體編碼

作者:LearningYard學苑
C語言(二十二):習題練習-記憶體編碼

分享興趣,傳播快樂,

增長見聞,留下美好!

親愛的您,這裡是LearningYard新學苑。

今天小編為大家帶來的是

C語言(二十二):習題練習-記憶體編碼。

Sharing interests,

Spread happiness,

Growth insights,

Leave behind the beauty!

Dear you, this is LearningYard New School.

Today, the editor brings you C language (22):

Exercise Exercise - Memory Coding.

一、思維導圖

此推文關于習題練習系列的内容主要如下:

The main content of this tweet about the exercise series is as follows:

C語言(二十二):習題練習-記憶體編碼

通過一系列習題的練習,進行思維和能力上的提升。

By practicing a series of exercises, improve your thinking and abilities.

二、解題思路

題目:

Title:

C語言(二十二):習題練習-記憶體編碼

首先需要定義一個字元數組 str,用于存儲使用者輸入的字元串,然後使用 fgets函數讀入使用者輸入的字元串。在循環中,使用指針 str+i通路字元串中每個字元的位址。(unsigned char*)将位址轉換成unsigned char類型的指針,以便可以輸出每個字元的存儲編碼(unsigned char型指針讀取變量在記憶體中每個位元組的内容)。使用 printf函數以%x格式輸出每個字元的存儲編碼("%x"表示輸出16進制數)。

需要注意的是,在讀入字元串時需要考慮字元串中可能包含空格和漢字等特殊字元,是以建議使用 fgets函數讀入字元串。同時,漢字在記憶體中占用兩個位元組,是以需要使用unsigned char類型的指針來輸出每個字元的存儲編碼。

Firstly, you need to define a character array str to store the user input string, and then use the fgets function to read in the user input string. In a loop, use the pointer str+i to access the address of each character in the string. (unsigned char *) Converts an address to a pointer of type unsigned char, so that the storage encoding of each character can be output (unsigned char pointers read the content of each byte of the variable in memory). Use the printf function to output the storage encoding of each character in% x format ("% x" represents the output of hexadecimal numbers). It should be noted that when reading a string, it is necessary to consider that the string may contain special characters such as spaces and Chinese characters. Therefore, it is recommended to use the fgets function to read the string. Meanwhile, Chinese characters occupy two bytes in memory, so unsigned char type pointers need to be used to output the storage encoding of each character.

關于fgets函數的知識補充:

C 庫函數 char *fgets(char *str, int n, FILE *stream) 從指定的流 stream 讀取一行,并把它存儲在 str 所指向的字元串内。當讀取 (n-1) 個字元時,或者讀取到換行符時,或者到達檔案末尾時,它會停止,具體視情況而定。

Additional knowledge on the fgets function: The C library function char * fgets (char * str, int n, FILE * stream) reads a line from the specified stream stream and stores it in the string pointed to by str. When reading (n-1) characters, or when a newline character is reached, or when it reaches the end of the file, it will stop, depending on the situation.

詳細連結:

Detailed link:

fgets函數及其用法,C語言fgets函數詳解 (biancheng.net)

C 庫函數 – fgets() | 菜鳥教程 (runoob.com)

三、代碼分析

1、輸入字元串

1. Input string

C語言(二十二):習題練習-記憶體編碼

2、去掉fgets函數讀入的換行符

2. Remove line breaks read in by the fgets function

C語言(二十二):習題練習-記憶體編碼

3、輸出字元編碼

3. Output Character encoding

C語言(二十二):習題練習-記憶體編碼

四、解題代碼

#include <stdio.h>

#include <string.h>

int main()

{

char str[101];

printf("請輸入一個字元串,長度小于等于100:");

fgets(str, 101, stdin);

str[strlen(str)-1] = '\0'; //去掉fgets函數讀入的換行符

printf("該字元串的記憶體編碼為: ");

int i;

for (i = 0; i < strlen(str); i++) {

printf("%x ", *(unsigned char*)(str+i));

}

printf("\n");

return 0;

}

結語:C語言的學習需要配合不斷地練習,對于推文有什麼需要改進的地方,代碼錯誤的地方還請大家提出來。

Conclusion: The editor thinks that the pointer part is difficult to understand, so if there is any problem in the article, please ask everyone to raise it.

今天的分享就到這裡了。

如果您對今天的文章有獨特的想法,

歡迎給我們留言,

讓我們相約明天,

祝您今天過得開心快樂!

That's all for today's sharing.

If you have unique ideas about today's article,

please leave us a message.

Let's meet tomorrow.

I wish you a happy day!

翻譯:百度翻譯

參考:菜鳥教程,百度

聲明:本文由LearningYard新學苑原創,若有侵權請聯系删除

文案&排版:易春秀

稽核:闫慶紅