(01-13-2014, 01:38 AM)VolPlus Wrote: So I'm learning C right now, and i'm having a fun time learning it, and I'm glad I started, but I have a problem, when I use the data type char to the variable test assigning it to "Z" I get a # or sometimes a weird symbol.
How come, if I use PHP Code:
#include <stdio.h>
int main()
{
char test = "Z";
printf("The letter %c is cool.\n", test);
getchar();
return(0);
}
The output is:
main.c: In function ‘main’:
main.c:5:17: warning: initialization makes integer from pointer without a cast [enabled by default]
or sometimes I'll get
Why do I HAVE to use 'A' with single quotes rather than doubles? Why is it converted?
(01-13-2014, 02:28 AM)w00t Wrote: The argument to printf is char * is why you're getting that warning, but that's irrelevant to your problem.
I don't exactly recall, but I believe you wind up directly printing the first byte of the pointer. By using "a", you wind up making char test[2] = {'a', '\x00'}
(01-13-2014, 03:02 AM)VolPlus Wrote: Oh yeah woops, forgot to add int main(char argc, char *argv[])
I'm having a bit of trouble understanding, do you mind going a bit more indepth, possibly? 
He isn't referring to the declaration of the function main(), he's referring to the arguments that the printf function expects.
@w00t: Actually, the error has everything to do with this particular situation.
Quote:main.c:5:17: warning: initialization makes integer from pointer without a cast [enabled by default]
This error basically indicates that one is trying to assign a pointer value to a variable that is expecting a non-pointer value.
If you want a fairly in-depth explanation of how this error relates to your issue, I will do so below... Keep in mind that since you're now starting out, you probably won't need to understand most of this for a while... I'll try to keep it as simple as possible:
In C and C++ alike, text encapsulated within string literals ( " ) are translated as a const char * (a pointer to a constant string somewhere in memory). Also... this may come as a "shock" but char variables are actually integer types. Think about it... the char variables hold ASCII values which C will then represent as a symbol for you. Therefore, in your code you are actually trying to assign a pointer value to an integer (char test). This is why you are getting an error...
Anyways, generally speaking, when making use of string literals ( " ) in an assignment operation, you should assign the string to an array of characters. Don't forget... strings are arrays of characters (string objects in C++ are a bit more than that as they have a lot of built-in functionality, but essentially that's all they are).When using char literals ( ' ) in an assignment operation, you should assign the character to a single char variable.
If you've anymore questions, let me know.