U E D R , A S I H C RSS

Our Major Lang IsCAndCPlus Plus/stdlib.h

stdlib.h

담당 : 도현
stdlib.h - Standard library 정의

매크로 (Macros)

매크로 명 설명
NULL 널 포인터 상수 값
EXIT_FAILURE 실패한 종료 (0이 아닌 값을 가짐)
EXIT_SUCCESS 성공한 종료 (0인 값을 가짐)
RAND_MAX 랜덤 함수에 의해서 리턴되는 최대 값 (적어도 32, 767)
MB_CUR_MAX 현재 사용 중인 로케일에서 멀티바이트 문자의 최대 길이

변수 (Variables)

변수 명 설명
typedef size_t sizeof 키워드의 unsigned 정수형 결과
typedef wchar_t 확장 문자 상수 크기의 정수 타입
struct div_t div() 함수에 의해 리턴되는 구조체형
struct ldiv_t idiv() 함수에 의해 리턴되는 구조체형

함수 (Functions) - String Functions

함수 명 설명
double atof(const char *str); 문자열을 실수(double precision)로 변환
int atoi(const char *str); 문자열을 정수(integer)로 변환
double strtod(const char *str, char **endptr); 문자열을 실수(double precision)로 변환
long int strtol(const char *str, char **endptr, int base); 문자열을 정수(long integer)로 변환
unsigned long int strtoul(const char *str, char **endptr, int base); 문자열을 정수(unsigned long)로 변환

함수 (Functions) - Memory Functions

함수 명 설명
void *calloc(size_t nitems, size_t size); 요청된 메모리 할당 (모든 원소를 0으로 초기화 한다)
void free(void *ptr); calloc(), malloc(), realloc()에 의해 할당된 메모리 해제
void *malloc(size_t size); 요청된 메모리 할당 (원소를 초기화 하지 않는다)
void *realloc(void *ptr, size_t size); calloc(), malloc()에 의해 할당된 메모리 크기를 재조정한다

== 함수 (Functions) - Environment Functions ==
함수 명 설명
void abort(void); 비정상적인 프로그램 종료를 발생시킨다
int atexit(void (*func)(void)); 프로그램이 정상적으로 종료될 때 전달인자로 넘겨진 함수포인터를 이용해서 특정 함수 실행
void exit(int status); 정상적인 프로그램 종료를 발생시킨다
char *getenv(const char *name); 환경 변수를 얻는다
int system(const char *string); 전달인자로 받은 명령 실행

함수 (Functions) - Searching and Sorting Functions

함수 명 설명
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)); 이진검색 수행
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)); 퀵 소트 수행

함수 (Functions) - Math Functions

함수 명 설명
int abs(int x); 정수형 절대값 리턴
div_t div(int numer, int denom); 전달인자의 numer를 denom으로 나눈 값과 나머지를 구조체형식으로 리턴
long int labs(long int x); long형 정수의 절대값을 리턴
ldiv_t ldiv(long int numer, long int denom); div()와 동일하고 변수 타입만 long int
int rand(void); 0부터 RAND_MAX까지의 범위사이의 난수 리턴
void srand(unsigned int seed); rand()에 의해 사용되는 난수 생성기에 인자 공급

함수 (Functions) - Multibyte Functions

함수 명 설명
int mblen(const char *str, size_t n); 다중 바이트 문자의 길이 리턴
size_t mbstowcs(schar_t *pwcs, const char *str, size_t n); 다중 바이트 문자 스트링을 wide 문자 스트링으로 변환
int mbtowc(whcar_t *pwc, const char *str, size_t n); 다중 바이트 문자를 wide 문자로 변환
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n); wide 문자 스트링을 다중 바이트 스트링으로 변환
int wctomb(char *str, wchar_t wchar); wide 문자를 다중 바이트 문자로 변환

stdlib.h 의 함수 예제

strtod(), strtol(), strtoul() 예제코드

~cpp
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char   *string, *stopstring;
   double x;
   long   l;
   int    base;
   unsigned long ul;
   string = "3.1415926This stopped it";
   x = strtod( string, &stopstring );
   printf( "string = %s\n", string );
   printf("   strtod = %f\n", x );
   printf("   Stopped scan at: %s\n\n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s\n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)\n", ul, base );
      printf( "   Stopped scan at: %s\n", stopstring );
   }
}

strtod(), strtol(), strtoul() 실행 결과

string = 3.1415926This stopped it
strtod = 3.141593
Stopped scan at: This stopped it

string = -10110134932This stopped it strtol = -2147483647 Stopped scan at: This stopped itstring = 10110134932
strtol = 45 (base 2)
Stopped scan at: 34932
strtol = 4423 (base 4)
Stopped scan at: 4932
strtol = 2134108 (base 8)
Stopped scan at: 932

malloc() 예제코드

~cpp
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>

int main( void )
{
   char *string;

   /* Allocate space for a path name */
   string = malloc( _MAX_PATH );

   // In a C++ file, explicitly cast malloc's return.  For example, 
   // string = (char *)malloc( _MAX_PATH );

   if( string == NULL )
      printf( "Insufficient memory available\n" );
   else
   {
      printf( "Memory space allocated for path name\n" );
      free( string );
      printf( "Memory freed\n" );
   }
}

malloc() 실행 결과

Memory space allocated for path name
Memory freed

qsort(), bsearch() 예제코드

~cpp
#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

int main(void) 
{ 
	int i;
	char string_array[10][50]={"John", "Jane",  "Mary", "Rogery", "Dave",  
		"Paul", "Beavis", "Astro",  "George", "Elroy"}; 
	
	/* 정렬 전 */
	for (i = 0; i < 10; i++)
		printf("%s, ", string_array[i]);
	printf("\n");

	/* Sort the list */ 
	qsort(string_array, 10, 50, strcmp);
	
	/* 정렬 후 */
	for (i = 0; i < 10; i++)
		printf("%s, ", string_array[i]);
	printf("\n");

	/* Search for the item "Elroy" and print it */ 
	printf("%s\n",bsearch("Elroy", string_array, 10, 50, strcmp)); 
	
	return 0; 
} 

qsort(), bsearch() 실행 결과

John, Jane, Mary, Rogery, Dave, Paul, Beavis, Astro, George, Elroy,
Astro, Beavis, Dave, Elroy, George, Jane, John, Mary, Paul, Rogery,
Elroy

----
OurMajorLangIsCAndCPlusPlus
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:57
Processing time 0.0384 sec