반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AI생성함
- ImportError: cannot import name 'Mapping' from 'collections'
- dice loss
- tesorboard
- 공개키 docker
- OPIC 당일치기
- 일기 #다짐
- cross entoropy
- gpt excel 사용
- focal loss
- OPIC 번개
- reinforced learning
- docker 환경 문제
- 강화학습 간단 정리
- OPIC 하루 전
- whisper jax
- 빅데이터 분석기사 #빅분기실기 #데이터마님 # 빅분기실기준비
- OPIC 시험 전날
- AI모델
- ImportError: cannot import name 'Mapping' from 'collections' tensrorbaord
- ollama
- OPIC 하루전 시작
- 인간종말
- tersorboard mapping
- tensorboard 에러
- DDPG
- 음성전처리 #음성처리 #python 음성추출 # python 음성 추출 #moviepy
- tensorboard html5
- OPIC 오늘 시작
- 엑셀에 ollama
Archives
- Today
- Total
Moonie
06 – sizeof 연산자와 형변환 본문
반응형
인프런 C 와 C++ 을 동시에 배워보자 - 두들낙서의 C/C++ 의 강의를 기반으로 작성되었습니다.
ex1.cpp
sizeof(x): x의 크기를 알려줌
x: 형 (int, float, ...)
변수 이름
#include <stdio.h>
int main() {
printf("%d %d %d %d\n",sizeof(int), sizeof(char),sizeof(float),sizeof(double));
int a; char b; float c; double d;
printf("%d %d %d %d \n ", sizeof(a),sizeof(b),sizeof(c),sizeof(d));
}
실행을 하면 다음과 같은 결과가 나온다
ex2.cpp
우선 기존 ex1.cpp은 빌드에서 제외 설정을 하여준다.
#include <stdio.h>
int main() {
int a = 3.14;
double b = 10;
printf("%d %f \n", a,b);
}
int 형에 저장이 된다면 3 으로
실수는 정수형 변수에 담을 수 없다. 담는다변 변한다.
정수를 실수형 변수에 담을 수 있다.
#include <stdio.h>
int main(){
int math = 90, korean =95, english =96;
int sum = math +korean + english;
double avg = sum /3 ;
printf("%f\n" , avg); // 93.66667
}
코드를 실행하면 93.0000000의 결과가 나타난다.
정수 / 정수 = 정수 가 나타난다. 즉 int a = 5/3; 과 같다.
실수/ 정수 = 실수
실수 /실수 = 실수
더 큰 쪽의 자료형을 기준으로 진행된다.
이를 고치려면 두가지 방법이 존재한다.
#include <stdio.h>
int main(){
int math = 90, korean =95, english =96;
double sum = math +korean + english;
double avg = sum /3 ;
printf("%f\n" , avg); // 93.66667
}
결과 : 93.666667
#include <stdio.h>
int main(){
int math = 90, korean =95, english =96;
int sum = math +korean + english;
double avg = (double)sum /3 ;
printf("%f\n" , avg); // 93.66667
}
(double)을 사용함으로써 실수로 변환함
결과 : 93.666667
빼기(-), 곱하기(*)도 똑같은 원리가 적용
정수 + 정수 = 정수
정수 + 실수 = 실수
실수 + 실수 = 실수
반응형
'공부 > C언어' 카테고리의 다른 글
10 - 변수로 연산하기 1 (0) | 2022.01.08 |
---|---|
08. char 형과 ASCII 코드 (0) | 2022.01.06 |
07 - 입력 받기 (0) | 2022.01.06 |
자료형 (0) | 2021.12.29 |
VScode 에서 C++ 환경 설정하기 (0) | 2021.12.28 |
Comments