K. N. King C Programming - Chapter 2. Exercises, Programming Projects

[Week 1] Hacking Cat’s Weekly Study


Exercises

Section 2.1

    1. Create and run Kemighan and Ritchie’s famous “hello, world” program:

Section 2.2

    1. Consider the following program:
    • (a) directives → #include <stdio.h>
    • (b) statements → printf(...); , return 0;

Section 2.4

#include <stdio.h>
 
# dweight.c
int main(void)
{
	int height, length, width, volume, weight;
	height = 8; length = 12; width = 10;
	volume = height * length * width; weight = (volume + 165) / 166;
	printf("Dimensions: %dx%dx%d\n", length, width, height); 
	printf("Volume (cubic inches): %d\n", volume); 
	printf("Dimensional weight (pounds): %d\n", weight);
	return 0;
}

    1. Condense the dweight.c program by (1) replacing the assignments to height, length, and width with initializers and (2) removing the weight variable, instead calculating (volume + 165) / 166 within the last printf.
#include <stdio.h>
 
# dweight.c
int main(void)
{
	int height = 8;
	int length = 12;
	int width = 10;
	int volume = height * length * width;
	printf("Dimensions: %dx%dx%d\n", length, width, height); 
	printf("Volume (cubic inches): %d\n", volume); 
	printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166);
	return 0;
}

    1. Write a program that declares several int and float variables—without initializing them—and then prints their values. Is there any pattern to the values? (Usually there isn’t.)
➜  exercises cat 4.c
#include <stdio.h>
 
int main(void)
{
        int intNumber;
        float floatNumber;
 
        printf("%d\n", intNumber);
        printf("%f\n", floatNumber);
        return 0;
}


Section 2.7


    1. Which of the following are not legal C identifiers?
    • 변수명의 맨 앞에 숫자가 오는 것은 허용되지 않으므로, (a) 가 정답입니다.
➜  exercises gcc -o 5 5.c
5.c: In function ‘main’:
5.c:5:13: error: invalid suffix "_bottles" on integer constant
    5 |         int 100_bottles;
      |             ^~~~~~~~~~~
5.c:5:13: error: expected identifier or ‘(’ before numeric constant

    1. Why is it not a good idea for an identifier to contain more than one adjacent underscore (as in current_balance, for example)?
    • C 표준에서 이미 예약(선점)되어 사용하고 있기 때문에 사용해서는 안 됩니다. (컴파일/링크 시 오류 유발 가능)

    1. Which of the following are keywords in C?
    • for, while 키워드는 C 키워드에 속합니다.

Section 2.8


    1. How many tokens are there in the following statement?
    • 14개의 토큰으로 구성됩니다. (answer = ( 3 * q - p * p ) / 3 ;)

    1. Insert spaces between the tokens in Exercise 8 to make the statement easier to read.
    • answer = ( 3 * q - p * p ) / 3 ;

    1. In the dweight. c program (Section 2.4). which spaces are essential?
    • int main(void), int height, ..., weight;, return 0; 에서의 공백은 필수적으로 사용되어야 합니다.
# dweight.c
int main(void)
{
	int height, length, width, volume, weight;
	height = 8; length = 12; width = 10;
	volume = height * length * width; weight = (volume + 165) / 166;
	printf("Dimensions: %dx%dx%d\n", length, width, height); 
	printf("Volume (cubic inches): %d\n", volume); 
	printf("Dimensional weight (pounds): %d\n", weight);
	return 0;
}

Programming Projects

Programming Progject 1.


Programming Progject 2.

  • 4/3πr^3 값을 계산하면 아래와 같은 결과를 얻을 수 있습니다.
    • 이 때, 정수형(4/3), 실수형(4.0/3.0)의 계산 방식에 따라 결과가 다르게 계산됩니다.

Programming Progject 3.


Programming Progject 4.


Programming Progject 5.


Programming Progject 6.


Programming Progject 7.


Programming Progject 8.

🐛.. 🐛.. 🐛..