{"id":504,"date":"2024-12-25T00:47:13","date_gmt":"2024-12-24T16:47:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=504"},"modified":"2024-12-25T00:47:35","modified_gmt":"2024-12-24T16:47:35","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%ae%9e%e7%8e%b0%e6%95%b0%e5%ad%97%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%9b%b8%e4%ba%92%e8%bd%ac%e6%8d%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e8%af%ad%e8%a8%80%e4%b8%ad%e5%ae%9e%e7%8e%b0%e6%95%b0%e5%ad%97%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%9b%b8%e4%ba%92%e8%bd%ac%e6%8d%a2%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728 C \u8bed\u8a00\u4e2d\u5b9e\u73b0\u6570\u5b57\u548c\u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u6362\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 C \u8bed\u8a00\u4e2d\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u548c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u5e93\u51fd\u6570\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 <code>sprintf<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sprintf<\/code> \u662f\u4e00\u4e2a\u6807\u51c6\u5e93\u51fd\u6570\uff0c\u53ef\u4ee5\u683c\u5f0f\u5316\u6570\u5b57\u5e76\u5199\u5165\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    int number = 123;\n    char str&#91;20]; \/\/ \u786e\u4fdd\u6570\u7ec4\u8db3\u591f\u5927\u4ee5\u5b58\u50a8\u7ed3\u679c\n\n    sprintf(str, \"%d\", number); \/\/ \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\n    printf(\"String: %s\\n\", str);\n\n    double floatNum = 123.456;\n    sprintf(str, \"%.2f\", floatNum); \/\/ \u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\n    printf(\"String: %s\\n\", str);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u7075\u6d3b\uff0c\u53ef\u4ee5\u683c\u5f0f\u5316\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<br><strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd\u76ee\u6807\u5b57\u7b26\u4e32\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u5b58\u50a8\u7ed3\u679c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 <code>itoa<\/code>\uff08\u975e\u6807\u51c6\u51fd\u6570\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e9b\u7f16\u8bd1\u5668\uff08\u5982 MSVC\uff09\u63d0\u4f9b <code>itoa<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt; \/\/ \u5305\u542b itoa \u7684\u58f0\u660e\n\nint main() {\n    int number = 123;\n    char str&#91;20];\n\n    itoa(number, str, 10); \/\/ \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u57fa\u6570\u4e3a 10\n    printf(\"String: %s\\n\", str);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u76f4\u63a5\u3002<br><strong>\u6ce8\u610f<\/strong>\uff1a<code>itoa<\/code> \u662f\u975e\u6807\u51c6\u51fd\u6570\uff0c\u8de8\u5e73\u53f0\u652f\u6301\u6027\u8f83\u5dee\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u624b\u52a8\u5b9e\u73b0<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u901a\u8fc7\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u5e76\u624b\u52a8\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid intToStr(int num, char* str) {\n    int i = 0, isNegative = 0;\n\n    if (num &lt; 0) { \/\/ \u5904\u7406\u8d1f\u6570\n        isNegative = 1;\n        num = -num;\n    }\n\n    do {\n        str&#91;i++] = (num % 10) + '0'; \/\/ \u63d0\u53d6\u6700\u4f4e\u4f4d\u5e76\u8f6c\u6362\u4e3a\u5b57\u7b26\n        num \/= 10;\n    } while (num &gt; 0);\n\n    if (isNegative) str&#91;i++] = '-'; \/\/ \u6dfb\u52a0\u8d1f\u53f7\n\n    str&#91;i] = '\\0'; \/\/ \u6dfb\u52a0\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\n\n    \/\/ \u53cd\u8f6c\u5b57\u7b26\u4e32\n    for (int j = 0; j &lt; i \/ 2; j++) {\n        char temp = str&#91;j];\n        str&#91;j] = str&#91;i - 1 - j];\n        str&#91;i - 1 - j] = temp;\n    }\n}\n\nint main() {\n    int number = -123;\n    char str&#91;20];\n\n    intToStr(number, str);\n    printf(\"String: %s\\n\", str);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u8de8\u5e73\u53f0\uff0c\u65e0\u9700\u4f9d\u8d56\u5e93\u51fd\u6570\u3002<br><strong>\u7f3a\u70b9<\/strong>\uff1a\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u9002\u5408\u5b66\u4e60\u548c\u7ec3\u4e60\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 <code>atoi<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>atoi<\/code> \u662f\u6807\u51c6\u5e93\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt; \/\/ \u5305\u542b atoi \u7684\u58f0\u660e\n\nint main() {\n    char str&#91;] = \"123\";\n    int number = atoi(str); \/\/ \u8f6c\u6362\u4e3a\u6574\u6570\n    printf(\"Number: %d\\n\", number);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u76f4\u63a5\u3002<br><strong>\u6ce8\u610f<\/strong>\uff1a<code>atoi<\/code> \u4e0d\u5904\u7406\u9519\u8bef\u8f93\u5165\uff0c\u8f93\u5165\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u65f6\u8fd4\u56de <code>0<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 <code>strtol<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>strtol<\/code> \u63d0\u4f9b\u66f4\u591a\u63a7\u5236\uff0c\u652f\u6301\u9519\u8bef\u68c0\u67e5\u548c\u57fa\u6570\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt; \/\/ \u5305\u542b strtol \u7684\u58f0\u660e\n\nint main() {\n    char str&#91;] = \"123abc\";\n    char* endptr;\n    long number = strtol(str, &amp;endptr, 10); \/\/ \u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u57fa\u6570\u4e3a 10\n\n    printf(\"Number: %ld\\n\", number);      \/\/ \u8f93\u51fa\u8f6c\u6362\u540e\u7684\u6570\u5b57\n    printf(\"Unconverted part: %s\\n\", endptr); \/\/ \u8f93\u51fa\u672a\u8f6c\u6362\u90e8\u5206\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u66f4\u5b89\u5168\uff0c\u53ef\u4ee5\u5904\u7406\u65e0\u6548\u8f93\u5165\u3002<br><strong>\u6ce8\u610f<\/strong>\uff1a\u68c0\u67e5 <code>endptr<\/code> \u662f\u5426\u6307\u5411\u5b57\u7b26\u4e32\u672b\u5c3e\u4ee5\u786e\u8ba4\u8f6c\u6362\u662f\u5426\u6210\u529f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 <code>sscanf<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sscanf<\/code> \u662f\u8f93\u5165\u683c\u5f0f\u5316\u51fd\u6570\uff0c\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    char str&#91;] = \"123\";\n    int number;\n\n    sscanf(str, \"%d\", &amp;number); \/\/ \u63d0\u53d6\u6574\u6570\n    printf(\"Number: %d\\n\", number);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u7075\u6d3b\uff0c\u53ef\u4ee5\u63d0\u53d6\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<br><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u660e\u786e\u6307\u5b9a\u6570\u636e\u683c\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u624b\u52a8\u5b9e\u73b0<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u9010\u4f4d\u89e3\u6790\u6570\u5b57\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt; \/\/ \u7528\u4e8e isdigit\n\nint strToInt(const char* str) {\n    int num = 0, isNegative = 0;\n\n    if (*str == '-') { \/\/ \u5904\u7406\u8d1f\u6570\n        isNegative = 1;\n        str++;\n    }\n\n    while (*str) {\n        if (!isdigit(*str)) break; \/\/ \u8df3\u8fc7\u975e\u6570\u5b57\u5b57\u7b26\n        num = num * 10 + (*str - '0'); \/\/ \u7d2f\u52a0\u6570\u5b57\n        str++;\n    }\n\n    return isNegative ? -num : num;\n}\n\nint main() {\n    char str&#91;] = \"-123\";\n    int number = strToInt(str);\n\n    printf(\"Number: %d\\n\", number);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f18\u70b9<\/strong>\uff1a\u8de8\u5e73\u53f0\uff0c\u65e0\u9700\u4f9d\u8d56\u5e93\u51fd\u6570\u3002<br><strong>\u7f3a\u70b9<\/strong>\uff1a\u4ee3\u7801\u590d\u6742\uff0c\u9700\u81ea\u884c\u5904\u7406\u5404\u79cd\u7279\u6b8a\u60c5\u51b5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6570\u5b57\u8f6c\u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a8\u8350\uff1a<code>sprintf<\/code>\uff08\u6807\u51c6\u51fd\u6570\uff0c\u7075\u6d3b\uff09\u6216 <code>itoa<\/code>\uff08\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u975e\u6807\u51c6\uff09\u3002<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u5b9e\u73b0\uff1a\u7528\u4e8e\u5b66\u4e60\u6216\u65e0\u6807\u51c6\u5e93\u652f\u6301\u7684\u73af\u5883\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5b57\u7b26\u4e32\u8f6c\u6570\u5b57<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a8\u8350\uff1a<code>strtol<\/code>\uff08\u5b89\u5168\u4e14\u7075\u6d3b\uff09\u3002<\/li>\n\n\n\n<li>\u7b80\u5355\u573a\u666f\uff1a<code>atoi<\/code> \u6216 <code>sscanf<\/code>\u3002<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u5b9e\u73b0\uff1a\u7528\u4e8e\u7279\u5b9a\u9700\u6c42\u6216\u5b66\u4e60\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5e94\u7528\u573a\u666f\u3001\u8f93\u5165\u590d\u6742\u5ea6\u548c\u6027\u80fd\u9700\u6c42\u6743\u8861\u4f7f\u7528\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u548c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u5e93\u51fd\u6570\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\uff1a 1.  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-504","post","type-post","status-publish","format-standard","hentry","category-css"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}