字符串

字符串( character string )是以空字符(/0)结尾的char数组。

字符串常量(string constant),是指位于一对双引号中的任何字符。双引号里德字符加上编译器自动提供的结束标志/0字符,作为

一个字符串被存储在内存里。

运行结果:

Hi! I'm Clyde the Computer. I have many talents.
Let me tell you some of them.
What were they? Ah, yes, here's a partial list.
Adding numbers swiftly
Multiplying accurately
Stashing data
Following instructions to the letter
Understanding the C language

 

Enough about me -- what's your name?
Nigel Barntwit
Well, Nigel Barntwit, You must have many talents. Tell me some.
Limit yourself to one line's worth.
If you can't think of anything, fake it.
Fencing,yodeling,malingering,cheese tasting,and sighing.
Let's see if I've got that list:
Fencing,yodeling,malingering,cheese tasting,and sighing.
Thanks for the information, Nigel Barntwit.

 

 

如果想在字符串中使用双引号,可以在双引号前加一反斜杠

printf(" /" Run spot,run!/"exclaimed./n");

 

 

字符串数组

      初始化数组可以不用指定数组大小,编译器能通过查找空字符来确定字符串结束,但是定义数组时必须指定数组大小

const char m2[]="if you can't think of anything,fate it";

 

#define LINELEN 81

char name[LINELEN];

 

 

 

char heart[ ]="I love Tillie!";

char *head="I love Tillie!";

 

for(i=0;i<6;i++)                       for(i=0;i<6;i++)

     putchar(heart[i]);                     putchar(head[i]);

 

for(i=0;i<6;i++)                       for(i=0;i<6;i++)     

    putchar(*(heart+1));                 putchar(*(head+1)); 

但是只有指针可以使用增量运算符

while(*(head)!='/0')

         putchar(*(head++));

在数组形式中heart是个地址常量,改变heart意味着更改数组存储的位置,可以使用heart+1标识数组下一个元素。

 

 

const char *mytal={

                                   "Adding numbers swiftly",

                                   "Multiplying accurately",

                                   "Stashing data",

                                   "Following instructions to the letter",

                                   "Understand the C language"

                              }

mytal是一个由5个指向char的指针组成的数组。mytal是一个一维数组,而且数组里的每一个元素都是一个char类型值的地址。

*mytal[0]=='A'     *mystal[1]=='M'  *mytal[2]=='S'

mytal[0]= "Adding numbers swiftly"

可以把mytal[0]看作表示第一个字符串,并被看作是地址,*mytal[0]表示第一个字符串的第一个字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值