Rylah's Study & Daily Life
[Linux C] 01. 시스템과 네트워크 프로그래밍 개요 본문
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <stdlib.h>
int main(void)
{
time_t* cur_time;
// 포인터변수만 선언하고 메모리를 참조하지 않고 사용해서 오류 발생
time(cur_time);
printf("Current Time = %d\n", (int) *cur_time);
return 0;
}
수정한 소스
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <stdlib.h>
int main(void)
{
time_t* cur_time =(time_t*)malloc(sizeof(time_t));
// 포인터변수만 선언하고 메모리를 참조하지 않고 사용해서 오류 발생
time(cur_time);
printf("Current Time = %d\n", (int) *cur_time);
free(cur_time);
return 0;
}
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <stdlib.h>
int main(void)
{
time_t* cur_time =(time_t*)malloc(sizeof(time_t));
// 포인터변수만 선언하고 메모리를 참조하지 않고 사용해서 오류 발생
time(cur_time);
printf("Current Time = %d\n", (int) *cur_time);
char* cur_string;
cur_string = ctime(cur_time);
printf("Current Time Str = %s\n", cur_string);
free(cur_time); // 동적 할당 해제
return 0;
}
man 2 time
SYNOPSIS | 시스템 콜 이용 시 소스코드에 반드시 포함해야 되는 헤더 파일 및 함수 원형 |
DESCRIPTION | 이 함수의 기능을 설명 |
RETURN VALUE | 시스템 콜 반환 값에 따라 성공, 실패 여부를 확인 가능 |
man 3 memset
SYNOPSIS | 포함해야되는 헤더 파일 및 함수 원형 |
DESCRIPTION | 함수의 기능 정의 |
RETURN VALUE | Return Value |
#include <stdio.h>
#include <math.h>
int main(void)
{
double dNum = 100.0;
printf("SQRT(%lf) = %lf\n", dNum, sqrt(dNum));
return 0;
}
MSVC에서라면 컴파일이 잘될거 같은 코드이다.
에러다
math.h는 표준 라이브러리가 아니기 때문이다.
이제서야 실행이 된다. 라이브러리 추가는 컴파일에 옵션으로 -lm을 줘야 한다.
#include <stdio.h>
void static_function(int num)
{
printf("this is static library function : %d\n", num);
}
#include <stdio.h>
void shared_function(int num1, int num2)
{
printf("this is shared library function : %d, %d\n", num1, num2);
}
#include <stdio.h>
#include "static.h"
#include "shared.h"
int main(void)
{
printf("main start\n");
static_function(100);
shared_function(200, 300);
printf("main end.\n");
return 0;
}
#include <stdio.h>
#include <sys/sysinfo.h>
int main(void)
{
struct sysinfo sinfo;
sysinfo(&sinfo);
printf("부팅 이후 시스템의 생존시간(sec) : %ld\n", sinfo.uptime);
printf("1분, 5분, 15분 이전의 시스템 평균 부하 : %ld, %ld, %ld\n",
sinfo.loads[0], sinfo.loads[1], sinfo.loads[2] );
printf("메모리의 총 크기/ 사용 가능한 메모리 = %ld/%ld\n",
sinfo.totalram, sinfo.freeram);
printf("스왑 공간의 총 크기 / 사용가능한 스왑 공간 = %ld/%ld\n",
sinfo.totalswap, sinfo.freeswap);
printf("현재 수행되고 있는 프로세스 수 : %hd\n", sinfo.procs);
return 0;
}
'Study > C' 카테고리의 다른 글
[Linux C] 02. 개발 도구 (0) | 2022.01.21 |
---|---|
Linux 02. 시스템 콜 및 라이브러리 사용 (0) | 2022.01.15 |
Linux 01. Hello World (0) | 2022.01.15 |
10. 파일 입출력 (0) | 2021.11.03 |
09. 구조체 (0) | 2021.11.03 |