By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

Associate argument with entry in header file

P: n/a
All,

We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.

For example,
__________________________________________________ ______________________________
myheader.h
-----------

#define VARIABLE 10
__________________________________________________ ______________________________

Checkheader.c
__________________________________________________ ______________________________
#include myheader.h

int main(int argv, char * argc){
?????????
}
__________________________________________________ ______________________________

We should be able to run Checkheader VARIABLE and it should return 10.

Thanks in advance,
Shekar
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
All,

We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.

For example,
__________________________________________________ __________________________
____ myheader.h
-----------

#define VARIABLE 10
__________________________________________________ __________________________
____
Checkheader.c
__________________________________________________ __________________________
____ #include myheader.h

int main(int argv, char * argc){
?????????
}
__________________________________________________ __________________________
____
We should be able to run Checkheader VARIABLE and it should return 10.


#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE"));
}
-Mike
Nov 14 '05 #2

P: n/a
Chandrashekar Tippur wrote:

We want to associate a argument with a corresponding entry in
header file. How do we do this? The argument is defined in a
header file. We want to extract the value of the #define variable.

For example,
__________________________________________________ _______________
myheader.h
-----------

#define VARIABLE 10
__________________________________________________ _______________

Checkheader.c
__________________________________________________ _______________
#include myheader.h

int main(int argv, char * argc){
?????????
}
__________________________________________________ _______________

We should be able to run Checkheader VARIABLE and it should
return 10.


---- File checkheader.c ----
#include <stdio.h>
#include "myheader.h"

int main(void)
{
printf("%d\n", VARIABLE);
return 0;
}

Time to read your C book.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

P: n/a
In article <41***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
We should be able to run Checkheader VARIABLE and it should
return 10.
[...] printf("%d\n", VARIABLE);


Note that he specifies the #defined variable name as an argument.

I take it he wants to be able to map from strings to preprocessor
variables at run time, which can't be done. He would need a table
of variable names and values.

-- Richard
Nov 14 '05 #4

P: n/a
Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variable name>)

I appreciate the help.

Shekar
#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE"));
}
-Mike

Nov 14 '05 #5

P: n/a
ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62**************************@posting.google. com>...
We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.
[snip...]


You cannot accomplish it in C directly. However, I can propose you a
solution: insert a CodeWorker script into your main() function.
CodeWorker is a parsing tool and a source code generation, freeware
available at "http://www.codeworker.org".

Your file, "main.cpp" becomes:
#include <stdio.h>
#include <string.h>
#include "myHeader.h"

int main(int argc, char **argv) {
if (argc != 2) {
printf("Argument expected!");
return -1;
}
// this markup announces a CodeWorker script
/*##markup##"generate mapping"*//*##script##*/
/*
// parse the file "myHeader.h" with an extended-BNF script
parseAsBNF({
mapping ::=
#ignore(C++) // ignore C-like comments
// looking for every '#define <numeric> <EOL>
[
// jump to the next '#define'
->[
'#' #readIdentifier:"define"
// register the name of the preprocessor definition
#readIdentifier:sDefine
// numeric value ?
#readNumeric:dValue
#!ignore // stop to ignore whitespaces or comments
['\r']? '\n' // <EOL>
// store the 'define' and the associated value
=> insert this[sDefine] = dValue;
]
]*
;
}, project, "myHeader.h");
// code generation of a sequence of if/else on the preprocessor
// definitions: display their corresponding numeric value.
foreach i in project {
// the beginning isn't the same if it is the first definition
// or not
if i.first() {
@
@
} else {
@ else @
}
// the test on the value of the command-line argument:
// display it on the console if valid.
@if (strcmp(argv[1], "@i.key()@") == 0) {
printf("\"@i.key()@\" = @i@");
}@
}
@
@
*/
/*##script##*/
return 0;
}
Type:
codeworker -autoexpand main.cpp -commentbegin /* -commentend */
and it will add the following code just after the last
'/*##script##*/' and just before ' return 0;':
/*##begin##"generate mapping"*/
if (strcmp(argv[1], "VARIABLE") == 0) {
printf("\"VARIABLE\" = 10");
} else if (strcmp(argv[1], "OTHER") == 0) {
printf("\"OTHER\" = 25.5");
}
/*##end##"generate mapping"*/
with "myHeader.h" being worth:
#ifndef _myHeader_h_
// this one is refused: not a number
#define _myHeader_h_

// this one is retained
#define VARIABLE 10
// this one is refused, because not an integer
#define WRONG "constant string aren't accepted"
// this one is refused, because points to another definition
#define WRONG2 VARIABLE
// this one is accepted too
#define OTHER 25.5

#endif
Regards,

Cedric Lemaire
Nov 14 '05 #6

P: n/a
ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62**************************@posting.google. com>...
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.


OK, so replace '#readNumeric:dValue' by '#readInteger:dValue' in the
BNF script, to accept long only.
Then, in the template-based script, replace 'printf("\"@i.key()@\" =
@i@");' by 'printf("sysconf(\"@i.key()@\") = %ld",
sysconf(@i.key()@));'.
You will need to include the C header for 'sysconf()'.

On your example, it will generate:
printf("sysconf(\"VARIABLE@\") = %ld", sysconf(VARIABLE));
Regards,

Cedric Lemaire
--
Nov 14 '05 #7

P: n/a
Chandrashekar Tippur said to us:
Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variable name>)

I appreciate the help.

Shekar


Yes, but it would require an outside program such as a perl script.

Without creating an outside program, you could create a table of
configuration variable names and their values:

#define MAX_ARRAY_SIZE 1000
#define TIMEOUT 600
#define ANOTHER_VALUE 17
struct sysconf_value {
const char * name;
long value;
};
struct sysconf_value values [] = {
"max_array_size", MAX_ARRAY_SIZE,
"timeout", TIMEOUT,
"another_value", ANOTHER_VALUE,
0, 0,
};

Define sysconf like so:
long sysconf (const char * str) {
int ii;
for (ii = 0; values[ii].name; ii++) {
if (0 == strcmp (str,values[ii].name)) return values[ii].value;
}
fprintf (stderr, "Can't find sysconf: %s\n", str);
exit(1);
}

In main:
int ii = 0;
const char * tests [] = { "max_array_size", "timeout", "another_value", 0 };

for (ii = 0; tests[ii]; ii++) {
printf ("name: %s value: %ld\n", tests[ii], sysconf(tests[ii]));
}

HTH

Nov 14 '05 #8

P: n/a
All,

Thanks for all your replies. I will certainly try the code worker
approach. I will update the thread with the results.
Thanks again.

Shekar
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.