Swift 기본 문법 정리


1. 변수(Variable) 와 상수(Constant)

var variable_name = inital_value

let constant_name = constant_value

* 상수는 값이 변경 될 수 없음

* 안정성을 위해서는 상수를 쓸 것을 권장함

* 변수/상수 이름으로는 공백, 연산자, 화살표 등을 제외한 왠만한 Unicode 문자는 다 가능함

* 변수/상수 이름을 숫자로 시작 할 수 없음


2. 자료형(Data types)

String / Float / Int / Bool / Double / Character

var name: String = "홍길동"

var age: Int = 20

* 자료형을 입력하지 않으면 Compiler가 값을 바탕으로 자료형을 추론함


Range / ClosedRange

let underFive = 0.0..<5.0 // Range

let lowercase = "a".."z" // ClosedRange


3. 배열(Array) 과 Dictionary

Dictionary는 C 언어에는 없는 Data structure 인데, Array와 비슷함

var names: [String] = ["홍길동", "김철수", "신짱구"]

var smartphones: [String: String] = ["삼성": "갤럭시", "애플": "아이폰"]

* 변수/상수 모두 배열/Dictionary로 선언 가능

* 초기 값을 공백으로 빈 배열/Dictionary 선언 가능

* 빈 Dictionary는 대괄호 안에 :(Colon) 넣어줘야함

* 생성자(Initializer) 호출을 통해서도 빈 배열/Dictionary 선언 가능


4. 조건문과 반복문

if / else if / else

if a >= 3 && a < 5 {

//then

} else if a < 7 {

//then

} else {

//then

}

* 조건문의 조건은 Bool type 만 될 수 있음


switch / case

switch a {

case 1:

//

case 2..<5:

//

case default:

//

}

* case에 범위(Range/ClosedRange)를 지정할 수도 있음


for

for i in 0..<100 {

//

}


for name in names {

print("이름 : \(name)");

}


for (maker, brand) in smartphones {

print("\(maker) 의 smartphone brand는 \(brand) 이다.");

}

* for 문에 배열/Dictionary 를 사용할 수 있음


while

while i < 10 {

//

}


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

Xilinx Vivado 에서 3rd party 혹은 customized IP 를 Import 하고자 하는 경우



Tools - Project Settings


Add IP Repositories in IP tab



'Programming > Verilog' 카테고리의 다른 글

AXI Register Slice  (0) 2020.02.12
Verilog Generate  (2) 2018.07.05
Xilinx bootgen  (0) 2017.06.21
Xilinx SDK disable cache  (0) 2017.06.12
Xilinx Vivado Project가 안 열릴때  (0) 2017.05.30

Error when launching "Vivado"

Launcher time out


위와 같은 Error로 Xilinx vivado가 열리지 않는 경우,

project path에 들어가면 안되는 문자가 들어가 있는 경우 일 수 있다.


path에 포함되면 Error를 일으키는 문자: 괄호( )



'Programming > Verilog' 카테고리의 다른 글

AXI Register Slice  (0) 2020.02.12
Verilog Generate  (2) 2018.07.05
Xilinx bootgen  (0) 2017.06.21
Xilinx SDK disable cache  (0) 2017.06.12
Xilinx Vivado IP import  (0) 2017.05.31

+ Recent posts