오늘은 함수포인터를 사용해서 C 의 출력문을 비슷하게 구현 해보았다.
먼저 main.h 에 이렇게 소스코드를 작성한다.
#include <stdarg.h>
#ifndef _MAIN_H
#define _MAIN_H
#define null NULL
typedef enum{false, true} boolean;
typedef char *String;
void println(const String format, ...) {
va_list ap;
char buf[4096];
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
fprintf(stdout, "%s\n", buf);
}
#pragma pack(push, 1)
typedef struct _System{
struct _OUT_{
void (*println)(const String, ...);
}out;
}Sys;
#pragma pack(pop)
Sys System = {println};
#endif
메인문을 아래와 같이 작성한다.
#include <stdio.h>
#include "main.h"
int main(void){
int a = 2;
double b = 0.12;
String str = "Hello World!";
System.out.println("Hello World!");
System.out.println("%d %lf", a, b);
System.out.println(str);
return 0;
}
출력
더보기
Hello World!
2 0.12
Hello World!
이렇게 println 을 구현 할 수있다.
'C' 카테고리의 다른 글
C) C언어로 자바 입출력 따라하기! (4) | 2022.09.18 |
---|---|
C) 여러 C 파일 한번에 컴파일 하는법! (0) | 2022.09.14 |
C) 2022 지방 기능경기대회 공업전자기기 3과제 풀이 (4) | 2022.08.22 |
C언어로 클래스 구현해보기 (1) | 2022.06.23 |
C) 1. Hello World! 출력하기 (0) | 2022.02.21 |