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
    2 hours ago


















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
    2 hours ago
















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 2 hours ago









alk

58.3k763171




58.3k763171










asked 2 hours ago









setevoysetevoy

1,52843052




1,52843052








  • 2





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

    – Jose
    2 hours ago
















  • 2





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

    – Jose
    2 hours ago










2




2





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

– Jose
2 hours ago







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

– Jose
2 hours ago














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


























  • There should be a space after SELECT * FROM

    – Spikatrix
    1 hour ago











  • @Spikatrix Yep, correct. Edited

    – Sourav Ghosh
    1 hour ago



















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
    2 hours ago











  • @SouravGhosh: Absolutely!

    – alk
    2 hours ago






  • 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
    2 hours ago








  • 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
    2 hours ago













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


























  • There should be a space after SELECT * FROM

    – Spikatrix
    1 hour ago











  • @Spikatrix Yep, correct. Edited

    – Sourav Ghosh
    1 hour ago
















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


























  • There should be a space after SELECT * FROM

    – Spikatrix
    1 hour ago











  • @Spikatrix Yep, correct. Edited

    – Sourav Ghosh
    1 hour ago














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 1 hour ago

























answered 2 hours ago









Sourav GhoshSourav Ghosh

109k14130188




109k14130188













  • There should be a space after SELECT * FROM

    – Spikatrix
    1 hour ago











  • @Spikatrix Yep, correct. Edited

    – Sourav Ghosh
    1 hour ago



















  • There should be a space after SELECT * FROM

    – Spikatrix
    1 hour ago











  • @Spikatrix Yep, correct. Edited

    – Sourav Ghosh
    1 hour ago

















There should be a space after SELECT * FROM

– Spikatrix
1 hour ago





There should be a space after SELECT * FROM

– Spikatrix
1 hour ago













@Spikatrix Yep, correct. Edited

– Sourav Ghosh
1 hour ago





@Spikatrix Yep, correct. Edited

– Sourav Ghosh
1 hour ago













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
    2 hours ago











  • @SouravGhosh: Absolutely!

    – alk
    2 hours ago






  • 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
    2 hours ago








  • 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
    2 hours ago


















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
    2 hours ago











  • @SouravGhosh: Absolutely!

    – alk
    2 hours ago






  • 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
    2 hours ago








  • 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
    2 hours ago
















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 2 hours ago

























answered 2 hours ago









alkalk

58.3k763171




58.3k763171













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

    – Sourav Ghosh
    2 hours ago











  • @SouravGhosh: Absolutely!

    – alk
    2 hours ago






  • 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
    2 hours ago








  • 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
    2 hours ago





















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

    – Sourav Ghosh
    2 hours ago











  • @SouravGhosh: Absolutely!

    – alk
    2 hours ago






  • 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
    2 hours ago








  • 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
    2 hours ago



















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

– Sourav Ghosh
2 hours ago





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

– Sourav Ghosh
2 hours ago













@SouravGhosh: Absolutely!

– alk
2 hours ago





@SouravGhosh: Absolutely!

– alk
2 hours ago




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
2 hours ago







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
2 hours ago






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
2 hours ago







@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
2 hours ago




















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

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana