c primer plus----第4章 字符串和格式化输入/输出(一)
//==============================================================================
#include <stdio.h> #include <string.h> #define DENSITY 62.4 int main() { float weight, volume; int size, letters; char name[40]; printf("Hi! What is your name?\n"); scanf("%s", name); //指针 printf("%s, what is your weight in pounds?\n", name); scanf("%f", &weight); //取地址 size = sizeof name; letters = strlen(name);//获取字符串的长度 volume = weight / DENSITY; printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume); printf("Also,your first name has %d letters.\n", letters); printf("and we have %d bytes to store it in.\n", size); return 0; }
//字符串简介 #include <stdio.h> #define PRAISE "what a super narvelous name!" int main(void) { char name[40]; printf("What is your name?\n"); scanf("%s", name); printf("Hello, %s, %s\n", name, PRAISE); return 0; } // \0空字符,不是数字0,是非打印字符,ASCII码的值为0 //scanf()开始读取以后会在遇到第一个空白字符空格,制表符,或者换行符处停止读取 //scanf()只会把一个单词而不是整个语句作为字符串读入 //"x",'x'的区别:1)'x'是基本类型char "x"是派生类型char数组,2)"x"是由两个字符'x','\0'组成