lecture11一字符串 绳伟光 上海交通大学微纳电子学系 2017年04月07日
lecture 11 —— 字符串 绳伟光 上海交通大学微纳电子学系 2017年04月07日
内容 ■字符串 ■字符串函数 8 2
字符串 字符串函数 内容 2
字符串(String简介 字符串包括字符串常量(string constants,or literals in C standard)和字符串变量(string variables) 字符串:Strings are arrays of characters in which a special character(the null character)marks the end ■C标准库另提供了一系列操作字符串的函数 3
3 字符串(String)简介 字符串包括字符串常量(string constants, or literals in C standard)和字符串变量(string variables) 字符串:Strings are arrays of characters in which a special character(the null character)marks the end C标准库另提供了一系列操作字符串的函数
字符串字面量(String Literals) 字符串字面量是一系列括在双引号内的字母,可表示 有意义或无意义的句子 "When you come to a fork in the road,take it." ■字符串字面量中可以包含转义序列,就像在printf和 scanf的格式串中那样 ■ 包含转义序列字符串示例: "Candy\nls dandy\nBut liquor\nls quicker.\n --Ogden Nash\n" 实际打印出来效果如下: Candy Is dandy But liquor Is quicker. --Ogden Nash
4 字符串字面量(String Literals) 字符串字面量是一系列括在双引号内的字母,可表示 有意义或无意义的句子 "When you come to a fork in the road, take it." 字符串字面量中可以包含转义序列,就像在printf和 scanf的格式串中那样 包含转义序列字符串示例: "Candy\nIs dandy\nBut liquor\nIs quicker.\n --Ogden Nash\n" 实际打印出来效果如下: Candy Is dandy But liquor Is quicker. --Ogden Nash
字符串字面量续行 1当字符串字面量分布在多行,可以用反斜杠来续行: printf("When you come to a fork in the road,take it.\ --Yogi Berra"); 反斜杠八更一般的用途是连接多行程序为一行 ■ 字符串字面量续行更好的办法:将两个字符串放置在 一起,编译器会自动进行拼接形成一个字符串 printf("When you come to a fork in the road,take it. "--Yogi Berra"); 5
5 字符串字面量续行 当字符串字面量分布在多行,可以用反斜杠\来续行: printf("When you come to a fork in the road,take it. \ --Yogi Berra"); 反斜杠\更一般的用途是连接多行程序为一行 字符串字面量续行更好的办法:将两个字符串放置在 一起,编译器会自动进行拼接形成一个字符串 printf("When you come to a fork in the road,take it. " "--Yogi Berra");