본문 바로가기
카테고리 없음

size_t

by 가능성1g 2023. 2. 1.
반응형

modern C에서 쓰이는 size_t

플랫폼에 따라 자동으로 크기가 바뀌는 양의 정수를 표현함!

크기나 원소 개수를 표현하는데 적합하다.

헤더는 stddef.h

 

이러한 비슷한 얘들이 있다.

ptrdiff_t : 크기차이를 표현하는데 적합

포인터의 크기차이를 이용하여, 배열의 크기 차이를 나타낼때 표현한다.

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    const size_t N = 100;
    int numbers[N];
 
    printf("PTRDIFF_MAX = %ld\n", PTRDIFF_MAX);
    int *p1=&numbers[18], *p2=&numbers[23];
    ptrdiff_t diff = p2-p1;
    printf("p2-p1 = %td\n", diff);
 
    return EXIT_SUCCESS;
}

실행 결과!

PTRDIFF_MAX = 9223372036854775807

p2-p1 = 5

Program ended with exit code: 0

 

clock_t : 프로세서의 시간을 나타냄 - time.h

특히, CLOCKS_PER_SEC 를 이용하여 초로 변환이 가능!

#include <stdio.h>
#include <time.h>
#include <math.h>
 
volatile double sink;
int main (void)
{
  clock_t start = clock();
 
  for(size_t i=0; i<3141592; ++i)
      sink+=sin(i);
 
  clock_t end = clock();
  double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
 
  printf("for loop took %f seconds to execute \n", cpu_time_used);
}

실행결과!

for loop took 0.143789 seconds to execute 

Program ended with exit code: 0

반응형