영어로 읽는 코딩

02 [C] 입력 최대 글자 출력하는 코드

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line*/
int main() {
    int len;				/*current line length*/
	int max;				/*maximum length seen so far*/
	char line[MAXLINE];		/*current input line*/
	char longest[MAXLINE];	/*longest line saved here*/

	max = 0;
	while ((len = getline(line, MAXLINE)) > 0) {
		if (len > max) {
			max = len;
			copy(longest, line);
		}
	}
	if (max > 0)
		printf("가장 긴 단어는 = %s 최대 길이는 = %d", longest, max-1);
	return 0;
}

/*getline: read a line into s, return length*/
int getline(char s[], int lim) {
	int c, i;

	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
		s[i] = c;
	}
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\0';
	return i;
}

/* copy: copy 'from' into 'to'; assume to is big enough*/
void copy(char to[], char from[]) {
	int i;

	i = 0;
	while ((to[i] = from[i]) != '\0')
		++i;
}

/*The C Programming Language (second edition, Prentice Hall) by Brian W. Kernighan and Dennis M. RitchieS*/
/* p.29 character arrays*/

[교재 28 - 30의 내용임]

* 인수로 배열의 이름을 넘겨줄 때는 cally by value가 아니라 주소가 넘어가서 원본을 변경 가능하다는 말임When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array - there is no copying of array elements. By subscripting this value, the function can access and alter any argument of the array.

 

* getline에서 EOF면 0 리턴해서 메인 함수 종결하는 내용.
At the minimum, getline has to return a signal about possible end of file; a more useful design would be to return the length of the line, or zero if end of file is encountered. Zero is an acceptable end-of-file return because it is never a valid line length. Every text line has at least one character; even a line containing only a newline has length 1.

 

* main앞에 선언 하는 이유 - 같은 화일

The functions getline and copy are declared at the beginning of the program, which we assume is contained in one file. 

 

* 배열의 사이즈 선언
main and getline communicate through a pair of arguments and a returned value. In getline, the arguments are declared by the line
    int getline(char s[ ], int lim) 
which specifies that the first argument, s, is an array, and the second, lim, is an integer. The purpose of supplying the size of an array in a declaration is to set aside storage. The length of the array s is not necessary in getline since its size is set in main.
 
* 리턴값 int정하는 경우와 void로 처리하는 경우
getline uses return to send a value back to the caller. This line also declares that getline returns an int; since int is the default return type, it could be omitted. Some functions return a useful value; others, like copy, are used only for their effect and return no value. The return type of copy is void, which states explicitly that no value is returned.
 

* 문자열의 경우 마지막에 \0 값 넣어줌

getline puts the character ’ \0 ’ (the null character, whose value is zero) at the end of the array it is creating, to mark the end of the string of characters. This convention is also used by the C language: when a string constant like

hello\n ”

appears in a C program, it is stored as an array of characters containing the characters of the string and terminated with a '\0’ to mark the end.

 

 

댓글

댓글 본문
작성자
비밀번호
버전 관리
Yoo Moon Il
현재 버전
선택 버전
graphittie 자세히 보기