[ft_printf] subject와 printf 함수 이해

Cadet_42·2021년 7월 19일
0

ft_printf

목록 보기
1/6
post-thumbnail

💡printf 함수란 ?

printf 함수의 원형

int printf(const char *format, ...)

  • printf 첫번째 입력 매개변수 const char *
  • printf 두번째 입력 매개변수 ... : 가변인자. printf는 매개변수의 개수가 정해지지 않는다. 즉 여러개의 인자를 넣어도 함수가 작동한다.
    ❗️❗️❗️ printf 반환값은 : int다 ❗️❗️❗️

printf 출력 방식

  • printf의 첫번째 인자: 어떤 형태로 출력할것인지 표현함.
    예시) %d, %s, %c (정수, 문자열, 문자.. )

  • printf 두번째 인자: 출력할 데이터들이 옴.

printf 함수 원리

int main(void)
{
	int re;

	re = printf("hi%d\n", 123); // ==> hi123
	printf("re: %d \n", re); // ==> re: 6
	return (0);
}

실행 결과

hi123
re: 6
  • 위 예제를 보면 hi123 [개행문자] 를 출력한 뒤에 반환값 6을 다시 출력 하고 있다. - printf 함수는 출력한 문자의 개수를 반환하는데 공백이나 탭, 개행문자(엔터)도 하나의 문자로 취급한다.
  • 따라서, 실제 출력한 문자 개수는 6개 이며, printf함수의 결과값으로 6 (int, 정수)을 반환받음을 알 수 있다.
  • printf 함수의 반환 값은 출력되는 문자의 개수이다. 출력되는 문자의 개수는 공백( )과 이스케이프 시퀀스인 개행(\n), 가로 탭(\t) 등을 포함한다.

ft_printf 문제 이해하기

Write a library that contains ft_printf, a function that will mimic the real printf.

Mandatory part

  • prototype of ft_printf:
int ft_printf(const char*, ...)
  • You have to recode the libc's printf function
  • It must not do the buffer management like the real printf
  • It will manage the following conversions: cspdiuxX%
  • It will be compared with the real printf
  • You must use the command ar to cerate your library, using the command libtool is forbidden.
  • %c : 문자 (character) 출력
  • %s : 문자열 (string) 출력
  • %p : void* 포인터를 hexadecimal로 출력
  • %d : decimal (base 10) 숫자를 출력
  • %i : integer (base 10) 숫자를 출력
  • %u : unsigned decimal (base 10) 10진수 정수를 출력
  • %x : hexadecimal (base 16) 숫자를 출력, 알파벳은 소문자로 표현
  • %% : 퍼센트 사인을 출력.
// 다양한 포멧을 지정하여 출력

#include <stdio.h>

int main(void)
{
	printf("============cspdiuxX퍼센트=========\n");
	printf("1.c 문자 출력 : %c\n", 'a');
	printf("2.s 문자열 출력: %s\n", "Hello World");
	printf("3.p 메모리 주소 출력: %p\n", "Hi");
	printf("4.d 십진수로 출력: %d\n", 123);
	printf("5.i 부호있는 십진수로 출력: %i\n", 123);
	printf("6.u 부호없는 십진수로 출력: %u\n", 123);
	printf("7.x 부호없는 16진수로 출력(소문자): %x\n",123);
  	printf("8.X 부호없는 16진수로 출력(대문자): %X\n", 123);
	printf("9.퍼센트 문자 출력: %%\n");
	return (0);
}

출력 결과

============cspdiuxX퍼센트=========
1.c 문자 출력 : a
2.s 문자열 출력: Hello World
3.p 메모리 주소 출력: 0x10a925eb9
4.d 십진수로 출력: 123
5.i 부호있는 십진수로 출력: 123
6.u 부호없는 십진수로 출력: 123
7.x 부호없는 16진수로 출력(소문자): 7b
8.X 부호없는 16진수로 출력(대문자): 7B
9.퍼센트 문자 출력: %

16진수(16진법)이란?
16진법이란, 0~9 까지의 10개의 숫자를 사용하고 남는 자리는 A~F 까지 6개의 문자를 사용해서 수를 표현한다.

Bonus part

  • Manage any combination of the following flags: ’-0.’ and minimum field width with all conversions
  • '#', '+' : Manage all the following flages (yes, one of them is a space).

The Zero flag : 0

Using 0 will force the number to be padded with 0s. This only really matters if you use the width settin to ask for minimal width for your number.

printf( "%05d\n", 10 );

출력결과 : 00010

출처 :
1. printf 함수의 기능
https://ehpub.co.kr/24-printf-%ED%95%A8%EC%88%98/
2. 16진수
https://itbeginner2020.tistory.com/17
3. Printf definition
https://www.cypress.com/file/54441/download
https://www.cprogramming.com/tutorial/printf-format-strings.html

profile
안녕하세요! 개발공부를 하고 있습니다. 감사히 배우겠습니다. ;)

0개의 댓글