0

I'm very new to C programming and studying on K&R C Programming Language book. I'm trying to write a program converts hexadecimal numbers to decimal numbers repeatedly and want to use a function in my code. There are some problems prevent me from understanding C. I've read about them but not clearly understand.

I want to read input with length max 4, but as I understand scanf() doesn't limit length although the length of char array is 4. So I tried to eliminate non-integer characters with a for loop. Is this ok? What is the proper way of getting input from users? I don't want to use while((cr=getchar())!=EOF).

When I run the program, I get an error message:"undefined reference to 'convert'". How should I define functions?

I use Code::Blocks 16.01 and MinGW.

Here is my try:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>

#define LENGTH 4

int convert (char hex[]);
int result,validLength,j;
char input[LENGTH];

int main(){

    while(1){

        printf("Please enter valid hexadecimal number %d length:",LENGTH);
        scanf("%c", input);  //reading to char array

        for(j=0;j<LENGTH;j++)   // buffer
            if(input[j] > 48 && input[j] < 58)
                validLength++; 


        printf("Here is the decimal result: %d", convert(input[validLength]));

        validLength=0;

    }

    int convert(char hex[]){

        int t;

        for(t=0; t < validLength; t++)
            result= result+ hex[t]*(pow(16,(validLength-t)));

        return result;

    }

    return 0;
}
10
  • int convert(char hex[]){ ... } move this to outside of main.
    – BLUEPIXY
    Aug 9, 2017 at 17:42
  • convert(input[validLength]) --> convert(input)
    – BLUEPIXY
    Aug 9, 2017 at 17:44
  • scanf("%c", input); --> scanf(" %4c", input);
    – BLUEPIXY
    Aug 9, 2017 at 17:46
  • input[j] > 48 --> input[j] >= '0'
    – BLUEPIXY
    Aug 9, 2017 at 17:48
  • 1
    @KubilayCanDEMİR I tested that and it does work, maybe your implementation was faulty. But as BLUEPIXY uses in his code isxdigit is there in ctype.h for you. Aug 10, 2017 at 16:10

3 Answers 3

1

1] You cant have nested function. Move it out of the body.

2] Here

printf("Here is the decimal result: %d", convert(input[validLength]));

You are passing an char, but convert function expect's char*. Correct:

printf("Here is the decimal result: %d", convert(input));

3] %c specifier is for character's, use %s and better with how many chars to read %4s. Dont forget to check return value e.g.

if(scanf("%4s", input) != 1)
{
    // Fail
}
0

You have defined convert inside of main function - move it outside and it fix the error.

Also, in order to store scanf result of lenght 4 your input should be 5, since so called C-string is terminated with NULL. (E.g. when you type A0CD, values in input will be {'A', '0', 'C', 'D', 0}

0

This is invalid:

scanf("%c", input);

The %c format specifier will only read a single character. If you want to read a string, you need to use %s. Also, you should put a length modifier on this field to specify the maximum number of characters to read:

scanf("%4s", input);

You'll need to make input one element larger to have room for the terminating null byte.

Also, you attempted to define your convert function inside of your main function. Nested functions are not supported in standard C.

You need to move the definition of convert outside of main:

int convert (char hex[]);
int result,validLength,j;
char input[LENGTH+1];

int convert(char hex[]){

    int t;

    for(t=0; t < validLength; t++)
        result= result+ hex[t]*(pow(16,(validLength-t)));

    return result;

}

int main(){

    while(1){

        printf("Please enter valid hexadecimal number %d length:",LENGTH);
        scanf("%4s", input);  //reading to char array

        // initialize validLength, otherwise you increment an uninitialized value
        validLength = 0;
        for(j=0;j<LENGTH;j++)   // buffer
            if(input[j] > 48 && input[j] < 58)
                validLength++; 

        // null terminate after the invalid characters
        input[validLength] = 0;

        printf("Here is the decimal result: %d", convert(input));

        validLength=0;

    }

    return 0;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.