This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a single char into an int
I am working on a program that requires an integer be pulled from a string, and then added to other integers.
I.E. Input "123456789". I can pull out the 5th char, '5', but I need to add this to another int. If I try to convert this to int, it returns the ASCI value. Is there any way to convert a char to its corresponding int?
Just subtract away the ASCII value of character '0'.
e.g. '5' - '0' = 5
Related
This question already has answers here:
Sizeof(char[]) in C
(8 answers)
Why the sizeof character constant is 4 bytes? [duplicate]
(2 answers)
Sizeof string literal
(2 answers)
Closed 4 years ago.
As far as I know a character constant, viz, 'a', is stored in ASCII format which is internally treated as integer, 97 in case of 'a', that's why sizeof('a') returns 4 on executing but when I use sizeof("a") it returns 2. I have not found any explanation regarding that yet.
My code:
#include <stdio.h>
void main()
{
int x,y;
x = sizeof('a');
y = sizeof("a");
printf("%d\n",x);
printf("%d",y);
}
which gives the output:
4
2
'a' is an integer. It has a size of 4 on most computers. It may also be something else, however. 2 is also common on more specialized hardware.
"a" is a string literal. There are two characters: a and \0. They have a size of 1 each, for a total of size 2. However, when you try to assign that, what you get usually is a const char* and that may have a different size.
This question already has answers here:
How to convert integer to char in C? [duplicate]
(6 answers)
Closed 5 years ago.
I am having problem comparing an int value and a char value in C. Lets say I variable int value1 that is 0 and a char that has value '0'. I know that that char's value is actually an ascii number and that '0' is 48, but how do I compare int's 0 value with chars '0' value, in an if statement as a example?
You can use the idiom c - '0' to convert a char c to its equivalent digit.
This is an expression of type int.
Note that it works in any character encoding supported by the C Standard, since such an encoding must order 0 to 9 consecutively.
This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 8 years ago.
I tried to convert dec to hex. For example convert 255 to hex.
opa = 255.ToString("X");
gives me error:
error: invalid suffix "ToString" on floating constant
I spent lots of time to convert, but couldn't find right way.
You're asking about the wrong language. C does not support the dot operator on integers. To do this in C, you need to print it to a string like so.
char numstr[10];
sprintf(numstr, "%X", 255)
This question already has answers here:
Difference between "3" and '3' in C
(5 answers)
Closed 9 years ago.
Am I right in saying that the difference between 'A' and "A" in C is:
'A' - Means a character 1 byte (8 bits) long containing A.
"A" - Means a string which is 2 bytes (16 bits) long which holds an A and a NULL character.
Are my explanations correct?
Thanks.
You are absolutely correct.But you got to bear in mind that when you use those as rvalues they are different.One has the type char, and can be used as an int as it is implicitly converted to the ASCII value, while the other has type char*.
Let me illustrate my point with a code if that helps:
int num='A'; //Valid, assigns 65 to num
char test=65; //Valid, as test will be 'A' after this
char *ptr="A" //Valid, assigns address of string "A" to pointer ptr
printf("%c,%d",'A','A'); // Output will be A,65
printf("%p",(void*)"A"); //Will print address where string "A" is
printf("%c","A"); ///WRONG
printf("%s","A"); //Works
Edit For the finer nuances, if you feel your understanding is up to that mark yet,refer to Mat's comment.Else read it after a few weeks when you have advanced further in your study of C.
Basically yes. The main nuance is byte vs. char. Where you say byte, you should say char. On most systems a char is one byte. There are a few that use larger objects to store a char.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Size of character ('a') in C/C++
Why does this program output 4 and not 1?
void main()
{
printf("%d",int(sizeof('z')));
}
'z' is a character and sizeof('z') must print 1?
'z' is a character literal and in C a character literal is of type int. So sizeof('z') equals sizeof(int) on your implementation.
Sizeof char.
Perhaps surprisingly, character
constants in C are of type int, so
sizeof('a') is sizeof(int)