C基礎(31——35)

wKiom1ecxprhN9GLAACit6UEtsQ485.png

大端:數據的高位字節存放在地址的低端,低位字節存放在地址的高端

小端:數據的高位字節存放在地址的高端,低位字節存放在地址的低端

大端是按照數字的書寫順序進行存儲

小端顛倒書寫順序進行存儲

wKioL1ecxs3Qza9zAAAWSa6_Vgs621.png

#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;
//}

結果:

wKiom1ecxvmgKpqnAAALFnJUlj4541.png


wKioL1ecxxewan4WAABqP1jFZPI278.png

#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;
}

結果:

wKiom1ecx1nw3E5eAAAPev7QZzk695.png


wKiom1ecx3nzkTk1AABg3W6dP7M691.png

#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;
}

結果:

wKioL1ecx5-hUKkjAAAJfskD9s4539.png


wKiom1ecx7qT4hy8AACwebj6nNo724.png

#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;
}

結果:

wKioL1ecyHmQWnQNAAAMCu8Qntk618.png


wKioL1ecyJ6QiTAeAACUX5HBfaA475.png

#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;
}

結果:

wKiom1ecyN2hR7TWAAAQJ8tr674786.png



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章