프로그래밍/C언어

C언어콘서트 3장 Exercise/C언어콘서트 3장 연습문제 답

이번주에 2015. 3. 5. 11:50
반응형

1. 이번 장에서 학습한 내용을 다음과 같은 표로 정리하여 보자.

자료형

저장하는 값

printf()에서의 형식 지정자

int

정수값

%d

long

정수값

%d

float

실수값

%f

double

실수값

%lf

char

문자값

%c

unsigned int

부호없는 정수값

%d



2.다음과 같은 데이터를 표현하는 데 적합한 자료형과 변수 이름을 설정하여라.

데이터

자료형

변수 이름

월급(500만원 이하)

int

payment

아파트의 면적(100제곱미터 이하)

float

area

사람의 나의(100살 이하)

short

age

사람의 키(200센티미터 이하)

float

height

수학시험 점수(100점 이하)

short

score

빛의 속도(30만 킬로미터/)

double

light_speed

<!--[if !supportEmptyParas]--> <!--[endif]-->

3.이진수를 십진수로 변환하여라

이진수

십진수

00000001

128

00000010

64

00000011

192

00000100

32

<!--[if !supportEmptyParas]--> <!--[endif]-->

4. 다음의 식별자 중에서 잘못된 것은 (3)

_number sales_expectation 1st_number logical

변수 이름은 숫자로 시작할 수 없다.

<!--[if !supportEmptyParas]--> <!--[endif]-->

5. 다음 중 C에서 지원하는 자료형의 이름이 아닌 것은? (3)

char long byte float

<!--[if !supportEmptyParas]--> <!--[endif]-->

6. 다음의 상수 중에서 올바르지 않은 상수를 지적하면? (1)

'abc' "A" 0x10 .1

<!--[if !supportEmptyParas]--> <!--[endif]-->

7. 다음의 설명에 적합한 문장을 작성하여라.
int main(void){

	int sum;
	double score;
	char answer;

	sum = 100;
	score = 1.23;
	anser = ‘X’
	printf(“%d”, sum);
	printf(“%lf”, score);
	printf(“%c”, anwer);

	return 0;
}
8. 다음 프로그램의 실행결과를 써라.
#include <stdio.h>

int main(void){

	int a = 100;
	char b = ‘X’;
	float c = 1.2345;

	printf(“\a”);
	printf(“예제\t프로그램\n”);
	printf(“%d, %c, %f\n”, a, b, c);

	return 0;
}
실행결과
예제    프로그램
100, X, 1.12345
  

9. 태양에서 오는 빛이 몇 분 만에 지구로 도착하는지를 계산해보고자 한다. 아래 코드에서 빈칸을 채워라. double형 변수들을 사용하고, 변수의 초기 값을 지수형식으로 표시한다.

#include <stdio.h>

int main(void){

	double light_speed = 300000;
	double distance = 149600000;
	double time;

	time = distance / light_speed;
	time = time = 60.0;
	printf(“%lf”, time);

	return 0;
}
10. 다음 프로그램의 결과를 예측하여 오른쪽에 기록하여라.
#include <stdio.h>

int main(void)
{
char code = ‘B’;
printf(“%c\n”, code-1);
printf(“%c\n” code+1);

return 0;
}
결과
A

11. 다음 프로그램의 결과를 예측하여 오른쪽에 기록하여라
#include <stdio.h>

int main(void)
{
	int x=10;

	printf("8진수=%o\n", x)
	printf("10진수=%d\n", x)
	printf("16진수=%x\n", x);

	return 0;
}
결과
8진수=12
10진수=10
16진수=a 

12. 잠시 컴파일러가 되었다고 가정하자. 다음 소스 파일이 컴파일되어 실행되는 것인지 말하고, 컴파일이 되지 않는 다면 어디가 문제인지를 적어라.
#include <stdio.h>

int main(void){

	double radius;

	printf(“반지름을 입력하시오: ”);
	scanf(“%f”, radius);

	double circ, area;
	circ = 2.0 * 3.14592 * radius;
	area = 3.141592 * radius * radius;

	printf(“원주: %f\n”, circ);
	printf(“면적: %f\n”, area);

	return 0;
}
scanf() 함수에서 변수 radius가 double형이므로 %lf를 사용하여야 한다. 
원주값과 면적을 출력할 때에 변수가 double 형이므로 %lf를 사용하여야 한다.



반응형