본문 바로가기

<프로그래밍 언어>/[C 언어]5

[C 언어] strlen 함수 정의 strlen - calculate the length of a string. string 의 길이를 계산한다. 시놉시스 #include #include int strlen(string s) 리턴 value this function returns the number of characters in s, excluding the terminating NUL byte (i.e., '\0'). 예시 #include #include #include int main(void) { string s = get_string("Input: "); printf("Output: "); for (int i = 0, n = strlen(s); i < n; i++) { printf("%c", s[i]); } printf("\n.. 2022. 2. 28.
[C 언어] Syntactic Sugar 문법적 설탕 문법적 설탕? 설탕은 맛있고 접근성도 좋고 쉽다! 문법적 설탕도 동일한 문법적 기능도 더 직관적이고 읽기 쉽게 만드는 것이다. 다음 예시를 보자. #include #include int main(void) { int scores[3]; scores[0] = get_int("Score: "); scores[1] = get_int("Score: "); scores[2] = get_int("Score: "); printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0); 3개의 스코어의 평균을 내는 코딩이다. 문법적 문제는 없지만 똑같은 걸 3번 반복해야 한다는 번거로움이 있다. 하지만 문법적 설탕을 사용한다면? #include #include int.. 2022. 2. 28.
[C 언어] 탐욕 알고리즘 (Greedy algorithm) Coin change problem 탐욕 알고리즘은 눈 앞에 놓인 선택지 중에 현재 상황에서 가장 최선의 것을 선택하도록 디자인 된 알고리즘이다. 알고리즘은 일상에서 coin change problem, 즉 상점에서 잔돈 돌려주는 상황에 써먹을 수 있는데, 돌려줘야 될 잔돈의 수에 따라서 quarters 25, dimes 10, nickels 5, pennies 1 중 최선을 선택해야 한다. 여기서 최선의 선택이란 가장 높은 가치의 돈 (만약 >=25 이라면 25)을 우선으로 선택하고 남는 돈은 그 다음 (10->5->1)로 넘어가는 것이다. 이렇게 잔돈을 돌려주도록 코딩을 하기 위해서는 알고리즘처럼 직관적으로 생각하면 된다. 25 부터 1까지 순서대로 정렬한 후, 반복문을 돌면서 조건이 끝나는 때 남는.. 2022. 2. 27.
[C 언어] Datatype in C: int float double char bool long string 정수: byte, short, int, and long 실수: float and double 문자: char 논리: boolean Data Type Memory (bytes) Range Format Specifier short int 2 -32,768 to 32,767 %hd unsigned short int 2 0 to 65,535 %hu unsigned int 4 0 to 4,294,967,295 %u int 4 -2,147,483,648 to 2,147,483,647 %d long int 4 -2,147,483,648 to 2,147,483,647 %ld unsigned long int 4 0 to 4,294,967,295 %lu long long int 8 -(2^63) to (2^63)-1 %l.. 2022. 2. 27.