고급 매크로 표현식에 사용되는 여섯 가지 테크닉 :: 2007/07/05 13:18


C언어를 다년간 사용한 프로그래머들도 종종 매크로 연산자에 대해서 정확하게 이해하고 있지 못한 경우가 많다. 매크로를 사용하지 않는다고 대답할 수도 있지만, 이미 복잡한 매크로를 사용한 수많은 소스가 있다는 점을 생각해 본다면 분명하게 이해해 두는 것이 정신 건강에 좋을 것이다. 복잡한 매크로 표현 식을 구성하는데 많이 사용되는 방법에 대해서 살펴보자.

첫째, 문자열 리터럴은 합쳐진다. 이는 매크로라기 보다는 C언어의 특징이다. 이 기능을 사용하면 긴 출력 문장을 손쉽게 여러 개의 부분 문자열로 나눌 수 있다. 또한 다음에 소개될 매크로 연산자를 사용할 때의 표현식도 좀 더 풍부하게 구성할 수 있다는 장점이 있다.


위의 코드를 보자. printf 다음에는 총 세 개의 연속된 문자열 리터럴이 나타난다. 이 세 개의 리터럴은 합쳐져서 "이름: %s\n나이: %d\n전화번호: %s\n" 과 동일한 문자열이 된다.

둘째, ## 연산자를 사용해서 토큰을 합성해서 만들어 낼 수 있다. ##은 합치기 연산자 이다. 다음과 같은 기능을 생각해 보자. COUNT(start)라고 선언을 하면 DWORD startCnt; 라는 변수를 선언하는 기능이다. 이를 매크로를 이용해서 만들어 보면 아래와 같다.


셋째, # 연산자는 전달된 인자를 문자열로 변환시킨다. start를 매크로 인자로 전달했다면 "start"가 된다는 말이다. 변수 값을 출력하는 매크로를 생각해 보자. PRINT(start)를 하면 화면에 start = 3과 같은 형태로 출력하고 싶은 경우다. 이럴 땐 아래와 같이 매크로를 만들면 된다. #연산자와 위에서 소개한 문자열 리터럴이 합쳐진다는 점을 이용한 것이다. 좀 꽁수 같이 보인다면 두 번째 방식같이 구성할 수 도 있다.


넷째, #@ 연산자는 전달된 인자를 문자로 변환시킨다. a를 매크로 인자로 전달했다면 'a'를 만들어 주는 것이다. 아래와 같이 전달된 인자에 대한 문자를 생성해주는 매크로를 예로 들 수 있다.


다섯째, 매크로의 내용이 복잡하고 한 줄 이상의 표현식이 필요한 경우엔 주로 do ~ while(0)문을 사용한다. 이렇게 하는 이유는 do ~ while(0)가 하나의 구문으로 해석되고 자체 블록을 가지기 때문이다. 이것을 단순 괄호({, })로 대체하면 안 된다. 중첩 if문에서 에러가 나기 때문이다. 주로 아래와 같이 사용한다.


여섯째, 릴리즈 버전에서는 무시되는 매크로를 구성하는 경우다. 주로 디버깅 출력을 하는 매크로가 여기에 속한다. Visual C++ 6.0에서는 '?' 연산자를 사용해서 쇼트 서킷을 구성하는 방법을 주로 사용했다. 하지만 이 후 출시된 Visual C++에는 __noop이라는 내장 함수를 가지고 있다. 이 함수는 인자를 모두 무시하는 기능을 한다. 따라서 예전에 복잡한 쇼트서킷을 사용했던 함수를 두 번째와 같이 간단하게 구성할 수 있다.



스폰서
글타래

  • 2주간 인기 글
  • 2주간 인기글이 없습니다.
Trackback Address :: http://jiniya.net/tt/trackback/528
  • judi poker

    Tracked from domino 99 | 2018/11/23 18:26 | DEL

    괴짜 프로그래머의 일상사~@@ - 고급 매크로 표현식에 사용되는 여섯 가지 테크닉

  • Relationship Problems

    Tracked from want ex back | 2018/12/07 15:57 | DEL

    괴짜 프로그래머의 일상사~@@ - 고급 매크로 표현식에 사용되는 여섯 가지 테크닉

  • life coaching secret

    Tracked from social life | 2019/03/09 12:04 | DEL

    괴짜 프로그래머의 일상사~@@ - 고급 매크로 표현식에 사용되는 여섯 가지 테크닉

  • Gravatar Image.
    풀리비 | 2007/07/16 13:24 | PERMALINK | EDIT/DEL | REPLY

    좋은 정보 잘 봤습니다.
    C에서 문자열을 위에서처럼 사용할 수 있다는 것은 충격적이네요. ^^; 처음 알았습니다.
    1200페이지가 넘는 C++ 책인 C++PrimerPlus에도 안나오는 멤버 포인터 문법을 알았을때와 같은 충격이...

    • Gravatar Image.
      codewiz | 2007/07/17 17:07 | PERMALINK | EDIT/DEL

      도움이 되셨다니 저도 기분이 좋네요 ^^
      저 속성 때문에 잡기 힘든 에러가 생기기도 합니다.
      printf("%s %s", "a", "b" )
      위 문장이 printf("%s %s" "a", "b" )로 바뀌어도
      정상적으로 컴파일 되죠.

  • Gravatar Image.
    pafrica | 2007/07/22 23:33 | PERMALINK | EDIT/DEL | REPLY

    3등입니다

    정말 좋은글 읽고 갑니다 ^^

    종종 찾아 와서 좋은 가르침 받고 가겠습니다^^~

    • Gravatar Image.
      codewiz | 2007/07/23 17:07 | PERMALINK | EDIT/DEL

      방문해 주셔서 감사합니다.
      앞으로도 좋은 정보 많이 올리도록 하겠습니다.

  • nice blogs.....
    <a href="https://www.kitsonlinetrainings.com/django-training.html">django training</a><br>
    <a href="https://www.kitsonlinetrainings.com/business-analysis-online-training.html">business analysis training</a><br>

  • Gravatar Image.
    ravi | 2019/05/16 22:35 | PERMALINK | EDIT/DEL | REPLY

    Thank you for this nice article.i surely look into this insight and it will help to clear my doubts.share more information.

  • To have more enhanced results and optimized benefits, you are able to take the help of experts making a call at QuickBooks Payroll Support mobile phone number. Well! If you’re not in a position to customize employee payroll in.

  • All the above has a specific use. People working with accounts, transaction, banking transaction need our service. Some people are employing excel sheets for a few calculations. But, this sheet cannot calculate accurately the figures. This becomes one of many primary good reasons for poor cashflow management in lot of businesses. It should be the full time for QuickBooks support help. The traders can’t make money. But, we've been here to aid a forecast.

  • The QuickBooks Payroll has many awesome features that are good enough in terms of small and middle sized business. QuickBooks Payroll also offers a passionate accounting package which include specialized features for accountants also. You can simply all from the QuickBooks Payroll support number for more information details. Let’s see several of your choices that are included with QuickBooks that has made the QuickBooks payroll service exremely popular.

  • QuickBooks Enterprise issues and seeking for a few technical assistance in QuickBooks Enterprise? It’s time and energy to say good bye to everyone your worries and give us a call today at QuickBooks Enterprise Tech Support Phone Number to let our QuickBooks

  • <a href="https://www.247supportphonenumber.com/">QuickBooks Support Phone Number</a> offers an extensive financial solution, where it keeps your entire business accounting requirements in one single place. From estimates to bank transfers, invoicing to tracking your expenses and staying on top of bookkeeping with regards to tax time, it really is prepared for many from it at one go.

  • QuickBooks Toll-Free offers an extensive financial solution, where it keeps your entire business accounting requirements in one single place. From estimates to bank transfers, invoicing to tracking your expenses and staying on top of bookkeeping with regards to tax time, it really is prepared for many from it at one go.

  • QuickBooks Toll-Free offers an extensive financial solution, where it keeps your entire business accounting requirements in one single place. From estimates to bank transfers, invoicing to tracking your expenses and staying on top of bookkeeping with regards to tax time, it really is prepared for many from it at one go.

  • QuickBooks Enterprise is sold as an all in one single accounting package geared towards mid-size businesses who do not require to manage the accounting hassle by themselves. The various industry specific versions add cherry regarding the cake.

  • At QuickBooks Support contact number, you will discover solution each and every issue that bothers your projects and creates hindrance in running your business smoothly. Our team is oftentimes ready to allow you to with all the best support services you should possibly ever experience.

  • QuickBooks offers a number of features to trace your startup business. Day by day it is getting popular amonst the businessmen and entrepreneurs. But with the increasing popularity, QuickBooks is meeting a lot of technical glitches. And here we show up with our smartest solutions. Have a look at the problem list and once you face any of them just call for the assistance. We are going to help you with…

  • What’re basic reasons for corruption of QuickBooks Company Data file? Prior to trying to fix it, the following logical step you need to perform will be to investigate the complexities behind such corruption.

  • On the planet filled with smart devices and automations, QuickBooks online provides you the working platform to control and automate your accounting process through the elimination of the requirement of traditional accounting process. This might be a real fact that QuickBooks online has more features and faster compared to one centered on desktop. To overcome the problems in the software, you need to choose an intelligent technical assistance channel. QuickBooks Online Customer Support is your best companion in the event of any technical assistance you require in this outstanding software.

  • so now you are becoming well tuned in to great things about QuickBooks online payroll in your company accounting but since this premium software contains enhanced functions that will help you as well as your accounting task to accomplish, so you could face some technical errors when using the QuickBooks payroll solution.

  • By using <a href="https://www.payrollsupportnumber.com/quickbooks-support/">Quickbooks Support Phone Number</a>, you're able to create employee payment on time. However in any case, you might be facing some problem when making use of QuickBooks payroll such as for instance issue during installation, data integration error, direct deposit issue, file taxes, and paychecks errors, installation or up-gradation or simply just about some other than you don’t panic, we provide quality QuickBooks Payroll help service. Here are some features handle by our QB online payroll service.

  • By using Quickbooks Support Phone Number, you're able to create employee payment on time. However in any case, you might be facing some problem when making use of QuickBooks payroll such as for instance issue during installation, data integration error, direct deposit issue, file taxes, and paychecks errors, installation or up-gradation or simply just about some other than you don’t panic, we provide quality QuickBooks Payroll help service. Here are some features handle by our QB online payroll service.

  • In May 2002 Intuit thrown QuickBooks Enterprise Solutions for medium-sized businesses. <a href="https://www.helplinephonenumber.com/quickbooks-enterprise-support-number/">QuickBooks Enterprise Support</a> here to create tech support team to users. In September 2005, QuickBooks acquired 74% share connected with market in the usa. A June 19, 2008 Intuit Press Announcement said that during the time of March 2008, QuickBooks’ share of retail units within the industry accounting group touched 94.2 percent, based on NPD Group.

  • In May 2002 Intuit thrown QuickBooks Enterprise Solutions for medium-sized businesses. QuickBooks Enterprise Support here to create tech support team to users. In September 2005, QuickBooks acquired 74% share connected with market in the usa. A June 19, 2008 Intuit Press Announcement said that during the time of March 2008, QuickBooks’ share of retail units within the industry accounting group touched 94.2 percent, based on NPD Group.

  • More often than not when individuals are protesting about incorrect calculation and defaults paychecks results. Similarly fixing QuickBooks structure of account can certainly be a confusing strive to do and difficult to handle dozens of for a regular user.

  • Intuit payroll will likely not accept direct deposit fees, however, $2.00 monthly fees are imposed when you have one client. Clients may have own logins to process own payment after they intend to customize Intuit online payroll.

  • The QuickBooks Payroll Technical Support Desktop version offers a hand filled with services. We should do the installation on our desktop to possess it working then. It boils down with three types of services basic, enhanced and assisted. Basic payroll is most affordable amongst all the three service types. Enhanced is a tad bit more expensive then basic and the most high-priced one is assisted.

Name
Password
Homepage
Secret