Programing

"리터럴"이란 단어는 무엇을 의미합니까?

lottogame 2020. 10. 31. 09:12
반응형

"리터럴"이란 단어는 무엇을 의미합니까?


리터럴 문자열 및 리터럴 값과 같은 컨텍스트에서 사용될 때 "리터럴"이라는 단어는 무엇을 의미합니까?

리터럴 값과 값의 차이점은 무엇입니까?


리터럴은 " 소스 코드 내의 값 나타내는 모든 표기법 "( wikipedia )

(이것을 메모리의 값 참조 하는 식별자 와 대조 하십시오 .)

예 :

  • "hey" (문자열)
  • false (참이나 거짓)
  • 3.14 (실수)
  • [1,2,3] (숫자 목록)
  • (x) => x*x (기능)
  • /^1?$|^(11+?)\1+$/ (정규식)

리터럴이 아닌 것 :

  • std::cout (식별자)
  • foo = 0; (성명서)
  • 1+2 (표현)

리터럴은 소스에 직접 하드 코딩 된 값입니다.

예를 들면 :

string x = "This is a literal";
int y = 2; // so is 2, but not y
int z = y + 4; // y and z are not literals, but 4 is
int a = 1 + 2; // 1 + 2 is not a literal (it is an expression), but 1 and 2 considered separately are literals

일부 리터럴은 특수 구문을 가질 수 있으므로 리터럴이 어떤 유형인지 알 수 있습니다.

//The 'M' in 10000000M means this is a decimal value, rather than int or double.
var accountBalance = 10000000M; 

변수 또는 리소스와 구별되는 점은 컴파일러가 상수로 처리하거나 사용되는 코드로 특정 최적화를 수행 할 수 있다는 것입니다. 변경되지 않을 것이 확실하기 때문입니다.


리터럴은 다음과 같은 명시 적 값에 대한 할당입니다.

int i = 4;  // i is assigned the literal value of '4'
int j = i   // j is assigned the value of i.  Since i is a variable,  
            //it can change and is not a 'literal'

편집 : 지적했듯이 할당 자체는 리터럴의 정의와 관련이 없으며 내 예제에서는 할당을 사용했지만 리터럴을 메서드 등에 전달할 수도 있습니다.


리터럴은 소스 코드에 값을 포함하는 경우입니다 (변수 또는 상수 참조와 반대). 예를 들면 :

int result = a + 5; // a is a variable with a value, 5 is a literal

string name = "Jeff Atwood"; // name is a variable initialized
                             // with the string literal, "Jeff Atwood"

int[] array = new int[] {1, 2, 3}; // C# has array literals (this is actually three
                                   // int literals within an array literal)

리터럴이 물리적 상수와 같은 양을 나타내는 경우 필요한 모든 곳에 동일한 리터럴을 작성하는 대신 이름을 지정하는 것이 좋습니다. 이렇게하면 소스 코드를 읽을 때 숫자가 의미하는 바를 알 수 있습니다 . 이는 일반적으로 값 (어쨌든 변경 될 수 있음)보다 더 중요합니다.

const int maxUsers = 100;
const double gravitationalAcceleration = 9.8;

일반적으로 내가 사용하는 유일한 숫자 리터럴 (위와 같이 상수를 초기화하는 것 외에)은 0 또는 1이며, 루프의 다른 모든 항목을 건너 뛰는 경우 2입니다. 숫자 의미 가 실제 (보통 그렇습니다)보다 중요 하다면 이름을 지정하는 것이 좋습니다.


리터럴 값 값이지만 값을 변수에 저장할 수도 있습니다. 성명에서

string str = "string literal";

문자열 변수 (str)와 문자열 리터럴이 있습니다. 명령문이 실행 된 후 둘 다 동일한 값을 갖습니다.

많은 언어에서 변수와 리터럴 값이 반드시 동일한 유형일 필요는 없습니다. 예를 들면 :

int a = 1.0;

위의 리터럴 값은 부동 소수점 유형입니다. 값은 int 변수에 맞게 컴파일러에 의해 강제됩니다.

다른 예를 들어, C ++ 코드의 첫 번째 줄에서 문자열 리터럴 유형은 실제로 라이브러리 유형이 아닙니다 string. C와의 하위 호환성을 유지하기 위해 C ++의 문자열 리터럴은 char 배열입니다.


빠른 예 :

int my_int_var = 723;

723-이 문자 집합은 리터럴 정수 값을 나타냅니다 .

my_int_var-이 문자 세트는 가변 정수 값을 나타냅니다 .


A literal is when you put it in the code. So a string literal is

string s = "SomeText";

This is as opposed to building the string, or taking it in as a parameter.


Generally when someone uses the word literal they mean the value is decipherable from the code (text) as shown in many of the examples in other posts.

Another common usage is for values that are converted to immediate values in assembly. These are values that are inserted directly into the machine instruction rather than requiring register loads.


I have heard string literals used casually to refer to what the C# specification actually refers to as verbatim string literals. A regular string literal allows for the escaping of certain characters (prefixed by a ), like \t for a tab. A verbatim string literal is prepended by @ and processed verbatim, \ has no special meaning.

//regular
string regular = "\thello world";
//verbatim
string verbatim = @"C:\";
//the regular equivalent of this would be "C:\\"

A literal is "source code representation of data".


Literals are shorthand notation for values in certain types that language considers so important or fundamental that it has dedicated syntax sugar just for them.

Example of types whose values are often represented by literals:

Boolean = { true, false }
Integer = { ..., -2, -1, 0, 1, 2, ... }
Character = { 'a', 'b', ... }
String = { "hello", "world", ... }

Some languages have literals even for function types:

Integer -> Integer = { (x: Integer) => x + 2), (x: Integer) => x/2, ...}

An example of values that are usually not represented with literals would be values of class types:

Car(brand: String, seats: Integer) = { Car("BMW", 5), Car("Ferrari", 2), ...}

Here the value Car("BMW", 5) in the type Car is indeed uniquely denoted using a sequence of symbols, however, the value is not represented using a dedicated shorthand literal, but instead uses general (verbose) notional mechanisms for denoting values of any class type.

The term literal is synonymous with terms value, instance, constant, member, and element of a type, but carries a bit of extra meaning that tells us there is a shorthand for writing it down.

I like to think about literals as Egyptian hieroglyphs as opposed to stringing together characters from an alphabet in order to express a concept.

참고URL : https://stackoverflow.com/questions/485119/what-does-the-word-literal-mean

반응형