Machine size judgment:
1 #include <stdio.h> 2 3 typedef union{ 4 char x; 5 int i; 6 }un; 7 8 int main() 9 {10 un tt; 11 tt.i = 1;12 13 if(tt.x == 1)14 { 15 printf("little-endian !\n");16 } 17 else18 { 19 printf("big-endian !\n");20 } 21 printf("tt.x = %d\n", tt.x);22 return 0;23 }
Implement atoi () and ITOA ()
1 # include <stdio. h> 2 3 int my_atoi (char * s) 4 {5 Int Sign = 1, num = 0; 6 switch (* s) 7 {8 case '-': 9 Sign =-1; 10 s ++; 11 break; 12 case '+': 13 s ++; 14 break; 15 default: 16 break; 17} 18 19 while (* s )! = '\ 0') 20 {21 num = num * 10 + (* s-'0'); 22 s ++; 23} 24 25 return num * sign; 26} 27 void ITOA (INT value, char * Str) 28 {29 int I, j; 30 char TMP = 0; 31 if (value <0) 32 {33 STR [0] = '-'; 34 value = 0-value; 35} 36 37 // in reverse order; 38 for (I = 1; value> 0; I ++, value/= 10) 39 {40 STR [I] = Value % 10 + '0 '; 41} 42 // array re-order 43 for (j = I, I = 1; I <= J/2; I ++) 44 {45 TMP = STR [I]; 46 STR [I] = STR [J-I]; 47 STR [J-I] = TMP; 48} 49/* 50 for (j = I-1, I = 1; j-I> = 1; j --, I ++) // stores digit characters in reverse order 51 {52 STR [I] = STR [I] ^ STR [J]; 53 STR [J] = STR [I] ^ STR [J]; 54 STR [I] = STR [I] ^ STR [J]; 55} 56 */57 if (STR [0]! = '-') // If it is not a negative number, you need to move the subscript of the number to the left, that is, minus 158 {59 for (I = 0; STR [I + 1]! = '\ 0'; I ++) 60 STR [I] = STR [I + 1]; 61 STR [I] =' \ 0 '; 62} 63 64} 65 66 void main () 67 {68 int value =-1212345; 69 char STR [10] = {'\ 0 '}; // remember to fill STR with '\ 0' 70 71 ITOA (value, STR); 72 printf ("the result is: % s \ n", STR); 73}