构造一个字符串,使得程序运行后输出如下:
请输入构造的串:
len = [1361654881]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int len = 0;
char str[8] = {0};
printf("请输入构造的串\n");
scanf("%s", str);
printf("len = [%d]\n", len);
return 0;
}
你现在的程序len从头到尾一直处于定义时赋值的0的状态
不同的编译器不一样,我的VS2010字符串分配16个字节
// Q1057539.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int x = 1361654881;
for (int i = 0; i < 4; i++)
{
printf("%c", (char)x % 256); x /= 256;
}
printf("\n");
int len = 0;
char str[8] = {0};
printf("%p %p \n", str, &len);
printf("请输入构造的串\n");
scanf("%s", str);
printf("len = [%d]\n", len);
return 0;
}
所以输入的是 1111111111111111a4)Q (前面16个字符随意),你别的编译器可以参考这个思路