변수의 type을 알수 있나요?

moonzoo의 이미지

예를 들어 코드중에 x 라는 변수가 있을 경우

이 변수 x 의 type이 무엇인지를 알아낼수 있는 방법이 있나요?

선언이나 정의부분을 보라~ 라는것 말고요 --;

x라는 변수에 대해 직접 알아 낼수 있는 방법이 있는지 궁금합니다..

winner의 이미지

실행시간 도중 찾고, C 를 말하시는 거라면 없습니다.

객체지향언어나 interpreter 들은 지원합니다.

cdpark의 이미지

gcc에서라면 변수 x의 형은 typeof(x) 형입니다.

simpid의 이미지

cdpark wrote:
gcc에서라면 변수 x의 형은 typeof(x) 형입니다.

헉~ 이런것도 있었나요?
전 처음봅니다.

VC에는 찾아봐도 없는데...
C에서 typeof 란 키워드 지원하는것 본적 없는것 같습니다.

ㅡ,.ㅡ;;의 이미지

moonzoo wrote:
예를 들어 코드중에 x 라는 변수가 있을 경우

이 변수 x 의 type이 무엇인지를 알아낼수 있는 방법이 있나요?

선언이나 정의부분을 보라~ 라는것 말고요 --;

x라는 변수에 대해 직접 알아 낼수 있는 방법이 있는지 궁금합니다..

그걸알아내서 어디에다 쓸지 궁금합니다. ㅡ,.ㅡ;;

설마 있다해도 아마 전처리에서 알아낼거 같은데요.. 전처리는 컴파일러가

컴파일하기 전에 찾아주는거니까.. 님이 찾기 귀찮아서 그런것이 아니라면..

크게 사용될일이 없을듯한데..ㅡ,.ㅡ;


----------------------------------------------------------------------------

전웅의 이미지

simpid wrote:
cdpark wrote:
gcc에서라면 변수 x의 형은 typeof(x) 형입니다.

헉~ 이런것도 있었나요?
전 처음봅니다.

VC에는 찾아봐도 없는데...
C에서 typeof 란 키워드 지원하는것 본적 없는것 같습니다.

오래된 gcc extension 입니다. 1989년 표준에서부터 배제된 불운한
기술입니다.

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

cskblue의 이미지

저도 몇일전에 잠깐 궁금했었거든요..

The C Programing Language에 나온 예젠데 궁금하네요

6.8장 유니온 설명에서

일단 선언하고

union u_tag{
    int    ival;
    float  fval;
    char  *sval;
} u;

책 설명부분
    만약 변수 utype이 현재 기억된 u의 형을 추적하는 일에 사용된다면 다음과같은 프로그램도 가능하다.
라고 써있고 다음 코드가 나오는데
if (utype == INT)
    printf("%d\n", u.ival);
else if (utype == FLOAT)
    printf("%f\n", u.fval);
else if (utype == STRING)
    printf("%s\n", u.sval);
else 
    printf("bad type %d in utype\n", utype);

utype == INT 이부분들이 어떻게 가능할까 생각이 안나던데요
궁금합니다.[/]
sj316의 이미지

삭제하기가 없네요...

yamainu의 이미지

cskblue wrote:

utype == INT 이부분들이 어떻게 가능할까 생각이 안나던데요

느낌상....
#define INT 1
.
.

struct TEST
{
int utype;
union u_tag{
    int    ival;
    float  fval;
    char  *sval;
} u;
} 

Programmers never die: They just GOSUB without RETURN.

cskblue의 이미지

감사.. 간단한 거였네요;;
이런식으로 쓰는거였군요.

#define INT     1
#define FLOAT   2

struct
{
    int utype;
    union u_tag {
        int ival;
        float fval;
        char *sval;
    }u;
} s1;

int main()
{
    s1.u.ival =4; s1.utype=1;

    if (s1.utype == INT)
        printf("%d\n",s1.u.ival);
    else 
    ....
    return 0;
}

moonzoo wrote:
x라는 변수에 대해 직접 알아 낼수 있는 방법이 있는지 궁금합니다

원래 질문은 유니온이 아니였는데 제가 괜히 헛다리짚기를 해서
moonzoo님께 죄송합니다;;; 수고..

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.