Posts Tagged ‘c语言’

c problems series 1

1.
>老师你好,有个C语言问题请教。
> #include <stdio.h>
> main()
> {
>       union
>       {
>            char ch[2];
>            int d;
>       }s;
>       s.d=0x4321;
>       printf("%x,%x\n",s.ch[0],s.ch[1]);
>
> }
> 为什么输出21,43 而不是43,21
>
>
这个问题不复杂,考到了数据在内存中如何存储问题。数据在内存中存储是从低位到高位,也
就是说先存储的21,在存储的43.所以按序输出时候就输出这个结果了。

2.
%ns和%.ns
从 Planet Chinese Security Community 作者:void@planetchinesesecuritycommunity.org(void)
这段代码有什么问题?
void func(char *p) {
   char buf[10+1];
   memset(buf,0,sizeof(buf));

   // limit string to 10 chars
   sprintf(buf,"%10s",p);
   printf("Hello, %s\n",buf);
}

答案:
%10s只是用于宽度对齐,超过10字节,仍然全部拷贝,导致溢出.
%.10s才是限制了拷贝长度为10字节
 

Damn it ~

今天很倒霉哦,上了实验课去图书馆借书,因为我们同学穿的是拖鞋,而我恰好看到图书馆的门口写着:拖鞋不能入内~!
就叫他在外边等我,我怕别人等急了,就搞得很匆忙,结果忙中出乱:我的借书证不见了,Damn~!
白白浪费了别人和自己的时间20+20=40分钟。下次还要到图书馆去补办,爽了~
今天看了张NCRE200604的笔试,一下是需要注意的地方。

1.  char str[]; -->错误  storage size of `str' isn't known 

2.-------------------------------------------
#include <stdio.h>
int fun(int x[],int n)
{
    static int sum=0,i;
    for(i=0;i<n;i++) sum+=x[i];
    return sum;
}
main()
{
      int a[]={1,2,3,4,5},b[]={6,7,8,9},s=0;
      s=fun(a,5)+fun(b,4);printf("%d\n",s);
}
------------------------------------------------
如果sum定义为static,结果为60,如果去掉static,结果为45。因为static的局部变量的定义,
是sum的值保持再内存中不变,第一次fun(a,5)后其值为15,fun(b,4)是在15的基础上继续加上45而得到60.

3.
#include <stdio.h>
main()
{
      int y=10;
      while(y--);
      printf("y=%d\n",y);
}

while(y--);
先看y是不是0如果不是0,就执行while loop,然后y=y-1;
当y=0;就退出while loop,然后y=y-;
所以y最后得-1;

-------------
4.
    #include
    main()
    {
          union
          {
               char ch[2];
               int d;
          }s;
          s.d=0x4321;
          printf("%x,%x\n",s.ch[0],s.ch[1]);

    }这个输出为什么不是43,21? 

s所占空间为4Byte是..
可以做int用或了个字节 ch[2];
s.d=0x4321;
addr  value
00   0x21
01   0x43
02   0x00
03   0x00
输出是21,43..涉及到汇编,以后再说。
 

最近研究的c语言的数个实例,很菜的哦

1:#include “stdio.h”
void main()
{
printf(“你好啊  我靠!\n”);
}
2:
#include <stdio.h>
void main()
{
int a,b;
a=485555;
b=3825;
printf(“a=%d\n”,a);
printf(“%d%d\n”,a,b);
printf(“%d%8d\n”,a,b);
}
3:
#include <stdio.h>
void main()
{
int A,B,a,b;
printf(“输入一个数,然后按回车键”);
scanf(“%d”,&A);
printf(“输入一个数,然后按回车键”);
scanf(“%d”,&B);
printf(“先输入一个数,按空格键后,再输入第二个数,再按回车键”);
scanf(“%d %d”,&a,&b);
printf(“%d%8d\n”,A,B);
printf(“%d%8d\n”,a,b);
}
4:
#include <stdio.h>
#define RED 0
#define GREEN 1
#define BLUE 2
void main()
{
int a=66;
char c=’A';
double salary;
char name[20]=”张国强  男  28岁”;
salary=99.99;
printf(“%s\n”,name);
printf(“工资 = %8.2f\n”,salary);
printf(“\n%d,%x,%c\n”,a,a,a);
printf(“%d,%x,%c\n\n”,c,c,c);
printf(“红色%2d    绿色%2d    蓝色%2d\n”,RED,GREEN,BLUE);
printf(“\n”);
}
5:
#include <stdio.h>
#define RED 0
#define GREEN 1
#define BLUE 2
void main()
{
int a;
char name[20]=”张国强 男 28岁”;
a=sizeof(char);
printf(“size of char = %d\n”,a);
a=sizeof(int);
printf(“size of int = %d\n”,a);
a=sizeof(short int);
printf(“size of short int = %d\n”,a);
a=sizeof(float);
printf(“size of float = %d\n”,a);
a=sizeof(double);
printf(“size of double = %d\n”,a);
a=sizeof RED;
printf(“\nsize of RED = %d\n”,a);
a=sizeof name;
printf(“size of \’name\’ = %d\n”,a);
a=sizeof “file name”;
printf(“size of \”file name\” = %d\n\n”,a);
}
6:
#include <stdio.h>
void main()
{
printf(“%d\n”,’A');    //字符A
printf(“%d\n”,’\x41′);  //字符A
printf(“\n%d\n”,’\”‘);  //双引号
printf(“%d\n”,’\”);   //单引号
printf(“%d\n”,’\\’);   //反斜线
printf(“\n%d\n”,’\a’);  //响铃
printf(“%d\n”,’\b’);   //退格
printf(“%d\n”,’\t’);    //tab
printf(“%d\n”,’\n’);   //回车换行
printf(“%d\n”,’\v’);   //垂直制表
printf(“%d\n”,’\f’);   //换页
printf(“%d\n\n”,’\r’);   //回车
}
7:
#include <stdio.h>
int a=66;
void main()
{
int data[3] = {100,201,3300};
static char str[ ]={‘A’,'b’,67,68,’\0′};
auto char name[ ]=”张国强 男 28岁”;
printf(“%d,%d,%d\n”,data[0],data[1],data[2]);
printf(“%s\n”,str);
printf(“%s\n”,name);
printf(“\n”);
a=sizeof data;
printf(“size of \’data\’ = %d\n”,a);
a=sizeof str;
printf(“size of \’str\’ = %d\n”,a);
a=sizeof name;
printf(“size of \’name\’ = %d\n”,a);
printf(“\n”);
printf(“请输入你的身份证号码\n”);
gets(str);          //允许输入的数字间有空格
printf(“\n身份证号码: %s\n\n”,str);
puts(str); //和printf功能相同,
}
8:
#include “stdio.h”
void main()
{
double a,b,c;
a=1155+112.3+11.4+20.5+10.2+5.2+10.1;
b=a/110.3;
c=2450*a;
printf(“\n总使用面积=%f 平方米”,a);
printf(“利用率=%f %c\n\n”,b*100,36);
printf(“售价=%f 元\n\n”,c);
}
就是这些了,才在研究所以都是一些简单的撒.
20081108评论:原来我是2006年开始学C语言的