안녕하세요. 언제나 휴일입니다.
이번에는 1/100초 단위로 현재 시각을 콘솔 화면에 출력하는 시계 소스 코드입니다.
이를 통해 time.h 에 정의하고 있는 다양한 일시와 시간에 관한 형식과 함수 사용 방법을 알 수 있습니다.

소스 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* https://ehpub.co.kr 언제나 C언어 예제 Center 1/100초 단위의 시계 구현 */ #include <time.h> #include <stdio.h> #include <conio.h> #include <Windows.h> void print_time(struct tm* now, int tail) { COORD CursorPostion = { 0,1 }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPostion); printf("%2d시 %2d분 %2d초 %2d\n", now->tm_hour, now->tm_min, now->tm_sec, tail); } int main() { clock_t sclock, nclock; time_t seconds; struct tm now; int tail=0; printf("종료를 원하시면 아무키나 누르세요.\n"); sclock = clock(); time(&seconds); localtime_s(&now, &seconds); print_time(&now, tail); while (1) { if (_kbhit()) { break; } nclock = clock(); if (nclock - sclock >= (CLOCKS_PER_SEC / 100)) { tail++; if (tail == 100)//1초가 지나면 { tail = 0; sclock = clock(); time(&seconds); localtime_s(&now, &seconds); } print_time(&now, tail); } } printf("프로그램을 종료합니다."); return 0; } |
관련 게시글