C语言之字符串与运算符篇

xiaoxiao2021-02-28  75

字符串简介

字符串(character string)是指一个或多个字符的序列。例如:

"Zing went the strings of my heart!"     双引号不是字符串的一部分。它们只是通知编译器其中包含了一个字符串,正如单引号标识着一个字符一样。

    C没有为字符串定义专门的变量类型,而是把它存储在char数组中。字符串中的字符存放在相邻的存储单元中,每个字符占用一个单元。数组最后一个位置显示字符\0,这个字符是空字符(null character),C用它来标记字符串的结束。该字符的存在意味着数组的单元数必须至少比要存储的字符数多1。

字符和字符串

    字符串常量"x"与字符常量'x'不同。第一个区别是'x'属于基本类型(char),而"x"属于派生类型(char数组)。第二个区别是"x"实际上由两个字符('x'和空字符'\0')组成。 

#include <stdio.h> #define SEC_PER_MIN 60            // seconds in a minute  int main(void) {     int sec, min, left;     printf("Convert seconds to minutes and seconds!\n");     printf("Enter the number of seconds (<=0 to quit):\n");     scanf("%d", &sec);            // read number of seconds      while (sec > 0)      {         min = sec / SEC_PER_MIN;  // truncated number of minutes         left = sec % SEC_PER_MIN; // number of seconds left over         printf("%d seconds is %d minutes, %d seconds.\n", sec,                  min, left);         printf("Enter next value (<=0 to quit):\n");         scanf("%d", &sec);                }     printf("Done!\n");        return 0; }

转载请注明原文地址: https://www.6miu.com/read-71169.html

最新回复(0)