首页 > 编程语言 >以c primer plus(第六版)为大纲的C语言初学手记,含示例代码及编程练习(第四章)

以c primer plus(第六版)为大纲的C语言初学手记,含示例代码及编程练习(第四章)

时间:2024-08-18 22:23:43浏览次数:11  
标签:main name 示例 int scanf C语言 第六版 printf include

//第4章

//example 4.1
//#include <stdio.h>
//#include <string.h>  //to include strlrn()
//#define DENSITY 62.4  //to define a constant
//int main()
//{
//    float weight,volume;
//    int size,letters;
//    char name[40];   //to declare a digit group which is used to contain character string
//    printf("Hi!What is your first name?\n");
//    scanf("%s",name); //for character groups,"%s",no &
//    printf("%s,what is your weight in pounds?\n",name);
//    scanf("%f",&weight);
//    size = sizeof name; //to get the bytes 
//    letters = strlen(name); //length
//    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.\n",size);
//    return 0;
// } 

//example4.2.2
//#include <stdio.h>
//#define PRAISE "You are an extraordinary being."
//int main(void)
//{
//    char name[40];
//    printf("What is your name?");
//    scanf("%s",name);
//    printf("Hellow,%s.%s\n",name,PRAISE);
//    return 0;
//}

//PS. scanf only reads the first wordds before blanks

//example 4.3
//#include <stdio.h>
//#include <string.h>
//#define PRAISE "You are an extraordinary being."
//int main(void)
//{
//    char name[40];
//    printf("What is your name?");
//    scanf("%s",name);
//    printf("Hello,%s.%s\n",name,PRAISE);
//    printf("Your name of %zd letters occupies %zd memory cells.\n",
//    strlen(name),sizeof name);
//    printf("The phrase of praise has %zd letters.",strlen(PRAISE));
//    printf("and occupies %zd memory cells.\n",sizeof PRAISE);
//    return 0;
//}

//4.3
//define:to make a constant 
//#define NAME value

//example4.4
//#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 = radius * PI * radius;
//    circum = 2.0 *radius*PI;
//    printf("Your basic pizz parameters are as follows:\n");
//    printf("circumference = %1.2f,area = %1.2f\n",circum,area);
//    return 0;
//}

//'const' is used to make variables read-only

//'express constant' can be dirictly use as a certain value
//example 4.5
//#include <stdio.h>
//#include <limits.h>
//#include <float.h>
//int main(void)
//{
//    printf("Some number limits for this system:\n");
//    printf("Biggest int :%d\n",INT_MAX);
//    printf("Smallest longlong :%lld\n",LLONG_MIN);
//    printf("One byte = %d bits on this system.\n", CHAR_BIT);
//    printf("Largest double: %e\n", DBL_MAX);
//    printf("Smallest normal float: %e\n", FLT_MIN);
//    printf("float precision = %d digits\n", FLT_DIG);
//    printf("float epsilon = %e\n", FLT_EPSILON);
//    return 0;
//}

//4.4.2
//example 4.6
//#include <stdio.h>
//#define PI 3.151493
//int main(void)
//{
//    int number=7;
//    float pies=12.75;
//    int cost=7800;
//    printf("The %d contestants ate %f berry pies.\n",number,pies);
//    printf("The value of pi is %f.\n",PI);
//    printf("Farewell!thou art too dear for my possessing,\n");
//    printf("$%d\n",2*cost);
//    /*equal to  */
//    //printf("%c%d\n",'$',2*cost); 
//    return 0;
//}

//printf(format character string,item1,item2,...)

//to print '%',use %%

//example 4.7
//#include <stdio.h>
//#define PAGES 959
//int main(void)
//{
//    printf("*%d*\n",PAGES);
//    printf("*%2d*\n",PAGES);
//    printf("*%10d*\n",PAGES);
//    printf("*%-10d*\n",PAGES);
//    return 0;
//}

//example4.8
//#include <stdio.h>
//int main(void)
//{
//    const double RENT=3852.99;        //output:
//    printf("*%f*\n", RENT);            //3852.99
//    printf("*%e*\n", RENT);            //3.85299e3
//    printf("*%4.2f*\n", RENT);        //3852.99
//    printf("*%3.1f*\n", RENT);        //3853.0
//    printf("*%10.3f*\n", RENT);        //3852.990
//    printf("*%10.3E*\n", RENT);        //3.853e3
//    printf("*%+4.2f*\n", RENT);        //+3852.99
//    printf("*%010.2f*\n", RENT);    //0003852.99
//    return 0;
//}
//why %10.nf '10' unshown


//example4.9
/* flags.c -- 演示一些格式标记 */
/*#include <stdio.h>
int main(void)
{
printf("%x %X %#x\n", 31, 31, 31);
printf("**%d**% d**% d**\n", 42, 42, -42);
printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6);
return 0;
}
该程序的输出如下:
1f 1F 0x1f
**42** 42**-42**
** 6** 006**00006** 006**
第1行输出中,1f是十六进制数,等于十进制数31。第1行printf()语句
中,根据%x打印出1f,%F打印出1F,%#x打印出0x1f。

第 2 行输出演示了如何在转换说明中用空格在输出的正值前面生成前导
空格,负值前面不产生前导空格。这样的输出结果比较美观,因为打印出来
的正值和负值在相同字段宽度下的有效数字位数相同。

第3行输出演示了如何在整型格式中使用精度(%5.3d)生成足够的前
导0以满足最小位数的要求(本例是3)。然而,使用0标记会使得编译器用
前导0填充满整个字段宽度。最后,如果0标记和精度一起出现,0标记会被
忽略

*/

//example 4.10
//#include <stdio.h>
//#define BLURB "Authentic imitation!"
//int main(void)
//{
//    printf("[%2s]\n",BLURB);
//    printf("[%24s]\n", BLURB);
//    printf("[%24.5s]\n", BLURB);
//    printf("[%-24.5s]\n", BLURB);
//    return 0;
//}
/* printf("[%字符串目标长度.取字符数s] ,item"); */

//学以致用 
//#include <stdio.h>
//#define NAME "Halery"
//int main(void)
//{
//    float income;
//    income = 3000.00;
//    printf("The %s family just may be $%.2f dollars richer!",NAME,income);
//    return 0;
// } 

//P208转换不匹配???

// the returned value of printf is the length of character strings
//example 4.13 
//#include <stdio.h>
//int main(void)
//{
//    int bph2o = 212;
//    int rv;
//    rv = printf("%dF is water 's boiling point.\n",bph2o);
//    printf("The printf() function printed %d characters.\n",rv);
//    return 0;
//}

//divide the character string in printf()
//example4.14
//#include <stdio.h>
//int main(void)
//{
//    printf("Here's one way to print a");
//    printf("long string.\n");
//    
//    printf("Here's another way to print a \
//long string.\n");
//    
//    printf("Here's the newest way to print a "
//    "long string.\n");
//    
//    return 0;
//}

//4.15
//#include <stdio.h>
//int main(void)
//{
//    int age;
//    float assets;
//    char pet[30];
//    printf("Enter your age,assets,and favourite pet.\n");
//    scanf("%d%f",&age,&assets);
//    scanf("%s",pet);
//    printf("%d$%.2f%s\n",age,assets,pet);
//    return 0;
//}

//notice whether to use '&'
//a newline is available except %c 
/*对于float类型和double类型,printf()都使用%f、%e、%E、%g和%G转换说
明。而scanf()只把它们用于float类型,对于double类型时要使用l修饰符 */

/*scanf
1.从第一个非空白开始,到结尾或第一个非空白或非类型结束
2.空白字符是克星。读取的字符串也不能含空白。
3. scanf("%d,%d", &n, &m);
用户必须像下面这样进行输入两个整数:88,121
4.%c的特殊性。
用空格来使空白available .
scanf("%c", &ch)从输入中的第1个字符开始读取,
而scanf(" %c", &ch)则从第1个非空白字符开始读取。 */

// *修饰符
//#include <stdio.h>
//int main(void)
//{
//    unsigned width,precision;
//    int number=256;
//    double weight = 243.5;
//    printf("Enter a field width:\n");
//    scanf("%d",&width);
//    printf("The number is :%*d:\n",width,number);  // *指示了width
//    printf("Now enter a width and a precision:\n");
//    scanf("%d%d",&width,&precision);
//    printf("Weight = %*.*f\n",width,precision,weight);
//    printf("Done!\n");
//    return 0;
//}

//#include <stdio.h>
//int main(void)
//{
//    int n;
//    printf("Please enter three integers:\n");
//    scanf("%*d%*d%d",&n);
//    printf("The last integer was %d\n",n);
//    return 0;
//}

//使用足够大的固定字段宽度可以让输出整齐美观
//#include <stdio.h>
//int main(void)
//{
//    int val1=12,val2=234,val3=1222;
//    printf("%9d %9d %9d\n", val1, val2, val3);
//    return 0;
//}

//exercise 4.7
//1

//#include <stdio.h>
//#include <string.h>  //to include strlrn()
//#define DENSITY 62.4  //to define a constant
//int main()
//{
//    float weight,volume;
//    int size,letters;
//    char name[40];   //to declare a digit group which is used to contain character string
//    printf("Hi!What is your first name?\n");
//    scanf("%s",name); //for character groups,"%s",no &
//    printf("%s,what is your weight in pounds?\n",name);
//    scanf("%f",&weight);
//    size = sizeof name; //to get the bytes 
//    letters = strlen(name); //length
//    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.\n",size);
//    return 0;
// } 
//只会读取名 ,因中间的空白喊停了scanf ,weight也得不到赋值。 

/*2.a. 
He sold the painting for $%234.50f.

b.


c.
His Hamlet was funny without being vulgar 
has  characters.

d.
Is 1.20e3 the same as 1201.00?

3.
c.#define Q "\"His Hamlet was funny without being vulgar.\""
printf("%s\nhas %dcharacters.\n", Q, strlen(Q));

*/
//4
//#include <stdio.h>
//#define B "booboo"
//#define X 10
//int main(void)
//{
//    int age;
//    int xp;
//    char name[40];
//    printf("Please enter your first name.\n");
//    scanf("%s",name);
//    printf("\nAll right,%s,what is your age?\n",name);
//    scanf("%d",&age);
//    xp = age + X;
//    printf("That is a %s!You must be at least %d.\n",B,xp);
//    return 0;
//}

//5
//#include <stdio.h>
//#define BOOK "War and Peace"
//int main(void)
//{
//    float cost =12.99;
//    float percent = 80.0;
//    printf("This copy of \"%s\" sells for %.2f. \n",BOOK,cost);
//    printf("That is %.0f%% of list.",percent);
//    return 0;
//}

/*
6
%d;%0x;%10.3f;%12.2e;%-30s
  ;%4X; 
  (一个形如8A、字段宽度为4的十六进制整数) !!!十六进制是%x 
7
%15ud;%4.#0x;%-12.2E;%+10.3f;%8s
%15lu;%#4x;                    ;%8.8s
(字段宽度为15的unsigned long类型的整数) !!! unsigned long类型是lu 
(一个形如0x8a、字段宽度为4的十六进制整数) !!!字段宽度在#后 
(一个字段宽度为8的字符串的前8个字符) !!!这里用精确到第八位来打印前八个字符 
8
%6d;%nx;%2s;%+.2f;%-5s;
%6.4d;%*o;%2c;     ;%7.5s;
a.(一个字段宽度为6、最少有4位数字的十进制整数)!!!用精准到第4位来表示至少有四位数字 
b.(一个在参数列表中给定字段宽度的八进制整数)??? 
c.(一个字段宽度为2的字符)!!!字符用c 
e.(一个字段宽度为7、左对齐字符串中的前5个字符)!!!别忘了字符宽度 
*/

//9
/*
int n;
scanf("%d",&n);

float n;
scanf("%f",&n);
scsanf("%e",&n);

char pasta[20];
scanf("%s",pasta);

d.
char name[40];        char action[20];
scanf("%s",name);    int value;
                    scanf("%s %d", action, &value);  !!!字符串和数分开输入 

#include <stdio.h>
int main(void)
{
    char n[40];
    scanf("%s",n);
    printf("%s",n);
    return 0;
 }
此实际可行。22被识别为字符串的一部分 

#include <stdio.h>
int main(void)
{
    char n[40];
    int m;
    scanf("%s %d",n,&m);
    printf("%s%d",n,m);
    return 0;
 } 
此也实际可行,或许更规范。 
 
int n;                int value;
scanf("%s",n);      scanf("%*s %d",&value);

10.空白包括空格、制表符和换行符;
scanf()使用空白分隔连续的输入项

11.
printf("The double type is %zd bytes..\n", sizeof(double));
%z 中的 z 是修饰符,不是转换字符,所以要在修饰符后面加上一个
它修饰的转换字符。可以使用%zd打印十进制数,或用不同的说明符打印不
同进制的数,例如,%zx打印十六进制的数。

12.
可以分别把(和)替换成{和}。但是预处理器无法区分哪些圆括号应替
换成花括号,哪些圆括号不能替换成花括号。因此,
#define ( {
#define ) }
int main(void)
(
printf("Hello, O Great One!\n");
)
将变成:
int main{void}
{
printf{"Hello, O Great One!\n"};
} */

//exercise 4.8

//1
//#include <stdio.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    printf("Please input your first name:");
//    scanf("%s",f);  //scanf自带换行 
//    printf("Please input your last name:");
//    scanf("%s",l);
//    printf("%s %s\n",f,l);
//    return 0;
//}

//2.!!!名和姓一起输入的程序,则会有输入时不可含空格 
//#include <stdio.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    printf("Please input your first name:");
//    scanf("%s",f); 
//    printf("Please input your last name:");
//    scanf("%s",l);
//    printf("\"%s %s\"",f,l);
//    return 0;
//}

//#include <stdio.h>
//int main(void)
//{
//    char f[40];
//    char l[40];
//    printf("Please input your first name:");
//    scanf("%s",f);  
//    printf("Please input your last name:");
//    scanf("%s",l);
//    printf("\"%s %s\"\n",f,l);
//    return 0;
//}

//#include <stdio.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    printf("Please input your first name:");
//    scanf("%s",f);  
//    printf("Please input your last name:");
//    scanf("%s",l);
//    printf("\"%s %-20s\"\n",f,l);
//    return 0;
//}

//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    int a;
//    printf("Please input your first name:");
//    scanf("%s",f);  
//    printf("Please input your last name:");
//    scanf("%s",l);
//    a=strlen(f)+strlen(l)+3;
//    printf("\"%*s %s\"\n",a,f,l);
//    return 0;
//}

//3
//#include <stdio.h>
//int main(void)
//{
//    float n;
//    scanf("%f",&n);
//    printf("小数点记数法:%.1f\n",n);
//    printf("指数记数法:%.1e\n",n);
//    return 0;
// } 
// 
// #include <stdio.h>
//int main(void)
//{
//    float n;
//    scanf("%f",&n);
//    printf("小数点记数法:%.3f\n",n);
//    printf("指数记数法:%.3e\n",n);
//    return 0;
// } 

//4
//#include <stdio.h>
//int main(void)
//{
//    float h;
//    char name[20];
//    printf("Please input your height:");
//    scanf("%f",&h);  //scanf自带换行 
//    printf("Please input your name:");
//    scanf("%s",name);
//    printf("%s,you are %.3f feet tall.\n",name,h);
//    return 0;
//}

//5
//#include <stdio.h>
//int main(void)
//{
//    float speed;
//    float size;
//    float time;
//    printf("Please input speed:");
//    scanf("%f",&speed);  //scanf自带换行 
//    printf("Please input the size:");
//    scanf("%f",&size);
//    time = size*8/speed;
//    printf("At %.2f megabits per second,a file of %.2f megabytes\n\
//downloads in %.2f seconds.\n",speed,size,time);
//    return 0;
//}

//6
//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    int a,b;
//    printf("Please input your first name:");
//    scanf("%s",f); 
//    printf("Please input your last name:");
//    scanf("%s",l);
//    a=strlen(f);
//    b=strlen(l);
//    printf("%20s %20s\n",f,l);
//    printf("%20d %20d",a,b);
//    return 0;
//}

//#include <stdio.h>
//#include <string.h>
//int main(void)
//{
//    char f[20];
//    char l[20];
//    int a,b;
//    printf("Please input your first name:");
//    scanf("%s",f); 
//    printf("Please input your last name:");
//    scanf("%s",l);
//    a=strlen(f);
//    b=strlen(l);
//    printf("%-20s %-20s\n",f,l);
//    printf("%-20d %-20d",a,b);
//    return 0;
//}

//7
//#include <stdio.h>
//int main(void)
//{
//    double a;
//    float b;
//    a=1.0/3.0;
//    b=1.0/3.0;
//    printf("%.6f %.12f %.16f\n",a,a,a);
//    printf("%.6f %.12f %.16f",b,b,b);
//    return 0;
//}
/*result
0.333333 0.333333333333 0.3333333333333333
0.333333 0.333333343267 0.3333333432674408 */

//8
//#include <stdio.h>
//#define V 3.785
//#define D 1.609
//int main(void)
//{
//    float volum;
//    float distance;
//    float per;
//    printf("Please input the volum:");
//    scanf("%f",&volum);
//    printf("Please input the distance:");
//    scanf("%f",&distance);
//    per = V*volum/100*D*distance;
//    printf("100 kilometer %.1f gas.");
//    return 0;
//}

标签:main,name,示例,int,scanf,C语言,第六版,printf,include
From: https://blog.csdn.net/2401_86742819/article/details/141303263

相关文章

  • C语言基础- Hello World
    第一个C语言程序HelloWorld#include<stdio.h>//#关键标识符,表示用用头文件;include:引入都文件关键字 //stdio.h:系统标准输入、输出库对用的头文件。给printf函数服务 //<>:表示系统库函数,"":表示用户自定义库函数intmain(void) //int:函数返回值是整形数据......
  • C语言编程-GCC编译过程
    gcc编译预处理->编译->汇编->链接预处理gcc-Ehelloworld.c-ohelloworld.i头文件展开;不检查语法错误,即可以展开任意文件;宏定义替换;将宏名替换为宏值;替换注释;将注释替换为空行;展开条件编译;根据条件来展开指令;编译gcc-Shelloworld.i-ohelloworld.s逐行检查......
  • 专题:C语言操作符详解
    ⽬录1.操作符的分类2.⼆进制和进制转换3.原码、反码、补码4.移位操作符5.位操作符:&、|、^、~6.单⽬操作符7.逗号表达式8.下标访问[]、函数调⽤()9.结构成员访问操作符10.操作符的属性:优先级、结合性11.表达式求值1.操作符的分类•算术操作符:+、-......
  • PAT乙级1032 || 挖掘机技术哪家强(C示例)
    挖掘机技术哪家强为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。输入格式:输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开......
  • C语言目录操作(2024.8.12)
    用递归删除目录(多个目录可以从参数传进来)#include<stdio.h>#include<sys/types.h>#include<dirent.h>#include<unistd.h>#include<string.h>#include<stdlib.h>/*从目录中读到的所谓目录项,是一个这样的结构体:structdirent{ ino_td_ino;//文件索引号 of......
  • C语言 之 strlen、strcpy、strcat、strcmp字符串函数的使用和模拟实现
    文章目录strlen的使用和模拟实现函数的原型strlen模拟实现:方法1方法2方法3strcpy的使用和模拟实现函数的原型strcpy的模拟实现:strcat的使用和模拟实现函数的原型strcat的模拟实现:strcmp的使用和模拟实现函数的原型strcmp的模拟实现本章的内容主要讲解这4个与字符......
  • Codesy是使用加法函数实现加法运算示例
    Codesys的函数比较常用,需要熟练掌握,下面介绍如何通过函数实现加法运算:首先添加对象,添加POU函数名改为Add1,类型函数,返回类型int;Add1里面的定义如下:FUNCTIONAdd1:INTVAR_INPUT   In1:INT;   In2:INT:=1;END_VARVAREND_VAR程序如下:Add1:=In1+In2;然后看......
  • C语言工程实践实现完整的五子棋项目一
    五子棋第一章玩家信息部分五子棋五子棋第一章前言一、所需技术二、玩家信息部分实现1.头文件部分2.菜单部分3.玩家信息部分总结每文推荐前言接下来的几篇文章我将带领大家实现一个工程实践的项目–五子棋。它是一个比较完整的项目,主要包括了三个部分:1.玩家信......
  • 【C语言篇】数组和函数的实践:扫雷游戏(附源码)
    文章目录前言扫雷游戏的分析和设计扫雷游戏的功能说明游戏的分析和设计文件结构设计扫雷游戏的代码实现初始化棋盘打印棋盘布置雷排查雷扫雷游戏的拓展前言源码在最后扫雷游戏的分析和设计经典扫雷游戏扫雷游戏的功能说明使⽤控制台实现经典的扫雷游戏游戏可......
  • 【C语言】内存函数
    memcpy使用和模拟实现void*memcpy(void*destination,constvoid*source,size_tnum);介绍:•函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。•这个函数在遇到‘\0’的时候并不会停下来。•如果source和destinati......