{"id":3437,"date":"2025-04-05T23:57:01","date_gmt":"2025-04-05T15:57:01","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3437"},"modified":"2025-04-05T23:57:01","modified_gmt":"2025-04-05T15:57:01","slug":"c-%e8%af%ad%e8%a8%80%e7%9a%84%e5%bc%ba%e5%88%b6%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%ef%bc%88type-casting-type-conversion%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/05\/c-%e8%af%ad%e8%a8%80%e7%9a%84%e5%bc%ba%e5%88%b6%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%ef%bc%88type-casting-type-conversion%ef%bc%89\/","title":{"rendered":"C \u8bed\u8a00\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff08Type Casting \/ Type Conversion\uff09"},"content":{"rendered":"\n<p>\u672c\u6587\u5168\u9762\u3001\u7cfb\u7edf\u5730\u8bb2\u89e3 <strong>C \u8bed\u8a00\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff08Type Casting \/ Type Conversion\uff09<\/strong>\uff0c\u5305\u62ec\u5176\u8bed\u6cd5\u3001\u5206\u7c7b\u3001\u4f7f\u7528\u573a\u666f\u3001\u6ce8\u610f\u4e8b\u9879\uff0c\u786e\u4fdd\u4f60\u4e0d\u4ec5\u201c\u77e5\u9053\u600e\u4e48\u7528\u201d\uff0c\u8fd8\u201c\u77e5\u9053\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7528\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 \u4e00\u3001\u4ec0\u4e48\u662f\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff1f<\/h2>\n\n\n\n<p><strong>\u7c7b\u578b\u8f6c\u6362\uff08Type Conversion\uff09<\/strong>\u662f\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u5206\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b\u8f6c\u6362<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\uff08Implicit\uff09<\/td><td>\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff08\u5982 <code>int \u2192 float<\/code>\uff09<\/td><\/tr><tr><td><strong>\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/strong>\uff08Explicit \/ Type Casting\uff09<\/td><td>\u7a0b\u5e8f\u5458\u663e\u5f0f\u6307\u5b9a\u8f6c\u6362\u65b9\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e8c\u3001\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7684\u8bed\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>(type)value<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>type<\/code>\uff1a\u76ee\u6807\u7c7b\u578b\uff08\u5982 <code>int<\/code>, <code>float<\/code>, <code>char<\/code> \u7b49\uff09<\/li>\n\n\n\n<li><code>value<\/code>\uff1a\u8981\u8f6c\u6362\u7684\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u6216\u5e38\u91cf<\/li>\n<\/ul>\n\n\n\n<p>\u2705 \u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 10;\nfloat b = (float)a \/ 3;  \/\/ \u8f6c\u6362\u4e3a float\uff0c\u518d\u8fdb\u884c\u9664\u6cd5\uff0c\u7ed3\u679c\u4e3a\u5c0f\u6570<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4e0d\u5f3a\u8f6c\uff0c\u6574\u6570\u9664\u6cd5\u4f1a\u76f4\u63a5\u53d8\u6210 <code>10 \/ 3 = 3<\/code>\uff0c\u7ed3\u679c\u53ef\u80fd\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcc2 \u4e09\u3001\u5e38\u89c1\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7528\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. int \u2194 float \/ double<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 5;\nfloat f = (float)i;   \/\/ i \u53d8\u6210 5.0\ndouble d = (double)f; \/\/ f \u53d8\u6210 5.0\uff08double \u7cbe\u5ea6\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. float \u2194 int\uff08\u4f1a\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>float pi = 3.14159;\nint a = (int)pi;  \/\/ a = 3\uff08\u5c0f\u6570\u90e8\u5206\u88ab\u4e22\u5f03\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. char \u2194 int\uff08ASCII \u8f6c\u6362\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>char c = 'A';\nint code = (int)c;   \/\/ code = 65<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6307\u9488\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\uff08\u9ad8\u7ea7\u7528\u6cd5\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void *ptr;\nint *iptr = (int *)ptr;<\/code><\/pre>\n\n\n\n<p>\u26a0\ufe0f \u901a\u5e38\u7528\u4e8e\u901a\u7528\u63a5\u53e3\uff08\u5982 <code>malloc<\/code> \u8fd4\u56de <code>void*<\/code>\uff09\u7684\u8f6c\u6362\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 \u56db\u3001\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7684\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u793a\u4f8b<\/th><th>\u76ee\u7684<\/th><\/tr><\/thead><tbody><tr><td>\u7cbe\u786e\u63a7\u5236\u7c7b\u578b<\/td><td><code>(float)a \/ b<\/code><\/td><td>\u907f\u514d\u6574\u6570\u9664\u6cd5\u8bef\u5dee<\/td><\/tr><tr><td>\u5185\u5b58\u8bbf\u95ee<\/td><td><code>(char *)ptr<\/code><\/td><td>\u5bf9\u5185\u5b58\u7684\u4e0d\u540c\u65b9\u5f0f\u8bbf\u95ee<\/td><\/tr><tr><td>\u51fd\u6570\u53c2\u6570\u5339\u914d<\/td><td><code>(int)(ch)<\/code><\/td><td>\u786e\u4fdd\u7c7b\u578b\u517c\u5bb9<\/td><\/tr><tr><td>\u6291\u5236\u7f16\u8bd1\u5668\u8b66\u544a<\/td><td><code>(void)foo()<\/code><\/td><td>\u660e\u786e\u4e22\u5f03\u8fd4\u56de\u503c<\/td><\/tr><tr><td>\u4f4d\u64cd\u4f5c<\/td><td><code>(unsigned int)<\/code><\/td><td>\u907f\u514d\u7b26\u53f7\u6269\u5c55\u9519\u8bef<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a0\ufe0f \u4e94\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u9677\u9631<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u26a0\ufe0f \u95ee\u9898<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u7c7b\u578b\u8f6c\u6362\u4e0d\u80fd\u6539\u53d8\u503c\u6240\u5360\u7684\u5185\u5b58\u5927\u5c0f<\/td><td>\u53ea\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u201c\u5982\u4f55\u770b\u5f85\u201d\u8fd9\u4e2a\u503c<\/td><\/tr><tr><td>\u8f6c\u6362\u6307\u9488\u7c7b\u578b\u9700\u8c28\u614e<\/td><td>\u5426\u5219\u53ef\u80fd\u4ea7\u751f\u672a\u5b9a\u4e49\u884c\u4e3a\uff08\u5982\u8d8a\u754c\u8bbf\u95ee\uff09<\/td><\/tr><tr><td>\u6d6e\u70b9\u8f6c\u6574\u6570\u4f1a\u622a\u65ad<\/td><td><code>(int)3.9 == 3<\/code>\uff0c\u4e0d\u662f\u56db\u820d\u4e94\u5165<\/td><\/tr><tr><td>\u4f7f\u7528 <code>(void)<\/code> \u4e22\u5f03\u8fd4\u56de\u503c\u65f6\u614e\u7528<\/td><td>\u5982\u679c\u51fd\u6570\u6709\u526f\u4f5c\u7528\uff0c\u4e0d\u5e94\u5f3a\u884c\u5ffd\u7565<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddea \u516d\u3001\u793a\u4f8b\u6f14\u793a\uff1a\u7c7b\u578b\u8f6c\u6362\u5bf9\u7ed3\u679c\u7684\u5f71\u54cd<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n    int a = 5, b = 2;\n\n    float x1 = a \/ b;             \/\/ \u6574\u6570\u9664\u6cd5\uff0c\u518d\u8d4b\u7ed9 float\uff1a\u7ed3\u679c\u4e3a 2.0\n    float x2 = (float)a \/ b;      \/\/ \u5f3a\u5236\u8f6c\u6362\uff0c\u7ed3\u679c\u4e3a 2.5\n\n    printf(\"x1 = %f\\n\", x1);  \/\/ \u8f93\u51fa\uff1ax1 = 2.000000\n    printf(\"x2 = %f\\n\", x2);  \/\/ \u8f93\u51fa\uff1ax2 = 2.500000\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\">\ud83e\udde0 \u4e03\u3001\u6df1\u5c42\u673a\u5236\u5256\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7f16\u8bd1\u5668\u5904\u7406\u8fc7\u7a0b<\/h3>\n\n\n\n<p>\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u662f\u7531\u7f16\u8bd1\u5668\u5728\u8bed\u6cd5\u5206\u6790\u9636\u6bb5\u8bc6\u522b\u5e76\u751f\u6210\u76f8\u5e94\u7684\u673a\u5668\u6307\u4ee4\u3002<\/p>\n\n\n\n<p>\u2705 \u793a\u4f8b\uff1aGCC \u53ef\u901a\u8fc7 <code>-S<\/code> \u67e5\u770b\u6c47\u7f16\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc -S test.c -o test.s<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u53d8\u91cf\u5982\u4f55\u88ab\u5f3a\u5236\u8f6c\u6362\uff08\u4f8b\u5982\u4f7f\u7528 <code>cvtsi2ss<\/code> \u6307\u4ee4\u5c06 <code>int<\/code> \u8f6c\u4e3a <code>float<\/code>\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u516b\u3001\u6743\u5a01\u8d44\u6599\u548c\u51fa\u7ad9\u94fe\u63a5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u8d44\u6e90<\/th><th>\u94fe\u63a5<\/th><\/tr><\/thead><tbody><tr><td>ISO \u6807\u51c6<\/td><td>C99 \u5b98\u65b9\u6807\u51c6\uff08N1256\uff09<\/td><td><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1256.pdf\">open-std.org\/n1256.pdf<\/a><\/td><\/tr><tr><td>C \u6559\u7a0b<\/td><td>cppreference.com &#8211; Type Conversion<\/td><td><a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/conversion\">https:\/\/en.cppreference.com\/w\/c\/language\/conversion<\/a><\/td><\/tr><tr><td>\u6559\u6750<\/td><td>\u300aC Primer Plus\uff08\u7b2c6\u7248\uff09\u300b<\/td><td><a href=\"https:\/\/www.amazon.com\/Primer-Plus-6th-Developers-Library\/dp\/0321928423\">Amazon \u94fe\u63a5<\/a><\/td><\/tr><tr><td>C \u5165\u95e8\u6559\u7a0b<\/td><td>tutorialspoint.com C Type Casting<\/td><td><a href=\"https:\/\/www.tutorialspoint.com\/cprogramming\/c_type_casting.htm\">https:\/\/www.tutorialspoint.com\/cprogramming\/c_type_casting.htm<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u4e5d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5185\u5bb9\u70b9<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u7c7b\u578b\u8f6c\u6362\u6709\u9690\u5f0f\u548c\u663e\u5f0f\u4e24\u79cd<\/td><td>\u663e\u5f0f\u8f6c\u6362\u901a\u8fc7 <code>(type)value<\/code> \u5b9e\u73b0<\/td><\/tr><tr><td>\u63a7\u5236\u8ba1\u7b97\u7cbe\u5ea6\u548c\u8868\u8fbe\u5f0f\u884c\u4e3a<\/td><td>\u5982\u907f\u514d\u6574\u6570\u9664\u6cd5\u9020\u6210\u7cbe\u5ea6\u635f\u5931<\/td><\/tr><tr><td>\u5e94\u8c28\u614e\u4f7f\u7528\u6307\u9488\u7c7b\u578b\u8f6c\u6362<\/td><td>\u9632\u6b62\u5185\u5b58\u8bbf\u95ee\u9519\u8bef<\/td><\/tr><tr><td>\u8f6c\u6362\u4e0d\u7b49\u4e8e\u7c7b\u578b\u5b89\u5168<\/td><td>\u7c7b\u578b\u517c\u5bb9\u6027\u4ecd\u9700\u9a8c\u8bc1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5168\u9762\u3001\u7cfb\u7edf\u5730\u8bb2\u89e3 C \u8bed\u8a00\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff08Type Casting \/ Type Conversion\uff09\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3438,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3437","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\/3437","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=3437"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3437\/revisions"}],"predecessor-version":[{"id":3439,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3437\/revisions\/3439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3438"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}