Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to convert int to char array in C++

3 Answers

0 votes
#include <iostream>

int main() {
    int n = 1234;

    std::string s = std::to_string(n);
    char const *arr = s.c_str();
    
    for (int i = 0; arr[i]; i++)
        std::cout << arr[i] << " ";
 
    return 0;
}
 
 
 
 
/*
run:
 
1 2 3 4 
 
*/

 



answered May 16, 2021 by avibootz
0 votes
#include <iostream>
#include <sstream>

int main() {
    int n = 1234;

    std::stringstream s;
    s << n;

    char const *arr = s.str().c_str();
    
    for (int i = 0; arr[i]; i++)
        std::cout << arr[i] << " ";
 
    return 0;
}
 
 
 
 
/*
run:
 
1 2 3 4 
 
*/

 



answered May 16, 2021 by avibootz
0 votes
#include <iostream>
#include <charconv>

int main() {
    int n = 1234;
    char arr[10] = "";

    std::to_chars(arr, arr + 10, n);
    
    for (int i = 0; arr[i]; i++)
        std::cout << arr[i] << " ";
 
    return 0;
}
 
 
 
 
/*
run:
 
1 2 3 4 
 
*/

 



answered May 16, 2021 by avibootz

Related questions

1 answer 145 views
1 answer 111 views
111 views asked Sep 20, 2021 by avibootz
1 answer 171 views
1 answer 169 views
169 views asked Feb 28, 2021 by avibootz
1 answer 244 views
244 views asked Jun 13, 2017 by avibootz
2 answers 197 views
197 views asked Apr 2, 2017 by avibootz
1 answer 163 views
...