본문 바로가기
42cursus/printf

printf 자료조사

by 인듯아닌듯 2020. 7. 6.

Variadic function

Variadic function : function that takes a arbitrarily many argument.

"The C function printf and the Common Lisp function format are two such examples. Both take one argument that specifies the formatting of the output, and any number of arguments that provide the values to be formatted."[1]

Variadic functions can expose type-safety problems in some languages. high-severity security risk!![1]

 

가변인자 사용하기(코딩도장) https://dojang.io/mod/page/view.php?id=577

printf / stdarg

printf

1. To portably implement variadic functions in the C programming language, the standard stdarg.h header file is used. The older varargs.h header has been deprecated in favor of stdarg.h. In C++, the header file cstdarg is used.[1]

2. for example printf, the number and types of arguments are figured out from a format string.[1]

3. If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the format string attack.[1]

 

C언어레퍼런스[2]

형식 문자열(format string)에는 stdout에 출력할 문자열이 들어있다.

이 때, 형식 문자열에는 형식 태그(format tag)라 불리는 것이 추가적으로 들어갈 수 있는데, 이는 이에 대응하는 인자를 형식 태그가 지정한 형태로 치환되어 출력된다.

형식 태그는 아래와 같은 꼴로 생겼다.

서식문자(=specifier) : "cspdiuxX%"를 필수로 구현

printf에서 %i 와 %d은 차이가 없다. 하지만 scanf에서는 %i와 %d는 차이를 보인다. scanf에서 %i는 8진법 16진법을 받을 수 있다.[3]

scanf()에서는 8진법은 023 16진법은 0x14 or 0XAf로 받는다 (0을 꼭 붙여줘야한다.)

printf()에서는 8진법 %o 16진법 %x(소문자), %X(대문자)

 

과제에서 It will manage any combination of the following flags : '-0.*' and minimum field width with all conversions.

flags : '-0.*' (정확히 어떤 것을 원하는지.)

https://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html

flag에 관한 설명이 있는 사이트

stdarg

va_list, va_start, va_end, va_arg, va_copy

  • va_list: 가변 인자 목록. 가변 인자의 메모리 주소를 저장하는 포인터입니다.
  • va_start: 가변 인자를 가져올 수 있도록 포인터를 설정합니다.
  • va_arg: 가변 인자 포인터에서 특정 자료형 크기만큼 값을 가져옵니다.
  • va_end: 가변 인자 처리가 끝났을 때 포인터를 NULL로 초기화합니다
  • va_copy:  void va_copy(va_list dest, va_list src); 가변인자목록 포인터를 복사함

가변인자와 stdarg.h에 대한 친절한 설명

https://jangsalt.tistory.com/entry/%EA%B0%80%EB%B3%80-%EC%9D%B8%EC%88%98-vastart-vaend-vaarg-valist

 

printf의 return값은 출력되는 문자의 갯수[4]


참고자료

1.https://en.wikipedia.org/wiki/Variadic_function#In_C

2.https://modoocode.com/35

3.https://www.geeksforgeeks.org/difference-d-format-specifier-c-language/

4.https://www.geeksforgeeks.org/return-values-of-printf-and-scanf-in-c-cpp/

 

 

 

 

 

 

'42cursus > printf' 카테고리의 다른 글

printf flowchart  (0) 2020.07.25
printf_check-list  (0) 2020.07.10