1. 기본 출력 비교:
C언어:
#include <stdio.h>
int main() {
printf("Hello World\n"); // 줄바꿈 포함 출력
putchar('A'); // 단일 문자 출력
puts("Hello"); // 자동 줄바꿈 포함 출력
return 0;
}
Java:
public class Output {
public static void main(String[] args) {
System.out.println("Hello World"); // 줄바꿈 포함 출력
System.out.print("Hello"); // 줄바꿈 없는 출력
System.out.printf("Hello %s", "World"); // 형식화된 출력
}
}
2. 형식 지정자 비교:
C언어:
#include <stdio.h>
int main() {
int num = 42;
float pi = 3.14f;
char ch = 'A';
char str[] = "Hello";
printf("정수: %d\n", num); // 정수 출력
printf("실수: %f\n", pi); // 실수 출력
printf("문자: %c\n", ch); // 문자 출력
printf("문자열: %s\n", str); // 문자열 출력
printf("16진수: %x\n", num); // 16진수 출력
printf("8진수: %o\n", num); // 8진수 출력
return 0;
}
Java:
// Java 예제
public class FormatExample {
public static void main(String[] args) {
int num = 42;
double pi = 3.14;
char ch = 'A';
String str = "Hello";
System.out.printf("정수: %d%n", num); // 정수 출력
System.out.printf("실수: %f%n", pi); // 실수 출력
System.out.printf("문자: %c%n", ch); // 문자 출력
System.out.printf("문자열: %s%n", str); // 문자열 출력
System.out.printf("16진수: %x%n", num); // 16진수 출력
System.out.printf("8진수: %o%n", num); // 8진수 출력
// Java의 추가적인 기능
System.out.printf("천단위 구분: %,d%n", 1000000); // 1,000,000
System.out.printf("왼쪽 정렬: %-10s%n", str); // 왼쪽 정렬
System.out.printf("0채움: %05d%n", num); // 00042
}
}
3. 버퍼 관리:
C언어:
setbuf(stdout, NULL); // 버퍼링 비활성화
fflush(stdout); // 버퍼 비우기
setvbuf(stdout, NULL, _IONBF, 0); // 버퍼링 모드 설정
Java:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("Hello");
bw.flush(); // 버퍼 비우기
bw.close(); // 자원 해제
4. 에러 스트림
C언어:
fprintf(stdout, "정상 출력\n");
fprintf(stderr, "에러 출력\n");
Java:
System.out.println("정상 출력");
System.err.println("에러 출력"); // 빨간색으로 표시됨
5. 출력 리다이렉션
C언어:
freopen("output.txt", "w", stdout);
printf("파일로 출력됨\n");
Java:
PrintStream originalOut = System.out;
try {
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.out.println("파일로 출력됨");
} finally {
System.setOut(originalOut);
}
6. 성능 최적화
C언어:
// 버퍼 크기 최적화
char buffer[1024];
setvbuf(stdout, buffer, _IOFBF, sizeof(buffer));
Java:
// 문자열 연결
// 나쁜 예
for(int i = 0; i < 1000; i++) {
System.out.println(i); // 매번 I/O 발생
}
// 좋은 예
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 1000; i++) {
sb.append(i).append("\n");
}
System.out.print(sb.toString()); // 한 번의 I/O
7. 인코딩 처리
C언어:
#include <locale.h>
setlocale(LC_ALL, "ko_KR.UTF-8");
printf("한글\n");
Java:
OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8");
writer.write("한글");
writer.flush();
8. 스레드 안정성
C언어:
// C에서는 직접 구현 필요
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void thread_safe_print(const char* message) {
pthread_mutex_lock(&mutex);
printf("%s\n", message);
pthread_mutex_unlock(&mutex);
}
Java:
// Java는 기본적으로 System.out이 스레드 안전
class ThreadSafeOutput {
private static final Object lock = new Object();
public static void threadSafePrint(String message) {
synchronized(lock) {
System.out.println(message);
}
}
}
9. 자원 관리
C언어:
// C - 수동 관리
FILE* file = fopen("output.txt", "w");
if (file != NULL) {
fprintf(file, "수동으로 close 필요\n");
fclose(file);
}
Java:
// Java - try-with-resources
try (PrintWriter writer = new PrintWriter(new FileWriter("output.txt"))) {
writer.println("자동으로 close() 호출");
}
10. 디버깅 테크닉
C언어:
#ifdef DEBUG
fprintf(stderr, "Debug: x=%d, y=%d\n", x, y);
#endif
Java:
System.out.printf("Debug: x=%d, y=%d%n", x, y);
System.err.println("Error: " + errorMessage);