关于C语言中代码的一些小问题

Test_ 2012-06-04 06:58:57
本人C刚入门,遇到关于 gets;strlen(s);这两个代码名词,谁能解释下,谢了!!
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
博诺那 2012-06-05
  • 打赏
  • 举报
回复
刚写了一代码用到get(),LZ参考下
#include "stdafx.h"
#include<stdio.h>
int main(int argc, char* argv[])
{char a[10]="I study";
char b[21]=" computer technology";
int i=0;
char *p=a;
for(;a[i]!='\0';i++,p++)
{printf("%c",*p);
}
for(i=0,p=b;b[i]!='\0';i++,p++)
printf("%c",*p);
printf("\n");

}
strlen(s)用于测量字符串长度
strlen(字符数祖)

strlen是STRing LENgth(字符串长度)的缩写。它是测试字符串长度的函数。函数的值为字符串中的实际长度(不包括'\0'在内)。

例如:

char str[10]={"china"};
printf("%d",strlen(str));

输出的结果不是10,也不是6,而是5。也可直接测试字符串常量的长度,例如

strlen("china");
  • 打赏
  • 举报
回复
为什么不自己去查资料呢?

char * gets ( char * str );
Get string from stdin
Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.
The ending newline character ('\n') is not included in the string.
A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.
Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.


size_t strlen ( const char * str );
Get string length
Returns the length of str.
The length of a C string is determined by the terminating null-character: A C string is as long as the amount of characters between the beginning of the string and the terminating null character.
This should not be confused with the size of the array that holds the string. For example:
char mystr[100]="test string";
defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
diaolingle 2012-06-04
  • 打赏
  • 举报
回复
建议楼主去看一下书,基础内容,自己看比较好
proorck6 2012-06-04
  • 打赏
  • 举报
回复
这是两个库函数,准确地说不是C语言的内容。
但是不掌握一些库函数是不行的,
这相当于学习外语中的单词量,单词量大了才能写出好文章。
qq120848369 2012-06-04
  • 打赏
  • 举报
回复
fgets/strlen
W170532934 2012-06-04
  • 打赏
  • 举报
回复
gets是一个函数,用于获得一串字符串
strlen是一个函数,用于获得字符串的长度。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧