# 표준입출력함수의 종류
- 키보드 입력 함수
- scanf(): 모든 자료형 입력
- getchar(), getch(): 문자형 자료 입력
- gets(): 문자열 자료 입력
- 화면 출력함수
- printf(): 모든 자료형 출력
- putchar(), putch(): 문자형 자료 출력
- puts(): 문자열 자료 출력
# 버퍼를 사용하지 않는 문자 전용 입출력 함수
- getch(): 문자 전용 입력함수, 입력되는 글자를 화면에서 볼 수 없음
- putch(): 문자 전용 출력함수
// 문자형 자료의 입출력 프로그램(소문자 -> 대문자 변환)
#include <stdio.h>
int main(){
char c;
printf("소문자 한 개 입력하시요: \n");
c = getchar();
c=c-32; //소문자를 대문자로 변환 (ASCII 코드값 사용)
putchar(c);
}
# ASCII 코드값
- 영문자 대문자 A
Z: 6590 - 영문자 소문자: a
z: 97122
# 대문자 <-> 소문자변환(ASCII 코드값 이용)
- 대문자 = 소문자 - 32
- 소문자 = 대문자 + 32
#include <stdio.h>
#define UNIV "My university"
#define DEP "컴퓨터과학과"
int main(){
char ch, name[20];
printf("이름을 입력하세요: ");
gets(name); //gets: 문자열 자료 입력함수
printf("학점을 입력하세요: ");
ch=getchar();
printf("\n\n");
puts(UNIV); //puts: 문자열 자료 출력함수
puts(DEP);
puts(name);
printf("학점은");
putchar(ch);
}
# 산술연산 프로그램
- 연산자의 종류
- 산술 연산자 : 사칙연산
+
,++
,--
,%
- 관계 연산자 : 대,소비교
>
,<
,>=
,!=
- 논리 연산자:
&&
,||
,!
- 대입 연산자:
+=
,-=
,*=
,!=
,&=
,%=
- 조건 연산자:
?:
- 비트 연산자:
&
,!
,^
,~
,<<
,>>
- 기타 연산자:
sizeof()
,cast
,&
,*
- 산술 연산자 : 사칙연산
- 조건 연산자 : (조건) ? 수식1: 수식2
- 비트 연산자
^ (bit XOR) : 대응되는 두 bit가 서로 다를 때만 결과는 1
| (bit OR): 대응되는 두 bit가 하나라도 1이면 결과는 1
<< : bit 좌로 이동
>>: bit 우로 이동
- 기타연산자
- sizeof: 지정한 자료형, 수식, 변수가 차지하는 기억공간의 크기(byte) 구함
- cast(형변환) : 지정한 자료형을 다른 자료형으로 강제로 바꿈
- &: 주소연산자, 피연산자의 주소 내용
- *: 내용연산자, 피연산자의 내용
// 산술 연산
#include <stdio.h>
int main(){
int x,y;
x=10;
y=3;
printf("x+y=%d\n",x+y);
printf("x/y=%d\n",x/y);
printf("x%y=%d\n",x%y);
printf("y%x=%d\n",y%x);
}
#include <stdio.h>
#define KILO 1.609
int main(){
float miles, kms;
printf("\t input(miles) ==>");
scanf("%f", &miles);
kms = KILO*miles;
printf("\t %f mile=%f km \n", miles, kms);
}
//입력된 정수의 짝수 홀수 판별
#include <stdio.h>
int main(){
int i;
printf("정수를 입력하세요: ");
scanf("%d", &i);
printf("%d \n", (i%2)? 1: 0); //짝수면 0 홀수면 1 출력
}
//1부터 100까지의 정수에서 2와 3의 공배수 구하기
#include <stdio.h>
int main(){
int i;
for(i=1; i<=100; ++i)
if(i%2==0 && i%3 == 0)
printf("%d\t", i);
}
//비트연산자를 이용한 곱셈과 나눗셈 연산 프로그램
#include <stdio.h>
int main(){
int result;
printf("4 x 8 / 16 = \n");
result = 3;
result = result<<3; //2진수에서 3칸 좌로 시프트
printf("%d \n", result);
result = result>>4; //2진수에서 4칸 우로 시프트
printf("%d \n", result);
}
//4 x 8 / 16 =
//24
//1
반응형
'프로그래밍 > C 프로그래밍' 카테고리의 다른 글
[C언어] 배열 (0) | 2024.06.04 |
---|---|
[C언어] 함수 (0) | 2024.06.04 |
[C언어] 선택제어문/반복제어문 (0) | 2024.06.04 |
[C] 선행처리기 매크로 (0) | 2024.06.04 |
[C언어] C언어의 개요 (0) | 2024.02.21 |