Strlen() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* #include <string.h> size_t strlen(const char *s); */ #include <stdio.h> #include <string.h> int main() { char *str = "hello!! wolrd!!"; printf("str : %lu\n" , (unsigned long)strlen(str)); return 0; } |
NULL 문자를 제외한 문자열의 길이를 리턴한다.
strcpy() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* #include <string.h> char *strcpy(char *dest, const char *src) */ #include <stdio.h> #include <string.h> int main() { char buf[100]; char *str = "hello!! world!!"; strcpy(buf,str); printf("%s\n" , buf); return 0; } | cs |
NULL 문자를 포함한 문자열을 복사한 후 dest에 저장하는 함수이다
문자열을 복사한 후에 dest의 주소를 리턴한다.
strcmp() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /* #include <string.h> int strcmp(const char *s1 , const char *s2); */ #include <stdio.h> #include <string.h> int main() { char p1[] = "hello"; char p2[] = "hello"; if(p1 == p2) { printf("p1 == p2\n"); }else{ printf("p1 : %p\n", p1); printf("p2 : %p\n", p2); } if(!strcmp(p1 , p2)) { printf("%s == %s\n", p1, p2); } return 0; } | cs |
두 개의 문자열을 비교한뒤 두 개의 문자열이 일치하면 0을 리턴하고
문자열 s1이 더 크면 양수를 리턴하고 문자열 s2가 더 크면 음수를 리턴한다
strcat() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* #include <string.h> char *strcat(char *dest, const char *src); */ #include <stdio.h> #include <string.h> int main() { char buf[100] = "hello "; strcat(buf, "world!!"); printf("%s\n",buf); return 0; } | cs |
문자열을 연결하는 함수 dest 문자열의 NULL 문자 위치부터 src 문자열을 합친다.
sprintf() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* #include <stdio.h> int sprintf(char *str, const char *format, ...); */ #include <stdio.h> int main() { char buf[1024]; sprintf(buf, "msg : %s", "hello!!"); printf("%s\n", buf); return 0; } | cs |
sptrintf() 와 printf()는 사용법이 동일하다
다만 그 둘의 차이점은 printf()는 화면으로 출력하는데 반해 sprintf()는 해당 내용을 지정된 버퍼로 출력하는 것이다 .
sscanf() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* #include <stdio.h> int sscanf(const char *str, const char *format, ...); */ #include <stdio.h> int main() { int i; char buf[] = {"11:22:33:44:55:66"}; int mac[6]; sscanf(buf, "%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]); for(i =0; i < 6; i++) { printf("mac[%d] : %x\n" , i ,mac[i]); } return 0; } | cs |
scanf() 함수는 사용자로부터 데이터를 입력받는 함수지만 sscanf()함수는 지정된 버퍼로부터 내용을 입력받는 함수이다
isprint()함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /* #include <ctype.h> int isprint(int c); */ #include <stdio.h> #include <ctype.h> int main() { int count =0; unsigned int i = 0; for(i = 0; i<256; i++) { if(!isprint(i)) continue; printf("%d<%c> ",i ,i); if(!(++count % 10 )) printf("\n"); } printf("\n"); return 0; } | cs |
isprint() 함수는 화면에 출력 가능한 문자면 0이 아닌 값을 리턴하고, 화면에 출력 불가능한 문자일 경우에는 0을 리턴한다.
exit() 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* #include <stdlib.h> void exit(int status); */ #include <stdio.h> #include <stdlib.h> int main() { int i = 0; while(1) { i++; printf("value : %d\n" , i); if(i == 5) { exit(0); } } return 0; } | cs |
exit() 함수는 호출하는 즉시 프로그램을 종료하는 함수이다 exit() 함수를 호출할 때 전달하는 status 값은 main() 함수가 운영체제(윈도우 명령 프롬프트 또는 리눅스 쉘)에 return 하는 값이다.
'c언어' 카테고리의 다른 글
스레드(Thread) 대충 (0) | 2016.08.11 |
---|---|
동적 메모리 할당과 해제 (0) | 2016.08.10 |
구조체 배열 포인터 중얼-1 (0) | 2016.08.10 |
void형 포인터 (0) | 2016.08.09 |
구조체 (0) | 2016.08.08 |