-4

I have tried the standard methods but still I get error in my answer.

My code:

int main() { int val; char str[] = {'1', '45', '0'}; val = str[1] - '0'; printf("Int value = %d\n", val); return(0); } 

I am getting answer as 5 instead of 45.

How do I solve this issue?


[update from comment:]

I actually need to process an array of strings..suppose I want to convert octal numbers to decimal, and my input has to be in the form of an array of strings. I wish to convert these no.s to decimal : {45,17,100} For that I would, at first be requiring to extract each element and change it to integer. Could you plz suggest what would be the best way to do it?

3
  • char is and integer type. What is your problem, what do you want to accomplish? Commented Jul 8, 2016 at 6:31
  • '45' is a multi-character constant. See this answer to another SO question for further info. Commented Jul 8, 2016 at 6:31
  • Have a look at what the compiler tells you, when compiling the code shown. Commented Jul 8, 2016 at 7:12

3 Answers 3

1

I actually need to process an array of strings

What you have defined here

 char str[] = {'1', '45', '0'}; 

is not an array of strings, but exactly one array of char with 3 elements. It is not even a C-"string", as this would require a trailing value of 0. Note that the value of the character '0' isn't 0, but, for example, for the ASCII character set it's 48.

'45' is a multi-byte character literal, which in fact is an integer. The code above tries to initialise the 2nd element of the char-array str (str[1], which is a char) using this very literal.

This does not work.

A char cannot (necessarily) hold the int value of this multi-byte character literal. In this case the initialisation of str[1] overflows, resulting in the unexpected value of 5.

To see the issue try the following code:

#include <limits.h> #include <stdio.h> int main(void) { char c_min = CHAR_MIN; char c_max = CHAR_MAX; unsigned char uc = '45'; printf("'1'=%d\n", '1'); printf("'45'=%d\n", '45'); printf("'0'=%d\n", '0'); printf("lowest possible value for char=%d\n", c_min); printf("highest possible value for char=%d\n", c_max); printf("'45' converted to an (unsigned) char=%u\n", uc); return 0; } 

The example above shows how the value of 45 gets truncated when being assigned to char.

Depending on the C implementation you use the conversion of '45' to a char might even invoke the infamous Undefined Behaviour. This is not good.

What you seem to be wanting is:

 #define ARRAY_SIZE_MAX 3 char * str[ARRAY_SIZE_MAX] = {"1", "45", "0"}; /*. Mind the double quotes. */ 

This defines an array of 3 pointers to char , with each pointing to a C-"string".

Sign up to request clarification or add additional context in comments.

Comments

1

Here, you are getting 5 instead of 45 because st[1] = '5', this is because we have only ASCII value of 0 to 9 integers and 45 have no ASCII value. To store 45 in your string you have to declare multidimensional string. for example:

char st[3][3]={'1', '45', '0'}; 

Here is the working code:

#include<stdio.h> int main(){ int i,ans,j; char st[3][3]={{'1'}, {'4','5'},{'0'}}; for(j=0;j<3;j++){ for(i=0;st[j][i]>=48 && st[j][i]<=57;i++){ ans=st[j][i]-'0'; printf("%d",ans); } printf("\n"); } return 0; } 

Output is

1 45 0 

1 Comment

To make your code fully portable, that is independent from the ASCII character set, you want to change the two Magic Numbers 48 to '0' and 57 to '9'.
-1

It depends, what do you want.

Every character has it's own int value- It's simply ASCII code

If you will iterate over integers and make it print like %c you will get ASCII table (you can see it eg. there.

But if you want to read int values from string / char datatype you will have to parse it- atoi (ascii to integer) function- example there

Btw I dont know how exactly your example works but the problem is you are doing following: int val = '45' - '0'; int value of '0' should be 48, '45' rly I dont know, but '4' is 52int and '5' is 53 int, so something like that..

As i wrote you should to do something like int val = atoi('45') - atoi('0') just for sure maybe better to cast into int as follows int val = (int) (atoi('45') - atoi('0')) - exactly asi in your example

int main() { int val; char str[] = {'1', '45', '0'}; val = (int) (atoi(str[1]) - atoi('0')); printf("Int value = %d\n", val); return(0); } 

Not sure, but i think that should works, hope that will help

1 Comment

atoi() takes either a char-array or a char-pointer, but does not take a char, nor an int. str[1] is a char and '0' is char literal, which is of type int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.