#define number 10 이와 같은 선언을 스트링으로 이용하고 싶습

beta의 이미지

안녕하세요. 작업좀 하다가 궁금한게 있어서 이렇게 질문 드립니다.

#define number 10
#define str "header" number "footer"

전처리후 str "header 10 footer" 와 같이 된 문자열을 사용하고 싶습니다.

#define N2S ?????????????? <--- 어떻게 정의해야 하나요?
#define "header" N2S(number) "footer"

어떻게 해야 하나요?

꾸벅.
c (ansi로 코딩중이지만) gcc 에서 혹시 사용할수 있는 방법이 혹시 있나요?

bugiii의 이미지

#define STRIZE( num ) #num

코퍼스의 이미지

#define K_FUN(a, b) a ## b

A few Good Man

beta의 이미지

      1 #include <stdio.h>
      2
      3 #define STRIZE( num ) #num
      4
      5 #define DEF_NUM 10
      6 #define DEF_STR "header " STRIZE( DEF_NUM ) " footer"
      7
      8 int main(void)
      9 {
     10     fprintf( stderr, "DEF_STR = %s\n\r", DEF_STR ) ;
     11
     12     exit(0);
     13 }
     14

[beta@eLinux _test]$ ./preproc
DEF_STR = header DEF_NUM footer
[beta@eLinux _test]$

bugiii wrote:
#define STRIZE( num ) #num

발 담갔다. 이제 익숙해 지는길만이..

beta의 이미지

1 #include <stdio.h>
2
3 #define K_FUN( a, b ) a##b
4
5 int main(void)
6 {
7 fprintf( stderr, "DEF_STR = %s\n\r", K_FUN("header", 10)) ;
8
9 exit(0);
10 }
11

"header"10 으로 봐서 컴파일에러냅니다.

꾸벅.

코퍼스 wrote:
#define K_FUN(a, b) a ## b

발 담갔다. 이제 익숙해 지는길만이..

bugiii의 이미지

아.. 숫자를 매크로로 사용하신다구요.
그렇다면 다음과 같이 하시면 됩니다.
한번 더 싸시면 되죠. 이 팁은 __LINE__ 에도 유용합니다. 근데 VC 6.0 에서는 __LINE__ 처리가 좀 이상한지 va_ 뭔가인가 하는 황당한게 붙어서 쓸모없지만 gcc 에서는 괜찮습니다. -Z? 어떤 옵션이 영향을 줍니다. (제가 기억력이 좀...)

#define _STRIZE( a )    #a
#define STRIZE( a )     _STRIZE( a )
elecguy의 이미지

#define ToStr(a) # a
ToStr(30a) --> "30a"

#define ToMerge(a, b) a ## b
ToMerge(a, 30) --> #define a30

이렇게 됩니다..
원하는 결과를 얻을려면..
#define ToMStr(a,b) #(a##b)
이렇게 해야될 것 같은데 괄호를 전처리기가 어떻게 처리할지 모르겠네요..
테스트해보시길..

폐인, 노가다 그 끝은..?

beta의 이미지

신경써 주셔서 감사합니다.

bugiii wrote:
아.. 숫자를 매크로로 사용하신다구요.
그렇다면 다음과 같이 하시면 됩니다.
한번 더 싸시면 되죠. 이 팁은 __LINE__ 에도 유용합니다. 근데 VC 6.0 에서는 __LINE__ 처리가 좀 이상한지 va_ 뭔가인가 하는 황당한게 붙어서 쓸모없지만 gcc 에서는 괜찮습니다. -Z? 어떤 옵션이 영향을 줍니다. (제가 기억력이 좀...)

#define _STRIZE( a )    #a
#define STRIZE( a )     _STRIZE( a )

발 담갔다. 이제 익숙해 지는길만이..

beta의 이미지

신경 써 주셔서 감사합니다.

elecguy wrote:
#define ToStr(a) # a
ToStr(30a) --> "30a"

#define ToMerge(a, b) a ## b
ToMerge(a, 30) --> #define a30

이렇게 됩니다..
원하는 결과를 얻을려면..
#define ToMStr(a,b) #(a##b)
이렇게 해야될 것 같은데 괄호를 전처리기가 어떻게 처리할지 모르겠네요..
테스트해보시길..

발 담갔다. 이제 익숙해 지는길만이..

댓글 달기

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