C题库2

输入输出、格式化

1.如下程序用于把“blue”字符串打印出来,请指出其中的错误:

#include <stdio.h>

void main()
{
	char pcBlue[] = {'b', 'l', 'u', 'e'/*, '\0'*/};
	printf("%s", pcBlue);
	return;
}

答案:缺少结束符

2.指出代码错误:

#include <stdio.h>

#define OK			0
#define ERR			1
#define ERROR		(-1)
#define BUFFER_SIZE	256
//
int GetMemory(char** ppszBuf, int num)
{
	if (NULL == ppszBuf)
	{
		ASSERT(0);
		return ERROR;
	}

	*ppszBuf = (char*)malloc(num);
	if (NULL == *ppszBuf)
	{
		return ERROR;
	}
	return OK;
}

void main(void)
{
	char* pcStr = NULL;
	if (OK == GetMemory(&pcStr,BUFFER_SIZE))//这里假定BUFFER_SIZE足够大,不会导致越界
	{
		scanf("%s",pcStr);
		free(pcStr);
	}

	return;
}

答案:要采用printf("%s", str)的形式打印,否则如果输入为%s, %d等形式可能会导致不可知现象。

 3.指出错误:

	char acMsg[16];
	strcpy(acMsg, "系统内核工作正常");
	printf("%s", acMsg);

答案:数组越界。

结构程序与循环控制

 1指出代码的错误:

unsigned long FUNC_B(unsigned long ulCount)
{
	unsigned long ulSum = 0;

	while (0 <= ulCount)
	{
		ulSum += ulCount;
		ulCount--;
	}

	return ulSum;
}

答案:unsigned long ulCount永远不会小于0,为死循环。
2.如下代码,首次处理DO_Something()后,每次调用FUNC_A函数,如果与上次执行DO_Something()的时间间隔超过TIME_INTERVAL毫秒,就再执行一次DO_Something()操作。(假设是在单任务下执行这个函数)

 指出代码错误:

#define ULoNG unsigned long
#define TIME_INTERVAL 200
void DO_Something(void)
{
	/*....*/
	return;
}

void FUNC_A()
{
	static ULONG ulPreCall = 0;
	ULONG ulNowInMsHigh = 0;
	ULONG ulNowInMsLow = 0;

	/*获取当前系统已经运行的时间,以毫秒为单位,用64bits表示
	,ulNowInMsHigh为高32位,ulNowInMsLow为低32位*/
	(VOID) VOS_Tm_Now(&ulNowInMsHigh, &ulNowInMsLow);

	if((0 == ulPreCall) || ulNowInMsHigh >= (ulPreCall + TIME_INTERVAL))
	{
		ulPreCall = ulNowInMsLow;
	}
	else
	{
		return;
	}

	DO_Something();

	return;
} 

 答案:没有判断时间的高位,ulNowInMsLow溢出后将不能执行到DO_Something。

数组、字符串

1.如下程序用于把“blue”字符串返回,请指出其中的错误:

char* GetBLUE(void)
{
	char* pcColor;
	char* pcNewColor;
	pcColor = "blue";

	//此处应该是(strlen(pcColor)+1)
	pcNewColor = (char*)malloc(strlen(pcColor));
	if (NULL == pcNewColor)
	{
		return NULL;
	}
	strcpy(pcNewColor, pcColor);

	return pcNewColor;
}

答案:申请内存空间不足,字符串结尾还有'/0'。strlen函数长度没有将“\0”包括进内。
2.输出为?

void example()
{
	int i;
	char acNew[20];

	for (i=0; i<10; i++)
	{
		acNew[i] = '0';
	}
	printf("%d\n", strlen(acNew));
	return;
}

答案:不确定。这里把字符0送到axNew[0:9]。但是axNew[10]没放\0,即从地10 个元素起,都是不定的元素。而strlen这个函数是遇到\0才会停止计数的。如果字符串总没有\0它就可能算到定义空间之外去。所以这个程序的结果是不可预测的。只有将\0赋给数组元素最后一个才会得到正确的字符串长度。考察数组初始化以及‘0’与0的区别。

3.输出为?

void example()
{
	int i;
	char acNew[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	for (i=0; i<10; i++)
	{
		acNew[i] = '0';
	}
	printf("%d\n", strlen(acNew));
	return;
}

答案:10。strlen()函数是求数组在内存中实际占有的空间大小,就是遇到'\0'就结束。char  acNew[20] = {0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};我们为数组分配可20个字节的大小,但是里面的值都是'\0',
for(i = 0; i < 10; i++)
      {
          acNew[i] = '0';
      }
这是我们为数组赋值10个字节的大小。后面的都是‘\0’,所以输出10。
strlen是计算到第一个字符串结束符'\0'为止的字符串长度,而'\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以在数值类型里就代表数字0。

再来看这个程序 char  acNew[20] = {0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //全部是0

  for(i = 0; i < 10; i++)
      {
          acNew[i] = '0';
      }
给acNew[0]~[9]赋'0',也就是ASCII码 48。所以strlen计算到acNew[10]就碰到了'\0',所以答案就是长度10了 符串长度。

 4.a,b,c,d值?

void main(void)
{
	char* pcColor = "blue1";
	char acColor[] = "blue1";

	int a = strlen(pcColor);//5
	int b = strlen(acColor);//5
	int c = sizeof(pcColor);//4
	int d = sizeof(acColor);//6

	return;
}

答案:而strlen不区分是数组还是指针,就读到\0为止返回长度。而且strlen是不把\0计入字。

5.a,b,c的值?

void main(void)
{
	char str[] = "\\\0";
	char*p = str;
	int n = 1000;
	int a = sizeof(str);//3。数组长度3,分别是:\,0,0
	int b = sizeof(p);//4
	int c = sizeof(n);//4

	return;
}

6.a,b,c值?

void main(void)
{
	char *pszTest = "hello";
	char aucTest[] = "hello";
	int a = sizeof(pszTest);
	int b = sizeof(*pszTest);
	int c = sizeof(aucTest);

	return;
}

答案:4,1,6。sizeof(*pszTest)=sizeof(UCHAR)

6.输出:

#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
#include <mbstring.h>

//#pragma pack(4)
#define CHAR char
#define ULONG unsigned long
#define VOID void

CHAR* VOS_strncpy(CHAR *pcDest, const CHAR *szSrc, ULONG ulLength)
{
	char *pcPoint = pcDest;

	if ((NULL == szSrc) || (NULL == pcDest))
	{
		return NULL;
	}

	while (ulLength && (*pcPoint = *szSrc))/*这里采用了在判断语句中赋值的方式(*pcPoint = *szSrc)
										   ,建议尽量不使用*/
	{
		pcPoint++;
		szSrc++;
		ulLength--;
	}

	if (!ulLength)
	{
		*pcPoint = '\0';
	}
	return pcDest;
}

void main(void)
{
	CHAR szStrBuf[] = "1234567890";
	CHAR szStrBuf1[] = "1234567890";
	strncpy(szStrBuf, "ABCD", strlen("ABCD"));
	VOS_strncpy(szStrBuf1, "ABCD", strlen("ABCD"));
	printf("Str1 = %s\nStr2 = %s", szStrBuf, szStrBuf1);

	return;
}

答案:Str1 = ABCD567890 Str2  = ABCD


 


 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值