Make use of the log10
function to determine the number of digits and do like below:
char * toArray(int number) { int n = log10(number) + 1; int i; char *numberArray = calloc(n, sizeof(char)); for (i = n-1; i >= 0; --i, number /= 10) { numberArray[i] = (number % 10) + '0'; } return numberArray; }
Or the other option is sprintf(yourCharArray,"%ld", intNumber);
Related Posts:
- What does “collect2: error: ld returned 1 exit status” mean?
- How to convert an int to string in C?
- Why should we typedef a struct so often in C?
- What is the difference between ++i and i++?
- Why does ENOENT mean “No such file or directory”?
- Floating point exception (core dumped)
- Stack smashing detected
- Openssl : error “self signed certificate in certificate chain”
- How to convert a string to integer in C?
- What is the behavior of integer division?
- Difference between malloc and calloc?
- What causes the Broken Pipe Error?
- Why does the terminal show “^[[A” “^[[B” “^[[C” “^[[D” when pressing the arrow keys in Ubuntu?
- Bad File Descriptor with Linux Socket write() Bad File Descriptor C
- What is the difference between char s[] and char *s?
- Why use bzero over memset?
- What does “1e” mean?
- Warning comparison between pointer and integer
- The importance of c enumeration (typedef enum) [duplicate]
- What does #pragma once mean in C? [duplicate]
- Warning comparison between pointer and integer
- what is the meaning of == sign?
- What does “control reaches end of non-void function” mean?
- Printing long int value in C
- How do I use extern to share variables between source files?
- In C programming, what is `undefined reference`error, when compiling?
- warning: implicit declaration of function
- Process finished with exit code 11 | Error during malloc [duplicate]
- C read file line by line
- How does wait(NULL) exactly work?
- Returning string from C function
- Difference between scanf() and fgets()
- How to check if a string is a number?
- What is a bus error? Is it different from a segmentation fault?
- Xcode – Warning: Implicit declaration of function is invalid in C99
- Does connect() block for TCP socket?
- how to convert negative hexadecimal to decimal
- where does stdio.o live in linux machine?
- What’s the equivalent of new/delete of C++ in C?
- Understanding INADDR_ANY for socket programming
- C read file line by line
- How do I lowercase a string in C?
- What does “Size in TCHARs” means?
- The Definitive C Book Guide and List[
- Python ? (conditional/ternary) operator for assignments
- conflicting types error when compiling c program using gcc
- Get the current time in C
- C read file line by line
- Copying a part of a string (substring) in C
- Incompatible implicit declaration of built-in function ‘malloc’
- What is a file with extension .a?
- What is the difference between exit(0) and exit(1) in C?
- What does “%.*s” mean in printf?
- Why is %c used in C?
- Display value found at given address gdb
- Does C have a “foreach” loop construct?
- What’s wrong with my code? What is argv[1]?
- Expected declaration specifier error in function
- Does sizeof return the number of bytes or the number of octets of a type in C?
- Reversing a string in C
- How to remove the character at a given index from a string in C?
- Zero an array in C code
- Expected declaration specifier error in function
- strcmp giving segmentation fault
- What does “pointer being freed was not allocated” mean exactly?
- (.text+0x20): undefined reference to `main’ and undefined reference to function
- Writing binary number system in C code
- Error: initializer element is not computable at load time
- Check if input is integer type in C
- Return a `struct` from a function in C
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- How to simply convert a float to a string in c?
- How does one represent the empty char?
- Scanning Multiple inputs from one line using scanf
- Removing trailing newline character from fgets() input
- hash function for string
- double free or corruption (!prev) error in c program
- xorl %eax – Instruction set architecture in IA-32
- What does this GCC error “… relocation truncated to fit…” mean?
- Printf width specifier to maintain precision of floating-point value
- Which of sprintf/snprintf is more secure?
- Two or more data types in declaration specifiers error
- how use EOF stdin in C
- ‘sprintf’: double precision in C
- C printing bits
- How does the strtok function in C work? [duplicate]
- Error: “Access not within mapped region at address” (Valgrind)
- Allocating char array using malloc
- Implementation of strtok() function
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- typedef struct pointer definition
- How does the fscanf function work?
- Removing last character in C
- The code does not execute properly. Try to figure out why
- error: ISO C forbids nested functions – What’s wrong?
- Dereference void pointer
- What is char ** in C? [duplicate]
- Carriage return in C?
- How to Compare 2 Character Arrays [duplicate]
- Difference between “while” loop and “do while” loop