{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# printf 이해\n", "\n", "# 서식\n", "\n", "```printf``` 는 쌍따옴표 안에 있는 글자를 모두 출력한다. 서식있는 문자열도 출력을 할 수 있다. 서식을 적용하기 위해서는 몇가지 특수 기호들을 기억해야 한다. 가장 많이 쓰는 서식은 \\\\t와 \\\\n 이다. \\\\t는 tab의 약자로 탭(묶음 공백)을 삽입한다. \\\\n 은 new line의 약자로 줄바꿈 기호이다. \n", "\n", "## 제어 문자\n", "\\\\t 와 \\\\n 과 같은 특수기호를 포함하여 출력 할 때 \\\\로 시작하는 제어문자를 사용한다. 몇 가지 예는 다음과 같다. \n", "* \\\\n 줄바꿈\n", "* \\\\t 탭 문자\n", "* \\\\ 백슬래시\n", "* \\' 작은 따옴표\n", "* \\\" 쌍따옴표" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 서식지정자\n", "많이 사용되는 서식지정자는 다음과 같다. \n", "* %d : 10진수 정수 출력\n", "* %ld : long 자료형의 정수 출력\n", "* %f : 실수 출력 (float, double) 모두\n", "* %e : 지수형으로 출력\n", "* %c : 문자 출력\n", "* %c : 문자열 출력\n", "* %u : 부호없는 10진수 정수 출력\n", "* %% : % 문자 출력" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "// understand the usage of printf function\n", "#include \n", "\n", "int main()\n", "{\n", " // declare new variables\n", " int int_num;\n", " char character;\n", " \n", " // initialize new variables\n", " int_num = 100;\n", " character = 'A';\n", " \n", " // declare and initialize new variables\n", " float float_num = 3.14;\n", " char string[7] = \"SCHOOL\";\n", " \n", " // printing\n", " printf(\"print value in variables\\n\");\n", " printf(\"integer type variable int_num : \\t%d\\n\", int_num);\n", " printf(\"real type variable float_num : \\t%f\\n\", float_num);\n", " printf(\"character type variable character : \\t%c\\n\", character);\n", " printf(\"string type variable string : \\t%s\\n\\n\", string);\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 1\n", "위의 코드에는 ```printf```가 5개 사용되었다. 다음을 해결해보자. \n", "* ```printf```을 한 번만 사용하여 똑같이 출력하기\n", "* ```printf```을 10 번 사용하여 똑같이 출력하기\n", "* ```printf```의 결과에 문자열, 문자, 실수, 정수가 3번 연속 나오도록 하되 ```printf```는 한 한 번만 사용해보자" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 변수의 서식\n", "다음은 예는 변수에 서식을 적용하는 예이다. 변수에 저장된 값에 따라 표현하는 방식을 달리하고 싶은 경우가 있다. 예를 들어 실수를 정수부만 출력하는 경우처럼 말이다. 다음의 그림에 설명이 나와 있다. \n", "![format](figure/format_var.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "소수점 중심으로 앞에 숫자 뒤에 숫자가 있는데, 앞의 숫자는 표현가능한 숫자의 길이를 나타내고 뒤의 숫자는 정밀도를 나타낸다. 음수로 표현하는 경우 왼쪽 정렬이 된다. 아래의 예를 살펴보자." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "// understand the usage of printf function\n", "#include \n", "\n", "int main()\n", "{\n", " // declare new variables\n", " int int_num;\n", " char character;\n", " \n", " // initialize new variables\n", " int_num = 100;\n", " character = 'A';\n", " \n", " // declare and initialize new variables\n", " float float_num = 3.14;\n", " char string[7] = \"SCHOOL\";\n", " \n", " // printing with format\n", " printf(\"%10d\\n\", int_num);\n", " printf(\"%-10d\\n\", int_num);\n", " printf(\"%07d\\n\", int_num);\n", " printf(\"%10.2f\\n\", float_num);\n", " printf(\"%-10.2f\\n\", float_num);\n", " printf(\"%10.1f\\n\", float_num);\n", " printf(\"%10s\\n\", string);\n", " printf(\"%10.4s\\n\", string);\n", " printf(\"%-10.4s\\n\", string);\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 2\n", "\n", "다음 그림을 보고 자릿수 등을 조정하여 ```printf``` 서식을 정해보자.\n", "![format](figure/format_ex.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int cnt = 001;\n", " double sec = 0.000000389;\n", " int process = 380;\n", " char status = 'G';\n", " char IOstat = 'W';\n", " int lba = 38028;\n", " int length = 8;\n", " char[] = \"kjournald\";\n", " \n", " // printing with format\n", " printf(\"\");\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 3\n", "```printf```는 쌍따옴표 안에 있는 글자를 모두 출력하도록 정의되어 있다. 그러면 쌍따옴표를 출력하고 싶다면 어떻게 해야 할까? \\\\t에서 사용되는 \\\\ 를 출력하려면 어떻게 해야 할까? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " // your code here\n", " return 0;\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "C", "language": "c", "name": "c" }, "language_info": { "file_extension": ".c", "mimetype": "text/plain", "name": "c" } }, "nbformat": 4, "nbformat_minor": 2 }