#include<stdio.h> int main(void) { int f = 4; int g = 5; float h = 5.0f; printf("%d\n", f, g); printf("%d %d\n", f); printf("%d %f\n", h, g); return 0; }
上面的代码是错误的。参数不正确。
第四章学习知识点:
函数:
strlen()
关键字:
const
字符串:
如何创建和存储字符串
如何使用scanf()和printf()读取和显示字符串
如何使用strlen()函数获取字符串的长度
使用C预处理器的#define 指令和ANSIC的const修饰符创建符号常量。
遇到strlen函数时计算占的空间大小,sizeof是计算多少个字节。
头文件:string.h
格式:strlen (字符数组名)
功能:计算字符串s的(unsigned int型)长度,不包括'\0'在内
说明:返回s的长度,不包括结束符NULL。
#include<stdio.h> #include<string.h> #define DENSITY 62.4 int main() { float weight, volume; int size,letters; char name[40]; printf("Hi! What's your first name?\n"); scanf("%s", name); printf("%s, what's 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; }
上面用到了strlen()和sizeof
sizeof是关键字。
#include<stdio.h> #define PI 3.14159 int main(void) { float area, circum, radius; printf("What is the radius of your pizza?\n"); scanf("%f", &radius); area = PI * radius * radius; circum = 2.0 * PI * radius; printf("Your basic pizza parameters are as follows:\n"); printf("circumference = %1.2f, area = %1.2f\n", circum, area); return 0; }
下面这个就是注意PRINTF的参数:
#include<stdio.h> #define PAGES 931 int main(void) { printf("*%d*\n", PAGES); printf("*%2d*\n", PAGES); printf("*%10d*\n", PAGES); printf("*%-10d*\n", PAGES); }
#include<stdio.h> int main(void) { const double RENT = 3852.99; printf("*%f*\n", RENT); printf("*%e*\n", RENT); printf("*%4.2f*\n", RENT); printf("*%3.1f*\n", RENT); printf("*%10.3f*\n", RENT); printf("*%10.3e*\n", RENT); printf("*%10.3e*\n", RENT); }