본문 바로가기

WIL(What I Learned)/KNK C Programming

Chapter 2. 연습문제

3. Condense the dweight.c program by (1) replacing the assignmetns 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>

int main(void)
{
    int height = 8 , length = 12 , width = 10, volume;

    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)에서는 변수를 선언함과 동시에 초기화시켜줘야한다.

(2)의 경우 weight 변수를 없애고 (volume + 165) / 166을 바로 출력해줘야 한다.

 

4. Write a program that declares several int and float variable - wihout initializing them - and then prints their values. Is there any pattern to the values? (Usally there isn't.)

예시코드

#include <stdio.h>

int main(void)
{
  int i, j, k;
  float x, y, z;

  printf("Value of i: %d\n", i);
  printf("Value of j: %d\n", j);
  printf("Value of k: %d\n", k);

  printf("Value of x: %g\n", x);
  printf("Value of y: %g\n", y);
  printf("Value of z: %g\n", z);

  return 0;
}

실행할 때마다 6개의 값에는 각각의 자료형에 맞게 무작위로 값이 출력된다. 다음은 출력 예시이다.

Value of i: 41525344
Value of j: 0
Value of k: 38
Value of x: 1.4013e-45
Value of y: 1.59682e-36
Value of z: 4.59051e-41

5. Which of the follwing are not legal C identifiers?

(a) 100_bottles

(b) _100_bottles

(c) one__hundred__bottles

(d) bottles_by_the_hundred_

-> (a)의 경우 10진수(digit)로 시작됐으므로 식별자로 쓰면 안된다. 

 

6. Why is it not a good idea for an identifier to contain more than one adjacent underscore (as in current__balance, for example)?

https://stackoverflow.com/questions/35243026/use-of-double-underscore-in-c

 

Use of double underscore in C

I was studying C programming a modern approach book. I found a question in it: Why is it not a good idea for an identifier to contain more than one adjacent underscore (As in current__balance for

stackoverflow.com

밑줄(_)이 하나일 때와 둘일 때를 명확히 구분하지 못하기 때문이다.

 

7. Which of the following are keywords in C?

(a) for

(b) If

(c) main

(d) printf

(e) while

https://learn.microsoft.com/en-us/cpp/c-language/c-keywords?view=msvc-170

 

C Keywords

Keywords in Standard C and Microsoft C compiler extensions.

learn.microsoft.com

 

8. How many tokens are there in the following statements?

answer= (3*q-p*p) /3;

https://www.javatpoint.com/tokens-in-c

 

Tokens in C - javatpoint

Tokens in C with Tutorial or what is c programming, C language with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.

www.javatpoint.com

-> answer, =, (, 3, *, q, -, p, *, p, ), /, 3, ; 이렇게 총 14개의 토큰이 있다.

 

 

9. Insert spaces between the tokens in Exercise 8 to make the statement easier to read.

-> answer = ( 3 * q - p * p) / 3;

 

10.(검증안됨) In the dweight.c program(Section 2.4), which spaces are essential?

-> int 와 return 다음의 공백이 필수다. (둘다 keyword임)

 

 

'WIL(What I Learned) > KNK C Programming' 카테고리의 다른 글

Chapter.2 Programming Projects  (0) 2022.12.01