Here i have a char type variable which holds a number value as string say 21,I want to assign 21 to a integer variable.I did the following thing but it prints -12.why it is printing -12 and how i can get 21 in my int variable?
#include<stdio.h>
int main(){
char character = "21";
int x = (int)character - '0';
printf("%d",x);
}
char character = "21";. The data is a string literal which should be assigned to achararray likechar character[] = "21";char *character = "21";is a valid alternative.int x = (int)character - '0';this would work, but only for a single digit such aschar character = '7';