Trouble comparing value in C [duplicate] - c

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.

Related

Why sizeof() operator gives different value for 'a' and "a" in C? [duplicate]

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.

Difference between 'A' and "A" literals in C [duplicate]

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.

How to convert char '5' into int '5' in C? [duplicate]

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

why sizeof('a') is 4 in C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why are C character literals ints instead of chars?
#include<stdio.h>
int main(void)
{
char b = 'c';
printf("here size is %zu\n",sizeof('a'));
printf("here size is %zu",sizeof(b));
}
here output is (See live demo here.)
here size is 4
here size is 1
I am not getting why sizeof('a') is 4 ?
Because in C character constants, such as 'a' have the type int.
There's a C FAQ about this suject:
Perhaps surprisingly, character constants in C are of type int, so
sizeof('a') is sizeof(int) (though this is another area where C++
differs).
The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.
A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.
So sizeof('a') is equivalent to sizeof(int)
'a' by default is an integer and because of that you get size of int in your machine 4 bytes.
char is 1 bytes and because of this you get 1 bytes.

sizeof('z') result unexpected [duplicate]

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)

Resources