C基础(31——35)

来源:互联网 发布:mac mini macbook air 编辑:程序博客网 时间:2024/05/22 10:24

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 10void 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



本文出自 “追寻内心的声音” 博客,请务必保留此出处http://ljy789.blog.51cto.com/10697684/1832221

0 0
原创粉丝点击