C内置标准函数

printf函数

  • printf直接输出字符串,不加‘\n’
char* GetMemory_1()
{
	char p[] = "hello world";
	return p;//返回局部变量
}
const char* GetMemory()
{
	const char* p = "hello world";//字符串常量
	return p;
}
int main()
{

	const char* str = NULL;
	str = GetMemory();
	printf(str);//hello world
	//相当于printf("hello world")
	
	return 0;
}

rand随机数

#include<stdio.h>
#include <stdlib.h>

void random(int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d\n",rand());//int rand(void);//伪随机数,有可能重复出现
	}
}

int main() 
{
	srand((unsigned)time(NULL));//void srand(unsigned int seed);//随机数发生器rand的初始化函数,避免获得相同的随机数
	//random(10);

	return 0;
}

时间类函数

#include<stdio.h>
#include <time.h>

int main()
{
	clock_t t1 = clock();//clock_t为clock函数返回值类型,单位毫秒,1/1000秒,记录从进入函数到执行该语句时所用的时间
	
	printf("%d",t1);
	
	time_t t2 = time(NULL);//time_t为time函数返回值类型,单位秒
	                       //time_t指向的时间(即从历年1970.0.0到现在的时间)
						   //32位存在2038问题,即返回值为long int最大值2^31-1秒=68年,1970+68=38年
						   
	return 0;
}
#include<stdio.h>
#include <time.h>

int main()
{
	time_t t2 = time(NULL);
	struct tm newtime;//时间格式的结构体
	//gmtime_s(&newtime,&t2);//GMT格林尼治时间,子午线
	localtime_s(&newtime, &t2);//本地时间(转换的时间,获取时间)
	//将time_t指向的时间(即从历年1970.0.0到现在的时间)转换为以struct tm结构体格式保存的时间

	printf("%d\n", newtime.tm_year+1900);//c标准定义为tm结构体的年份为1900年开始
	printf("%d\n", newtime.tm_mon + 1);//月份从0开始计数
	printf("%d\n", newtime.tm_mday);
	printf("%d\n", newtime.tm_hour);//gmtime10,localtime18
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值