c언어 기본구조,표준 출력 함수 printf()의 이해

2021. 2. 2. 08:58개발하는중/c

728x90
반응형

01 - 기본구조

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h> ➡ 전처리기 
 
 
 
int main(int argc, char * argv[])
 
{
 
 
 
선언문;
 
실행문; ➡ 메인함수
 
 
 
 
return 0;
 
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
 
 
 
 
void main()
 
{
 
선언문;
 
실행문;
 
 
}
cs

02 - 프로그램 실행

1.Compile

-작성된 프로그램의 기계어 변환 

- Ctrl + F7

*작성된 프로그램을 컴퓨터가 알아듣는 언어인 0,1로 변환시켜 확인함

 

2.Build

-구성 요소들의 결합 및 실행 파일 생성

-F7

 

3.프로그램 실행

-Ctrl + F5

2.표준 출력 함수 printf()의 이해

01 - 형식

int printf( const char *format [, argument]...);

             parameter 파라미터

02 - 기능

괄호 안에 있는 parameter의 내용을 모니터에 출력

 

03 - 헤더파일

#include <stdio.h>

 

04 - parameter (인자값)

1.format

   - 출력 내용 및 출력 형식

2.argument

   - 출력 형식에서 사용할 데이터

 

05 - 예 : printf()의 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
 
 
int main(int argc, char * argv[])
 
{
 
printf(“Hello C”);
 
return 0;
 
}
cs
  • compile : ctrl + f7

  • build : f7

  • excute : ctrl + f5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
 
 
int main(int argc, char * argv[])
 
{
 
printf(“Hello ”);
 
printf(“C”); *줄바꿈 지원하지 않음 
 
return 0;
 
}
cs

06 - ESCAPE 문자의 기능 및 종류

   - 출력되는 형식과 관련된 문자 명령어

ESCAPE 문자

기 능

ASCII 값

\n

New line : 새로운 줄로 이동

10

\r

Carriage return : 줄의 처음으로 이동

13

\b

Back space : 왼쪽 한 칸으로 커서 이동 

8

\t

Tab : 캡 크기만큼 커서 이동

9

\a

Alarm : 벨 소리 표현

7

 

07 - 예 : 줄바꿈 기능 \n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
 
 
 
void main()
 
 
printf(“hello \n C”); 
 
출력  = hello
        C
 
}
 
 
 
※ \n 과 ₩n 은 같은 표현
 
#include <stdio.h>
 
 
 
void main()
 
{
 
printf(“hello \n ”);
 
printf(“C”); 
 
출력 = hello
       C
}            
cs

08 - 예 : 커서 왼쪽 이동 \b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
 
 
void main()
 
{
 
printf(“H ”);
 
printf(“\b”);
 
printf(“E”) 출력 = E
 
}
cs

09 - 예 : 탭 간격 띄어쓰기 \t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
 
 
void main()
 
{
 
printf(“H ”);
 
printf(“\t”);
 
printf(“E”) 출력 = H E
 
}
cs

10 - 제어문자의 종류 및 출력 결과

제어문자

출 력 결 과 

%d

10진 (Decimal) 정수

%o

8진 (Octal) 정수

%x

16진수 (Hexa Decimal) 정수

%p

포인터 정수(16진수)

%u

부호없는(Unsingned) 10진 정수 

%f

10진형 부동 (Floating) 소수점수

%e

지수형(Exponential) 부동소수점수

%c

단일문자(Character)

%s

문자열(String)

 

11 -  제어문자의 진법 표현

지정된 범위의 수로 표현하는 방법

진 법

범 위

표현식

2진수

0, 1

 

C언어 표현 불가

8진수

0 ~ 7

0(숫자)

0101

10진수

0 ~ 9

 

65

16진수

0 ~ 9,A ~ F

0x

0x41

12 - 예: 정수 데이터 10진수 출력 %d

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
 
 
void main()
 
{
 
printf(“%d \n”, 10); 출력 = 10
 
printf(“%d + %d = %d \n” ,102010+20); 출력 = 10 + 20 = 30
 
}
cs

13 - 예 : 정수 데이터 진수 별 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
 
 
 
void main()
 
{
 
printf(“%d \n”, 173); 10진수 출력 
 
printf(“%o \n”, 173); 8진수 출력
 
printf(“%x \n”, 173); 16진수 출력
 
}
cs

14 - 예 : 정수 데이터 진수 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
 
 
 
void main()
 
{
 
printf(“%d \n”, 0xAD); 10진수 출력 
 
printf(“%o \n”, 0xAD); 8진수 출력
 
printf(“%x \n”, 0xAD); 16진수 출력
 
}
cs

15 - 예 : 실수 데이터 출력 %f

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
 
 
 
void main()
 
{
 
printf(“%f \n”, 1.234);
 
}
cs

16 - 예 : 제어문자 %f 와 %d

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
 
 
 
void main()
 
{
 
printf(“%f \n”, 10); 출력 안됨 /정수는 정수 제어문자
 
printf(“%d \n”, 1.234); 출력 안됨 /실수는 실수 제어문자 사용해야함
 
}
cs

17 - 예 : 단일문자 데이터 출력 %c

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
 
 
void main()
 
{
 
  printf(“%c \n”, ‘a’);
 
}
cs

- 단일문자 데이터를 외따움표로 표기함

 

18 - 예 : 제어문자 %c 와 %d

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
 
 
void main()
 
{
 
  printf(“%c \n”, 65); 출력 = A
 
  printf(“%d \n”, ‘a’); 출력 = 97
 
}
cs

ASCII CODE

 - 숫자에 문자형식을 적용한 표

 - 두 가지 형식(%c, %d) 출력이 가능

 

19 = 예 : 제어문자 %s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
 
 
void main()
 
{
 
  printf(“%c \n”, “가나다라”); 출력 = 가나다라
 
  printf(“%d \n”, ‘abcd 123’); 출력 = abcd123
 
}
 
 
cs

-문자열 데이터를 쌍따옴표로 표기함








728x90

'개발하는중 > c' 카테고리의 다른 글

자료형, 선행 처리기  (0) 2023.03.20
c언어 개요  (0) 2023.03.17
c언어_연산자의 이해  (0) 2021.05.18
c언어_표준 입력 함수 scanf()의 이해  (0) 2021.02.04
c언어_상수의 이해  (0) 2021.02.03