Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 주가예측
- 코딩테스트
- 코테
- 이웃한 칸
- 프로그래밍
- 백준
- Project
- 코딩독학
- 모의 주식
- 자바정리
- SNS
- 코딩
- 자바독학
- 개발자
- Reflex
- 자바
- 코스닥
- 개발
- 풀스택프레임워크
- 개인프로젝트
- 프로그래머스
- backend
- java정리
- C++
- 사물인터넷
- 프로그램
- frontend
- 빅데이터
- java
- Python
Archives
- Today
- Total
Creative Code
main.c(rand함수) 본문
※main.c 파일
#include <stdio.h>
#include <time.h> // user의 헤더파일을 가져올 때
#include "rand.h" // 같은 디렉토리 안에 있는 헤더파일을 가져올 때
int main(void)
{
my_srand(time(NULL));
for (int i = 1; i<=10; i++) {
int num = my_rand();
printf("%d\n",num);
}
return 0;
}
※rand.h파일
//#Pragma ONCE헤더파일을 한번만 include 한다는 뜻
#ifndef RAND_H
#define RAND_H
void my_srand(int); // function declaration 함수 선언
int my_rand(void);
#endif
※rand.c파일
static int seed; // 같은 .c파일 안에 있는 함수들만 접근가능
// -> my_stand함수와 my_rand함수만 접근이 가능하다.
// -> 허용된 연산을 통해서만 데이터에 접근하게 할 때 사용(직접적으로 접근x)
void my_srand(int s) // function definition
{
seed = s;
}
int my_rand(void)
{
seed = 1103515245 * seed +12345;
return ((unsigned)(seed/65536)%32768);
}
'C Programming' 카테고리의 다른 글
main.c(구조체) (0) | 2023.08.04 |
---|---|
main.c(serial함수) (0) | 2023.08.04 |
main.c(함수 선언) (0) | 2023.08.04 |
main.c (랜덤함수 라이브러리 생성) (0) | 2023.08.04 |
swap3.c(전역변수 사용) (0) | 2023.08.04 |