Posts: 0
Threads: 0
Joined: Jan 2017
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
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?
|
Posts: 0
Threads: 0
Joined: Oct 2017
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
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'}
|
Posts: 0
Threads: 0
Joined: May 2021
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-13-2014, 02:28 AM)w00t Wrote: [To see links please register here] 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'}
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?
|
Posts: 0
Threads: 0
Joined: Jul 2022
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-13-2014, 01:38 AM)VolPlus Wrote: [To see links please register here] 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?
Quote:(01-13-2014, 02:28 AM)w00t Wrote: [To see links please register here] 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'}
Quote:(01-13-2014, 03:02 AM)VolPlus Wrote: [To see links please register here] 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? :blush:
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.
|
Posts: 0
Threads: 0
Joined: Dec 2021
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-14-2014, 10:45 AM)Aristotle Wrote: [To see links please register here] 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.
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.
Ah thank you so much for the good explanation
|
Posts: 0
Threads: 0
Joined: Oct 2021
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-14-2014, 01:24 PM)VolPlus Wrote: [To see links please register here] Ah thank you so much for the good explanation :smile:
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.
|
Posts: 0
Threads: 0
Joined: Nov 2016
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-15-2014, 01:24 AM)Aristotle Wrote: [To see links please register here] 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)
|
Posts: 0
Threads: 0
Joined: Dec 2022
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Quote:(01-15-2014, 02:33 AM)VolPlus Wrote: [To see links please register here] 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)
[To see links please register here]
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.
|
|