728x90

<๋ณ€์ˆ˜์— ๋‹ด๊ธด ๊ฐ’์„ ์›ํ•˜๋Š” ํ˜•์‹์— ๋งž์ถฐ ์ถœ๋ ฅํ•˜๋Š” ๋ฒ•>

: ๋ณ€์ˆ˜ ํฌ๋งท(%d, %s, ...)๊ณผ %๋ฅผ ์‚ฌ์šฉ

 

  • printf ํ•จ์ˆ˜์˜ ๋ฌธ์ž์—ด์— ํ•ด๋‹น ๋ณ€์ˆ˜์˜ type์— ํ•ด๋‹นํ•˜๋Š” ํฌ๋งท์„ ์ ์–ด์ฃผ๊ณ , ๋งจ ๋’ค์—๋Š” , ์™€ ๋ณ€์ˆ˜๋ฅผ ๋‚˜์—ดํ•˜๋Š” ์‹์œผ๋กœ ํฌ๋งท์„ ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๋ฌธ์ž์—ด: %s
    • ๋ฌธ์ž: %c
    • ์ •์ˆ˜: %d
    • ์‹ค์ˆ˜: %lf 
#include <stdio.h>

int main() {

	int a = 5;
	printf("A is %d\n", a);

	char b[10] = "apple";
	printf("B is %s\n", b);

	printf("A is %d and B is %s", a, b);
	
	return 0;

}

 

โ–ถ๋ฌธ์ œ: ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ ๋ณ€์ˆ˜์— 7, 23, 30์„ ๋Œ€์ž…ํ•œ ํ›„ ๊ทธ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ง์…ˆ์‹์„ ์™„์„ฑํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

#include <stdio.h>

int main() {
    int a = 7, b = 23, c = 30;
    printf("%d + %d = %d",a,b,c);
    return 0;
}

โ–ถ๋ฌธ์ œ: ๋ณ€์ˆ˜ a, b์— ๊ฐ๊ฐ 3, 'C'์„ ๋„ฃ์–ด์ฃผ๊ณ , ์ถœ๋ ฅ ํ˜•์‹์— ์•Œ๋งž๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

a, b์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋˜ ์‚ฌ์ด์— "..."๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

#include <stdio.h>

int main() {
    int a = 3;
    char b = 'C';
    printf("%d...%c",a,b);
    return 0;
}

โ–ถ๋ฌธ์ œ: ๋ณ€์ˆ˜ a, b์— ๊ฐ๊ฐ 3, 'C'์„ ๋„ฃ์–ด์ฃผ๊ณ , ์ถœ๋ ฅ ํ˜•์‹์— ์•Œ๋งž๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

b, a์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋˜ ์‚ฌ์ด์— "!.....!"๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

#include <stdio.h>

int main() {
    int a = 3;
    char b = 'C';
    printf("%c!.....!%d",b,a);
    return 0;
}

โ–ถ๋ฌธ์ œ: ๋ณ€์ˆ˜ a, b, c์— ๊ฐ๊ฐ 1, 2, 'C'์„ ๋„ฃ์–ด์ฃผ๊ณ , ์ถœ๋ ฅ ํ˜•์‹์— ์•Œ๋งž๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

a, b, c์˜ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋˜ ์‚ฌ์ด์— "->"๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

#include <stdio.h>

int main() {
    int a = 1, b = 2;
    char c = 'C';
    printf("%d->%d->%c",a,b,c);
    return 0;
}

+ Recent posts