๋ฐ๋ฐฐ์จ 4.5 ~
4.5 ๊ธฐํธ์ ์์์ ์ ์ฒ๋ฆฌ๊ธฐ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define PI 3.141592f // = X
#define AI_NAME "Jarvis" // ๋๋ฌธ์๊ฐ ์ผ๋ฐ์
int main()
{
//const Float pi = 3.141592f;
float radius, area, circum;
printf("I'm %s.\n", AI_NAME);
printf("Please, input radius\n");
scanf("%f", &radius);
area = PI * radius * radius; // area = pi*r*r
circum = 2.0 * PI * radius; // circum = 2.0 * pi * r
printf("Area is %f\n", area);
printf("Circumference is %f\n", circum);
//TODO: wriong usage, string, const
return 0;
}
#define ์ผ๋ก๋ ๊ฐ๋ฅํ๊ณ
const ๋ฅผ ์ด์ฉํด ๊ธฐํธ์ ์์๋ฅผ ๋ง๋ค ์ ์๋ค.
4.6 ๋ช ๋ฐฑํ ์์๋ค manifest constants
#include <stdio.h>
#include <limits.h> // INT_MAX, ..., etc
#include <float.h> // FLT_MAX, ..., etc
#define PI 3.141592 // manifest constants, symbolic constants
int main()
{
printf("PI is %f\n", PI);
printf("Biggest int: %d\n", INT_MAX);
printf("One byte in this system is %d bits\n", CHAR_BIT);
printf("Smallest normal float %e\n", FLT_MIN);
return 0;
}
์ฌ๊ธฐ์ PI, INT_MAX, CHAR_BIT, FLT_MIN์ ๋ณ์๊ฐ ์๋๋ผ ๊ฐ์ ธ๋ค ์ฐ๋ ๋๋์ด๋ค.
4.7 printf() ํจ์์ ๋ณํ ์ง์ ์๋ค
printf(์ ์ด-๋ฌธ์์ด, ์์ดํ 1, ์์ดํ 2, ...)
int a = 2;
printf("%d + %d = %d", 1, a, 1 + a);
ํ์ ์ง์ ์
%[flags][width][.precision][lentf]specifier
printf("%+10.5hi", 256);
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
double d = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749;
printf("%c\n", 'A');
printf("%s", "I love you\n");
printf("Even if there's a small chance, \
we owe this to everyone who's in this room to try.\n");
printf("\n");
printf("%d %i %i %i\n", 1004, 1234, INT_MAX, UINT_MAX); // overflow
printf("%u %u %u\n", 1023, -1, UINT_MAX); // overflow
printf("\n");
printf("%f %f %lf\n", 3.141592f, d, d); // l in %lf is ignored
printf("%a %A\n", d, d);
printf("%e %E\n", d, d);
printf("\n");
printf("%g %g\n", 123456.789,1234567.89);
printf("%G %G\n", 123456.789,1234567.89);
printf("%g %g\n", 0.00012345, 0.000012345);
printf("%G %G\n", 0.00012345, 0.000012345);
printf("\n");
printf("%o\n", 9);
printf("%p\n", &d); // pointer-of operator
printf("\n");
printf("%9d\n", 12345); // ์์ ์ซ์๋ฅผ ์ฐ๋ฉด ์๋ฆฟ์๋ฅผ ๋ง์ถฐ์ค๋ค.
printf("%09d\n", 12345);
printf("%.2fn", 3.141592);
printf("%.20f %.20lf\n", d, d);
printf("\n");
int n_printed = printf("Counting!");
printf("%u\n", n_printed);
return 0;
}
๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉฐ ๋น๊ตํด๋ณด์
4.8 ๋ณํ ์ง์ ์์ ์์์ด๋ค modifiers
ํ์ ์ง์ ์
%[flags][width][.precision][lentf]specifier
printf("%+10.5hi", 256);
#include <stdio.h>
#include <limits.h>
int main()
{
printf("%10i\n", 1234567);
printf("%-10i\n", 1234567);
printf("%+i %+i\n", 123, -123);
printf("% i \n% i\n", 123, -123);
printf("%X\n", 17);
printf("%#X\n", 17);
printf("%05i\n", 123);
printf("%*i\n", 7, 456);
printf("\nPrecision\n");
printf("%.3d\n", 1024);
printf("%.5d\n", 1024);
printf("%.3f\n", 123456.1234567);
printf("%.3f\n", 123456.1235); // ๋ฐ์ฌ๋ฆผ
printf("%10.3f\n", 123.45678);
printf("%010.3f\n", 123.45678);
printf("%.5s\n", "ABCDEFGHIJKLMN");
printf("%.s\n", "ABCDEFGHIJKLMN"); // .0
printf("\nLength\n");
printf("%hhd %hd %d\n", 25, 257, 257); //overflow
printf("%d %lld %lld\n", INT_MAX + 1, INT_MAX +1, 2147483648LL); // x64์ x86์ ๋ฐ๋ผ ๋ค๋ฆ
return 0;
}
๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉฐ ๋น๊ตํด๋ณด์
4.9 printf() ํจ์๊ฐ ์ธ์๋ค์ ํด์ํ๋ ๊ณผ์
#include <stdio.h>
int main()
{
float n1 = 3.14; // 4 byte
double n2 =1.234; // 8 byte
int n3 = 1024l; // 4 byte
printf("%f %f %d", n1, n1, n1);
//Notethe warnings in output window
printf("%d %d %d\n\n", n1, n2, n3); // 4, 4, 4
printf("%lld %lld %d\n\n", n1, n2, n3); // 8, 8, 4
printf("%f %d %d\n\n", n1, n2, n3); // 8, 4, 4
printf("%f %lld %d\n\n", n1, n2, n3); // 8, 8, 4
return 0;
}
์ธ์๋ค์ด ์คํ์ ์์๋๋ก (์ฐจ๋ก๋๋ก, ์ผ๋ ฌ๋ก) ์ ์ฅ๋๋ค
printf()์์๋ ๋ถ๋์์์ ์ด ๋ค์ด์ค๋ฉด float, double ๋ชจ๋ double๋ก ๋ณํ๋๋ค.
์์ ์์ ๋ฅผ ๋ณด๋ฉด
1. printf("%d %d %d\n\n", n1, n2, n3);
๋ฐ์ดํฐ์ ์ฌ์ด์ฆ์ ์ง์ ํ์์ด ๋ฌ๋ผ ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
์ฐ์ n1, n2๋ ๋ถ๋์์์ ์ด๊ธฐ์ %f๋ก ๋ฐ์์ผ ํ๋๋ฐ ์ ์ %d๋ก ๋ฐ์ ๋ฌธ์ ๊ฐ ์๊ฒผ๋ค.
๊ทธ๋ฆฌ๊ณ n1์ float์ด์ง๋ง printf()๋ก ๋ฐ์ผ๋ฉด์ double ์ฆ 8byte๋ก ๋ฐ๊ธฐ ๋๋ฌธ์, n2๋ 8byte๋ผ ๋ฐ์ดํฐ์ ์ฌ์ด์ฆ๊ฐ ๋ค๋ฅด๋ค.
๊ทธ๋ ๋ค๋ฉด n3๋ ๋ฐ์ดํฐ์ ์ฌ์ด์ฆ์ ์ง์ ํ์์๋ ๋ง๋๋ฐ ์ ๋ฌธ์ ๊ฐ ์๊ธฐ๋?
n1๊ณผ n2์ ๋ฐ์ดํฐ ์ฌ์ด์ฆ๊ฐ ๋ง์ง ์์ ๋ฐ๋ ค ์ฝ์ด ์ฐ์์ ์ผ๋ก ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
2. printf("%lld %lld %d\n\n", n1, n2, n3);
์ง์ ํ์์๊ฐ ๋ง์ง ์๋ค.
๋ฐ์ดํฐ์ ์ฌ์ด์ฆ๋ ๋ชจ๋ ๋ง์ง๋ง ๋ถ๋์์์ %f๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
์ฌ๊ธฐ์ n3์ n1, n2๊ฐ ๋ฐ์ดํฐ ์ฌ์ด์ฆ๋ ๋ง๊ธฐ ๋๋ฌธ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ถ๋ ฅ๋๋ค.
3. printf("%f %d %d\n\n", n1, n2, n3);
n2์ ๋ฐ์ดํฐ ์ฌ์ด์ฆ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ n2, n3 ๋ชจ๋ ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
4. printf("%f %lld %d\n\n", n1, n2, n3);
n2์ ์ง์ ํ์์๊ฐ ๋ฌ๋ผ n2๋ง ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
4.10 scanf() ํจ์์ ์ฌ์ฉ๋ฒ
int i;
float f;
char str[30];
scanf("%d %d %s", &i. &f, str);
printf("%d %d %s", i, f, str);
scanf()์์ double์ ์ ๋ ฅ๋ฐ์ ๋๋ %lf ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค. printf()fh ์ถ๋ ฅํ ๋๋ ๊ทธ๋๋ก %f
123 4567 hello hi ๋ฅผ ์ ๋ ฅํ๋ฉด ๋น์นธ๋ง๋ค ๋์ด์ ์ ๋ ฅ๋ฐ๊ณ hi๋ ๋ฒํผ์ ์ ์ฅ๋์ง๋ง ์ถ๋ ฅ๋์ง ์๋๋ค.
char c
scnaf("%c", &c);
printf("%i", c);
๋ฌธ์๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ ๋น์นธ(space)๋ ์ ๋ ฅ์ ๋ฐ๋๋ค
์์คํค์ฝ๋๊ฐ 32๋ก ์ถ๋ ฅ๋จ
unsigned i;
scanf("%i", &i);
printf("%i", i);
๋ง์ฝ -123 ์ ์ ๋ ฅํ๋ฉด ๋ฐ์ ๋ signed๋ก ๋ฐ๊ณ unsigned๋ก ์ ์ฅํด๋จ๋ค๊ฐ ๋ค์ signed๋ก ์ถ๋ ฅํ์ฌ
-123 ์ผ๋ก ์ถ๋ ฅ๋๋ค.!?
unsigned i;
scanf("%u", &i);
printf("%u", i);
๊ฐ์ ๊ฐ์ธ -123 ์ ์ ๋ ฅํ๋ฉด ์๋ชป๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
char str[30]
scanf("%5s", str); // width
printf("%s", str);
abcdefghi ๋ฅผ ์ ๋ ฅํ๋ฉด
abcde(5๊ธ์) ๊น์ง๋ง ์ ๋ ฅ๋ฐ๋๋ค.
int i
scanf("%i", &i);
printf("%i",i);
123a456 ์ ์ ๋ ฅํ๋ฉด ์ซ์๋ง ์ ๋ ฅ๋ฐ๊ธฐ ๋๋ฌธ์ ๋ฌธ์๊ฐ ๋์ค๊ธฐ ์ ๊น์ง์ธ
123 ๋ง ์ ๋ ฅ๋ฐ๋๋ค.
int a, b;
int i = scanf("%d%d", &a, &b);
printf("%d", i);
scnaf() ์ return value ๋ ๋ช๊ฐ์ ๊ฐ์ ์ ๋ ฅ๋ฐ์๋๊ฐ, ๋ฐ์ ๊ฐ์ ๊ฐ์๋ฅผ ์๋ฏธํ๋ค.
int i;
scanf("%*d%*d%d", &i)
printf("Your third input = %d", i)
scnaf()์์๋ *์ ์ ๋ ฅ์ ๋ฐ์ง๋ง ์ค์ ๋ก๋ ๋ฌด์ํ๊ณ ๋ณ์์ ๋์ ํด์ฃผ์ง ์๋๋ค.
1 2 3 ์ ์ ๋ ฅํ๋ฉด
3์ด ์ถ๋ ฅ๋๋ค.