读《C程序设计语言》笔记5

字符串常量:

  字符串常量也叫字符串字面值,是用双引号括起来的0个或多个字符组成的字符序列。双引号不是字符串的一部分,它只用于限定字符串。

  从技术角度看,字符串常量就是字符数组。字符串的内部表示使用一个空字符'\0'作为串的结尾,因此,存储字符串的物理存储单元比括在双引号中的字符数多一个。这种表示方法也说明,C语言对字符串的长度没有限制,但程序必须扫描完整个字符串后才能确定字符串的长度。

  标准库函数strlen(s)可以返回字符串参数s的长度,但长度不包括末尾的'\0'。

  下面给出strlen()的两个版本:

1 int strlen(char s[])
2 {
3 int n;
4 n=0;
5 while(s[n]!='\0')
6 {
7 ++n;
8 }
9 return n;
10 }

  为什么代码不会自动排版了....无语

1 int strlen(char *s)
2 {
3 char *p=s;
4 while(*p!='\0')
5 {
6 p++;
7 }
8 return p-s;
9 }

  标准头文件:<string.h>中声明了strlen和其他字符串函数

  该头文件中含有这样一段代码:

#ifndef RC_INVOKED

#ifdef __cplusplus
extern "C" {
#endif

/*
* Prototypes of the ANSI Standard C library string functions.
*/
_CRTIMP
void* __cdecl memchr (const void*, int, size_t) __MINGW_ATTRIB_PURE;
_CRTIMP
int __cdecl memcmp (const void*, const void*, size_t) __MINGW_ATTRIB_PURE;
_CRTIMP
void* __cdecl memcpy (void*, const void*, size_t);
_CRTIMP
void* __cdecl memmove (void*, const void*, size_t);
_CRTIMP
void* __cdecl memset (void*, int, size_t);
_CRTIMP
char* __cdecl strcat (char*, const char*);
_CRTIMP
char* __cdecl strchr (const char*, int) __MINGW_ATTRIB_PURE;
_CRTIMP
int __cdecl strcmp (const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP
int __cdecl strcoll (const char*, const char*); /* Compare using locale */
_CRTIMP
char* __cdecl strcpy (char*, const char*);
_CRTIMP size_t __cdecl strcspn (
const char*, const char*) __MINGW_ATTRIB_PURE;
_CRTIMP
char* __cdecl strerror (int); /* NOTE: NOT an old name wrapper. */

_CRTIMP size_t __cdecl strlen (
const char*) __MINGW_ATTRIB_PURE;
...
}

  其中extern "C"的作用是:

  为了方便C++编译器兼容C程序,因为有些函数是用c编译器编译出来的,如果想让这些函数能在c++程序中使用,则需要告诉c++编译器。使用extern"c"就是这个目的,只要使用到c源文件中的函数、变量等,都要用extern"c"告知。

原文地址:https://www.cnblogs.com/wangzhiyu811/p/2104450.html