当前位置:网站首页>C Expert Programming Preface
C Expert Programming Preface
2022-08-01 21:08:00 【weixin_Guest time】
所有的CPrograms all do the same thing,i.e. observe a character,然后啥也不干. ---Peter Weinberger
Good books:
C Traps and Pitfalls
The C Puzzle Book
Obfuscated C and Other Mysteries
CLanguage programming is a craft,It takes years of experience to achieve perfection.want to tasteClanguage nuances,and by writing a large number of different programs to becomeC语言专家,it takes a lot of time.
Programming experts build their technical toolbox over years of practice,There is a variety of usage、Code Snippets and Tips for Flexibility.They learn from the experiences of other successful people,Or directly understand their code,Or listen to other people's teachings while maintaining their code,随着时间的推移,gradually formed these things.成为CAnother way to become a programmer is to introspect,that is, progress in the process of recognizing mistakes.
//Incorrect use of assignment and equality operators
#include <stdio.h>
int main() {
//1.1
if (i = 3) ; //wrong
//1.2
if (i == 3) ; //right
//1.3
if (3 == i) ; //be supposed to suggest
//attempted assighment to literal
}
CThe language ability is also very strong,编译器对于“求一个表达式的值,But do not use this value”Such a statement is also acceptable,And don't send out any warning,just simply discard the returned result.
有些lintThe program has been able to detect this kind of problem,But it's easy to overlook these useful tools.
//Only for small program fragments,Real programs can't be like this:
char pear[40];
double peach;
int mango = 13;
long melon = 2001;
关键字:char、double、int、long
变量名:pear、peach、mango、melon
Humor is very important for learning new things.
Readers can take this book asCCollection of ideas for language programming,或是CA collection of language hints and idioms,Can also draw nourishment from experienced compiler authors,更轻松地学习ANSI C.
Enforce In_Order Execution of I/O (在I/Oimplement the policy in order)(IBM/Motorola/Apple PowerPC)tunefs(UNIX) Advanced system administrators use it to modify the dynamic parameters of the file system,And optimize the layout of the disk file blocks. 在早期的tunefson the online manual,Also for a title“Bugs”end of the subsection.内容如下:
Bugs:
This program should have been transferred(mounted)and running on an active filesystem,但事实上并非如此.因为超级块(superblock)Not keep telling buffer buffer,so only if the program is running in an uninstalled(dismounted)Only valid when in the file system.If running on the root filesystem,The system must be restarted.
you can optimize a filesystem,But can't optimize a fish.
If you ignore this passage,you just wait.一个UNIXThe monsters will keep haunting you,Until you can't stand.
mounted 安装好的
superblock 超级快
dismounted not installed
/*
**编程挑战
*/
1. 查看一下time_t的定义,它位于文件/user/include/time.h中.
解析:
#ifndef _TIME_T_DEFINED
#define _TIME_T_DEFINED
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif
#endif
#ifndef _TIME32_T_DEFINED
#define _TIME32_T_DEFINED
typedef long __time32_t;
#endif
#ifndef _TIME64_T_DEFINED
#define _TIME64_T_DEFINED
__MINGW_EXTENSION typedef __int64 __time64_t;
#endif
#define __int64 long long
2.编写代码,In one typetime_tstored in the variabletime_t的最大值,然后把它传递给ctime()函数,转换为ASCIIstring and print it.注意ctime()函数同Clanguage doesn't matter,它只表示“转换时间” .
/*ctime(time_t *t)函数,转换成ASCII字符串并打印出来.注意ctime()函数同C语言没有任何关系, *它只表示“转换时间”.
*/
/*time_t的最大值
*/
/*time()获得当前的时间
*difftime()And get the current timetime_tThe difference between the maximum time values that can be represented(以秒计算)
*/
#include <stdio.h>
#include <time.h>
int main() {
printf("%u %u\n", sizeof(long), sizeof(time_t));
time_t biggest = 0x7FFFFFFF;
//time_t biggest = 0x7FFFFFFFFFFFFFFF;
printf("biggest = %s\n", ctime(&biggest));
return 0;
}
/*ctime()Function parameters are converted to local time,Unified time with the worldUTC(格林尼治时间)并不一致,depends on where you are *的时区,And the current year is far from the year of the maximum time value
*/
/* 输出:

*/
#include <stdio.h>
#include <time.h>
int main() {
time_t biggest = 0x7FFFFFFF;
printf( "sizeof(time_t) = %zd\n", sizeof(time_t) );
printf("biggest = %s\n", asctime(gmtime(&biggest)));
return 0;
}
/*gmtime()function to get the maximumUTC时间值.This function does not return a printable string
*asctime()function to get a string like this
*/
/*但是,we're not done.If you are in the New Zealand time zone,will be more13个小时,Premise is in New Zealand2038Daylight saving time is still in use.新西兰在1Used in the month is daylight savings time(because of the southern hemisphere).但是,Since the easternmost point of New Zealand is east of the dateline,where it should be later than GMT10hours rather than early14小时.这样,New Zealand due to its unique geographical location,Unfortunate to be the first of the programBug的受害者.
*/
/* 输出:

*/
If the programmer uncommented the program,After so many years,He had to worry that the program wouldUNIXoverflow on platform.请修改程序,找出答案.
1.调用time()获得当前的时间.
2.调用difftime()And get the current timetime_tThe difference between the maximum time values that can be represented(以秒计算).
3.Format this value as a year、月、日、小时、分钟的形式,并打印出来.Does it outlive the average person??
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int main( void ){
time_t t, t2;
long second;
t = time( &t );
t2 = LONG_MAX;
printf( "now date and time is: %s", ctime( &t ) );
second = difftime( t2, t );
printf( "second = %ld\n", second );
struct tm* ptm;
ptm = localtime( &t2 );
printf( "year = %d\n", ptm->tm_year + 1900 );
printf( "month = %d\n", ptm->tm_mon + 1 );
printf( "week = %d\n", ptm->tm_mday / 7 );
printf( "day = %d\n", ptm->tm_wday );
printf( "hour = %d\n", ptm->tm_hour );
printf( "minute = %d\n", ptm->tm_min );
return EXIT_SUCCESS;
}
/* 输出:

*/
边栏推荐
- tiup mirror grant
- Suggestions and answer 8.1 C traps and defect chapter 8
- JSD - 2204 - Knife4j framework - processing - Day07 response results
- Pytorch框架学习记录13——利用GPU训练
- 1374. 生成每种字符都是奇数个的字符串 : 简单构造模拟题
- 【微信小程序】【AR】threejs-miniprogram 安装(76/100)
- string
- Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
- 淘宝获取收货地址列表的 API
- 这些 hook 更优雅的管理你的状态
猜你喜欢
随机推荐
property语法
360借条安全专家:陌生微信好友不要轻易加贷款推广多是诈骗
(七)《数电》——CMOS与TTL门电路
Get started with Grafana in 15 minutes
tiup mirror init
对C语言结构体内存对齐的理解
如何封装 cookie/localStorage/sessionStorage hook?
C陷阱与缺陷 第8章 建议与答案 8.2 答案
仿牛客论坛项目
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
Go Atomic
C专家编程 序
C陷阱与缺陷 第7章 可移植性缺陷 7.8 随机数的大小
Suggestions and answer 8.1 C traps and defect chapter 8
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
Postman 批量测试接口详细教程
Go Atomic
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
WeChat applet cloud development | personal blog applet









