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

字符串简介

字符串(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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值