Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186060
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T15:39:55+00:00 2022-06-10T15:39:55+00:00

c – Evaluate constant expression during compile time

  • 0

[ad_1]

I am trying to write a preprocessor macro MYPRINTF(x, ...) that should work as printf but with an additional length specifier w. The definition of w is supposed to be operating system dependent, but on Linux it is equivalent to l.

The code works fine, but I’m having problems with evaluating expressions during compile time when x is a constant expression. As an example, if I write

long int x = 13;
MYPRINTF("%wd\n", x);

on a Linux system, I would like the compiler to convert it during compile time to

long int x = 13;
printf("%ld\n", x);

Looking at the assembly code, I can say that this is not what is happening.

Here is the code that I’m running

#include <stdio.h>

#define SET_CHAR(ptr, cr)                       \
    do                                          \
    {                                           \
        *(ptr) = (cr);                          \
        (ptr)++;                                \
    } while (0)

#define SET_WORD_FMT(x)                         \
    do                                          \
    {                                           \
        SET_CHAR(x, 'l');                       \
    } while (0)

#define IS_FORMAT(c) ((c) == 'c' || (c) == 's' || (c) == 'd' || (c) == 'i' || (c) == 'o' || (c) == 'x' || (c) == 'X' || (c) == 'u' || (c) == 'f' || (c) == 'F' || (c) == 'e' || (c) == 'E' || (c) == 'a' || (c) == 'A' || (c) == 'g' || (c) == 'G' || (c) == 'n' || (c) == 'p')

#define MYPRINTF(x, ...)                        \
    do                                          \
    {                                           \
        char _str[512];                         \
        char * _strptr = _str;                  \
        const char * _xptr = (x);               \
        while (*_xptr != '\0')                  \
        {                                       \
            if (*_xptr != '%')                  \
            {                                   \
                SET_CHAR(_strptr, *_xptr);      \
                _xptr++;                        \
                continue;                       \
            }                                   \
                                                \
            SET_CHAR(_strptr, '%');             \
            _xptr++;                            \
                                                \
            if (*_xptr == '%')                  \
            {                                   \
                SET_CHAR(_strptr, '%');         \
                _xptr++;                        \
                continue;                       \
            }                                   \
            else while (!IS_FORMAT(*_xptr))     \
            {                                   \
                SET_CHAR(_strptr, *_xptr);      \
                _xptr++;                        \
            }                                   \
                                                \
            if (_strptr[-1] == 'w')             \
            {                                   \
                _strptr--;                      \
                SET_WORD_FMT(_strptr);          \
            }                                   \
                                                \
            SET_CHAR(_strptr, *_xptr);          \
            _xptr++;                            \
        }                                       \
        *_strptr="\0";                        \
        printf(_str, __VA_ARGS__);              \
    } while (0)

int
main()
{
    long int slx = 18273817628731872;
    int x = 13;
    int lx = 7128172;
    long long int llx = 1928398172938791872;

    MYPRINTF("hello %wd, %d, %ld, %% and %lld\n", slx, x, lx, llx);
}

which is compiled using GCC 12.1.0 with flags -O3 -march=native.

Is it possible to evaluate this during compile time? If so, how?

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.