ホームページへ戻る
 

上へ戻る
 

  C言語入門  サンプルコード 1
BY H.Y 2月 28 15:14:35 JST 2002


目次
1. argv.c
2. count.c
3. func1.c
4. print1.c
5. scanf.c
6. fact.c
7. dec.c
8. dec2.c
9. auto.c
10 enum.c
11 aliment.c
12 atof.c
13 3term.c
14 operator.c
15. canma.c
16 pointer2.c
17. pointer4.c
18. pointer5.c
19. func-p.c
20 func-p3.c
21. array.c
22. array2.c
23. struct1.c
24. struct2.c
25. struct3.c
26. bit.c
27. macro.c
28. file1.c
29. static.c
30. low1.c
31. low2.c
32. mem-read.c
33. mem-read2.c


// No.1 ---- argv.c d------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int ac,char *av[]){
int i;

printf("****** ");
for(i=1 ; i < ac ; i++)
printf("%s , ",av[i]);
printf(" ******\n");

return 0;
}

// No.2 ----- count.c ----------------------
#include <stdio.h>

int main(){
int counter;

counter = 0;
while(getchar() != '\n')
++counter;
printf(" Character = %d \n",counter);

return 0;
}

// No.3 ----- func1.c ----------------------
#include <stdio.h>
#include <stdlib.h>

void func(int i, int *j);

int main(){
int i, j, k;

i = 6;
j = i;
func(4, &j);
printf(" %d*%d = %d\n",4,i,j);

return 0;
}

void func(int i, int *j){
*j = (*j) * i;
}

// No.4 ------ print1.c -----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(){
printf("%30s\n","123456789012345678901234567890");
printf("%30.3f\n",3.14);
printf("%030.3f\n",3.14);
printf("%*s\n",30,"今日は");
printf("%*s\n",30,"Hello ワールド");

return 0;
}

// No.5 ---------- scanf.c --------------
#include <stdio.h>
#include <stdlib.h>

int main(){
char c, s[256];
int n;
float f;

scanf("Hello, %d, %g, %c, %[^\n]",&n, &f, &c, s);
// %[^\n] は \nで終了を意味する。 !\n の事。
printf("\n\n");
printf("%d\n%f\n%c\n%s\n",n,f,c,s);

return 0;
}

// No.6 ------ fact.c ---------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[]){
int i, j;

printf("factorial calculation\n");
j=atoi(argv[1]);
if(argc>0){
if(j){
i = fact(j);
printf("fact(%d)= %d\n",j, i);
return 0;
}
}
else
printf("fact = 0\n");

return 0;
}
int fact(int j){
printf("%d ",j);
if(j)
return( j * fact(j-1));
else
return 1;
}

// No.7 ----- dec.c --------------
#include <stdio.h>

int main(){
int i;
int j=0;

for(i=100; i>0; --i)
j += i;
printf("total = %d\n",j);

return 0;
}

// No.8 --------- dec2.c ---------
#include <stdio.h>

int main(){
int i;
int j=0;

for(i=100; i>0; i--)
j += i;
printf("total = %d\n",j);

return 0;
}

// No.9 ---- auto.c -------------
#include <stdio.h>
#include <stdlib.h>

static struct st{
int i;
char c;
} tst;

int main(){
tst.i = 128;
tst.c = 'A';
printf("i = %d c = %c\n",tst.i,tst.c);
return 0;
}

// No.10 ---- enum.c -------------------
#include <stdio.h>

int main(){
enum color_code{
black=0,
red=1000,
blue2000,
green=3000,
yellow=4000,
white=8000
};
enum color_code color;

color=green;
printf("color=%d\n",color);

return 0;
}

// No.11 ------------ aliment.c ---------
#include <stdio.h>

struct testcell{
char c;
double d;
float f;
short s;
int i;
long l;
char *cp;
}t;

int main(){
int i;

i=sizeof(int);
t.c='a';

i=sizeof(t.c);

printf("sizeof char c=%2d : %d\n",sizeof(t.c),&t.c);
printf("sizeof double d=%2d : %d\n",sizeof(t.d),&t.d);
printf("sizeof float f=%2d : %d\n",sizeof(t.f),&t.f);
printf("sizeof short s=%2d : %d\n",sizeof(t.s),&t.s);
printf("sizeof int i=%2d : %d\n",sizeof(t.i),&t.i);
printf("sizeof long l=%2d : %d\n",sizeof(t.l),&t.l);
printf("sizeof char *cp=%2d : %d\n",sizeof(t.cp),&t.cp);

printf("sizeof testcell =%2d : %d\n\n",sizeof(t),&t);

return 0;
}

// No.12 ---- atof.c -------------------
#include <stdio.h>
#include <stdlib.h>

int main(){
char ch[]={"9876.54321"};
double f1,f2;

f1 = atof("1234.5678");
f2 = atof(ch);
printf("f1=%10.5f f2=%12.6f \n",f1,f2);

return 0;
}

// No.13 ------ 3term.c ----------------------
#include <stdio.h>

char getch(){
int a, b;

do{
a = getchar();
if( a != '\n')
b = getchar();
}while(b != '\n');

return a;
}

int main(){
int a=0, b=0, c=0, d=0;

printf("input a = ");
a = getch();

printf("input b = ");
b = getch();

printf("input c = ");
c = getch();

d = (a < b) ? a : (a ==b) ? b : c;
printf("d = %c \n",d);

return 0;
}

// No.14 ------- operator.c -------
#include <stdio.h>

int main(){
int i;
int a[10];

a[1] = 0;
a[1] +=1; // a1 = a1 + 1;
a[1] -=1; // a1 = a1 - 1;

a[2] = 3;
a[2] <<=1; // a2 = a2 << 1;

a[3] = 3;
a[3] >>=1; //a3 = a3 >> 1;

a[4] = 0xaa; //1010 1010
a[4] |= 0x55;

a[5] = 0xaa;
a[5] &= 0xff;

a[6] = 0xaa;
a[6] ^= 0xff;

a[7] = 0xaa;
a[7] ^= 0;

for(i=1; i<=7; i++)
printf("a[%d]=%x, ",i , a[i]);
printf("\n");

return 0;
}

// No.15 ------ canma.c ----------
#include <stdio.h>
#include <stdlib.h>

int main(){
int i, j;

j = (i=6, i += 10);
printf("j= %d \n",j);

return 0;
}

// No.16 --------- pointer2.c -------------
#include <stdio.h>

int main(){
int a=1, b=2, c;

func(a, b, &c);
printf("%d + %d = %d \n", a, b, c);
return 0;
}

func(int s1, int s2, int *s){
*s = s1 + s2;
}

// No.17 -------- pointer4.c ------------
#include <stddef.h>
#include <string.h>

void remove_newline(char *p){
int len;
char *q;

if( p == NULL) return;
len = strlen(p);
if ( len == 0) return;

q = &p[len -1]; //最後の文字
if(*q == '\n')
*q = '\0';
}
int main(){
char ch[]="This is the test of pointer.\n";

remove_newline(ch);
printf(ch);
printf("......\n");

return 0;
}

// No.18 ------- pointer5.c -----------------------------------------
#include <stdio.h>

int main(){
char *pa[] = {"abcde", "abc", "f", ""}; //ポインタの配列
char aa[][6] = {"abcde", "abc", "f", ""};
char *p = {"This is the test of pointer."}; //文字列に体するポインタ

printf(" size aa=%d\n", sizeof(aa));
printf(" %s %s %s %s \n", pa[0],pa[1],pa[2],pa[3]);
printf(" %s \n",p);

return 0;
}

// No.19 -------- func-p.c ------------------
#include <stdio.h>
#include <stdlib.h>

int (*fp)(); //関数へのポインタ
int (*fp3[3])(); //関数へのポインタの配列

int func1(int j){
return j * 10 + 1;
}
int func2(int j){
return j * 10 + 2;
}
int func3(int j){
return j * 10 + 3;
}

int main(){
int k, i, j=12;
fp3[0] = func1;
fp3[1] = func2;
fp3[2] = func3;

for(k=0; k < 3; k++){
i = fp3[k](j);
printf(" j = %d i = %d \n", j, i);
}
return 0;
}

// No.20 ----- func-p2.c --------------------
#include <stdio.h>

int x, y;

int func1(){
return 10;
}

int func2(){
return 20;
}

int func3(){
return 30;
}

int func4(){
return 40;
}

int addfunc(int (*f1)(), int (*f2)() ){
return( (*f1)() + (*f2)() );
}

int main(){
x = addfunc( func1, func2);
y = addfunc( func3, func4);

printf("x = %d, y = %d\n", x, y);

return 0;
}

// No.21 ------- array.c --------------------------
#include <stdio.h>

void movstr(char buf[],char *str){
int i = 0;

while(*str)
buf[i++] = *str++;
buf[i] = '\0';
}

int main(){
char cbuf[100];

movstr(cbuf, "これは配列のテストです。");
printf("%s\n",cbuf);
return 0;
}

// No.22 -------- array2.c -------------------------
#include <stdio.h>

int main(){
char ch[3][5] = {1, 2, 3, 4, 5,
6, 7, 8, 8, 10,
11,12,13,14,15};
int i, j;

for(i=0; i<3; i++){
for(j=0; j<5; j++)
printf("%5d",ch[i][j]);
printf("\n");
}
}

// No.23 ------- struct1.c -----------------------
#include <stdio.h>

struct st{ //stは構造体のタイプの定義
int a;
float f;
long l;
char c;
char ch[100];
} ast; //タイプst の構造体の実態が ast

struct st astt[10]; //st型の構造体配列 astt

int main(){
strcpy(ast.ch, "これは構造体の文字です。");
ast.a = 0;
astt[0].l = 123.456;

printf("l = %d %s \n", astt[0].l, ast.ch);

return 0;
}

// No.24 -------- struct2.c -----------------------
#include <stdio.h>
struct st{ //stは構造体のタイプの定義
int a;
float f;
long l;
char c;
char ch[100];
};
struct st ast1, ast2;

void memcpy2(struct st *s1, struct st *s2){
int j;

for(j=0; j<100; j++)
s2->ch[j] = s1->ch[j];
}

int main(){
strcpy(ast1.ch, " 構造体のサンプル2。関数の構造体へはポインタで渡します。");
memcpy2(&ast1, &ast2);
printf("%s\n",ast2.ch);

return 0;
}

// No.25 ------- struct3.c -------------------------------------------
#include <stdio.h>

struct st{
struct st *next;
char ch[100];
};
struct st astt[10];

int main(){
int i;
struct st *sp;

for(i=1; i<10; i++){
sp = &astt[i-1];
astt[i].next = sp;
printf("struct%d = %d\n", i, sp);
}
return 0;
}

// NO.26 ------- bit.c -----------------
#include <stdio.h>

struct st{
unsigned bit0: 1;
unsigned bit1: 1;
unsigned bit2: 1;
unsigned bit3: 1;

unsigned : 0;

unsigned bit20: 1;
unsigned bit21: 2;
unsigned bit22: 3;
}flags ;

int main(){
int i;

flags.bit0 = 1;
flags.bit1 = 0;
flags.bit2 = 1;
flags.bit3 = 0;

flags.bit20 = 1;
flags.bit21 = 3;
flags.bit22 = 7;

if(flags.bit0==1)
printf("flags.bit0 = 1\n");
switch(flags.bit22){
case 0: printf("flags.bit22=0\n"); break;
case 7: printf("flags.bit22=7\n"); break;
default:
}
return 0;
}

// NO.27 -------- macro.c -----------------
#include <stdio.h>

#define max( x, y ) x > y ? x : y

int main( ){
int x=100, y=10, e;

e = max(x, y);
printf("max=%d \n",e);

e= ( x < y ? x : y );
printf("min=%d \n",e);

return 0;
}

// No.28 ----------file1.c --------------------------------------
#include <stdio.h>

FILE *fp;
char name1[] = {"test-file1.txt"};
char ch1[] = { "これはファイルの書き込みのテストです。No.1\n"};
char name2[] = {"test-file2.txt"};
char ch2[] = { "これはファイルの書き込みのテストです。No.2\n"};

int main(){
int i;

if( (fp = fopen(name1, "w+"))==NULL){
fprintf(stderr,"1. Cann't Open test-file1.txt/n");
return 1;
}
i = 0;
while(ch1[i]){
fputc((int)ch1[i], fp);
i++;
}
fclose(fp);

if( (fp = fopen(name2, "w+"))==NULL){
fprintf(stderr,"2. Cann't Open test-file2.txt/n");
return 1;
}
fwrite(ch2, sizeof(char), sizeof(ch2), fp);
fclose(fp);

if( (fp = fopen(name2, "r"))==NULL){
fprintf(stderr,"3. Cann't Open test-file2.txt/n");
return 1;
}
i = fread(ch2,sizeof(char),sizeof(ch2), fp);
fclose(fp);
if(i != 0)
printf("%s\n",ch2);
else
fprintf(stderr,"4. File Read Error");
return 0;
}

// NO.29 ------- static.c ---------------------
#include <stdio.h>

void func1(int i){
static int idata;
int iidata;

printf("idata= %d iidata=%d\n",idata,iidata);
idata = i;
iidata = i * 2;
printf("idata= %d iidata=%d\n\n",idata,iidata);
}
void func2(int i){
static int idata;
int iidata;

printf("idata= %d iidata=%d\n",idata,iidata);
idata = i;
iidata = i * 2;
printf("idata= %d iidata=%d\n\n",idata,iidata);
}

int main(){
int i;

func1(123);

//for(i=0;i<2;i++){
// int j=0,k=0;
//}

func2(789);
func1(123);

return 0;
}

// No.30 ------ low1.c -------------------
#include <unistd.h>

int main(){
char ch[]={"低水準書き込みのてすと\n"};
char che[]={"書き込みエラーです。\n"};
char chw[]={"書き込み成功です。\n"};

size_t size, sizew;

size = sizeof(ch);
sizew = write( 1,ch, size );
if(size != sizew)
write(2, che, sizeof(che));
else
write(1, chw, sizeof(chw));


printf("size = %d write size= %d\n",size,sizew);

exit(0);
}

// NO.31 ---- low1.c ------------------------
#include <unistd.h>

int main(){
char chre[]={"A Read error occured /n"};
char chwr[]={"A Write error occured /n"};
char ches[]={"Read OK !!/n"};
char chws[]={"Write OK !!/n"};

char ch[128];
int nread;

nread = read(0,ch,128);

if(nread=-1)
write(2,chre,sizeof(chre));
else
write(2,ches,sizeof(ches));

if( ( write(1,ch,sizeof(ch)) != sizeof(ch))
write(2,chwe,sizeof(chwe));
else
write(2,chws,sizeof(chws));

exit 0;
}
// No.32 -------- mem-read.c ----------------
#include <stdio.h>
#define ADD ( *(char *)0x8048528)

int main(){
char *p[] = {"1","2","3","4","5","6","7","8","9","10"};
char ch;

printf("address0=%x data0=%s\n",p[0], p[0]);
printf("address9=%x data9=%s\n",p[9], p[9]);

ch = ADD;
printf("ADD=%s ch=%c\n","0x8048528",ch);

return 0;
}

// No. 33 ---- mem-read2.c ----------------------------------------
#include <stdio.h>

#define ADDRESS 0x8048580
#define SADD "0x8048580"
#define ADD ( *(char *)ADDRESS)

int main(){
char *cp = (char *)(ADDRESS);
char *cp2 = (char *)ADDRESS; // ADDRESSの()無し

char *p[] = {"1","2","3","4","5","6","7","8","9","10"};
char ch;

printf("address0=%x data0=%s\n",p[0], p[0]);
printf("address9=%x data9=%s\n\n",p[9], p[9]);

//------- method 1 ----------------------
ch = (char)*cp;
printf("address1=%s ch=%x\n",SADD,ch);
//------- method 2 --------------------
ch = (char)ADD;
printf("address1=%s ch=%x\n",SADD,ch);
//------ method 3 ---------------------
ch = (char)*cp;
printf("address2=%s ch=%x\n",SADD,ch);

return 0;
}

//-------------------------------------------------------