#include <stdio.h>
void main()
{
int *a;
char s[20];
a=gets(s); //a表示取s的首地址
puts(a+2);
puts((int)a + 2);
}
puts(a+2)跳过了8个字符,puts((int)a + 2)只跳过2个字符
对地址的一些小思考
#include <stdio.h>
void main()
{
char *color[5]={"oneday","twoday","threeday",\
"fourday","fiveday"};
printf("%c",*(color[2]+2) ); //color[2]是个地址,用*指向r
printf("%c",color[2] ); //如果直接表示出来为0x403035,0x35代表数字5
}
关于argv的细节
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("hello");
printf("%s",argv[2]);
return 0;
}
写素数到文件
#include "stdio.h" //把1000以内的素数写入文件
#include <math.h>
#include <stdlib.h>
int issu(x)
{
int i=2;
while( (x%i!=0)&&(i<=sqrt(x)) ) i++;
if(i>sqrt(x)) return 1;
else return 0;
}
void main()
{
int i=3,count=1;
FILE *fp;
if((fp=fopen("iamtry.txt","w")) ==NULL)
{
printf("file can't open\n");
exit(0);
}
fprintf(fp,"%6d\t",2);
while(i<1000)
{
if(issu(i))
{
count++;
fprintf(fp,"%6d\t",i);
}
i+=2;
}
fprintf(fp,"\n总共有%d个素数",count);
if(fclose(fp) )
{
printf("can not close the file!\n");
exit(0);
}
}