Using define macro and its value in quotes
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
add a comment |
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
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
add a comment |
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
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
c macros preprocessor-directive
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
There should be a space afterSELECT * FROM
– Spikatrix
1 hour ago
@Spikatrix Yep, correct. Edited
– Sourav Ghosh
1 hour ago
add a comment |
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);
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
There should be a space afterSELECT * FROM
– Spikatrix
1 hour ago
@Spikatrix Yep, correct. Edited
– Sourav Ghosh
1 hour ago
add a comment |
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.
There should be a space afterSELECT * FROM
– Spikatrix
1 hour ago
@Spikatrix Yep, correct. Edited
– Sourav Ghosh
1 hour ago
add a comment |
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.
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.
edited 1 hour ago
answered 2 hours ago
Sourav GhoshSourav Ghosh
109k14130188
109k14130188
There should be a space afterSELECT * FROM
– Spikatrix
1 hour ago
@Spikatrix Yep, correct. Edited
– Sourav Ghosh
1 hour ago
add a comment |
There should be a space afterSELECT * 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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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