Floating Point 방식의 변수를 Bit 그대로 출력하는 방법


1. Union 활용

Union은 같은 메모리 주소에 여러가지 형태로 값을 저장하는 변수이다.

union{

float a;

int b;

};

a(float)에 값 저장 후 b(int) 출력


또는 typedef를 활용하여 아래와 같이 구성하면 된다.

typedef union{

float float_t;

int int_t;

}int_float_t;


int_float_t a;

a.float_t = 3.14;

printf("%x\n", a.int_t);


2. Pointer 활용

float a = 1.2345; //float value

int* b = (int*)&a;

*b를 출력

'Programming > C Language' 카테고리의 다른 글

static  (0) 2018.07.13
가변 인자(variable arguments) stdarg.h  (0) 2017.12.11
extern  (0) 2017.06.21
static  (0) 2017.06.20
전처리기 명령어  (0) 2017.06.07

#define, #include 등 C/C++ 에서 사용되는 #으로 시작되는 것들은 전처리기 명령어 혹은 전처리기 지시문(preprocessing directives)라 한다.

전처리기 명령어를 통해 컴파일 과정을 Control 할 수 있다.

전처리기 명령어는 반드시 독립된 Line 의 시작에 위치해야 한다.


#include

일반적으로 가장 많이 쓰이는 전처리기 명령어인 #include는 헤더파일을 포함시키기 위해 사용된다.

#include <stdio.h>

#include "my_header.h"

* 꺾쇠(<>)로 묶인 header 파일은 일반적인 header 파일 디렉토리에 있는 헤더파일, 큰따옴표("")로 묶인 header 파일은 사용자 지정 경로에 있는 헤더파일


#define

#define 은 자주 쓰이는 숫자를 문자로 정의하여, 다른 사람들이 알아보기 쉽게 만들어 주는 역할을 한다. 

#define PI 3.14

* #undef 로 정의 한 것을 풀어 줄 수 있음


#ifdef / #ifndef / #endif 등을 함께 활용하여, 컴파일 시 조건을 변경하기 용이하게 코드를 작성하기도 한다.

일반적으로, 헤더파일의 시작과 끝에 작성하여, 헤더파일을 여러곳에서 포함(include)시키더라도, 헤더파일을 중복으로 참조 방지(include guard / macro guard / header guard)의 방법으로 사용된다.

/****header file name is A.h******/

#ifndef A_H_

#define A_H_

// header file contents

#endif


#define 을 이용하여 매크로 함수를 만들 수 있다.

#define SQUARE(x) x*x



#if / #elif / #else / #endif

#if / #elif / #else / #endif 를 통해 compile 의 조건을 Control 한다.


#pragma

#pragma 는 전처리기 명령어를 정의하는 전처리기 명령어 이다. 

일반적으로, 아래와 같이 once 라는 전처리기 명령어를 정의해 헤더파일 중복 참조를 방지한다.

/*****header file *****/

#pragma once

// header file contents

* 위의 #ifndef / #define / #endif 를 이용해 헤더파일 중복 참조를 방지 한 것과 같은 역할을 함.

* pragma 를 support 하지 않는 compiler의 경우 무시됨

'Programming > C Language' 카테고리의 다른 글

static  (0) 2018.07.13
가변 인자(variable arguments) stdarg.h  (0) 2017.12.11
extern  (0) 2017.06.21
static  (0) 2017.06.20
Floating Point Bit 출력 방법  (0) 2017.06.08

It was an honor and a privilege.

Throw it away !!!

'English > Good Morning Pops' 카테고리의 다른 글

Jyly 11th, 2017  (0) 2017.07.20
July 1st, 2017  (0) 2017.07.09
June 23th, 2017  (0) 2017.06.28
June 19th, 2017  (0) 2017.06.25
June 7th, 2017  (0) 2017.06.12

+ Recent posts