変数と宣言 代入
int a, b;
a = 19;
b = -20;
変数初期化
int a = 1;
int b = 10;
int c = 4;

変数の演算
a = b + c;

(練習1)変数と宣言のサンプル
#include <stdio.h>
int main(int argc, const char * argv[])
{
// insert code here...
int a;
int b;
int result;
a = 4;
b = 2;
result = 4 + 2;
printf("4と2の和は %d\n", result);
result = 4 - 2;
printf("4と2の差は %d\n", result);
result = 4 * 2;
printf("4と2の積は %d\n", result);
result = 4 / 2;
printf("4と2の商は %d\n", result);
result = 4 % 2;
printf("4と2の余りは%d\n", result);
return 0;
}
(練習2)printf関数:書式化して表示を行う関数
// test4.c
#include <stdio.h>
int main(void )
{
int a,b,c;
/* これは加算のプログラム コメント
名前:stdio.h
C:\borland\bcc55\Include */
a=3; b=4;
c=a+b;
printf("c= %d\n",c); /* Cの値を書式化して表示 */
getchar( );
return 0;
}
書式指定文字列:
「””」で囲まれた文字列のことを指します。
変換指定文字列:
- %d 10進数で出力
- %s 文字列として出力
拡張表記:
一般的にはエスケープシーケンスと呼ばれています。
| エスケープシーケンス | 意味 |
|---|---|
| \n | 改行 |
| \t | タブ |
| \0 | 文字列の終了を表す |
「C programming (3) Veriables」への1件のフィードバック