Convert integer to string in C

Asked

Viewed 101 times

0

I have a problem with converting an integer to string in the scope of a function where the conversion return is given as null and I don’t know why.


#include <stdio.h>

char *getTexto(int num) {

    char numTxt[7];

    itoa(num, numTxt, 10);

    if ( num % 3 == 0 ) {
        if ( num % 5 == 0 ) {
            return "TicTac";
        }
        else {
            return "Tic";
        }
    }
    else if ( num % 5 == 0 ) {
        return "Tac";
    }
    else {
        return numTxt;
    }

}

int main() {

    for ( int x = 1; x <= 100; x++ ) {
        printf("\n%s", getTexto(x));
    }

    return 0;
}
  • Good morning @Noronha, welcome, see if this helps you https://answall.com/questions/436759/como-converter-string-para-int-em-c/436760#436760

  • vc can simplify by removing the last else and leave the return numTxt

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.