{"id":302177,"date":"2024-05-20T18:27:58","date_gmt":"2024-05-20T10:27:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/302177.html"},"modified":"2024-05-20T18:28:14","modified_gmt":"2024-05-20T10:28:14","slug":"c-%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e6%8a%8a%e5%b0%8f%e5%86%99%e5%ad%97%e6%af%8d%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%a4%a7%e5%86%99%e5%ad%97%e6%af%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/302177.html","title":{"rendered":"c \u8bed\u8a00\u5982\u4f55\u628a\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26214429\/38cb5c11-4144-4f5a-89c8-cd731ad9bc88.webp\" alt=\"c \u8bed\u8a00\u5982\u4f55\u628a\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\" \/><\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7ASCII\u7801\u5dee\u503c\u6765\u5b9e\u73b0\u5c0f\u5199\u5b57\u6bcd\u5411\u5927\u5199\u5b57\u6bcd\u7684\u8f6c\u6362\u3001\u6216\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>toupper()<\/code>\u51fd\u6570\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6bcf\u4e2a\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u7684ASCII\u7801\u503c\u90fd\u6bd4\u76f8\u5e94\u7684\u5927\u5199\u82f1\u6587\u5b57\u6bcd\u592732\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u4ece\u5c0f\u5199\u5b57\u6bcd\u7684ASCII\u7801\u4e2d\u51cf\u53bb32\u5373\u53ef\u5f97\u5230\u5bf9\u5e94\u7684\u5927\u5199\u5b57\u6bcd\u3002<strong>\u4f7f\u7528ASCII\u7801\u5dee\u503c\u8fdb\u884c\u8f6c\u6362<\/strong>\u662f\u4e00\u79cd\u76f4\u63a5\u4e14\u5bb9\u6613\u7406\u89e3\u7684\u65b9\u6cd5\u3002\u81f3\u4e8e<code>toupper()<\/code>\u51fd\u6570\uff0c\u5219\u662fC\u8bed\u8a00\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e13\u7528\u51fd\u6570\uff0c\u7528\u4ee5\u628a\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5927\u5199\u5b57\u6bcd\uff0c\u8fd9\u662f\u4e00\u4e2a\u66f4\u52a0\u901a\u7528\u548c\u5b89\u5168\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528ASCII\u7801\u503c\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u8981\u901a\u8fc7ASCII\u7801\u503c\u8f6c\u6362\u5c0f\u5199\u5b57\u6bcd\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u53ea\u9700\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u5b9a\u5b57\u7b26\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u5373\u5b83\u7684ASCII\u7801\u503c\u662f\u5426\u572897\u5230122\u4e4b\u95f4\u3002<\/li>\n<li>\u5982\u679c\u662f\u5c0f\u5199\u5b57\u6bcd\uff0c\u4ece\u5176ASCII\u7801\u503c\u51cf\u53bb32\uff0c\u4fbf\u53ef\u5f97\u5230\u5bf9\u5e94\u7684\u5927\u5199\u5b57\u6bcd\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    char ch = &#039;a&#039;;<\/p>\n<p>    if (ch &gt;= &#039;a&#039; &amp;&amp; ch &lt;= &#039;z&#039;) {<\/p>\n<p>        \/\/ \u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>        ch = ch - 32;<\/p>\n<p>    }<\/p>\n<p>    printf(&quot;\u5927\u5199\u5b57\u6bcd\uff1a%c\\n&quot;, ch);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u5b57\u7b26<code>ch<\/code>\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u5982\u679c\u662f\uff0c\u5c31\u901a\u8fc7\u51cf\u5c1132\u6765\u5c06\u5176\u8f6c\u6362\u6210\u5927\u5199\u5b57\u6bcd\uff0c\u5e76\u901a\u8fc7<code>printf<\/code>\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528C\u5e93\u51fd\u6570toupper()<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u9009\u9879\u662f\u4f7f\u7528<code>&lt;ctype.h&gt;<\/code>\u6807\u51c6\u5e93\u4e2d\u7684<code>toupper()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;ctype.h&gt;<\/p>\n<p>int main() {<\/p>\n<p>    char ch = &#039;a&#039;;<\/p>\n<p>    \/\/ \u4f7f\u7528toupper\u51fd\u6570\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>    char upper_ch = toupper(ch);<\/p>\n<p>    printf(&quot;\u5927\u5199\u5b57\u6bcd\uff1a%c\\n&quot;, upper_ch);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>toupper()<\/code>\u51fd\u6570\u65f6\uff0c\u5373\u4f7f\u8f93\u5165\u7684\u5df2\u7ecf\u662f\u5927\u5199\u5b57\u6bcd\u6216\u8005\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u8be5\u51fd\u6570\u4e5f\u80fd\u6b63\u786e\u5904\u7406\uff0c\u8fd9\u4f7f\u5f97<code>toupper()<\/code>\u6210\u4e3a\u8f6c\u6362\u5b57\u7b26\u65f6\u7684\u4e00\u4e2a\u66f4\u7a33\u5065\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0c<code>toupper()<\/code>\u51fd\u6570\u5728\u5904\u7406\u591a\u5b57\u8282\u5b57\u7b26\u96c6\uff08\u5982UTF-8\uff09\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5176\u884c\u4e3a\u4f1a\u7531\u6240\u4f7f\u7528\u7684C\u5e93\u7684\u672c\u5730\u5316\u8bbe\u7f6e\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u6279\u91cf\u8f6c\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;ctype.h&gt;<\/p>\n<p>void StringToUpper(char *str) {<\/p>\n<p>    while (*str) {<\/p>\n<p>        *str = toupper(*str);<\/p>\n<p>        str++;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>int main() {<\/p>\n<p>    char str[] = &quot;Hello, World!&quot;;<\/p>\n<p>    StringToUpper(str);<\/p>\n<p>    printf(&quot;\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\uff1a%s\\n&quot;, str);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>StringToUpper<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6307\u5411\u5b57\u7b26\u4e32\u7684\u6307\u9488\uff0c\u5e76\u9010\u4e2a\u5b57\u7b26\u5730\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u5b83\u901a\u8fc7\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u7b26\u53f7\uff08null terminator\uff0c\u5373<code>\\0<\/code>\uff09\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8003\u8651\u5b57\u7b26\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u73b0\u4ee3\u8ba1\u7b97\u73af\u5883\u4e2d\uff0c\u5b57\u7b26\u7f16\u7801\u53ef\u80fd\u591a\u4e8eASCII\uff0c\u4f8b\u5982UTF-8\u7f16\u7801\u3002UTF-8\u517c\u5bb9ASCII\u7f16\u7801\uff0c\u4f46\u540c\u65f6\u8fd8\u80fd\u8868\u793a\u66f4\u591a\u5b57\u7b26\u3002\u5728\u5904\u7406\u975eASCII\u7f16\u7801\u6216\u76f8\u5173\u7684\u672c\u5730\u5316\u5b57\u7b26\u4e2d\uff0c\u4ec5\u4ec5\u4f7f\u7528ASCII\u7801\u7684\u8f6c\u6362\u65b9\u6cd5\u53ef\u80fd\u4e0d\u591f\u7cbe\u786e\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e94\u8003\u8651\u4f7f\u7528\u652f\u6301\u5bbd\u5b57\u7b26\u548c\u591a\u8bed\u8a00\u73af\u5883\u7684\u51fd\u6570\uff0c\u5982<code>wchar_t<\/code>\u7c7b\u578b\u548c<code>&lt;wctype.h&gt;<\/code>\u4e2d\u7684<code>towupper()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5b57\u7b26\u5904\u7406\u65f6\uff0c\u8003\u8651\u5230\u7f16\u7801\u517c\u5bb9\u6027\u548c\u56fd\u9645\u5316\u53ef\u80fd\u53d8\u5f97\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u8f6f\u4ef6\u9700\u8981\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u65f6\u5019\u3002\u9009\u62e9\u6b63\u786e\u7684\u6570\u636e\u7c7b\u578b\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u7684\u5065\u58ee\uff0c\u5177\u6709\u66f4\u597d\u7684\u8de8\u5730\u533a\u6216\u8de8\u8bed\u8a00\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c\u867d\u7136\u901a\u8fc7ASCII\u7801\u503c\u8f6c\u6362\u5c0f\u5199\u5b57\u6bcd\u4e3a\u5927\u5199\u5b57\u6bcd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6709\u6548\u4e14\u5feb\u901f\uff0c\u4f46\u4f7f\u7528\u6807\u51c6\u5e93\u51fd\u6570<code>toupper()<\/code>\u4f1a\u662f\u4e00\u4e2a\u66f4\u52a0\u5b89\u5168\u3001\u53ef\u9760\u4e14\u5177\u6709\u5e7f\u6cdb\u517c\u5bb9\u6027\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u56fd\u9645\u5316\u548c\u591a\u8bed\u8a00\u652f\u6301\u65f6\u3002(&quot;&quot;)<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u5c06\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff1f<\/strong><\/p>\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u8981\u5c06\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u5904\u7406\u51fd\u6570toupper()\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u5927\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5c06\u5b57\u7b26c\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcdC\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-c\">char c = &#039;c&#039;;\nc = toupper(c);\n<\/code><\/pre>\n<p><strong>2. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528toupper()\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u53ef\u4ee5\u4f7f\u7528ASCII\u7801\u6765\u8fdb\u884c\u8f6c\u6362\u3002\u5728ASCII\u7801\u4e2d\uff0c\u5c0f\u5199\u5b57\u6bcda\u5230z\u7684\u503c\u76f8\u5bf9\u5e94\u5730\u662f97\u5230122\uff0c\u800c\u5927\u5199\u5b57\u6bcdA\u5230Z\u7684\u503c\u76f8\u5bf9\u5e94\u5730\u662f65\u523090\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u5c0f\u5199\u5b57\u6bcd\u7684ASCII\u7801\u503c\u51cf\u53bb32\u6765\u5f97\u5230\u5bf9\u5e94\u7684\u5927\u5199\u5b57\u6bcd\u7684ASCII\u7801\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5f97\u5230\u7684ASCII\u7801\u503c\u8f6c\u6362\u56de\u5b57\u7b26\u7c7b\u578b\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c\">char c = &#039;c&#039;;\nc = c - 32;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u7b26c\u5c31\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcdC\u3002<\/p>\n<p><strong>3. C\u8bed\u8a00\u4e2d\u5982\u4f55\u6279\u91cf\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u6279\u91cf\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u53ef\u4ee5\u4f7f\u7528C\u8bed\u8a00\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570strlwr()\u548cstrupr()\u3002strlwr()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u800cstrupr()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u4f7f\u7528strlwr()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u7136\u540e\u518d\u4f7f\u7528strupr()\u51fd\u6570\u5c06\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main() {\n   char str[] = &quot;Hello World&quot;;\n   \n   \/\/ \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\n   strlwr(str);\n   printf(&quot;\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd: %s\\n&quot;, str);\n   \n   \/\/ \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\n   strupr(str);\n   printf(&quot;\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd: %s\\n&quot;, str);\n   \n   return 0;\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre><code>\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd: hello world\n\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd: HELLO WORLD\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7ASCII\u7801\u5dee\u503c\u6765\u5b9e\u73b0\u5c0f\u5199\u5b57\u6bcd\u5411\u5927\u5199\u5b57\u6bcd\u7684\u8f6c\u6362\u3001\u6216\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684toupper()\u51fd\u6570\u3002\u5177 [&hellip;]","protected":false},"author":3,"featured_media":302200,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/302177"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=302177"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/302177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/302200"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=302177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=302177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=302177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}