0

I was trying to convert the string of numbers into integer value with the following code. The output is always one less than the original value. I am not getting what's wrong with my code.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){

    char a[6];

    int i,b;

    scanf("%s",a);

    for(i=strlen(a)-1;i>=0;i--){

        a[i]=a[i]-48;

        b=b+a[i]*pow(10,(strlen(a)-i-1));
    }

    printf("%d",b);

    getch();

    return 0;

}
6
  • Is the assignment to do the conversion yourself? Otherwise use strtol. Also don't use magic numbers. If by 48 you mean the character '0' then say so explicitly. Commented Jul 14, 2016 at 9:54
  • As for your problem, there's no need to use pow here, just a simple multiplication with 10. Commented Jul 14, 2016 at 9:57
  • 1
    Besides the indeterminate b value, shall we also assume negative values aren't an option, nor any value larger than 99999? Commented Jul 14, 2016 at 10:04
  • You are scanning the string from right to left, but there is an easier way. Initialize your variable b to 0. Start at the left of the string. For each digit that you see, multiply b by 10 and add in the digit. When you stop seeing digits, you're done. That way you don't even have to call strlen. Commented Jul 14, 2016 at 10:20
  • 1
    You are overwriting your string and using strlen on the modified string. If there is '0' in your string, then strlen will return a different value than originally. Commented Jul 14, 2016 at 11:00

2 Answers 2

3

The problem is most likely undefined behavior because you use uninitialized variables.

In the expression b=b+... you use the variable b without initializing it first. Non-static local variables are not initialized and will have an indeterminate value. Using them without initialization leads to UB. Initialize it to zero:

int i, b = 0;

You also have problems with the user entering to many characters for the array a and going out of bounds. You also don't have any checks that the user actually only entered digits.

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

3 Comments

even if i initialize the variable b the output is still 1 less than the original value but if i don't use the pow() function , i get the desired output. but what is wrong with the pow() function?
@shivam The pow function is for floating point, with all the rounding problems that come with it.
@shivam It's likely that, for example, pow(10, 3) is giving you 999.999 instead of 1000. (Or maybe pow(10, 0) is giving you 0.999 instead of 1.0, or pow(10, 1) is giving you 9.999 instead of 10.)
0

There are few issues in your code. For reducing risks of UB:

  1. If you treat char[] as string, its size should allow for accommodating the \0 terminating character.
  2. If your code doesn't initialize non-static local variable values, you should do it explicitly (variable int b).
  3. Treat scanf() cautiously if you read a string. Overflow leads to UB. Use a field width specifier.
  4. Don't mix data types (return of pow() is double).
  5. Make your code as lean as possible, with as little math as practical, especially inside loops. You don't need pow() altogether.

Please, see the comments along the lines based on your code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char a[7];      // extra byte for '\0'
    int i, l, b = 0;//initialize local variable b
    int ten = 1;

    scanf("%6s",a); //should use a field width specifier

    l = strlen(a) - 1;

    for( i=l ; i>=0 ; i--, ten*=10 )
        b += (a[i]-48)*ten; //do as simple math as practical

    printf("%d\n",b);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.