C言語を学んでみよう
2016/05/30
C言語を学んでみよう
統合開発環境をつくります。
Pleiades は Eclipse のような Java C さまざまなアプリケーションを日本語化するためのツールです。
今回はEclipseをダウンロードします。
ダウンロードしたら新規ファイル→Cプロジェクトを作成。
次に新規ファイル→ソース・ファイル●●.cを作成する。(拡張子は.c)にすること。
(補足:その他開発環境 Visual Studio Community https://www.visualstudio.com/ja-jp/products/visual-studio-express-vs.aspx)
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main(void) { printf("これはCの短いプログラムです"); return 0; } |
このように実行できました。
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main(void) { printf("これは"); printf("もう1つの"); printf("Cプログラムです"); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main(void) { int num; num = 100; printf("値は %d", num); return 0; } |
型指定子とは次のいずれかの組み合わせです。
型指定子 | 意味 |
---|---|
void | 型なし |
char | 文字型 |
int | 整数型 |
float | 単精度実浮動小数点型 |
double | 倍精度実浮動小数点型 |
short | 短 |
long | 長 |
signed | 符号付き |
unsigned | 符号なし |
struct | 構造体(後ページで説明) |
union | 共用体(後ページで説明) |
enum | 列挙体 |
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> int main(void) { printf("%d", 5/2); printf(" %d",5%2); printf(" %d",4/2); printf(" %d",4%2); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main(void) { float e_days; /* 地球での日数 */ float j_years; /* 木星での年数 */ /*地球での日数を得る*/ printf("地球での日数を入力してください:"); fflush(0); scanf("%f", &e_days); /* 木星での年数を計算 */ j_years = e_days / (365.0 * 12.0); /* 答えを表示 */ printf("地球での日数に相当する木星の年数:%f", j_years ); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int myfunc(void); void myfunc2(void); int main(void) { int i_main; i_main = myfunc(); myfunc2(); printf("%d\n", i_main); } int myfunc(void) { printf("test 1\n"); return 3; } void myfunc2(void) { printf("test 2\n"); return ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int get_sqr(void); int main(void) { int sqr; sqr = get_sqr(); printf("2乗値: %d", sqr); return 0; } int get_sqr(void) { int num; printf("数値を入力してください:"); fflush(0); scanf("%d", &num); return num * num; /*数値を2乗する*/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> void outchar(char ch); int main(void) { outchar('a'); outchar('b'); outchar('c'); return 0; } void outchar(char ch) { printf("%c", ch); } |