char 대신에 int8_t 를 사용할 수 없는건지요?

study의 이미지

아래 code와 같이, 자료형을 stdint.h를 사용해서 바꿔보았는데,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
 
int32_t main(int32_t argc, int8_t *argv[])
{
   int32_t i;
 
   if (argc < 1)
   {
      printf("Error\n");
   }
 
   for (i = 0 ; i < argc ; i++)
   {
      printf("argv[%d] = %s\n", i, argv[i]);
   }
 
   return 0;
}

컴파일하면 다음과 같은 warning이 나오네요

$ gcc -W -Wall main.c
main.c:6:9: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
 int32_t main(int32_t argc, int8_t *argv[])

어떻게하면 이 warning을 없앨 수 있을까요?

익명 사용자의 이미지

두 가지 방법이 있습니다.

1. int main(int argc, char *argv[]) 쓰세요. 권장.

2. 커맨드 옵션 -Wno-main 를 지정해서 해당 warning을 suppress하면 됩니다.

signed/unsigned를 지정하지 않으면 자동으로 signed가 되는 Integer types들과 달리, char는 독특한 정의를 가지고 있습니다.

(1) charsigned char 혹은 unsigned char 중 하나와 동등(equivalent)한데, 둘 중 어느 것과 동등한지는 implementation-defined 입니다.

=> 즉, 구현에 따라 char에 부호가 있을 수도, 없을 수도 있습니다.

(2) charsigned charunsigned char 중 어느 것과도 compatible하지 않습니다.

반면에 int8_tuint8_t는 명확하게 부호 있는/없는 32비트 정수형으로 typedef 되기 때문에 (보통은 각각 signed charunsigned char 입니다). 둘 중 어느 것도 char와 compatible하지 않습니다.

=> 어느 쪽을 사용하더라도 char를 대신할 수는 없다는 뜻입니다.

======

물론 이건 타입에 대한 논의일 뿐이고 런타임에서 이게 문제가 될 만한 상황을 찾기는 쉽지 않겠습니다.

warning 꺼버리고 그냥 진행하면 어지간해선 문제 없을지도 몰라요.

하지만 -Wall 줘놓고 저런 식으로 코딩을 하면서 아무 경고도 안 뜨길 바라는 건 과욕이죠.

======

애초에 왜 int main(int argc, char *argv[]) 놔두고 그렇게 코딩을 하세요?

꼭 그렇게 일부러 이식성 없는 프로그래밍을 해야 되겠습니까?

익명 사용자의 이미지

명확하게 부호 있는/없는 32비트 정수형 => 명확하게 부호 있는/없는 8비트 정수형

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.