데이터 형식

Fruit·2023년 3월 28일

✨ Hello C#!

목록 보기
3/34
post-thumbnail

🌸 숫자 데이터 형식

  • 원하는 자리에 자릿수 구분자 _ 를 사용할 수 있다.
using System;

namespace IntegralTypes
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int a = -1000_0000;		// 원하는 자리에 자릿수 구분자 _ 사용 가능
            uint b = 3_0000_0000;
            Console.WriteLine($"a={a}, b={b}");
        }
    }
}

[실행 결과]
a=-10000000, b=300000000



✔️ 2진수, 10진수, 16진수

진수접두사
2진수0b
10진수없음
16진수0x
using System;

namespace IntegralTypes
{
    class MainApp
    {
        static void Main(string[] args)
        {
            byte a = 240;				// 10진수 접두사 X
            byte b = 0b1111_0000;		// 2진수 접두사: 0b
            byte c = 0XF0;				// 16진수 접두사: 0X, 0x
            uint d = 0x1234_abcd;

            Console.WriteLine($"10진수 a={a}, 2진수 b={b}");
            Console.WriteLine($"16진수 c={c}, d={d}");
        }
    }
}

[실행 결과]
10진수 a=240, 2진수 b=240
16진수 c=240, d=305441741



✔️ 실수 형식

데이터 형식접미사
floatf
double없음
decimalm
using System;

namespace IntegralTypes
{
    class MainApp
    {
        static void Main(string[] args)
        {
            float a = 3.1415_9265_3589_7932_3846_2643_3832_79f;		// 접미사 f
            double b = 3.1415_9265_3589_7932_3846_2643_3832_79;
            decimal c = 3.1415_9265_3589_7932_3846_2643_3832_79m;	// 접미사 m

            Console.WriteLine($"a={a}\nb={b}\nc={c}");
        }
    }
}

[실행 결과]
a=3.1415927
b=3.141592653589793
c=3.1415926535897932384626433833



🌸 문자, 문자열 형식

  • char: 개별 문자, 작은 따옴표 ' '
  • string: 문자열, 큰 따옴표 " "
using System;

namespace IntegralTypes
{
    class MainApp
    {
        static void Main(string[] args)
        {
            char a = 'H';
            char b = 'e';
            char c = 'l';
            char d = 'l';
            char e = 'o';

            string f = " World!";

            Console.Write(a);
            Console.Write(b);
            Console.Write(c);
            Console.Write(d);
            Console.Write(e);
            Console.Write(f);
        }
    }
}

[실행 결과]
Hello World!
profile
🌼인생 참 🌻꽃🌻 같다🌼

0개의 댓글