大端:數據的高位字節存放在地址的低端,低位字節存放在地址的高端
小端:數據的高位字節存放在地址的高端,低位字節存放在地址的低端
即
大端是按照數字的書寫順序進行存儲
小端是顛倒書寫順序進行存儲
#include <stdio.h> #include <stdlib.h> void test() { int a=1; char* p=(char *)(&a); if(*p==1) printf( "Little_Endian\n"); else printf( "Big_Endian\n"); } int main() { test(); system( "pause"); return 0; } //一個時間點,只有一個變量可用 //union UN //{ // char c; // int i; //}un; //int main() //{ // un.i=1; // if(un.c==1) // printf("Little_Endian\n"); // else // printf("Big_Endian\n"); // // system("pause"); // return 0; //}
結果:
#include <stdio.h> #include <stdlib.h> void test() { int a=0; int b=0; printf( "a="); scanf( "%d",&a); printf( "b="); scanf( "%d",&b); a=a+b; b=a-b; a=a-b; printf( "交換之後\na=%d\nb=%d\n" ,a,b); } int main() { test(); system( "pause"); return 0; }
結果:
#include <stdio.h> #include <stdlib.h> void test() { int i=1; int count=0; for(;i<=100;i++) { int a=i; while(a) { if(a%10==9) count++; a=a/10; } } printf( "total=%d\n",count); } int main() { test(); system( "pause"); return 0; }
結果:
#include <stdio.h> #include <stdlib.h> #define N 10 void test() { int i=0; int arr[N ]={0}; int count=0; printf( "請輸入一個數:" ); scanf( "%d",&i); if(i==0) printf( "%d",0); while(i) { arr[count]=i%10; count++; i=i/10; } for(int m=count;m>0;m--) { printf( "%d ",arr[m-1]); } printf( "\n"); } int main() { test(); system( "pause"); return 0; }
結果:
#include <stdio.h> #include <stdlib.h> //相當於庫函數中的strchr,strchr()將會找出str字符串中第一次出現的字符c的地址,然後將該地址返回 //如果找到指定的字符,則返回該字符所在的地址,否則返回NULL //如果查找某字符在字符串中最後一次出現的位置,可使用strrchr()函數 int Find_Index(char * s,char c) { int index=0; while(*s !='\0') { if(*s ==c) return index+1; s++; index++; } return NULL ; } char* Find_Address(char * s,char c) { int index=0; while(*s !='\0') { if(*s ==c) return s ; s++; index++; } return NULL ; } void test() { char* str="0a,20b,123*cd,ef." ; int i=Find_Index(str,'0' ); char* p=Find_Address(str,'0' ); printf( "%d\n",i); printf( "%p\n",p); } int main() { test(); system( "pause"); return 0; }
結果: