{"id":3394,"date":"2025-03-30T22:02:32","date_gmt":"2025-03-30T14:02:32","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3394"},"modified":"2025-03-30T22:02:32","modified_gmt":"2025-03-30T14:02:32","slug":"c-%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e4%bd%9c%e7%94%a8%e5%9f%9f%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/30\/c-%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e4%bd%9c%e7%94%a8%e5%9f%9f%e8%a7%84%e5%88%99\/","title":{"rendered":"C \u8bed\u8a00\u4e2d\u7684\u4f5c\u7528\u57df\u89c4\u5219"},"content":{"rendered":"\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c<strong>\u4f5c\u7528\u57df<\/strong>\u6307\u7684\u662f\u4e00\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u7684\u533a\u57df\u3002C \u8bed\u8a00\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u7c7b\u578b\uff0c\u6bcf\u79cd\u4f5c\u7528\u57df\u7684\u89c4\u5219\u548c\u751f\u547d\u5468\u671f\u4e0d\u540c\u3002\u7406\u89e3\u8fd9\u4e9b\u4f5c\u7528\u57df\u89c4\u5219\u662f\u7f16\u5199\u6e05\u6670\u3001\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u4f5c\u7528\u57df\u7684\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p>C \u8bed\u8a00\u4e2d\u7684\u4f5c\u7528\u57df\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5757\u7ea7\u4f5c\u7528\u57df\uff08Block Scope\uff09<\/strong>\uff1a\u53d8\u91cf\u5728\u51fd\u6570\u6216\u4ee3\u7801\u5757\uff08\u4f8b\u5982 if\u3001for \u7b49\u8bed\u53e5\uff09\u5185\u90e8\u7684\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u4f5c\u7528\u57df\uff08Function Scope\uff09<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8e\u51fd\u6570\u7684\u6807\u7b7e\uff08\u4f8b\u5982 goto \u8bed\u53e5\u7684\u6807\u7b7e\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u4f5c\u7528\u57df\uff08File Scope\uff09<\/strong>\uff1a\u5728\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf\u6216\u51fd\u6570\uff0c\u4f5c\u7528\u57df\u662f\u6574\u4e2a\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u5916\u90e8\u4f5c\u7528\u57df\uff08External Scope\uff09<\/strong>\uff1a\u6307\u5728\u591a\u4e2a\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u7684\u5168\u5c40\u53d8\u91cf\u6216\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c40\u90e8\u53d8\u91cf<\/strong>\uff1a\u5c40\u90e8\u53d8\u91cf\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u6216\u8005\u4ee3\u7801\u5757\u5185\uff0c\u5176\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u8be5\u51fd\u6570\u6216\u4ee3\u7801\u5757\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\uff0c\u6574\u4e2a\u6587\u4ef6\u7684\u6240\u6709\u51fd\u6570\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u5168\u5c40\u53d8\u91cf\u3002\u5168\u5c40\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ece\u5b9a\u4e49\u5f00\u59cb\u76f4\u5230\u6587\u4ef6\u7ed3\u675f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n\/\/ \u5168\u5c40\u53d8\u91cf\nint global_var = 10;\n\nvoid function() {\n    \/\/ \u5c40\u90e8\u53d8\u91cf\n    int local_var = 20;\n    printf(\"\u5c40\u90e8\u53d8\u91cf\u503c\uff1a%d\\n\", local_var);\n    printf(\"\u5168\u5c40\u53d8\u91cf\u503c\uff1a%d\\n\", global_var);\n}\n\nint main() {\n    function();\n    printf(\"\u5168\u5c40\u53d8\u91cf\u503c\uff1a%d\\n\", global_var);\n    \/\/ printf(\"\u5c40\u90e8\u53d8\u91cf\u503c\uff1a%d\\n\", local_var); \/\/ \u9519\u8bef\uff0clocal_var \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u80fd\u5728 main \u4e2d\u8bbf\u95ee\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c40\u90e8\u53d8\u91cf\u503c\uff1a20\n\u5168\u5c40\u53d8\u91cf\u503c\uff1a10\n\u5168\u5c40\u53d8\u91cf\u503c\uff1a10<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>local_var<\/code> \u53ea\u80fd\u5728 <code>function()<\/code> \u5185\u90e8\u8bbf\u95ee\uff0c\u800c <code>global_var<\/code> \u53ef\u4ee5\u5728\u6574\u4e2a\u6587\u4ef6\u4e2d\u8bbf\u95ee\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong><\/h2>\n\n\n\n<p>C \u8bed\u8a00\u4e2d\u7684\u6807\u7b7e\uff08\u5982 <code>goto<\/code> \u8bed\u53e5\u4e2d\u7684\u6807\u7b7e\uff09\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df\uff0c\u5373\u5b83\u4eec\u53ea\u5728\u5176\u6240\u5728\u7684\u51fd\u6570\u4e2d\u6709\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid function() {\n    int x = 10;\n    start:  \/\/ \u6807\u7b7e\u5177\u6709\u51fd\u6570\u4f5c\u7528\u57df\n        if (x &gt; 0) {\n            printf(\"x = %d\\n\", x);\n            x--;\n            goto start;  \/\/ \u8df3\u8f6c\u5230 start \u6807\u7b7e\n        }\n}\n\nint main() {\n    function();\n    \/\/ goto start;  \/\/ \u9519\u8bef\uff1a\u6807\u7b7e start \u53ea\u80fd\u5728 function \u51fd\u6570\u5185\u90e8\u4f7f\u7528\n    return 0;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u5757\u7ea7\u4f5c\u7528\u57df\uff08\u5c40\u90e8\u4f5c\u7528\u57df\uff09<\/strong><\/h2>\n\n\n\n<p>\u5757\u7ea7\u4f5c\u7528\u57df\u662f\u6307\u53d8\u91cf\u5728\u82b1\u62ec\u53f7 <code>{}<\/code> \u5185\u7684\u4f5c\u7528\u8303\u56f4\u3002\u5373\uff0c\u53d8\u91cf\u5728\u5176\u58f0\u660e\u7684\u4ee3\u7801\u5757\u5185\u6709\u6548\uff0c\u51fa\u5757\u540e\u5373\u5931\u6548\u3002\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u6240\u5728\u7684\u51fd\u6570\u6216\u4ee3\u7801\u5757\uff0c\u5176\u4ed6\u5730\u65b9\u4e0d\u80fd\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid function() {\n    int x = 10;  \/\/ \u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u8bbf\u95ee\n    if (x &gt; 0) {\n        int y = 20;  \/\/ y \u53ea\u5728 if \u5757\u5185\u6709\u6548\n        printf(\"x = %d, y = %d\\n\", x, y);\n    }\n    \/\/ printf(\"y = %d\\n\", y);  \/\/ \u9519\u8bef\uff1ay \u5728 if \u5757\u5916\u65e0\u6cd5\u8bbf\u95ee\n}\n\nint main() {\n    function();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10, y = 20<\/code><\/pre>\n\n\n\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>y<\/code> \u4ec5\u5728 <code>if<\/code> \u5757\u5185\u6709\u6548\uff0c\u5728 <code>if<\/code> \u5757\u5916\u65e0\u6cd5\u8bbf\u95ee\u3002<code>x<\/code> \u662f\u5728 <code>function()<\/code> \u5185\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5728\u6574\u4e2a <code>function()<\/code> \u51fd\u6570\u5185\u90fd\u6709\u6548\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u5916\u90e8\u53d8\u91cf\uff08External Variables\uff09<\/strong><\/h2>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u53d8\u91cf\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u90fd\u9700\u8981\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>extern<\/code> \u5173\u952e\u5b57\u6765\u58f0\u660e\u5b83\u3002\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u8bbf\u95ee\u65f6\uff0c\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u58f0\u660e\u5e94\u8be5\u5339\u914d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<p><strong>file1.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n\/\/ \u58f0\u660e\u5916\u90e8\u53d8\u91cf\nextern int global_var;\n\nvoid function() {\n    printf(\"\u4ece file1.c \u4e2d\u8bbf\u95ee\u7684 global_var\uff1a%d\\n\", global_var);\n}<\/code><\/pre>\n\n\n\n<p><strong>file2.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n\/\/ \u5b9a\u4e49\u5916\u90e8\u53d8\u91cf\nint global_var = 100;\n\nint main() {\n    function();  \/\/ \u8c03\u7528 file1.c \u4e2d\u7684\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4ece file1.c \u4e2d\u8bbf\u95ee\u7684 global_var\uff1a100<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>global_var<\/code> \u5728 <code>file2.c<\/code> \u4e2d\u5b9a\u4e49\uff0c\u5e76\u4e14\u5728 <code>file1.c<\/code> \u4e2d\u901a\u8fc7 <code>extern<\/code> \u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\uff0c\u4f7f\u5f97\u5b83\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6e90\u6587\u4ef6\u4e2d\u5171\u4eab\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u4f5c\u7528\u57df\u89c4\u5219\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5757\u7ea7\u4f5c\u7528\u57df<\/strong>\uff1a\u53d8\u91cf\u5728\u51fd\u6570\u6216\u8bed\u53e5\u5757\u5185\u6709\u6548\uff0c\u8d85\u51fa\u4f5c\u7528\u57df\u65f6\u53d8\u91cf\u65e0\u6548\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u4f5c\u7528\u57df<\/strong>\uff1a\u6807\u7b7e\uff08\u4f8b\u5982 <code>goto<\/code> \u6807\u7b7e\uff09\u4ec5\u5728\u5b9a\u4e49\u5b83\u7684\u51fd\u6570\u5185\u6709\u6548\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u5728\u6574\u4e2a\u6587\u4ef6\u4e2d\u6709\u6548\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u51fd\u6570\u4e2d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u5916\u90e8\u4f5c\u7528\u57df<\/strong>\uff1a\u901a\u8fc7 <code>extern<\/code> \u5173\u952e\u5b57\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u5171\u4eab\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u4f5c\u7528\u57df\u7684\u751f\u547d\u5468\u671f<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c40\u90e8\u53d8\u91cf<\/strong>\uff1a\u5b83\u4eec\u5728\u51fd\u6570\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u51fd\u6570\u8fd4\u56de\u65f6\u9500\u6bc1\uff0c\u751f\u547d\u5468\u671f\u4ec5\u9650\u4e8e\u51fd\u6570\u7684\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u5b83\u4eec\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\uff0c\u751f\u547d\u5468\u671f\u8d2f\u7a7f\u6574\u4e2a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u5408\u7406\u7ba1\u7406\u4f5c\u7528\u57df\uff0c\u53ef\u4ee5\u51cf\u5c11\u547d\u540d\u51b2\u7a81\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u7ed3\u6784\u6e05\u6670\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u4f5c\u7528\u57df\u6307\u7684\u662f\u4e00\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u5728\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u88ab\u8bbf\u95ee\u7684\u533a\u57df\u3002C \u8bed\u8a00\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u7c7b\u578b\uff0c\u6bcf\u79cd\u4f5c\u7528\u57df [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3394","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=3394"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3394\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3394\/revisions\/3396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3395"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}