C#교과서 마스터하기 2. 변수

min seung moon·2021년 7월 8일
1

C#

목록 보기
2/54

https://www.youtube.com/watch?v=PrT9ZBkFJiU&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=10

1. 변수(Variable)

  • 프로그램에서 사용할 데이터를 임시로 저장해 놓는 그릇
  • Type name; 형태
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            // type name;
            char character;
            string str;
            int num;
            float floats;
            double doubles;
        }
    }
}

2. 리터럴 사용

  • 리터럴은 소스 코드의 고정된 값을 대표하는 용어
  • 거의 모든 프로그래밍 언어는 정수, 부동소수점 숫자, 문자열, 불린 자료형과 같은 용어를 가지고 있다
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine(12345); // 정수 리터럴
            WriteLine(12.345); // 실수 리터럴
            WriteLine('1'); // 문자 리터럴
            WriteLine("12345"); // 문자열 리터럴
            WriteLine(true); // boolean 리터럴
        }
    }
}

3. 변수를 만들어 값 저장 후 사용하기

  • 정수형 변수 num을 선언한 후 값을 대입하고 출력해보기
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            num = 10;
            WriteLine("{0}", num);
        }
    }
}

4. 변수 선언과 동시에 초기화하기

using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            char character = 'a';
            string str = "abc";
            int num = 10;
            float floats = 1.1f;
            double doubles = 1.1;
        }
    }
}

5. 형식이 같은 변수 여러 개를 한번에 선언하기

  • ","로 동일한 타입의 변수를 여러개 지정 가능
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 10, num2 = 5, num3 = 7;
            WriteLine(num1);
            WriteLine(num2);
            WriteLine(num3);
        }
    }
}

6. 상수 사용하기

  • const 키워드 사용
    • const type name = value;
    • 상수는 선언과 동시에 값을 초기화 해준다
  • 변하지 않는 변수, 읽기 전용 변수
  • 대문자로 작성
profile
아직까지는 코린이!

0개의 댓글