網絡学習管理 明解C言語入門1 C programming (3) Veriables

C programming (3) Veriables

変数と宣言 代入

int a, b;
a = 19;
b = -20;

変数初期化

int a = 1;
int b = 10;
int c = 4;

image

変数の演算

a = b + c;

image

(練習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 文字列の終了を表す

1 thought on “C programming (3) Veriables”

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Related Post

C programming (d) break & continueC programming (d) break & continue

break文 switch文のところでbreakというのが何回も登場しました。 switch文での意味は「switch文から抜け出す」という意味でしたね。 breakはswitch文以外にもfor文やwhile文のループの中でも使うことが出来ます。 この時もbreakは「for文やwhile文のループから抜け出す」という意味を持っています。 break文を使って抜けられるのはループひとつだけです。ですから、二重、三重のループを抜けるには、その度にbreak文を使うようになります。 continue文 continueはfor文やwhile文などのループ処理をスキップさせるに用います。 breakとは違い、continueはスキップした後は、ループの先頭に戻ります。 #include <stdio.h> int main(void) { int i = 1, n, sum; […]

C programming (4) puts and scanfC programming (4) puts and scanf

puts関数:表示を行う関数 書式化の必要がなく、改行もしたいの場合 puts(“ABC”); printf(“ABC\n”);   scanf関数:読込みを行う関数 scanf関数は、処理の途中でキーボードから文字の入力を求め、入力されたものを処理に利用するというものです。 実例: #include <stdio.h> int main(int argc, const char * argv[]) { // insert […]