![]() |
|
"A" is converted to a number? - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: "A" is converted to a number? (/Thread-A-is-converted-to-a-number) |
"A" is converted to a number? - Alan Turing - 01-13-2014 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] Quote:The letter <insert weird symbol here>is cool.or sometimes I'll get Quote:The letter 32 is cool. Why do I HAVE to use 'A' with single quotes rather than doubles? Why is it converted? RE: "A" is converted to a number? - w00t - 01-13-2014 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'} RE: "A" is converted to a number? - Alan Turing - 01-13-2014 (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. 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?
RE: "A" is converted to a number? - Dong - 01-14-2014 (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. (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. (01-13-2014, 03:02 AM)VolPlus Wrote: Oh yeah woops, forgot to add int main(char argc, char *argv[]) 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. RE: "A" is converted to a number? - Alan Turing - 01-14-2014 (01-14-2014, 10:45 AM)Aristotle Wrote: He isn't referring to the declaration of the function main(), he's referring to the arguments that the printf function expects. Ah thank you so much for the good explanation
RE: "A" is converted to a number? - Dong - 01-15-2014 (01-14-2014, 01:24 PM)VolPlus Wrote: Ah thank you so much for the good explanation No problem. When you get to the topic of arrays, pointers and whatnot and actually understand them, my explanation will become like 100x clearer. Well, unless you understand it already. Which in that case is good. RE: "A" is converted to a number? - Alan Turing - 01-15-2014 (01-15-2014, 01:24 AM)Aristotle Wrote: No problem. When you get to the topic of arrays, pointers and whatnot and actually understand them, my explanation will become like 100x clearer. Well, unless you understand it already. Which in that case is good. Atm I'm learning about why we use return(0); and getchar(); since in the books && guides i've been reading they never really went in depth. And, btw, if void is essentially "nothing" why do people use int main(void) as oppose to int main()? (Implying they do, I could be WAY off) RE: "A" is converted to a number? - Dong - 01-15-2014 (01-15-2014, 02:33 AM)VolPlus Wrote: Atm I'm learning about why we use return(0); and getchar(); since in the books && guides i've been reading they never really went in depth. http://www.cplusplus.com/reference/cstdio/getchar/ int main() { } can take any number of arguments and it returns a value of type int. int main(void) { } cannot take any number of arguments whatsoever and it returns a value of type int. You can call main() with any number of arguments. So you could call main like so if you wanted to: main(1, 5); You cannot call main(void) with any arguments at all. The compiler will generate an error if you try to. I just use int main() { } regularly or something like int main(int argc, char *argv[]) for usage with command-line parameters. |