#include <stdio.h>
#include <stdlib.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello, World! \n");
int a=1;
int b=1;
int c=1;
c=a+++(++b);
printf("a=%d\n",a);
printf("b=%d\n",b);
printf("c=%d\n",c);
return 0;
}
ANS:
Hello, World!
a=2
b=2
c=3
#include <stdio.h>
int main()
{
int c;
printf("c=%d\n",c);
typedef union{long i;int k[5];char c;}DATE;/*typedef不能少*/
DATE max;
c=sizeof(max);
printf("c=%d\n",c);
struct date{int cat;DATE cow;char duck;double dog;}too;/*typedef不能少*/
c=sizeof(struct date);
printf("c=%d\n",c);
return 0;
}
ANS:
c=0
c=24
c=48
#include <stdio.h>
int main()
{
int i;
int c=0;
printf("c=%d\n",c);
/*for(;;)*/ /*无限循环,打开后此程序运行结果为null,与for(;1;)一致 */
c++;
printf("c=%d\n",c);
return 0;
}
ANS:
c=0
c=1
#include <stdio.h>
#include <string.h>//no need
#include <malloc.h>//no need
int main()
{
int i;
int c=0;
int str_len;
printf("c=%d\n",c);
char *src="abcdef";
char *s;
printf("str string=%s\n",src);
c=sizeof(*src);
printf("poniter size date=%d\n",c);
c=sizeof(src);
printf("pointer size=%d\n",c);
str_len=strlen(src);
printf("str_len=%d\n",str_len);
s=&src[str_len-1];/////last char
printf("pointer to %s\n",&src[str_len-1]);
char *dest = (char *)malloc(str_len+1);
char *d=dest;
for(i=0;i<str_len;i++) //while(str_len--!=0)
{
*d++=*s--;
//d++;
//s--;
}
*d='\0';
//*d=0;
printf("dest string=%s\n",dest);
free (dest);
dest=NULL;
return 0;
}
ANS:
c=0
str string=abcdef
poniter size date=1
pointer size=8
str_len=6
pointer to f
dest string=fedcba
#include <stdio.h>
#include <string.h>//no need
#include <malloc.h>//no need
int main()
{
int i;
int c=0;
int str_len;
printf("c=%d\n",c);
char *src="abcdef";
char *s;
printf("str string=%s\n",src);
c=sizeof(*src);
printf("poniter size date=%d\n",c);
c=sizeof(src);
printf("pointer size=%d\n",c);
str_len=strlen(src);
printf("str_len=%d\n",str_len);
s=&src[str_len-1];/////last char
printf("pointer to %s\n",&src[str_len-1]);
//printf("pointer to %d\n",&src[str_len-1]);// 输出地址
//printf("pointer to %d\n",&src[str_len-2]);// 输出地址
//*src='\0';
//src=NULL;
//*src[str_len-2]='\0';
printf("str string=%s\n",src);
char *dest = (char *)malloc(str_len+2);
char *d=dest;
//for(i=0;i<str_len;i++)
while(str_len--!=0)
{
*d++=*s--;
//d++;
//s--;
}
//*d='c';//多分配一个字符,为什么可以??
//d++;//指向下一个位置,
*d='\0';
//*d=0;
printf("dest string=%s\n",dest);
//*dest='a'; //第一个内存值赋值;为什么不可以??
//dest++;
//*dest='\0';
printf("dest string=%s\n",dest);
free (dest);
dest=NULL;
//src="abcdeffffg";
////*src[0]='\0';/////字符串常量赋值报错
src='\0';//ok相当于指针null,同src=0;
printf("str string=%s\n",src);
return 0;
}
ANS:
c=0
str string=abcdef
poniter size date=1
pointer size=8
str_len=6
pointer to f
str string=abcdef
dest string=fedcba
dest string=fedcba
str string=(null)
#include <stdio.h>
#include <string.h>//no need
#include <malloc.h>//no need
int main()
{
int i;
int a[5]={1,2,3,4,5};
char b[5]={7,8,9,10,11};
int c=0;
int *temp;
for(i=0;i<5;i++)
printf("a[%d] address is %d\n",i,&a[i]);
/* for(i=0;i<5;i++)
printf("b[%d] address is %d\n",i,&b[i]);
for(i=0;i<5;i++)
printf("a[%d] value is %d\n",i,*(a+i));
*/ for(i=0;i<5;i++)
printf("b[%d] value is %d\n",i,*(b+i));
int *ptr=(int *)(&a+1);
printf("b[%d] value is %d\n",i,(&a+1));//越过整个数组
printf("pointer to value is %d\n",*(ptr-1));
temp=a; // 同temp=&a[0];
printf("temp pointer address is %d\n",temp+1);//越过一个数组元素
return 0;
}
ans:
a[0] address is -1707217456
a[1] address is -1707217452
a[2] address is -1707217448
a[3] address is -1707217444
a[4] address is -1707217440
b[0] value is 7
b[1] value is 8
b[2] value is 9
b[3] value is 10
b[4] value is 11
b[5] value is -1707217436
pointer to value is 5
temp pointer address is -1707217452
#include <stdio.h>
#include <string.h>//no need
#include <malloc.h>//no need
int CheckCPU()
{
/* typedef union {int a;char b;}c;//c是union类型
c max;
max.a=1;
return(max.b==1); */ //和下面的结果一样
union DATA{int a;char b;}c;//c是union变量,可省略DATA;
//union DATA mm;//另外定义一个mm union变量
c.a=1;
return(c.b==1);
}
int main()
{int xx;
/* typedef union {int a;char b;}c;//c是union类型
c max;
max.a=1;
return(max.b==1); */
union DATA{int a;char b;}c;//c是union变量,可省略DATA;
//union DATA mm;//定义一个mm union变量
xx=sizeof(union DATA); //xx=sizeof(c);结果一样
printf("%d\n",xx);
if(CheckCPU())
printf("Little_endian\n");
else
printf("Big_endian\n");
return 0;
}
ans:
4
Little_endian
#include <stdio.h>
#include <string.h>//no need
#include <malloc.h>//no need
int main()
{int i;
int c=0;
int str_len;
const char *src="abcdef";//字符串常量 const关键字
printf("src=%s\n",src);
//src[0]='B';//赋值不合法
printf("src=%s\n",src);
}
ans:
src=abcdef
src=abcdef
//int a=1;
char b[10];
int *str="rffffabc";
printf("%d\n",strlen(str));
int c;
c=strlen(b);
printf("%d\n",strlen(b));
c=sizeof(b);
printf("%d\n",c);
下面就说说C语言程序内存分配中的堆和栈,这里有必要把内存分配也提一下,大家不要嫌我啰嗦,一般情况下程序存放在Rom(只读内存,比如硬盘)或Flash中,运行时需要拷到RAM(随机存储器RAM)中执行,RAM会分别存储不同的信息,如下图所示:
内存中的栈区处于相对较高的地址以地址的增长方向为上的话,栈地址是向下增长的。
栈中分配局部变量空间,堆区是向上增长的用于分配程序员申请的内存空间。另外还有静态区是分配静态变量,全局变量空间的;只读区是分配常量和程序代码空间的;以及其他一些分区。
来看一个网上很流行的经典例子:
内存中的栈区处于相对较高的地址以地址的增长方向为上的话,栈地址是向下增长的。
栈中分配局部变量空间,堆区是向上增长的用于分配程序员申请的内存空间。另外还有静态区是分配静态变量,全局变量空间的;只读区是分配常量和程序代码空间的;以及其他一些分区。
来看一个网上很流行的经典例子:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void getmemory(char **p,int num)
{*p=(char*)malloc(num);
}
int main()
{char *str=NULL;
getmemory(&str,100);
strcpy(str,"hello");
printf("%s\n",str);
free (str);
if(str!=NULL)
// strcpy(str,"world");
printf("111 \n");
printf("%s\n",str);
strcpy(str,"world");
printf("%s\n",str);
return 0;
}
ans:
hello
111
world
#include <stdio.h>
#include <string.h>
int main()
{
char a[5]="ABCD";
int i;
for(i=0;i<strlen(a);i++)
{printf("%c\n",a[i]);
printf("%d\n",a[i]);
printf("%x\n",a[i]);
printf("\n");
}
return 0;
ans:
A
65
41
B
66
42
C
67
43
D
68
44
#include <stdio.h>
#include <string.h>
int main()
{
char b[10]="ab";//不初始化,str_len()为不确认,有试验到0 4 定义在指针后和指针前试验出不一样,why char b[10];
const int *str="rffffabc";
printf("%d\n",strlen(b));
printf("%d\n",sizeof(b));
printf("%d\n",strlen(str));
printf("%d\n",sizeof(str));
return 0;
}
2
10
8
8
#include <stdio.h>
#include <string.h>
int main()
{
int b[5]={1,2,3,4,5};
int *str=b;
int a=10;
printf("%d\n",sizeof(b));//字符串长度+1
printf("%d\n",sizeof(str));
printf("%d\n",sizeof(a));
printf("%d\n",b);
printf("%d\n",str);
printf("%d\n", (str-1));
printf("value %d\n", *(str-1));///做法好像是不对的
printf("\n");
int *q=str-1;
printf("%d\n",q);
printf("%d\n",*q);
printf("value %d\n", *(str-1));///做法好像是不对的
int *z=(int*)(str-1);//取某地址里寸的变量
*z=100;
printf("%d\n",z);
printf("%d\n",*z);
printf("%d\n",q);
printf("%d\n",*q);
//printf("value %d\n", *(str-1));
//报错signal: segmentation fault (core dumped)