블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

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
  • total
  • today
  • yesterday

Category


syntax
auto

break
Break is used within loops and switch statements to jump to the end of the code block. It causes the "//code..." above to be skipped and terminates the loop. In switch case statements, break causes the remaining cases to be skipped--it prevents "falling through" to other cases.
호~ 나도 이거 필요한 적 많았는데...

continue

to jump to the top of the loop block
그럼 왜 continue라고 불러??

for


goto
You cannot, for instance, jump into the middle of a function from a different function by using goto.

if



switch
The prevent falling through, include break;at the end of any code block.
신기하다!





#include
Generally, it is necessary to tell the preprocessor where to look for header files if they are not placed in the current directory or a standard system directory.

include file (짜넣기 파일)
: 복수의 프로그램이 공통으로 사용하는 정수나 영역 등에 대해서 그 속성을 뜻매김한 정보를 저장하고 있는 파일.


cout
standard output stream

standard output
The place where output goes unless you specify a different output device. The standard output device is usually the display screen.

stream
a succession of data elements made available over time

standard stream
The three I/O connections are called standard input, standard output and standard error.

사용자 삽입 이미지


console
: where text messages are shown.
: The combination of display monitor and keyboard (or other device that allows input). -> link


사용자 삽입 이미지



ifstream
Input file stream class
The file to be associated with the stream can be specified either as a parameter in the constructor or by calling member open.



File I/O

file I/O in C++
C++ has two basic classes to handle files, ifstream and ofstream. To use them, include the header file fstream. Ifstream handles file input (reading from files), and ofstream handles file output (writing to files).

file I/O in C
For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. (You can think of it as the memory address of the file or the location of the file).




질문>> main() 함수에 대하여

main() 함수란?
서 교수님:
main() 함수는 시스템 (오퍼레이팅 시스템: OS) 이 호출하는 함수이기 때문에 미리 리턴 타입을 정해 둔 것입니다. 또 이 함수의 첫번째 문장부터 수행되도록 미리 정해져 있습니다. 그래서, main() 이 없는 C 프로그램은 컴파일 에러가 나고 main() 여러 개가 있어도 에러가 나옵니다.
그리고, 좀 더 복잡한 프로그램에서는 외부 실행 파일을 직접 호출하여 사용하기도 합니다. 그런한 경우 main() 의 리턴값에 따라 수행결과를 판단할 수 있어요.

최근의 컴파일러 출력을 보면 C/C++ 의 main() 함수는 int 를 리턴하는 것으로 규정되어 있습니다. 그래서
    int main ()
이라고 선언을 하죠. 그래서, 이 함수가 끝날 때에는 항상 return return_val; 가 들어가야 합니다.  return_val은 에러가 없을 경우 통상적으로 0 을 주면 됩니다.

main() 함수 안에서 exit(0); 과 return 0; 는 같은 역할을 합니다.

컴파일할 때 아래와 같이 하면 int main() 으로 해야 된다고 경고가 나올 것입니다.
g++ V2008122-01.cpp -o V2008122-01 -Wall


main() 함수를 선언할 때 리턴 타입을 정해 주지 않으면 다음과 같은 오류 메시지가 뜬다.
/Users/lym/cintro/week1/V2008122-01.cpp:16: warning: ISO C++ forbids declaration of 'main' with no type



질문>> "int argc, char *argv[]"에 대하여

int main(int argc, char *argv[])
에서 main 다음 괄호 안의 것들은 command line argument라고 부른다.

cf. 교재 201쪽
The parameter argc is the number of arguments on the command line including the program name. The array argv contains the actual arguments.

argument
: a value that is passed to a routine
- In programming, a value that you pass to a routine. For example, if SQRT is a routine that returns the square root of a value, then SQRT(25) would return the value 5. The value 25 is the argument.
Argument
is often used synonymously with parameter, although parameter can also mean any value that can be changed. In addition, some programming languages make a distinction between arguments, which are passed in only one direction, and parameters, which can be passed back and forth, but this distinction is by no means universal.
An argument can also be an option to a command, in which case it is often called a command-line argument.

parameter
: a variable which takes on the meaning of a corresponding argument passed in a call to a subroutine.

command line argument
: argument sent to a program being called

command line


a. Xcode에서 실행시키면 다음과 같이 나오고

argc=1
argv[0]=/Users/lym/cintro/week1/build/Debug/week1
argv[1]=

b. 터미널에서 실행시키면 다음과 같이 나오고

argc=1
argv[0]=./V2008122-01
argv[1]=metelsmac:~/cintro/week1 lym$ ./V2008122-01 input.txt

c. 터미널에서 "./V2008122-01 input.txt"라고 치면 다음과 같이 나옵니다.

argc=2
argv[0]=./V2008122-01
argv[1]=input.txt
서 교수님:
main(argc, argv[])에서
argv[0] = 실행프로그램 이름; 1번째는 항상 실행프로그램의 패스/이름 이 들어갑니다.

매개변수를 터미널에서 입력하는 것은 사람이 입력하지만, 사실은 shell 프로그램이 fileio 함수를 호출할 때 매개변수로 주는 것이고, 좀 더 엄밀하게는 OS가 main 함수를 호출할 때 주는 것입니다.

나는 argv[0]라는 게 굉장히 tricky하게 느껴진다. 그럼 자기 자신을 매개변수로 갖는다는 말 아닌가? 완전... '내 이름을 불러 주오~'잖아.

아~ 교수님께서 말씀하셨던 fileio.exe가 내 경우 week1.exe이구나... 내가 새 프로젝트(week1)를 만들어서 다시 작성했기 때문에. Xcode에서는 프로젝트명으로 실행파일이 만들어지고 그것은 자동으로 Debug 폴더에 자리를 잡는다. 그러므로 다음과 같이 들어가서 치면 된다. 헤매인 끝에, 나는 week1폴더에 .cpp파일을 만들어 컴파일했으므로 V2008122-01.exe가 만들어졌던 것을 썼던 것인데. Xcode와 쉘에서의 접근 경로가 이번에도 다르다.
999:~/cintro/week1/build/Debug lym$ ./week1 input.txt
Their sum is 1969.
The maximum number is 999.
The minimum number is 1.
argc=2
argv[0]=./week1
argv[1]=input.txt





질문>> "using namespace std;"에 대하여

서 교수님:
한 개의 프로그램에서 여러 개의 동일한 이름을 가지는 함수를 사용하게 될  때namespace 라는 것을 사용하여 구분하기 쉽도록 한 것입니다.  C++ 의 한 가지 기능입니다.
그 줄 (using namespace std;) 은
#include <fstream>
#include <iostream>
이 있을 때 포함시키면 편리합니다.


(1) using namespace std; 을 사용하고 싶지 않은 경우
(2) 다른 헤더파일에서도 ifstream, cout, endl 등의 함수를 정의하고 있어서 그 함수들을 사용할 때 구별이 필요한 경우 다음과 같이 하면 됩니다.

ifstream -> std::ifstream
cout -> std::cout
endl -> std::endl



'@GSMC > 서용덕: DMD Programming' 카테고리의 다른 글

week 2 review  (0) 2008.03.15
[Steve Oualline] 4. Basic Declarations and Expressions  (0) 2008.03.10
hw1  (0) 2008.03.10
Key concepts in programming: statement & expression  (1) 2008.03.09
vi  (0) 2008.03.07
posted by maetel