basic #define usage

K

Kelly

Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.

Thanks.
Kel
 
B

Ben Pfaff

Kelly said:
#define Y one

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

Y expands to one.
one is undefined.
This won't compile.
 
M

Martin Ambuhl

Kelly said:
Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.

It's easy for anyone who checks the FAQ before posting.
Below is a version of your program which shows various behaviors that you
might one to check. The question and answer in the FAQ can be found at
<http://www.eskimo.com/~scs/C-faq/q11.17.html>

#define Y one
#define Z "one"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define STR_aux(x) #x
#define STR(x) STR_aux(x)

int main(void)
{
printf("%s\n", STR_aux(Y));
printf("%s\n", STR(Y));
printf("%s\n", STR_aux(Z));
printf("%s\n", STR(Z));
printf("%s\n", Z);
return 0;
}

[output]
Y
one
Z
"one"
one
 
J

Jeff

Kelly said:
Hi,

Ashamed to ask basic question but I cannot get this code to run,

#define Y one

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);

return 0;

}

I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.

Please help, I know this is easy for most of you.

Try #define Y "one"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top