Exercises on C Fundamentals: Difference between revisions

From Innovation
Jump to: navigation, search
Created page with "<!-- <source lang="c" line start="1" highlight="1-8"> --> == Chapter 2 & 3: Fundamentals and Printf & Scanf == some are adapted from [Functions - Learn C - Free Interactiv..."
 
(No difference)

Latest revision as of 13:31, 25 August 2017



Chapter 2 & 3: Fundamentals and Printf & Scanf

some are adapted from [Functions - Learn C - Free Interactive C Tutorial](http://www.learn-c.org/en/Functions)

1. 다음의 코드를 hello.c라는 이름으로 저장하고 gcc -o hello hello.c로 컴파일 해보자.

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) { printf("Hello, World!"); } </source>

오류 메시지가 출력되는가? gcc -Wall -o hello hello.c로 다시 컴파일 해보자. 오류 메시지가 출력되는가? 오류 메시지를 읽어보고 무슨 뜻인지 생각해보자. 어떻게 하면 오류를 해결 할 수 있는가?

2. Hello World 프로그램을 변형하여 I am the master of the C Language를 출력하도록 변형해보자.

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) {

 printf("Hello, World!\n");
 return 0;

} </source>

3. printf를 사용하여 다음과 같은 모양을 출력하도록 프로그램을 작성해보자.

<source lang="c">
       *
      *
     *
  • *
*  *
  *

</source>

4. 다음 세 문장의 차이를 생각해보고 검증해보라

<source lang="c" line start="1">

printf("To C, or not to C: that is the question.\n"); </source>

<source lang="c" line start="1">

printf("To C, or not to C: “); printf(“that is the question.\n"); </source>

<source lang="c" line start="1">

printf("Brevity is the soul of wit.\n --Shakespeare\n"); </source>


5. printf를 사용하여 변수 a, b, c의 합을 구하라.

<source lang="c" line start="1">
  1. include <stdio.h>

int main() {

 int a = 3;
 float b = 4.5;
 double c = 5.25;
 float sum;
 /* Your code goes here */
 printf("The sum of a, b, and c is %f.", sum);
 return 0;

} </source>

6. 다음 코드에서 weight를 계산할 때 165를 더해서 올림을 하고 있다. 165의 유무에 따라 결과가 어떻게 변하는지 확인해보자. 올림이나 내림을 하지 않고 소수점 자리까지 표기하도록 코드를 변경해보자.

<source lang="c" line start="1">

/* dweight.c (Chapter 2, page 20) */ /* Computes the dimensional weight of a 12" x 10" x 8" box */

  1. include <stdio.h>

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;

} </source>

7. 변수를 초기화하지 않고 사용할 때 생기는 문제를 다음의 코드를 사용하여 확인해보자. 초기화를 하지 않고 덧셈, 뺄쎔 등의 연산을 하여 그 결과를 예측할 수 있겠는가? 옆의 친구의 결과와 비교해보자.

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) { int a; float c; printf("%d\n", a); printf("%f\n", c); return 0; } </source>

8. 다음은 dweight2.c의 코드이다. scanf의 용법과 받아들이는 인자의 형태를 유심히 살펴보자. 실수를 입력 받을 수 있는지 검사해보자. 실수를 입력받도록 수정해보자.

<source lang="c" line start="1">

/* dweight2.c (Chapter 2, page 23) */ /* Computes the dimensional weight of a

  box from input provided by the user */
  1. include <stdio.h>

int main(void) {

 int height, length, width, volume, weight;
 printf("Enter height of box: ");
 scanf("%d", &height);
 printf("Enter length of box: ");
 scanf("%d", &length);
 printf("Enter width of box: ");
 scanf("%d", &width);
 volume = height * length * width;
 weight = (volume + 165) / 166;
 printf("Volume (cubic inches): %d\n", volume);
 printf("Dimensional weight (pounds): %d\n", weight);
 return 0;

} </source>

9. 다음은 celcius.c의 코드이다. 코드를 수정하여 화씨가 출력되도록 해보자.

<source lang="c" line start="1">

/* celsius.c (Chapter 2, page 24) */ /* Converts a Fahrenheit temperature to Celsius */

  1. include <stdio.h>
  1. define FREEZING_PT 32.0f
  2. define SCALE_FACTOR (5.0f / 9.0f)

int main(void) {

 float fahrenheit, celsius;
 printf("Enter Fahrenheit temperature: ");
 scanf("%f", &fahrenheit);
 celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
 printf("Celsius equivalent: %.1f\n", celsius);
 return 0;

} </source>

10. C언어에서 사용할 수 없는 변수명의 목록이 있다. 이 목록의 키워드들을 변수명으로 사용하면 어떤 에러를 발생하는지 예를 만들어 보자.

11. 책에서는 문자열 사이에 줄 바꿈 기호를 삽입하는 것은 안된다고 나와 있다. 다음의 코드가 컴파일 되는지 검사해보자. 에러 메시지를 살펴보자.

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) { printf("this is a

               test of long

line\n"); return 0; } </source>

12. 다음 코드의 결과를 확인해보자. xy변수의 f의 포함 여부에 따라 결과가 어떻게 바뀌는지 검사해보자.

<source lang="c" line start="1">

int i, j; float x, y;

i = 10; j = 20; x = 43.2892f; y = 5527.0f; printf("i = %d, j = %d, x = $f, y = %f\n", i, j, x, y); </source>


13. conversion specification 은 전달받은 변수의 수와 동일해야 한다는 제한 조건이 있다. 더 많거나 더 적은 경우 어떤 오류 메시지를 출력하는지 위의 예제를 변형하여 검사해보자.

14. conversion specification 이 전달받은 변수의 형과 다른 경우 어떤 결과를 출력하는지 검사해보자.

15. 다음은 tprintf.c의 코드이다. 자리수와 길이의 표현이 익숙해질 때까지 조정해보자.

<source lang="c" line start="1">

/* tprintf.c (Chapter 3, page 40) */ /* Prints int and float values in various formats */

  1. include <stdio.h>

int main(void) {

 int i;
 float x;
 i = 40;
 x = 839.21f;
 printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i);
 printf("|%10.3f|%10.3e|%-10g|\n", x, x, x);
 return 0;

} </source>

16. 다음의 코드의 결과를 예상해보자. 실행해서 예상과 같은지 확인해보자.

<source lang="c" line start="1">

printf("\"hello\b\b\bl\tl\to!\"\n"); printf("Item\tUnit\tPurchase\n\tPrice\tDate\n"); </source>

17. 다음의 코드를 사용하여 scanf에 공백문자가 포함되면 그 문자를 어떻게 인식하는지 검사해보자.

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) { int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y); printf("i = %d\tj = %d\tx = %f\ty = %f); return 0; } </source>

18. 다음과 같이 코드를 작성하여 입력을 10 11이라고 전달하면 각 변수에 어떤 값이 저장이 되는가? 10,11이라고 전달하면 어떤 값을 출력하는가?

<source lang="c" line start="1">
  1. include <stdio.h>

int main(void) { int i, j; scanf("%d, %d", &i, &j); printf("i = %d\tj = %d", i, j); return 0; } </source>

19. 다음은 addfrac.c의 코드이다. 실행해보고 코드를 한 줄씩 이해해보자.

<source lang="c" line start="1">

/* addfrac.c (Chapter 3, page 46) */ /* Adds two fractions */

  1. include <stdio.h>

int main(void) {

 int num1, denom1, num2, denom2, result_num, result_denom;
 printf("Enter first fraction: ");
 scanf("%d/%d", &num1, &denom1);
 printf("Enter second fraction: ");
 scanf("%d/%d", &num2, &denom2);
 result_num = num1 * denom2 + num2 * denom1;
 result_denom = denom1 * denom2;
 printf("The sum is %d/%d\n", result_num, result_denom);
 return 0;

} </source>