Language/C

✨C언어 기초/기본형

Dexter_- 2016. 9. 2. 11:54
728x90
반응형

 

 

 

C언어 Chapter 1.

 

C언어 = 순수 C 언어 + 전처리 언어(#) 합쳐진 언어이다.

 

 

 

메모장과 명령프롬프트를 이용한 컴파일

  • 새로만들기: notepad main.c
  • 컴파일 : cl main.c
  • 실행 : main

 

 

C언어 기본형

// #(전처리언어) include(포함) <stdio.h>(파일을찾음) " " (현재장소검색)
#include <stdio.h>  	
   
// int(정수형) or void ontry point = main함수 () 인수리스트
int main()         	
{			// { } 블럭
	return 0;	// return (반환)           
}
   
int (4 byte) A;	// 크기와 변수의 선언(선언부), int (자료형 date type) A (이름) : 선언부
   
ex)
int number;	// 정수형
float averge;	// 실수형
char ch;		// 문자형

 

 

 

예 제

🔔 예) 1-3 변수의 사용


#include <stdio.h>

int main()
{
    int number;	/* int형 변수 number의 선언 */
    
    number = 3;	// number 에 3을 넣음
    printf("The number is %d.\n", number);	// number=3 을 %d(decimal 십진수) 자리에 넣음

    return 0;
}

 

결과 :

 

 

🔔 예) 1-4 산술계산


#include <stdio.h>

void main()
{
    int length;	// 길이
    int area;	// 넓이

    length = 3;			// 변수 길이를 3으로 설정
    area =  length * length;	// 길이 * 길이 = 넓이
    printf("The area of rectangular is %d square meter.\n", area);	// 정사각형 넓이는 길이 제곱이다...

    return 0;
}

 

결과 :

 

 

🔔 예) 1-6 원의 둘레 계산


// 원의 둘레를 계산하는 프로그램
#include <stdio.h>
#define PI 3.1416 // 상수 3.1416을 PI로 정의 define(정의하다)

void main()
{
    int radius;  		// 원의 반지름
    float circumference;	// 원의 둘레
    radius = 5;
    printf("The radius of circle is %d m\n", radius);
    circumference = (radius + radius) * PI;  // PI 는 3.1416
    printf("the circumference of circle is %7.2f m.\n", circumference);
    /* %7.2f 에서 7은 전체 최소 자리수 .2는 소수점자리 계수이다. */

    return 0;
}

 

결과 :

 

 

🔔 예) 컴퓨터가 음수를 표현하는 방식은 2의 보수를 취하여 덧셈한다.

#include <stdio.h>

int main()
{
    char cNumber;	// char(1byte) 데이터 범위는 -128 ~ 127 이다.

    cNumber = 128;
    printf("%d\n", cNumber);	// -128
    cNumber = cNumber +1;
    printf("%d\n", cNumber);	// -127
    cNumber = cNumber -2;
    printf("%d\n", cNumber);	// 127

    return 0;
}

 

결과 :

 

 

Magnitube 의 양수와 음수의 표현방식

MSB

(부호)자리에 0 이면 양의정수 1이면 음의정수. 단, 연산불가.

 

음수전환 2의 보수 만드는법

검산) 정수 +5와 2의보수로 바꾼 5는 -5라 가정하고 더해보면 결과는 0 이다.

1 은 묻지도 따지지도 않고 버린다.

 

 

 

 

 

728x90
반응형