Programming/C

Linux C 프로그래밍 기초 ① / Linux C Programming basics ①

옥뀨 2020. 7. 15. 14:20

본 프로그래밍은 리눅스 터미널 내에서 vi로 작성되고 실행된다. 

This programming is written and executed in vi in a Linux terminal.

 

C 프로그래밍 첫 기초를 예제로 시작하려 한다.

Let's start with the basics of C programming as an example.

 

예제를 살펴보기 전에 먼저 디렉토리와 파일을 먼저 정리해보도록 하겠다.

Before looking at the example, let's first organize the directories and files.

 

나중에 복습을 할 때 디렉토리를 정리해놔야 찾기 쉽다.

It's a good idea to organize our directories for future review.

 

먼저 "pwd" 명령어를 입력해보자.

First, enter the "pwd" command.

 

"pwd" 명령어는 "Print Working Directory" 의 약자로 현재 작업하고 있는 디렉토리를 출력해준다.

The "pwd" command stands for "Print Working Directory" and shows the current working directory.

 

현재 작업하고 있는 디렉토리는 "/root" 디렉토리이다.

The current working directory is the "/root" directory.

 

# pwd

 

pwd 명령어 / pwd command

 

리눅스 카테고리에서 명령어를 다룰 예정이나 프로그래밍을 먼저 올리는 관계로 간단한 명령어를 짚고

넘어가려 한다.

In the Linux category, I will be dealing with Linux commands, but since I'm going to post programming first,

I'd like to briefly introduce simple Linux commands.

 

"mkdir" 은 디렉토리는 만드는 명령어로 형식은 "mkdir [디렉토리 명]" 이다.

"mkdir" is a command to create a directory. The format is "mkdir [directory name]".

 

디렉토리를 만들어주고 cd 명령어를 입력하여 디렉토리를 이동해주자.

Create a directory and enter the cd command to move the directory.

 

"cd" 명령어는 "Change Directory" 의 약자로 디렉토리 이동을 뜻한다.

The "cd" command stands for "Change Directory", which means moving a directory.

 

① programming 디렉토리를 만들고 programming 디렉토리로 이동하자.

① Create a programming directory and move to the programming directory.

 

② c 디렉토리를 만들고 c 디렉토리로 이동한다.

② Create c directory and move to c directory.

 

③ 한 줄에 다수의 명령어 입력이 가능하다. ";" 는 현재 명령어를 끝낸다는 의미로 뒤에 명령어 입력을

    받을 수 있다.

    형식 : [명령어]; [명령어]; [명령어]; ...

③ Multiple commands can be entered in one line. ";" handles the preceding command

    with the separator operator, followed by the command after ";".

    Format: [command]; [command]; [command]; ...

 

④ vi 명령어로 c 소스 파일을 만들어보자.

④ Let's make a c source file with vi command.

 

# mkdir programming

# cd programming

# mkdir c

# cd c

# mkdir test; cd test

# vi test1.c

 

디렉토리 만들기 및 이동 / Create and move directories

 

소스 파일을 만들어보겠다. "i" 를 눌러 입력 모드로 들어간 후 아래 소스 코드를 입력하자.

Let's create a source file. Press "i" to enter input mode and enter the source code below.

 

----------test1.c----------

 

#include <stdio.h>

 

int main()

{

    printf("Hello c");

    return 0;

}

 

----------test1.c----------

 

 

c 언어 기초 문법 / c language basic grammar

 

가장 간단한 기초 문법이다.

It is the simplest basic grammar.

 

① 프로그래밍의 가장 첫 번째 들어가는 문장이다. <stdio.h> 헤더 파일을 포함시키겠다는 의미다.

    <stdio.h> 헤더파일은 기본 입출력과 관련된 내용이 저장된 라이브러리 파일이다.

    소스 코드에 들어올 함수에 맞춰 라이브러리 파일을 추가해야 하지만 이번 포스팅에서 다룰

    함수는 printf 밖에 없기에 stdio.h 헤더 파일만 포함시키면 된다.

    stdio.h 헤더 파일에 "printf" 가 정의되어 있기 때문에 함수 호출을 통해 ③번 명령어의 "Hello C"

    출력이 가능한 것이다.

    문장의 끝에 ";" 를 붙이지 않는다.

 

① It is the first sentence in programming. It means to include the <stdio.h> header file.

    The <stdio.h> header file is a library file that stores contents related to basic input/output.

    We need to add the library file according to the function to be entered in the source code,

    but we only need to include the stdio.h header file because printf is the only function

    to be covered in this posting.

    Because "printf" is defined in the stdio.h header file,

    it is possible to output "Hello C" of command ③ through function call.

    At the end of the sentence Do not stick ";"

 

 

② 다음으로 오는 문장은 main 함수이다.

    main 함수는 모든 프로그래밍의 시작점이라고 생각하면 된다.

    컴파일을 통해 여러 목적 파일이 만들어져도 그 중 하나만이 main 함수를 가질 수 있다.

    main 함수 앞에 int 자료형이 왔는데 설명하자면 int 형(integer 형) 즉 정수 형으로 자료를 선언하고

    ④ 문장에서 0이라는 정수를 리턴 받는 것이다.

    현재 main 함수에는 무조건 int 형을 받도록 표준안이 변경되었다. (void 형 사용x)

    또한 문장의 끝에 ";" 를 붙이지 않는다.

 

② The next sentence is the main function.

    Think of the main function as the starting point for all programming.

    Even if multiple object files are created through compilation, only one of them can have a main function.

    The int data type comes before the main function. To explain, the data is declared as an int type (integer type),

    or an integer type, and ④ It returns the integer 0 in the sentence.

    Currently, the standard has been changed to use the int type unconditionally in the main function.

    (Use void type x)

 

 

③ printf 함수로 "Hello C" 라는 문구를 출력한다.

    "\n" 은 행 이동을 한다는 뜻으로 Enter를 치는 것과 같은 의미이다.

③ Print the phrase "Hello C" with the printf function.

    "\n" means to move the line, which is the same as hitting Enter.

 

④ ②번에서 설명했듯이 main 앞에 int(정수) 형 자료가 선언되었다.

    그러므로 리턴 값이 꼭 들어와야 한다.

    return 0 를 썼다는 것은 성공적으로 끝냈다는 것을 의미한다.

    0 대신에 1, -1, -2 등 다양한 숫자들이 들어올 수 있으나 다른 값들은 에러가 있거나 다른 동작을 했다는

    의미이므로 성공적으로 마친다는 의미로 0을 리턴하는 것이 맞다.

 

 As explained in ②, int (integer) type data was declared before main.

    Therefore, the return value must be entered.

    Writing return 0 means that it ended successfully.

    Instead of 0, various numbers such as 1, -1, and -2 can come in,

    but it is correct to return 0 as it means that other values have errors or have different actions.

 

F2키를 눌러 컴파일이 잘 되는지 확인해보자.

Let's check if it compiles well by pressing F2.

 

F2키를 컴파일 단축키로 만들어 놓았고 설명은 https://information-security-vlog.tistory.com/13 에 있다.

I made the F2 key a compile shortcut and the instructions are at information-security-vlog.tistory.com/13

 

 

컴파일 확인 / Check compilation

 

단축키 설정이 안되어 있다면 ESC 키를 누른 후 ":wq" 입력하고 빠져나와서 컴파일 해보자.

If the shortcut key is not set, press the ESC key, type ":wq" and exit to proceed with the compilation.

 

 

 

 

먼저 필자는 F2 키로 실행 파일이 만들어져 있으므로 지워보고 "gcc -o test1 test1.c"

명령어로 실행 파일을 만들겠다.

First, I created the executable file with the F2 key, so I will delete it and create the executable file 

with the command "gcc -o test1 test1.c".

 

실행 파일을 만드는 형식은 "gcc -o [실행 파일 명] [소스 파일 명]" 이다.

The format for creating an executable file is "gcc -o [name of executable file] [source file name]".

 

실행 파일이 만들어 졌다면 "./test1" 명령어로 실행시켜보자.

If the executable file is created, run it with the command "./test1".

 

추후에 포스팅하겠지만 "./"의 의미는 현재 디렉토리라는 의미이고 "./test" 는 현재 디렉토리에서

test 파일을 실행시킨다는 의미로 알고 있자.

As we will post later, let's know that "./" means the current directory and

"./test" means to run the test file in the current directory.

 

# gcc -o test1 test1.c

# ./test1

 

터미널 내에서의 컴파일 및 실행 / Compile and run within the terminal

 

test1을 실행 시킨 결과 결과가 잘 나오는 것을 확인할 수 있다.

As a result of running test1, we can see that the result is well.

 

 

 

'Programming > C' 카테고리의 다른 글

C 언어란 / What is C language  (0) 2020.07.07