[C Primer Plus] #3 데이터와 C

문연수·2022년 3월 3일
0

C Primer Plus

목록 보기
3/5

1. 복습 문제

- 1. 다음과 같은 종류의 각 데이터에는 어떤 데이터형을 사용해야 하는가?

  • a. unsigned int
  • b. unsigned long
  • c. unsigned int
  • d. unsigned int

2. int 형 대신 long 형 변수를 사용하는 이유는 무엇인가?

int 의 한계(limit) 를 뛰어넘는 데이터 처리에 대한 확장성과 이식성을 확보하기 위해서.

3. 32 비트 부호있는 정수를 얻기 위해서는 어떤 이식 가능한 데이터형들을 사용할 수 있는가? 각각의 데이터형을 선택한 이유는 무엇인가?

int32_t: int32_t 자료형은 C 표준에서 반드시 32 비트임을 보장하기 때문이다.

4. 다음과 같은 각 상수들에 대해 데이터형과 의미가 무엇인지 말하라.

  • a. 문자 자료형, 백스페이스
  • b. int 자료형, 1066 이라는 십진수 정수
  • c. double 자료형, 99.44 라는 실수
  • d. int 자료형, 0xAA 라는 16진수 정수
  • e. double 자료형, E 표기법으로 표기된 실수

5.

#include <stdio.h>

int main(void)
{
	float g, h;
	float tax, rate;

	rate = 1.04;
	g = 1.0e21;
	tax = rate * g;

	printf("tax: %f\n", tax);

	return 0;
}

6.

  • a. 12 %d
  • b. 0x3 %d
  • c. 'C' %c
  • d. 2.34E07 %f
  • e. '\040' %c
  • f. 7.0 %f
  • g. 6L %ld
  • h. 6.0f %f
  • i. 0x5.b6p12 %a

7.

  • a. %o
  • b. %Lf
  • c. %c
  • d. %u
  • e. %c
  • f. %f
  • g. %x
  • h. %d

8.

#include <stdio.h>

int main(void)
{
	int imate = 2;
	long shot = 53456;
	char grade = 'A';
	float log = 2.71828;

	printf("%d 등에 당첨될 확률은 %ld 분의 1이다.\n", imate, shot);
	printf("%f 의 성적은 %c 학점이다.\n", log, grade);

	return 0;
}

9.

#include <stdio.h>

int main(void)
{
	char ch;

	ch = 13;
	ch = '\r';
	ch = '\013';
	ch = '\x0D';

	return 0;
}

10.

#include <stdio.h>

int main(void)
{
	int cows, legs, integer;

	printf("젖소들의 다리를 세어 보니 모두 몇 개더냐? \n");
	scanf("%d", &legs);

	cows = legs / 4;

	printf("그렇다면 젖소가 %d 마리로구나.\n", cows);

	return 0;
}

11.

  • a. 개행
  • b. 백슬래쉬
  • c. 큰 따옴표
  • d. 수평 탭

2. 프로그래밍 연습

1.

#include <stdio.h>
#include <stdint.h>

#include <inttypes.h>

int main(void)
{
	int32_t a, b, c;
	float flt;
	double dlb;

	a = 0xFFFFFFFF; b = 1;
	c = a + b;

	flt = 1.0E+50;
	dlb = 512.1266E-326;

	printf("%" PRId32 "\n", c);
	printf("%f\n", flt);
	printf("%.400f\n", dlb);

	return 0;
}

2.

#include <stdio.h>

int main(void)
{
	int chr;

	scanf("%d", &chr);
	printf("%c", chr);

	return 0;
}

3.

#include <stdio.h>

int main(void)
{
	printf("\a갑작스런 소리에 깜짝 놀라 샐리는 외쳤다,\n"
	       "\"호박대왕이다!\"\n");
           
	return 0;
}

4.

#include <stdio.h>

int main(void)
{
	double flt;

	scanf("%lf", &flt);

	printf("고정소수점 표기: %f\n", flt);
	printf("지수 표기: %e\n", flt);
	printf("p 표기: %a\n", flt);

	return 0;
}

5.

#include <stdio.h>

int main(void)
{
	int year;
	long double seconds;

	printf("Enter the year: "); scanf("%d", &year);

	seconds = year * (3.156e+7);
	printf("years to seconds: %Lg\n", seconds);

	return 0;
}

6.

#include <stdio.h>

int main(void)
{
	const double gram_for_O2 = 3.0e-23;
	double gram;
	int quart;

	printf("Enter the quart: "); scanf("%d", &quart);
	gram = quart * 950;

	printf("quater to molecule: %f\n", gram / gram_for_O2);

	return 0;
}

7.

#include <stdio.h>

int main(void)
{
	double inch, centi;

	printf("Enter the your height (centi): ");
	scanf("%lf", &centi);

	printf("centimeter to inch: %f\n", centi / 2.54);

	return 0;
}

8.

#include <stdio.h>

int main(void)
{
	float pints;
	int cups, ounces, tablespoons, teaspoons;

	printf("cups: "); scanf("%d", &cups);

	pints = cups / 2.0f;
	ounces = cups * 8;
	tablespoons = ounces * 2;
	teaspoons = tablespoons * 3;

	printf("pints: %f\n", pints);
	printf("cups: %d\n", cups);
	printf("ounces: %d\n", ounces);
	printf("tablespoons: %d\n", tablespoons);
	printf("teaspoons: %d\n", teaspoons);

	return 0;
}

pints 를 제외한 모든 단위들이 cups 의 배수이므로

profile
2000.11.30

0개의 댓글