C/C ++ various data types conversion Summary

Source: Internet
Author: User

C/C ++ various data types conversion Summary

The following is a summary of C/C ++ data types commonly used in Windows/Linux systems:

 

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      int main(){// 1--> int to char[]int tmp1 = 100;char ch1[15];sprintf(ch1, %d, tmp1);std::cout<
      
        int to stringint tmp2 = 111;char ch2[15];sprintf(ch2, %d, tmp2);std::string str2;str2 = std::string(ch2);std::cout<
       
         int to enumenum enum3 {A,B};int tmp3 = 222;enum3 val3 = static_cast
        
         (tmp3);std::cout<
         
           char[] to stringchar arr4[] = this is a sample;std::string str4;str4 = std::string(arr4);std::cout<
          
            char to intchar ch5 = '8';int val5 = ch5 - '0';// val5 is bounded by 0 to 9std::cout<
           
             char[] to intchar arr6[] = 12345;int tmp6;sscanf(arr6, %d, &tmp6);std::cout<
            
              char* to intchar* pch7 = 444;int tmp7;tmp7 = atoi(pch7);std::cout<
             
               char* to floatchar* pch8 = 55.5;float tmp8;tmp8 = (float)atof(pch8);std::cout<
              
                char* to doublechar* pch9 = 66.666;double tmp9;tmp9 = atof(pch9);std::cout<
               
                 float to char[]float tmp10 = 11.11;char ch10[20];sprintf(ch10, %f, tmp10);std::cout<
                
                  int to char*int tmp11 = 777;char* pch11;char ch11[20];sprintf(ch11, %d, tmp11);pch11 = ch11;std::cout<
                 
                   double to char[]double tmp12 = 8.888;char arr12[20];sprintf(arr12, %f, tmp12);std::cout< char* to stringchar* pch13 = hello, world;std::string str13;str13 = std::string(pch13);std::cout<
                  
                    string to char[]std::string str14 = dog, cat;char arr14[255];strncpy(arr14, str14.c_str(), sizeof(arr14));arr14[sizeof(arr14) - 1] = 0;std::cout< string to const char*std::string str15 = ha ha;const char* pch15;pch15 = str15.c_str();std::cout<
                   
                     float to intfloat ftmp16 = 99.99;int tmp16;tmp16 = static_cast
                    
                     (ftmp16);// static_cast
                     
                      (ftmp16 + 0.5)std::cout<
                      
                       

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.