介绍

ANSI组织定义了C标准和标准库函数。

使用标准C函数优点:

使用标准C函数在任何平台上都支持,使得同一个源码,在Windows编译运行的结果和Linux上编译运行结果相同,无需更改代码。

随机数(rand)

产生指定范围内随机数(1~100)

#include <stdio.h>
#include <stdlib.h> int main()
{
for (int i=0; i<10; i++)
{
printf("%d\n", rand()%100);
}
}

每次运行会发现得到的是个随机数一样,为了解决这个问题,使用srand设置一个种子(seed),每次启动保证种子不同。

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int main()
{
srand(time(NULL));
for (int i=0; i<10; i++)
{
printf("%d\n", rand()%100);
}
}

时间函数(time)

获取当前时间戳(单位:s),时间戳即为距离1970-01-01 00:00:00的秒数

#include <stdio.h>
#include <time.h> int main()
{
time_t ts = time(NULL);
printf("%d\n", (int)ts);
}

通过时间戳获取年月日,时分秒,周几

#include <stdio.h>
#include <time.h> int main()
{
time_t ts = time(NULL);
tm time = *localtime(&ts); int year = time.tm_year + 1900;
int month = time.tm_mon + 1;
int day = time.tm_mday;
int hour = time.tm_hour;
int min = time.tm_min;
int sec = time.tm_sec;
int week = time.tm_wday ; return 1;
}

通过年月日,时分秒,获取time_t 时间戳

#include <stdio.h>
#include <time.h> int main()
{
//时间为2017-07-15 21:38:30
tm time = {0};
time.tm_year = 2017 - 1900;
time.tm_mon = 7 -1;
time.tm_mday = 15;
time.tm_hour = 21;
time.tm_min = 38;
time.tm_sec = 30; time_t ts = mktime(&time); //获得该天为周几
tm time1 = *localtime(&ts);
printf("周%d\n", time1.tm_wday);
return 1;
}

欢迎加群交流:C/C++开发交流

C++:标准C函数(随机数,时间函数)的更多相关文章

  1. Mysql日期函数,时间函数使用的总结

    一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...

  2. 0513JS数组内置方法、数学函数、时间函数

    |数组中常用的内置方法|-push()与pop()|--push()是往数组的尾部添加,同时返回新数组的长度 var attr = [1,2,3,4,5];var attr2 = [6,7,8,9,0 ...

  3. MySQL:日期函数、时间函数总结(MySQL 5.X)

    http://www.cnblogs.com/she27/archive/2009/01/16/1377089.html 原文:http://www.51sdj.com/phpcms/picture/ ...

  4. 【转】MySQL:日期函数、时间函数总结(MySQL 5.X)

    转自:http://www.cnblogs.com/she27/articles/1377089.html 一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函 ...

  5. 日期函数、时间函数总结(MySQL 5.X)

    一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函数:now()mysql> select now(); +---------------------+ ...

  6. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  7. SqlServer 查询的几种方式以及数字函数、时间函数的应用总结(回归基础)

    --语法:select * from 表名 *表示查询所有字段数据 select * from Class select * from Student select * from RankingLis ...

  8. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  9. MySQL:日期函数、时间函数总结

    MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...

随机推荐

  1. [bzoj 4939][Ynoi 2016]掉进兔子洞

    传送门 Description 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一 ...

  2. (转)SLOW READPROCESSOR;ERROR SLOW BLOCKRECEIVER错误日志分析

    1.总结 "Slow ReadProcessor" 和"Slow BlockReceiver"往往是因为集群负载比较高或者某些节点不健康导致的,本文主要是帮助你 ...

  3. 系统调优:如何解决系统报错too many open files

    一.检查系统版本是否手工升级 关于lsb_release -a和/etc/issue显示的发行版本号不同,原因只有一个:系统内核手动升级了 对于高并发高http连接的应用程序例如www或Java,会遇 ...

  4. Socket函数详解

    #include <sys/types.h>; #include <sys/socket.h>; --------------------------------------- ...

  5. 【翻译】JNA调用DLL

    一.前言 Jna调用的示范,基本包括了Java->C基本类型的转换,指针的转换等. 不过文章是2011年的,可能后面要查看下有什么改变. 二.原文 http://www.viaboxxsyste ...

  6. android studio: no idea annotations attached to the jdk 1.8 some issues will not be found

    Android Studio今天早上打开项目提示错误信息: No IDEA annotations attached to the JDK 1.8 (C:\Program Files\Android\ ...

  7. Ubuntu18.04修改主机名和网卡地址

    date: 2019-06-26 09:56:04 author :headsen chen notice :个人原创 1,Ubuntu18.04 设置固定IP: 2,Ubuntu 18.04 设置主 ...

  8. SQL-W3School-函数:SQL HAVING 子句

    ylbtech-SQL-W3School-函数:SQL HAVING 子句 1.返回顶部 1. HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使 ...

  9. Ionic4.x 中的button

    官方文档:https://ionicframework.com/docs/api/button 1.ion-button 组件可以定义一个按钮 <ion-button>Default< ...

  10. 如何正确查看Linux机器内存使用情况

    如何正确查看Linux机器内存使用情况 背景 只要工作上涉及到Linux机器,基本上都会有这样一个需求,查看内存使用情况,但是怎么看才正确呢?之前使用的是top命令,一直存在一个误区. 为什么top命 ...