program tip

C가 강력하게 입력 되었습니까?

radiobox 2020. 10. 18. 09:14
반응형

C가 강력하게 입력 되었습니까?


Wikipedia 를 인용하려면 :

많은 종류의 암시 적 변환을 지원하는 일반적으로 사용되는 두 가지 언어는 C와 C ++이며, 종종 약한 유형의 언어라고 주장됩니다. 그러나 다른 사람들은 이러한 언어가 서로 다른 유형의 피연산자가 혼합 될 수있는 방법에 대해 충분한 제한을두고 있으며 둘은 강력한 유형의 언어로 간주되어야한다고 주장합니다.

더 확실한 답이 있습니까?


"강하게 입력"및 "약하게 입력"은 널리 동의 된 기술적 의미가없는 용어입니다. 의미가 잘 정의 된 용어는 다음과 같습니다.

  • 동적 유형 은 유형이 런타임에 값에 첨부됨을 의미하며 다른 유형의 값을 혼합하려고하면 "런타임 유형 오류"가 발생할 수 있습니다. 예를 들어 Scheme에서 (+ 1 #t)이것을 작성하여 true에 추가하려고 하면 오류가 발생합니다. 문제가되는 코드를 실행하려는 경우에만 오류가 발생합니다.

  • 정적으로 형식화 됨은 컴파일 시간에 형식이 확인되고 정적 형식이없는 프로그램은 컴파일러에서 거부됨을 의미합니다. 예를 들어, ML에서를 작성하여 true에 1을 추가하려고 1 + true하면 프로그램이 (아마도 비밀스러운) 오류 메시지와 함께 거부됩니다. 코드가 실행되지 않더라도 항상 오류가 발생합니다.

유연성을 중시하는 정도와 런타임 오류에 대해 걱정하는 정도에 따라 사람들마다 다른 시스템을 선호합니다.

때때로 "강력하게 입력 됨"은 "정적으로 입력 됨"을 의미하기 위해 느슨하게 사용되고 "약하게 입력 됨"은 "동적으로 입력 됨"을 의미하기 위해 잘못 사용됩니다. "강하게 타이핑 된"이라는 용어의 더 나은 사용은 "타입 시스템을 우회하거나 전복 할 수 없다"는 반면, "약하게 타이핑 된"은 "타입 시스템에 허점이있다"는 의미입니다. 역으로 정적 유형 시스템을 사용하는 대부분의 언어에는 허점이있는 반면 동적 유형 시스템을 사용하는 많은 언어에는 허점이 없습니다.

이러한 용어 중 어느 것도 언어에서 사용할 수있는 암시 적 변환의 수와 어떤 식 으로든 연결되지 않습니다.

프로그래밍 언어에 대해 정확하게 이야기하려면 "강력한 유형"및 "약한 유형"이라는 용어를 피하는 것이 가장 좋습니다. C는 정적으로 입력되었지만 많은 허점이있는 언어라고 말하고 싶습니다. 한 가지 허점은 모든 포인터 유형을 다른 포인터 유형으로 자유롭게 캐스팅 할 수 있다는 것입니다. 또한 문제의 각 유형에 대해 하나씩 두 개의 멤버가있는 C 공용체를 선언하여 선택한 두 유형 사이에 허점을 만들 수도 있습니다.

나는 why-interpreted-langs-are-mostly-ducktyped-while-compiled-have-strong-typing 에서 정적 및 동적 타이핑에 대해 더 많이 썼습니다 .


모든 언어를 '약하게'또는 '강하게'입력 한 것으로 분류하기는 어렵습니다. 그러나 다른 언어에 비해 C는 상당히 강력한 유형입니다. 모든 객체에는 컴파일 타임 유형이 있으며 컴파일러는 해당 유형이 허용하지 않는 객체로 무언가를 수행하는 경우 (큰 소리로) 알려줍니다. 예를 들어, 잘못된 유형의 매개 변수를 사용하여 함수를 호출하거나 존재하지 않는 구조체 / 유니온 멤버에 액세스하는 등의 작업을 할 수 없습니다.

그러나 몇 가지 약점이 있습니다. 한 가지 주요 약점은 타입 캐스트입니다. 본질적으로 객체의 유형을 다루게 될 것이며 컴파일러는 조용해야한다고 말합니다. void*또한 또 다른 약점입니다. 알 수없는 유형에 대한 일반적인 포인터이며이를 사용할 때 올바른 일을하고 있는지 특히주의해야합니다. 컴파일러는 대부분의 void*. void*또한 캐스트가없는 모든 유형에 대한 포인터로 변환 될 수 있습니다 (C ++가 아닌 C에서만). 이는 또 다른 약점입니다.


C는 컴파일러 오류없이 캐스트를 통해 모든 유형을 다른 유형으로 변환 할 수 있기 때문에 약한 유형으로 간주됩니다. 여기 에서 문제에 대한 자세한 내용을 읽을 수 있습니다 .


이것에 대한 문헌은 명확하지 않습니다. 강력한 타이핑은 예 / 아니요가 아니라 다양한 정도의 강력한 타이핑이 있다고 생각합니다.

프로그래밍 언어에는 프로그램을 실행하는 방법에 대한 사양이 있습니다. 때때로 특정 프로그램으로 실행하는 방법이 명확하지 않습니다. 예를 들어, 숫자에서 문자열을 빼려고하는 프로그램이 있습니다. 또는 0으로 나누는 프로그램. 이러한 조건을 처리하는 방법에는 여러 가지가 있습니다. 일부 언어에는 이러한 오류를 처리하기위한 규칙이 있습니다 (예 : 예외 발생). 다른 언어에는 이러한 상황을 처리하는 규칙이 없습니다. 이러한 언어에는 일반적으로 지정되지 않은 동작을 유발하는 프로그램 컴파일을 방지하는 유형 시스템이 있습니다. 또한 지정되지 않은 동작이 있고 컴파일 타임에 이러한 오류를 방지 할 유형 시스템이없는 언어도 있습니다 (지정되지 않은 동작을 수행하는 프로그램을 작성하면 미사일을 발사 할 수 있음).

그래서:

모든 경우 (문자열에 숫자 추가 등)에서 런타임에 발생하는 작업을 지정하는 언어를 동적 유형이라고합니다. 컴파일 타임에 오류가있는 프로그램 실행을 방지하는 언어는 정적으로 입력됩니다. 어떤 일이 발생하는지 지정하지 않고 오류를 방지하는 유형 시스템도없는 언어를 약한 유형이라고합니다.

그렇다면 Java는 정적으로 입력됩니까? 예, 유형 시스템은 숫자에서 문자열을 빼는 것을 허용하지 않기 때문입니다. 아니요, 0으로 나눌 수 있기 때문입니다. 타입 시스템을 사용하면 컴파일 타임에 0으로 나누는 것을 방지 할 수 있습니다. 예를 들어 0이 될 수없는 숫자 유형 (예 : NonZeroInt)을 만들고이 유형의 숫자로만 나눌 수 있습니다.

그렇다면 C는 강력하게 입력됩니까 아니면 약하게 입력됩니까? C는 유형 시스템이 일부 유형 오류를 허용하지 않기 때문에 강력하게 입력됩니다. 그러나 어떤 일이 발생하는지 정의되지 않은 다른 경우에는 약하게 입력됩니다 (유형 시스템이 사용자를 보호하지 않음).


C는 Javascript보다 더 강력하고 Ada보다 덜 강력합니다.

나는 그것이 연속체의 강하게 타이핑 된쪽에 더 많이 떨어진다고 말하고 싶습니다. 그러나 다른 사람은 동의하지 않을 수 있습니다 (그들이 틀렸더라도).

그것은 어떻게 결정적입니까?


C는 정적으로 입력 된 것으로 간주됩니다 (int에서 float로 변수를 변경할 수 없음). 변수가 선언되면 그런 식으로 고정됩니다.

그러나 유형이 플립 플롭 될 수 있기 때문에 약한 유형으로 간주됩니다.

0은 무엇입니까? '\ 0', FALSE, 0.0 등.

많은 언어에서 조건은 부울 표현식의 부울 값만 가져 오기 때문에 IF (변수)라고 말할 수 없습니다. 이들은 더 강력한 형식입니다. 문자와 정수 사이를 이동할 때도 마찬가지입니다.

기본적으로 c에는 두 가지 주요 단순 데이터 유형, 정수 및 부동 소수점 숫자 (다양한 정밀도)가 있습니다. 그 밖의 모든 부울, 열거 형 (간단하지는 않지만 적합) 등은 그중 하나로 구현됩니다. 문자조차 기본적으로 정수입니다.

문자열 유형, 정의 된 값에만 할당 할 수있는 열거 형 유형, 부울 또는 참 / 거짓을 생성하는 표현식 만 사용할 수있는 부울 유형이있는 다른 언어와 비교하십시오.

그러나 Perl C에 비해 강력한 유형이 있다고 주장 할 수 있습니다. 그래서 이것은 유명한 주장 중 하나입니다 (vi vs emacs, linux vs windows 등). C #은 C보다 강력한 형식입니다. 기본적으로 어느 쪽이든 주장 할 수 있습니다. 그리고 당신의 대답은 아마도 양방향으로 갈 것입니다 :) 또한 일부 교과서 / 웹 페이지는 C가 약하게 입력되었다고 말하고 일부는 C가 강하게 입력되었다고 말합니다. 위키피디아에 가면 C 항목은 "부분적으로 약한 타이핑"이라고 말합니다. 파이썬 C에 비해 약한 유형이라고 말할 수 있습니다. 그래서 Python / C #, C, Perl은 연속체에 있습니다.


여기에 많은 좋은 답변이 있습니다. Real World Haskell 에서 중요한 점을 말씀 드리고 싶습니다 .

많은 언어 커뮤니티가 "강력한 유형"에 대한 자체 정의를 가지고 있다는 것을 아는 것이 유용합니다. 그럼에도 불구하고 우리는 타입 시스템의 강점 개념에 대해 간략하고 폭넓게 이야기 할 것입니다.

(한조각)

The fireworks around type systems have their roots in ordinary English, where people attach notions of value to the words “weak” and “strong”: we usually think of strength as better than weakness. Many more programmers speak plain English than academic jargon, and quite often academics really are throwing brickbats at whatever type system doesn't suit their fancy. The result is often that popular Internet pastime, a flame war.

So, look at the answers about C and C++, but remember that 'strong' and 'weak' do not map to 'good' and 'bad'.


In my opinion, C/C++ are strongly typed. The type of hacks that allow types to be converted (void*) are there because of C's closeness to the machine. In other words, you can call assembler commands from Pascal and manipulate pointers and Pascal is still regarded as a strongly typed language. You can call assembler and C executables from Java through JNI but it doesn't make Java weakly typed.

C just has assembler "embedded" in it with raw pointers and such.


The term strongly typed doesn't have a agreed-upon definition. Therefore, unless you define what you mean by "strongly typed", it is impossible to answer your question.

In my experience, the terms "strongly typed" and "weakly typed" are used exclusively by trolls, because their lack of definitions allows the trolls to redefine them mid-argument to suit their agenda. Other than for starting flamewars, these terms are pretty much useless.

You might also want to take a look at What are the key aspects of a strongly typed language? here on StackOverflow.


According to Dennis Ritchie (creator of C ) and Brian Kernighan , C is not a strongly typed language. Following lines are from the book The C programming language page 3 paragraph 5

C is not a strongly-typed language, but as it has evolved, its type checking has been strengthened.


There is a continuum with multiple parallel avenues between "weakly typed" and "strongly typed", two terms which are not even well defined.

C is statically typed, in that the compiler knows what the declared type of every local variable and struct member is.

Dynamically typed languages might still be strongly typed, if each object has a specific type but there is no way for the compiler to know that type.


c is weakly typed, b is typeless.


What is the reason for your query? The reason I ask is this is sort of a minor difference and your particular usage of "strongly-typed" might need more or less clarification. I would definitely say that Java and other languages have tighter restrictions on implicit type conversation.


It is difficult to provide a concrete answer when there isn't a concrete definition of "strongly typed". I would say that C is strongly typed in that every variable and every expression has a type but weakly typed in that it allows you to change types using casts and to reinterpret the representation of one type as another.


I would say that C is as strongly typed as your compiler / platform dictates. For instance, if you are building on a strict platform, dereferencing a type punned pointer is likely going to break:

void m_free(void **p)
{
        if (*p != NULL) {
                free(*p);
                *p = NULL;
        }
}

....
char *str = strdup("foo");
m_free((void **) &foo);

Now, if you told the compiler to skip strict aliasing, it would be a non issue, but not very portable. So, in that sense, pushing the bounds of the language is possible but probably not the best idea. That goes a step beyond typical casting, i.e. casting int as long and really shows one of the possible pitfalls of void.

So, I would say C is basically strictly typed, but its various compilers presume that the programmer knows best and allows some flexibility. This really depends on the compiler, some would not pick up on that potential oops. So in that sense, the compiler of choice really plays a role when answering the question. What is correct often differs from what your compiler will allow you to get away with.


Not strongly typed.

Consider what the following function prototype tells you about the data types of the arguments:

void func (int n, char ch, ...);

Nothing. So I suggest that strong typing does not apply here.


I'd say it is strongly types as every expression has a type that is not a function of it's value; e.i. it can be known before runtime.

OTOH I'm not sure that is the correct description of strongly typed. The only stronger claim I can see reason for a language to make would be the assurance that you can't subvert the type system at runtime via reinterpret type casts, unions, calling into other languages, pointers, assembly language, etc. Languages like this exist but are so crippled that they seem to not be of much interest to programmers outside of high assurance and academia. As pointed out by someone, to really do that right you start needing to have types like nonZeroInt and whatnot. Yuck.

참고URL : https://stackoverflow.com/questions/430182/is-c-strongly-typed

반응형