Using define macro and its value in quotes












6















Writing some C code just out of curiosity and I'd like to move some values for a MySQL connection to a kind of const.



Question 1
Is it a good idea to store host, user etc in preprocessor macros? I.e.:



#include <my_global.h>
#include <mysql.h>

#define DB_HOST "mysqlhost.com"
#define DB_USER "mysqlusername"
#define DB_TABLE "tablename"
...


To use them later like mysql_real_connect(con, DB_HOST, DB_USER, DP_PASS, DB_NAME, 0, NULL, 0) == NULL)
?



Question 2



Can I use DB_TABLE's value inside a quoted string? I.e. mysql_query(con, "SELECT * FROM DB_TABLE")



If so - what is corrcect way to use it here?










share|improve this question




















  • 2





    Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

    – Jose
    Jan 25 at 11:11


















6















Writing some C code just out of curiosity and I'd like to move some values for a MySQL connection to a kind of const.



Question 1
Is it a good idea to store host, user etc in preprocessor macros? I.e.:



#include <my_global.h>
#include <mysql.h>

#define DB_HOST "mysqlhost.com"
#define DB_USER "mysqlusername"
#define DB_TABLE "tablename"
...


To use them later like mysql_real_connect(con, DB_HOST, DB_USER, DP_PASS, DB_NAME, 0, NULL, 0) == NULL)
?



Question 2



Can I use DB_TABLE's value inside a quoted string? I.e. mysql_query(con, "SELECT * FROM DB_TABLE")



If so - what is corrcect way to use it here?










share|improve this question




















  • 2





    Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

    – Jose
    Jan 25 at 11:11
















6












6








6








Writing some C code just out of curiosity and I'd like to move some values for a MySQL connection to a kind of const.



Question 1
Is it a good idea to store host, user etc in preprocessor macros? I.e.:



#include <my_global.h>
#include <mysql.h>

#define DB_HOST "mysqlhost.com"
#define DB_USER "mysqlusername"
#define DB_TABLE "tablename"
...


To use them later like mysql_real_connect(con, DB_HOST, DB_USER, DP_PASS, DB_NAME, 0, NULL, 0) == NULL)
?



Question 2



Can I use DB_TABLE's value inside a quoted string? I.e. mysql_query(con, "SELECT * FROM DB_TABLE")



If so - what is corrcect way to use it here?










share|improve this question
















Writing some C code just out of curiosity and I'd like to move some values for a MySQL connection to a kind of const.



Question 1
Is it a good idea to store host, user etc in preprocessor macros? I.e.:



#include <my_global.h>
#include <mysql.h>

#define DB_HOST "mysqlhost.com"
#define DB_USER "mysqlusername"
#define DB_TABLE "tablename"
...


To use them later like mysql_real_connect(con, DB_HOST, DB_USER, DP_PASS, DB_NAME, 0, NULL, 0) == NULL)
?



Question 2



Can I use DB_TABLE's value inside a quoted string? I.e. mysql_query(con, "SELECT * FROM DB_TABLE")



If so - what is corrcect way to use it here?







c macros preprocessor-directive






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 10:58









alk

59k764177




59k764177










asked Jan 25 at 10:48









setevoysetevoy

1,56343053




1,56343053








  • 2





    Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

    – Jose
    Jan 25 at 11:11
















  • 2





    Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

    – Jose
    Jan 25 at 11:11










2




2





Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

– Jose
Jan 25 at 11:11







Regarding the first question, check this answer about the advantages of const vs define (vs enum): stackoverflow.com/a/1674459/7520531

– Jose
Jan 25 at 11:11














2 Answers
2






active

oldest

votes


















6














Answer 1: Technically, you can define it the way you have shown, but at times, it makes sense to make the parameters which are likely to change (like hostname, username) as environment variables, and read them during the program execution. This makes your program more robust against frequent changes. The parameters which are to remain same, can surely be used as a #define preprocessor (like tablename).



A sample can be found here.



Answer 2: No, you cannot use it like that. However, since pre-processor MACROS are compile time substitution, you can take advantage of string concatenation, like



mysql_query(con, "SELECT * FROM " DB_TABLE);


where DB_TABLE is defined as a MACRO.






share|improve this answer

































    4















    Is it a good idea to store host, user etc in preprocessor macros?




    It is common practise, at least if the values can be considered constant in the context of the program.



    Alternatively you can define constants by doing:



    const char * db_host = "localhost";


    The drawback here is that the simple concatenation as shown below won't work.




    Can I use DB_TABLE's value inside a quoted string?




    No, but you can just do:



    mysql_query(con, "SELECT * FROM " DB_TABLE);





    share|improve this answer


























    • Dont you think the second answer ought to start with "No...."...?

      – Sourav Ghosh
      Jan 25 at 10:54











    • @SouravGhosh: Absolutely!

      – alk
      Jan 25 at 10:55






    • 1





      I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

      – Sir Jo Black
      Jan 25 at 10:58








    • 2





      @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

      – alk
      Jan 25 at 11:02













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54363754%2fusing-define-macro-and-its-value-in-quotes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    Answer 1: Technically, you can define it the way you have shown, but at times, it makes sense to make the parameters which are likely to change (like hostname, username) as environment variables, and read them during the program execution. This makes your program more robust against frequent changes. The parameters which are to remain same, can surely be used as a #define preprocessor (like tablename).



    A sample can be found here.



    Answer 2: No, you cannot use it like that. However, since pre-processor MACROS are compile time substitution, you can take advantage of string concatenation, like



    mysql_query(con, "SELECT * FROM " DB_TABLE);


    where DB_TABLE is defined as a MACRO.






    share|improve this answer






























      6














      Answer 1: Technically, you can define it the way you have shown, but at times, it makes sense to make the parameters which are likely to change (like hostname, username) as environment variables, and read them during the program execution. This makes your program more robust against frequent changes. The parameters which are to remain same, can surely be used as a #define preprocessor (like tablename).



      A sample can be found here.



      Answer 2: No, you cannot use it like that. However, since pre-processor MACROS are compile time substitution, you can take advantage of string concatenation, like



      mysql_query(con, "SELECT * FROM " DB_TABLE);


      where DB_TABLE is defined as a MACRO.






      share|improve this answer




























        6












        6








        6







        Answer 1: Technically, you can define it the way you have shown, but at times, it makes sense to make the parameters which are likely to change (like hostname, username) as environment variables, and read them during the program execution. This makes your program more robust against frequent changes. The parameters which are to remain same, can surely be used as a #define preprocessor (like tablename).



        A sample can be found here.



        Answer 2: No, you cannot use it like that. However, since pre-processor MACROS are compile time substitution, you can take advantage of string concatenation, like



        mysql_query(con, "SELECT * FROM " DB_TABLE);


        where DB_TABLE is defined as a MACRO.






        share|improve this answer















        Answer 1: Technically, you can define it the way you have shown, but at times, it makes sense to make the parameters which are likely to change (like hostname, username) as environment variables, and read them during the program execution. This makes your program more robust against frequent changes. The parameters which are to remain same, can surely be used as a #define preprocessor (like tablename).



        A sample can be found here.



        Answer 2: No, you cannot use it like that. However, since pre-processor MACROS are compile time substitution, you can take advantage of string concatenation, like



        mysql_query(con, "SELECT * FROM " DB_TABLE);


        where DB_TABLE is defined as a MACRO.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 25 at 12:12

























        answered Jan 25 at 10:54









        Sourav GhoshSourav Ghosh

        110k14130189




        110k14130189

























            4















            Is it a good idea to store host, user etc in preprocessor macros?




            It is common practise, at least if the values can be considered constant in the context of the program.



            Alternatively you can define constants by doing:



            const char * db_host = "localhost";


            The drawback here is that the simple concatenation as shown below won't work.




            Can I use DB_TABLE's value inside a quoted string?




            No, but you can just do:



            mysql_query(con, "SELECT * FROM " DB_TABLE);





            share|improve this answer


























            • Dont you think the second answer ought to start with "No...."...?

              – Sourav Ghosh
              Jan 25 at 10:54











            • @SouravGhosh: Absolutely!

              – alk
              Jan 25 at 10:55






            • 1





              I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

              – Sir Jo Black
              Jan 25 at 10:58








            • 2





              @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

              – alk
              Jan 25 at 11:02


















            4















            Is it a good idea to store host, user etc in preprocessor macros?




            It is common practise, at least if the values can be considered constant in the context of the program.



            Alternatively you can define constants by doing:



            const char * db_host = "localhost";


            The drawback here is that the simple concatenation as shown below won't work.




            Can I use DB_TABLE's value inside a quoted string?




            No, but you can just do:



            mysql_query(con, "SELECT * FROM " DB_TABLE);





            share|improve this answer


























            • Dont you think the second answer ought to start with "No...."...?

              – Sourav Ghosh
              Jan 25 at 10:54











            • @SouravGhosh: Absolutely!

              – alk
              Jan 25 at 10:55






            • 1





              I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

              – Sir Jo Black
              Jan 25 at 10:58








            • 2





              @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

              – alk
              Jan 25 at 11:02
















            4












            4








            4








            Is it a good idea to store host, user etc in preprocessor macros?




            It is common practise, at least if the values can be considered constant in the context of the program.



            Alternatively you can define constants by doing:



            const char * db_host = "localhost";


            The drawback here is that the simple concatenation as shown below won't work.




            Can I use DB_TABLE's value inside a quoted string?




            No, but you can just do:



            mysql_query(con, "SELECT * FROM " DB_TABLE);





            share|improve this answer
















            Is it a good idea to store host, user etc in preprocessor macros?




            It is common practise, at least if the values can be considered constant in the context of the program.



            Alternatively you can define constants by doing:



            const char * db_host = "localhost";


            The drawback here is that the simple concatenation as shown below won't work.




            Can I use DB_TABLE's value inside a quoted string?




            No, but you can just do:



            mysql_query(con, "SELECT * FROM " DB_TABLE);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 25 at 11:03

























            answered Jan 25 at 10:51









            alkalk

            59k764177




            59k764177













            • Dont you think the second answer ought to start with "No...."...?

              – Sourav Ghosh
              Jan 25 at 10:54











            • @SouravGhosh: Absolutely!

              – alk
              Jan 25 at 10:55






            • 1





              I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

              – Sir Jo Black
              Jan 25 at 10:58








            • 2





              @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

              – alk
              Jan 25 at 11:02





















            • Dont you think the second answer ought to start with "No...."...?

              – Sourav Ghosh
              Jan 25 at 10:54











            • @SouravGhosh: Absolutely!

              – alk
              Jan 25 at 10:55






            • 1





              I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

              – Sir Jo Black
              Jan 25 at 10:58








            • 2





              @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

              – alk
              Jan 25 at 11:02



















            Dont you think the second answer ought to start with "No...."...?

            – Sourav Ghosh
            Jan 25 at 10:54





            Dont you think the second answer ought to start with "No...."...?

            – Sourav Ghosh
            Jan 25 at 10:54













            @SouravGhosh: Absolutely!

            – alk
            Jan 25 at 10:55





            @SouravGhosh: Absolutely!

            – alk
            Jan 25 at 10:55




            1




            1





            I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

            – Sir Jo Black
            Jan 25 at 10:58







            I don't think that is common practice to store values such as hostname and username in MACROS! ... It's common for very small applications ... ;)

            – Sir Jo Black
            Jan 25 at 10:58






            2




            2





            @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

            – alk
            Jan 25 at 11:02







            @SirJoBlack: Well I, generalised the question to whether it were common to #define strings. If to do so for host or user names made sense at least depends .... Reworded my answer.

            – alk
            Jan 25 at 11:02




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54363754%2fusing-define-macro-and-its-value-in-quotes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Brian Clough

            Cáceres