7 Hello!! C
2
. 3
([] ) < > [ ]; int array[10]; < > [ ][ ]; int array [3] [5]; 4
< > [ ]={ x1,,x10} ( ); (,). ({}). : int array[10]={1,2,3,4,5,6,7,8,9,10}; (" "). : char array[7]="turbo-c"; 5
int array[2][3]={{1,2},{3,4},{5,6}}; int array[3][10]={"turbo-c","is","easy!"}; 6
[ 7-1] #include <stdio.h> #define MAX 10 int prn(int x[max]); main(){ int num[max] = {1,2,3,4,5,6,7,8,9,10}; prn(&num[0]); } int prn(int x[max]){ int i; printf("the address of array name is %d \n", x); for ( i = 0 ; i < MAX ; i++ ){ printf("address : %d\t", &x[i]); printf("value : %d\n", x[i]); } } /* prn */ 7
[ 7-1] [ ] The address of array name is 404 ADDRESS : 4076 VALUE : 1 ADDRESS : 4078 VALUE : 2 ADDRESS : 4080 VALUE : 3 : : : : ADDRESS : 4094 VALUE : 10 8
. pnt a, pnt a. * ; 9
[ 7-2] #include<stdio.h> main() { int a = 50; int *pnt; } printf("a=%d pnt=%d\n ",a,pnt); [ ] a=50 pnt=1824 10
[ 7-3] #include<stdio.h> main() { int a = 50; int *pnt; /* */ /* */ } pnt = &a; /* */ printf("a=%d pnt=%d \n",a,pnt); [ ] a=50 pnt=4094 11
: int_pnt2=int_pnt1+1; int_pnt1 (int==> 2byte) int_pnt2 100 103 int_pnt1 int_pnt2 ` ' int_pnt++ ==> 2byte float_pnt++ ==> 4byte 12
[ 7-4] #include <stdio.h> #define MAX 10 int i_array[max] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10}; /* */ int I; int *i_pnt; /* */ float f_array[max] = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; /* */ float *f_pnt; /* */ main(){ i_pnt = i_array; /* */ f_pnt = f_array; /* */ [ ] for ( i=0 ; i<max ; i++) 1 0.100000 printf("%d\t%f\n",*i_pnt++,*f_pnt++); 2 0.200000 } 3 0.300000 : : 10 1.000000 13
(*) ([]) *(pnt+i) : pnt i. array[i] : array I. : char *pnt[3]={`kim',`lee',`park'}; pnt malloc(), calloc() 14
[ 7-5] #include<stdio.h> main(){ int a[5]={1,2,3,4}; int *pnt; } pnt = a; printf("%d %d %d %d \n",*pnt,*(pnt+1),*(pnt+2),*(pnt+3)); printf("%d %d %d %d \n",a[0],a[1],a[2],a[3]) ; [ ] 1 2 3 4 1 2 3 4 /* */ 15
[ 7-6] #include<stdio.h> main(){ int a[2][2] = { 1,2, 3,4 }; int *pnt; pnt=a[0]; printf("%d %d\n%d %d\n",*pnt,*(pnt+1),*(pnt+1*2),*(pnt+1*2+1)); printf("%d %d\n%d %d\n",a[0][0],a[0][1],a[1][0],a[1][1]); } [ ] 1 2 3 4 1 2 /* */ 3 4 16
[ 7-7] #include<stdio.h> main() { int a[4] = {1, 2, 3, 4}; int *pnt; } pnt=a; printf("%d %d %d %d \n",pnt[0],pnt[1],pnt[2],pnt[3]); [ ] 1 2 3 4 17
main int main() int main( argc, argv ) int argc; char **argv; char **string_array={"dog","cat","lion","tiger"}; void 18
[ 7-8] #include<stdio.h> void main() { char *pnt[] = {"Chan","Ho","Park"}; char array[3][5]= {"Chan","Ho","Park"}; int j; printf("memory address of pointer array pnt\n"); printf("pnt => %p\n",pnt); for(j=0;j<3;j++) printf("pnt[%d] (address %p) => %s\n",j,pnt[j],pnt[j]); /* */ /* */ printf("\nmemory address of two-dimensional array\n"); printf("array => %p\n",array); for(j=0;j<3;j++) printf("array[%d] (address %p) => %s\n",j,array[j], array[j]); } 19
[ 7-8] [ ] memory address of pointer array pnt pnt => 1747:0FF4 pnt[0] (address 16FB:00AF) => Chan pnt[1] (address 16FB:00B4) => Ho pnt[2] (address 16FB:00B7) => Park memory address of two-dimensional arrayarray => 1747:0FE4 array[0] (address 1747:0FE4) => Chan array[1] (address 1747:0FE9) => Ho array[2] (address 1747:0FEE) => Park 20
[ 7-9] #include<stdio.h> main() { int x; int *pnt=&x; int **p_pnt=&pnt; /* pnt x */ /* p_pnt pnt */ } **p_pnt=100; printf("x=%d\n",x); /* x 100 */ [ ] x=100 21
ASCII char char char array[6]="seoul"; 22
[ 7-10] /* */ #include <stdio.h> main() { char ch = 'a'; char num = '2'; } printf("ch=%c, num=%c\n",ch,num); [ ] ch=a, num=2 23
char *pnt="hello"; pnt `h'. char pnt[]="hello"; ==> `h. 24
gets(), scanf() stdio.h gets() gets(*str); str scanf() scanf("%s",*str); str 25
[ 7-11] #include <stdio.h> main() { char array[20]; } printf("enter a string : "); gets(array); printf("the received string : %s\n", array); [ ] Enter a string : I love you. The received string : I love you. 26
[ 7-12] #include <stdio.h> main() { char season[10]; char month[10]; printf("enter season and month\n"); scanf("%s%s", season, month); printf("season = %s, month = %s", season, month); } [ ] Enter season and month spring May season = spring, month = May 27
puts(), prinf() stdio.h puts() puts(*str); printf() str printf("%s",str); str 28
[ 7-13] /* Example using puts() library function */ #include <stdio.h> main(){ char *a="how"; char *b="old"; char string1[]="are"; char string2[]="you!"; puts(a); puts(b); puts(string1); puts(string2); [ ] } How old are you! 29
[ 7-14] /* Example using printf() library function */ #include <stdio.h> main(){ char f_name[30] = "Michael"; char l_name[30] = "Jordan"; } printf("what`s your name?\n"); printf("f_name : %s\n", f_name); printf("l_name : %s\n", l_name); [ ] What`s your name? f_name : Michael l_name : Jordan 30
string.h, strlen() size_t strlen(char *str);, strcpy() str char *strcpy(char *destination, char *source);, strcat() source destination strcat(char *destination, char *source); source destination 31
[ 7-15] /* Program for an example of string library functions */ #include <stdio.h> #include <string.h> main(){ char string[30]; int a; printf("enter a string : "); gets(string); a=strlen(string); printf("the length of string is %d.",a); } [ ] Enter a string : abcdefg The length of string is 7. 32
[ 7-16] /* Example using strcpy() function */ #include <stdio.h> #include <string.h> main(){ char str1[80],str2[80],*str3; printf("enter string1 : "); gets(str1); printf("enter string2 : "); gets(str2); str3=strcpy(str1,str2); printf("%s is copied in str1\n",str3); } [ ] Enter string1 : Korea Enter string2 : aerok aerok is copied in str1 33
[ 7-17] /* Example using strcat() library function */ #include <stdio.h> #include <string.h> main(){ char dest[80]; char sour[80]; } gets(dest); printf("this string, [%s] is in dest.\n", dest); gets(sour); strcat(dest, sour); printf("this string, [%s] is in sour.\n", sour); printf("this string, [%s] is in dest.", dest); 34
[ 7-17] [ ] Hi, This string, [Hi, ] is in dest. everyone. This string, [everyone.] is in sour. This string, [Hi, everyone.] is in dest. 35
, strchr() strchr(char *a, int b); a b, strlwr() strupr() strlwr(char *ptr); ptr 36
[ 7-18] /* Example using srtchr() library function */ #include <stdio.h> #include <string.h> void main(void){ char a[80],*d; int c; printf("enter a string : "); gets(a); printf("the character to be searched : "); c=getchar(); d=strchr(a,c); printf("the character you are searching,"); printf("'%c'is discovered %d position in a.",c,d-a+1); } 37
[ 7-18] [ ] Enter a string : beautiful The character to be searched : l The character you are searching,'l'is discovered 9 position in a. 38
[ 7-19] /* Example using strlwr() and strupr() */ #include <stdio.h> #include <ctype.h> main(){ char a,b,c; char ch; printf("enter alphabet : "); scanf("%c", &ch); b = tolower( ch ); printf("convert into lower case letter : %c\n",b); c = toupper( ch ); printf("convert into upper case letter : %c\n",c); } [ ] Enter alphabet : a Convert into lower case letter : a Convert into upper case letter : A 39