Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 300 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"A" is converted to a number?

#1
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?
Reply

#2
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'}
Reply

#3
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?
Reply

#4
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.
Reply

#5
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
Reply

#6
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.
Reply

#7
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)
Reply

#8
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through