기본 예제 프로그램 및 설명
Jump to navigation
Jump to search
첫 번째 Java 프로그램
public class FirstClass {
public static void main(String[] args) {
System.out.println("hello");
}
}
첫 번째 C 프로그램
#include <stdio.h>
int main()
{
fprintf(stdio, "hello\n");
}
- vscode 실행
mkdir projects cd projects mkdir helloworld cd helloworld code .
- Build
- 터미널 / 작업 실행 선택
- 실행 : 터미널에서 직접 helloworld.exe를 실행한다.
첫 번째 C++ 프로그램
#include <iostream>
using namespace std;
int main()
{
cout << "hello" << endl;
}
Unix에서 프로세스가 생성될 때 마다 열리는 파일들
- Unix에서 프로세스가 만들어지면 생성되는 세 개의 파일. 모든 프로세스는 세 개의 파일이 열려(open)있다.
- Standard input : stdin(C), cin(C++), System.in(Java)
- Standard output : stdout(C), cout(C++), System.out(Java)
- Standard error : stderr(C), cerr(C++), System.err(Java)
- Standard output 예제
#include <stdio.h> #include <fcntl.h> int main() { int fd; char buf[10]; strcpy(buf, "hello\n"); fd = open("stdoutput.c", O_RDONLY); printf("%d\n", fd); write(1, buf, sizeof(buf)); }
- 실행 결과
3 hello
- 사용자가 작성한 프로그램(user program)에서 첫 번째 open 하여 얻은 file descriptor가 0이 아니라 3이다. 왜냐하면 0, 1, 2는 이미 stdin, stdout, stderr로 사용중이기 때문이다.
write
함수는 파일에 데이터를 기록하는 함수이다. 이 함수는 첫 번째 인자로 대상 파일의 file descriptor 번호가 필요한데,write(1, ...)
에서 1은 stdout을 뜻한다. 그러므로 출력이 화면으로 나온다.