General Question

Theotherkid's avatar

Why won't xcode recognize these variables?

Asked by Theotherkid (884 points ) January 9th, 2010

I’m beginning to learn about C programming, and am trying to add a (character) variable in a standard tool in xcode. Here’s what I wrote:

#include <stdio.h>

int main (int argc, const char * argv[]) {
char exampleofcharacter = ‘J’;
return 0;
}

The variable (char/exampleofcharacter) is supposed to display the “J”, however, when I build the project, I get an error message saying “Unused variable ‘exampleofcharacter’”. There’s probably a very basic solution to this that I’m not understanding.

Observing members: 0 Composing members: 0

6 Answers

poisonedantidote's avatar

i wish i could help, but all i can really offer is a shot in the dark based off other lesser languages i know.

i only see one instance of ’‘exampleofcharacter’’ so my total uneducated guess, would be there is either no function calling on the variable, or it is a function calling on a variable that does not exist.

other than that i cant say anything, not familiar with this language at all.

benhodgson's avatar

I would guess that you’re not getting an error, just a warning message. It’s nothing to worry about — it’s just because you’re not reading from exampleofcharacter anywhere in your code.

HungryGuy's avatar

I don’t know C, but like @poisonedantidote, I’m a programmer who knows quite a few languages. And at first glance, it looks like you’re defining exampleofcharacter and setting it to an initual value of ‘J’, but you’re not using it anywhere.

Perhaps what you mean to say is: return exampleofcharacter; otherwise there’s absolutely no point to your function.

daemonelson's avatar

You’re only declaring the variable. If you want to output ‘J’, you need to tell the program that when you give it some kind of input to output the variable ‘exampleofcharacter’. Or just to automatically print the variable as soon as you run.

phoenyx's avatar

Try this:
—————

#include <stdio.h>

void main()
{
char example = ‘J’;
printf(”%c”, example);
}

—————
You’ll have to make sure the ’ are tick marks and not the pretty single quotes that fluther formats them to.

HasntBeen's avatar

@phoenyx and @daemonelson are correct: you’re creating and initialized the variable, but you’re not doing anything with it. All that code you wrote does is set a value in memory and then exit the program. You can’t see memory unless you output it somehow.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther