blog/content/post/2009/05/31/2009-05-31-00001167.md

34 lines
1.7 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 後で絶対に忘れそうだから、メモっておこう
author: kazu634
date: 2009-05-31
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4627;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
---
<div class="section">
<p>
char型に数値を入れたときに、int型として扱うには「&#8217;0&#8217;」を引きます:
</p>
<pre class="syntax-highlight">
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[]) {
<span class="synType">char</span> s = <span class="synConstant">'9'</span>;
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%c</span><span class="synConstant"> </span><span class="synSpecial">%d</span><span class="synConstant"> </span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, s, s, (s - <span class="synConstant">'0'</span>));
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
</pre>
<p>
実行例はこんな感じです:
</p>
<pre class="syntax-highlight">
~/working/tmp_c on simoom634 <span class="synStatement">[</span><span class="synConstant">503</span><span class="synStatement">]</span> $: gcc <span class="synSpecial">-o</span> foo kr_p87.c
~/working/tmp_c on simoom634 <span class="synStatement">[</span><span class="synConstant">504</span><span class="synStatement">]</span> $: ./foo
<span class="synConstant">9</span> <span class="synConstant">57</span> <span class="synConstant">9</span>
</pre>
</div>