10 Call by Value Call by Reference Call by Result Call by Name C call by value void int X int hap(int x, int y, int z, int w); printf("%d\n", hap(3, 4, 5, 6)); int hap(int x, int y, int z, int w) return x+y+z+w; main X 01 int expo(int a, int b) 02 03 int i,s=1; 04 for(i=1;i<=b;i++) 05 s=s*a; 06 return s; 07
for(i=1;i<=4;i++) s=s*3; 3 81 void gugudan(int k); int dan; printf(" >"); scanf("%d", &dan); gugudan(dan); void gugudan(int k) int i; for(i=1; i<=9; i++) printf("\n%d*%d=%2d", k, i, k*i); int num1, num2; printf(" Enter>"); scanf("%d", &num1); if (num1<0) num2=-num1; printf("\n%d %d", num1, num2); int absolute(int abs) if (abs<0) abs=-abs; return abs;
double factorial(int n); int n; printf("\n n!. "); printf("\n n Enter"); scanf("%d", &n); printf("\n n!=%.lf", factorial(n)); double factorial(int k) double product=1; int i; for(i=1; i<=k; i++) product=product*i; return product; int IsLeapYear (int y); int year; printf( : ); scanf( %d, &year); printf( \n %d ); if (IsLeapYear(year)) printf(. ); else printf(. ); int IsLeapYear(int y) if((y%4==0 && y%100!=0) y%400==0) return 1; else
01 02 #define square(x) x*x 03 04 05 int y=4; 06 printf("%d %d\n",square(y), square(y+1)); 07 08 square(y+1)=y+1*y+1 01 02 int number(int i); 03 04 05 number(6); 06 07 08 int number(int i) 09 10 if (i==0) 11 return i; 12 else 13 number(i/2); 14 printf("%d", i%2); 15 number() 2
11 X X X X RAM register register
01 02 03 04 int x=0; 05 06 int x=2; 07 printf("%d", x); 08 x+=3; 09 10 printf("%d", x); 11 12 line 10 x line 04 x 0
12 stdio.h string.h math.h dos.h scanf() scanf() gets() NULL 1 byte 2 byte fflush() fflush() #include <ctype.h> char ch='a'; printf("%d", isalnum(ch)); printf("%d", isalpha(ch)); printf("%d", isascii(ch)); printf("%d", isdigit(ch)); printf("%d", isupper(ch)); #include <string.h> char *a="visual c++", b[6]="turbo"; printf("%d \n", strlen(a)); printf("%d \n", strcmp(a,b)); printf("%s \n", strlwr(b)); printf("%s \n", strupr(a));
01 02 #include <time.h> 03 04 05 int i, x, y; 06 srand(time(null)); 07 for(i=1;i<=10;i+=1) 08 09 x=rand()%9+1; 10 y=rand()%9+1; 11 printf("%d*%d=?\n", x, y); 12 13 01 02 03 04 05 06 long num=9876543; 07 08 09 char s[25]; ltoa(num2, s, 10); printf( :%d, :%s\n, num, s); 10 printf( :%d\n, strlen(s); ); 11 12
01 02 #include <time.h> 03 #include <conio.h> 04 05 06 int i; 07 time_t t1. t2; 08 t1 = time(null); 09 printf( : \n%s, ctime(&t1)); 10 11 t2=t1+3*60l*60l; printf( : \n%s, ctime(&t2)); 12 13 01 02 #include <stdlib.h> 03 #include <time.h> 04 #include <conio.h> 05 06 07 int i; 08 time_t t; 09 srand(time(null)); 10 printf(". \n"); 11 printf(".\n"); 12 printf(".\n\n"); 13 printf(" \n"); 14 while(!kbhit()) 15 16 17 printf("%2d", rand()%9+1); 18 printf("%2d", rand()%10); 19 printf("%2d", rand()%10); 20 printf("%2d", rand()%10); 21 printf("\n."); 22 23
A Z 100 rand() 01 02 #include <stdlib.h> 03 #include <time.h> 04 05 06 int i, rnd; 07 char alpha[26]=0; 08 srand(time(null)); 09 printf(" \n"); 10 for(i=1;i<=100;i++) 11 12 rnd=rand()%26; 13 printf("%c", rnd+65); 14 if (i%20==0) 15 printf("\n"); 16 alpha[rnd]+=1; 17 18 printf("\n \n"); 19 for(i=0;i<26;i++) 20 21 printf("%c : %2d ", i+65, alpha[i]); 22 if ((i+1)%3==0) 23 printf("\n"); 24 25 26
01 02 #include <math.h> 03 #define PI 3.141592 04 05 06 int i, j ; 07 double sine; 08 for(i=0 ; i<=360 ; i+=15) 09 10 sine=sin((double)i*pi/180); 11 printf("sin(%3d)= %5.2f", i,sine); 12 for(j=0 ;j<=(int)(sine*10+10);j++) 13 printf(" "); 14 printf("* \n"); 15 16 17 #define PI 3.141592 sin((double)i*pi/180); for(j=0; j<=(int)(sine*10+10); j++) printf(" "); sine sine sine '*' sin(0 0 )=0 11 (for(j=0;j<=10;j++) '*' sin(45 0 )=0.71 18 (for(j=0;j<=17;j++) '*'
01 #include <time.h> 02 03 04 05 int birthy, birthm, birthd; 06 time_t curr; 07 struct tm *d; 08 curr=time(null); 09 d=localtime(&curr); 10 printf(" Enter.>"); 11 scanf("%d",birthy); 12 printf(" Enter.>"); 13 scanf("%d",birthm); 14 printf(" Enter.>"); 15 scanf("%d",birthd); 16 printf("\n"); 17 printf("%d %d %d \n",d->tm_year+1900,d->tm_mon+1, d->tm_mday); 18 printf(" \n); 19 printf("%d %d %d \n",birthy, birthm, birthd); 20 diffy=birthy-d->tm_year+1900; 21 diffm=birthm-d->tm_mon+1; 22 diffd=birthd-d->tm_mday; 23 if (diffm<=0 && diffd<=0) 24 diffy++; 25 printf(" : %d\n", diffy); 26 27
13 int korea[5]; int korea[ ]=2, 4, 9, 6; int korea[3]=2, 4, 9, 6; int korea[5]=2, 8; 0 char c[5]="test"; char c[2]="test"; char c[7]="test"; char c[ ]="test"; auto int num[8]; static int num[8]; register int num[8]; extern int num[8]; 2 3 cha int int arr[7]=11, 22, 33, 44, 55, 66, 77; int i; for(i=1; i<=6; i=i+2) printf("%d\n", arr[i]);
i 1 2 arr[1]arr[3]arr[5] 0 < int arr[3]=0; int i; for(i=0; i<3; i++) printf("%d", arr[i]); %c %d %d ASCII char ch[9]="cprogram"; int i; for(i=0; i<8; i++) printf("%c : %3d\n", ch[i], ch[i]); i 2 0 d[2-i] 0 2 double d[3]=0.1, 0.2, 0.3; int i; for(i=2;i>=0;i--) printf("%.1f\n", d[2-i]);
korea[9] japan[9] int korea[9]=0,0,0,0,1,0,0,2,0; int japan[9]=1,1,0,0,0,0,0,0,0; int i, sumk=0, sumj=0; printf(" \n\n"); printf(" \t 1 2 3 4 5 6 7 8 9 Run\n"); printf("korea\t"); for(i=0;i<9;i++) sumk+=korea[i]; printf("%2d", korea[i]); printf("%3d\n", sumk); printf("japan\t"); for(i=0;i<9;i++) sumj+=japan[i]; printf("%2d", japan[i]); printf("%3d\n", sumj); 6 char num[7];
atof() atol() ASCII char num[7]="871209"; 1987 (num[0]-48)*10+num[1]-48+1900; '8' ASCII 56 48 8 10 10 '7' 1 num[1]-48 1900 int birthy, birthm, birthd; char num[7]; printf(">"); scanf("%s", num); birthy=(num[0]-48)*10+num[1]-48+1900; birthm=(num[2]-48)*10+num[3]-48; birthd=(num[4]-48)*10+num[5]-48; printf("%d %d %d. \n", birthy, birthm, birthd); t(1,2)=3; t[1,2]=3; t(0)(1)=3; t[0][1]=3; C
int a[2][3]=, i, j; for(i=0; i<2;i++) for (j=0;j<3;j++); printf( %d, a[i][j]); printf( \n ); int a[2][2][2]=1,2,3,4,5,6,7,8 3 a a[0][0][0]=1, a[0][0][1]=2 a[0][1][0]=3, a[0][1][1]=4 a[1][0][0]=5, a[1][0][1]=6 a[1][1][0]=7, a[1][1][1]=8 transpose matrix matrix[i][j] matrix[j][i]
int matrix[3][3]=2, 4, 6, 8, 10, 12, 14, 16, 18; int i, j; printf("original matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf("%2d", matrix[i][j]); printf("\n"); printf("\ntranspose matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf("%2d", matrix[j][i]); printf("\n"); long money, i, num; long money_unit[]=10000, 5000, 1000, 500, 100, 50, 10; printf(" Enter>"); scanf("%ld", &money); printf("\n \n"); for(i=0; i<=6;i++) num=money/money_unit[i]; printf("%5ld : %3ld \n", money_unit[i], num); money=money-num*money_unit[i];
ASCII 'A' 10 65'a' 97 'A' 'A'-65 0 chr[0] 'a' 'a'-97=0 chr[0] char ch[100]=0; int chr[26]=0, i, nchr=0; printf(" count\n\n"); printf(" Enter\n"); printf(">"); gets(ch); for(i=0; ch[i]!=0 ; i++) if( ch[i]>=65 && 90>= ch[i]) chr[ch[i]-65]+=1; else if (ch[i]>=97 && 122>= ch[i]) chr[ch[i]-97]+=1; else nchr+=1; printf("\n\n"); printf(" count\n"); for(i=0; i<26 ; i++) if (chr[i]==0) continue; printf("%c \t %d\n", i+65, chr[i]); printf("\t %d", nchr);
strlen() ASCII #include <string.h> char str[11]; int i, len, sum; printf(" Enter>"); scanf("%s", str); len = strlen(str); sum = 0; for(i = 0; i < len; i++) sum += str[i]; printf(" : %d\n", sum);
14 int x; x &x x x x x *po+3 *(po+3) int a[4]=10,20,30,40; int *po=a; 13, 20 13, 40 23, 30 23, 40 13, 23 *po+3 a[0]+3 13*(po+3) a[3] 40 int a[5]=3,1,4,2,5; int *pt=a; *a+1 a[1] *(a+1) *++pt *(pt+1) a a[0] *a+1 a[0]+1 4 *(a+1) a[1] *(pt+1) *++pt ++pt; *pt; a[1] int i=15, j=20, *p; p=&i; j=*p; printf("%d",j); int *p, y[3]=2, 6, 9, a; p=y; a=*p++; printf("%d", a); printf("%d", *p);
int i=15, j=20, *p; p=&i; p i j=*p; p 15 j printf("%d",j); a=*p++ a=*p; p++ a=*p; a y[0] p p++ y[1] char *p="computer"; int i=0; while(*p!= '\0') i++; p++; printf("%d",i); char x[3], *p; x[1]='a'; p=&x[1]; *(p-1)='b'; *(p+1)='c'; printf("%c %c %c\n",*(p-1),*p,*(p+1)); char *p="computer"; p "computer" 'c' while p NULL p i while NULL i "computer" 8
int k[]=1,2,3,4,*p; p=&k[0]; printf("%d\n",*p); p++; printf("%d\n",*p); printf("%d\n", *p+1); printf("%d\n",*(p+1)); p=&k[0]; p k k[0] p++; p k[1] *p k[1] *p+1 k[1]+1 2+1 3 *(p+1) p &k[1] &k[2] k[2] 3 int a[3][3]=1,2,3,4,5,6,7,8,9; int b[ ][3]=7,8, 9,10,11; printf(" %d \n", a[1][1]); printf(" %d \n",*(a[2]+2)); printf(" %d \n",(*(b+1))[2]); a a[0][0]=1, a[0][1]=2, a[0][2]=3, a[1][0]=4, a[1][1]=5, a[1][2]=6, a[2][0]=7, a[2][1]=8, a[2][2]=9, b b[0][0]=7, b[0][1]=8, b[0][2]=0, b[1][0]=9, b[1][1]=10, b[1][2]=11, *(a[2]+2) a[2] a[2][0] a[2]+1 a[2][1]* a[2]+2 a[2][2] 9 (b+1)
b[1][0] (*(b+1))[2] b[1][2] 6 char num[7]; atof() atol() ASCII char num[7]="871209"; 1987 = (num[0]-48)*10+num[1]-48+1900; '8' ASCII 56 48 8 10 10 '7' 1 num[1]-48 1900 1900 1900 12 0 int birthy, birthm, birthd, day; char *animal[12]=" ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", ""; char num[7]; printf(">"); scanf("%s", num); birthy=(num[0]-48)*10+num[1]-48+1900; birthm=(num[2]-48)*10+num[3]-48; birthd=(num[4]-48)*10+num[5]-48; printf("%d %d %d, \n", birthy, birthm, birthd);
day=(birthy-1900)%12; printf("%s.\n",animal[day]); long long money; long money_unit[]=1000, 500, 100, 50, 10, 5, 1, i, num; char *unit[]=" ", "", " ", "", " ", "", " "; printf(" Enter>"); scanf("%ld", &money); printf("\n\n"); money=money/10l; for(i=0; i<=6; i++) num=money/money_unit[i]; printf("\n%8s %3d", unit[i], num); money=money-num*money_unit[i];
int i; long cost[5]=135000, 235000, 122000, 260000, 73500; long quantity[5], sum, total=0; char *good[5]="hard disk", "CPU(intel)", "main board", "LCD monitor", "DVD-RW(combo)"; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", good[i]); scanf("%ld", &quantity[i]); printf("\n\n"); printf("\n\t\t\t\t"); for(i=0; i<=4; i++) sum=cost[i]*quantity[i]; printf("\n%s\t%7ld\t%3ld %10ld ", good[i], cost[i], quantity[i], sum); total+=sum; printf("\n \t\t\t %12ld ", total);
15 struct user d_1; user PERSON; struct user data[10]; struct user *point; struct user $amount; struct std int number; char *name; ; std st1, st2; struct st1, st2; std struct st1, st2; struct std st1, st2; typedef st1, st2; var name name char name[10]; "kim" name="kim"; *name="kim"; var.name="kim"; strcpy(var.name, "kim"); strcpy() var name name char *name; "kim" name="kim"; *name="kim"; var.name="kim"; strcpy(var.name, "kim");
person struct person char name[10]; int age; ; struct person d="park", 18; strcpy(d.name, "Park"); scanf("%s", d.name); d.name="park"; d1 byte struct data char name[12]; long income; double saving; d1; 20 22 24 30 byte 12 byte + 4 byte + 8 byte = 24 byte #include <string.h> struct student char name[20]; int jumsu; ; struct student st; printf(" Enter"); scanf("%s", st.name ); printf(" Enter"); scanf("%d", &st.jumsu ); printf(" : %s \n, st.name ); printf(" : %d \n, st.jumsu ); scanf() &
& & struct planet char name[10]; long diameter; float distance; float speed; float cycle; char satellite; bit field bit field bit
struct person char name[10]; int age; ; struct person d, *pt; pt=&d; strcpy((*pt).name, "Choi"); strcpy(pt->name, "Choi"); scanf("%s", pt.name); scanf("%s", pt->name); struct person char *name; int age; ; struct person d, *pt; pt=&d; scanf("%s", pt->name); strcpy("%s", pt->name); (*pt).name="lee"; pt->name="lee"; struct human char name01[10]; char *name02; data01="miss Kim", "Mr Park"; struct company char com_name[10]; struct human people; int salary; data02="choi", "Kim", "Park", 500; printf("%s\n", data02.com_name); printf("%s\n", data02.people.name01); printf("%c\n", data01.name01[1]); printf("%d\n ",data02.salary);
company data02.com_name "Choi" data02.people.name01 "Kim" data01.name01 "Miss Kim" data01.name01[1] 'i' data02.salary 500
16 struct union 01 02 #include <string.h> 03 union univar 04 05 char ch1; 06 char ch2[4]; 07 ; 08 09 10 union univar data; 11 strcpy(data.ch2, "ABC"); 12 data.ch1='d'; 13 printf("%s\n", data.ch2); 14 15 DBC data 4 byte (char ch2[4];) strcpy(data.ch2, "ABC"); 1 bit byte 'A' 01000001 'B' 01000010 'C' 01000011
data.ch2[0] data.ch2[1] data.ch2[2] A B C NULL data.ch1= D ; data.ch2[10] data.ch1 data.ch2[1] data.ch2[2] D B C NULL DBC enum card spade, diamond, heart, clover; enum card card01; card01=heart; printf("card01 : %d\n", card01); printf("card01 : %d\n", diamond); printf("card01 : %d\n", spade); card01 : 2 card01 : 1 card01 : 0 enum tag_name week card01 enum card spade, diamond, heart, clover; enum card card01; 0 1 space 0 clover 3
struct patient char age; unsigned sex : 1; unsigned drinking : 1; unsigned smoking : 1; unsigned marriage : 1; ; struct patient a=24,0,1,1,0; printf("age :%d\n", a.age); printf("sex :%d\n", a.sex); printf("drinking :%d\n", a.drinking); printf("smoking :%d\n", a.smoking); char 1 byte 1 bit 4 bit 1 byte + 4 bit byte a 2 byte sex 01 drinking 1smoking 1 marriage 1 24 struct patient char age; unsigned sex : 1; unsigned drinking : 1; unsigned smoking : 1; unsigned marriage : 1; ; struct patient a=24,0,1,1,0; printf(" :%d\n", a.age); if (a.sex==0) printf(" : \n"); else printf(" : \n");
if (a.drinking==0) printf(" : \n"); else printf(" : \n"); if (a.smoking==0) printf(" : \n"); else printf(" : \n"); if (a.marriage==0) printf(" : \n"); else printf(" : \n");
17 ASCII byte type stream open fopen() open open fgets(), fputs() X 's' string fprintf printf fscanf() X fscanf() scanf() fgets() fread() FILE X C high level I/O low level I/O
CD X NULL X fopen() NULL NULL exit() r w a fgets() fscanf() fgetc() fputs()
#include <stdlib.h> char *name[3]=" "," ", " "; char *phone[3]="015-2745-2233", "016-0023-9983", "014-740-4502"; int i; FILE *fp; fp=fopen("sample", "w"); if (fp==null) printf("file open "); exit(1); for(i=0;i<3;i++) printf("%s %s\n", name[i], phone[i]); fprintf(fp, "%s %d\n", name[i], phone[i]); fclose(fp); #include <stdlib.h> char c; FILE *fp; fp=fopen( chr.txt, r ) if (fp==null) printf( File open ) exit(1); while((c=fgetc(fp))!= EOF ) printf( %c, c ) fclose(fp); printf( \n );
int i; long cost[5]=135000, 235000, 122000, 260000, 73500; long quantity[5], sum, total=0; char *good[5]="hard disk", "CPU(intel)", "main board", "LCD monitor", "DVD-RW(combo)"; FILE *fp; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", good[i]); scanf("%ld", &quantity[i]); fp=fopen("sale.txt", "w"); printf("\n\n"); fprintf(fp, "\n\n"); printf("\n\t\t\t\t"); fprintf(fp, "\n\t\t\t\t\n"); for(i=0; i<=4; i++) sum=cost[i]*quantity[i]; printf("\n%s\t%7ld\t%3ld %10ld ", good[i], cost[i], quantity[i], sum); fprintf(fp, "%s\t%7ld\t%3ld %10ld \n", good[i], cost[i], quantity[i], sum); total+=sum; printf("\n \t\t\t %12ld ", total); fprintf(fp, "\n \t\t\t %12ld ", total); fclose(fp);
wb ASCII ASCII byte type random access block block block block block block block fread()block fread() block fseek() fp=fopen("group.dat", "rb"); fseek(fp, 0L, SEEK_END); total_block= ftell(fp) / sizeof(stdata) ; ftell() byte byte block byte blockbyte SEEK_END offset 0 byte ftell() block fseek(fp, 0L, SEEK_END); total=ftell(fp)/sizeof(stdata); block block
10 byte 10 byte 10 byte 10 byte block block block block whence block (n=1) block (SEEK_SET) (SEEK_END) offset fseek(fp, -30L, SEEK_END); 10 byte 10 byte 10 byte block block block block fseek(fp, 20L, SEEK_SET); 10 byte block block block block #include <stdlib.h> struct sale char good[15]; long cost; long quantity; long total;
; int i; long total=0; struct sale sdata[5]="hard disk", 135000, "CPU(intel)", 235000, "main board", 122000, "LCD monitor",260000, "DVD-RW(combo)",73500; FILE *fp; printf(" \n\n"); printf(" Enter\n"); for(i=0; i<=4; i++) printf("%s: >", sdata[i].good); scanf("%ld", &sdata[i].quantity); fp=fopen("sale.bin", "wb"); printf("\n\n"); printf("\n\t\t\t\t"); for(i=0; i<=4; i++) sdata[i].total=sdata[i].cost*sdata[i].quantity; printf("\n%s\t%7ld\t%3ld %10ld ", sdata[i].good, sdata[i].cost, sdata[i].quantity, sdata[i].total); total+=sdata[i].total; printf("\n \t\t\t %12ld ", total); if(fwrite(sdata, sizeof(sale), 5, fp)!=5) printf("file Write Error!"); exit(1); fclose(fp);
#include <stdlib.h> struct sale char good[15]; long cost; long quantity; long total; ; int i; long total_block, block_number; struct sale d; FILE *fp; fp=fopen("sale.bin", "rb"); fseek(fp, 0L, SEEK_END); total_block=ftell(fp)/sizeof(sale); printf(" \n\n"); printf(" block %d.\n", total_block); printf("0 \n", total_block); while(1) printf("\n block (1-%d) Enter >", total_block); scanf("%ld", &block_number); if (block_number==0) break; fflush(stdin); if (block_number> total_block) break; fseek(fp, sizeof(sale)*(block_number-1), SEEK_SET); if(fread(&d, sizeof(sale), 1, fp)!=1) printf("file read Error!");
exit(1); printf("\n\t\t\t\t\n"); printf("%s\t%7ld\t%3ld %10ld \n", d.good, d.cost, d.quantity, d.total); fclose(fp);