代码拉取完成,页面将自动刷新
#include <stdlib.h>
#include <string.h>
//
// Created by 杨涛涛 on 2018/5/31.
//
int max(int num1, int num2) {
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
//region 常量变量
// 存储大小与系统位数
int test_base_type() {
printf("int 大小 %d", sizeof(int));
return 0;
}
extern int a; // 变量声明
void test_var() {
int a; // 变量声明和定义
a = 10;
a = 100;
printf("a 等于 %d", a);
}
void test_const() {
const long l = 10; // 声明常量
printf("l 常量:%d", l);
}
//endregion
//region 存储类
/*
* 存储类:
* 定义函数,
*
*/
static int mm = 10;//是全局变量的默认存储类
void test_cun_chu_lei() {
auto int i = 100;
register int j = 99; //定义存储在 寄存器,而非 RAM 中的变量
}
void test_cun_chu_static(){
// 'thingy' 是 'func1' 的局部变量 - 只初始化一次
// 每次调用函数 'func1' 'thingy' 值不会被重置。
static int thingy = 5;
thingy++;
printf(" thingy 为 %d , count 为 %d\n", thingy);
}
//endregion
void test_switch(){
int a;
printf("请输入 number");
scanf("%d",&a);
switch (a){
case 1:printf("星期1");
break;
case 2:printf("星期2");
default:printf("nonononono");
}
};
void test_array(){
double myarray[]={1,2,3};
printf("%f",myarray[1]);
}
void test_print(){
double a=1;
int b=2;
printf("dob:%f,int:%d",a,b);
printf("dob:%d,int:%d",a,b);
}
void test_zhizhen(){
float f=1;
double d[]={1,2,3};
printf("f:内存地址:%p \n",&f);
printf("d:内存地址:%p \n",&d);
printf("f:的值:%f \n",*&f);
}
int maxx(int x, int y)
{
return x > y ? x : y;
}
void test_hanshu_zhizhen(){
/* p 是函数指针 */
int (* p)(int, int) = & maxx; // &可以省略
int a, b, c, d;
printf("请输入三个数字:");
scanf("%d %d %d", & a, & b, & c);
/* 与直接调用函数等价,d = max(max(a, b), c) */
d = p(p(a, b), c);
printf("最大的数字是: %d\n", d);
};
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
for (size_t i=0; i<arraySize; i++)
array[i] = getNextValue();
}
// 获取随机值
int getNextRandomValue(void)
{
return rand();
}
int test_huidiao_hanshu(void)
{
int myarray[10];
populate_array(myarray, 10, getNextRandomValue);
for(int i = 0; i < 10; i++) {
printf("%d ", myarray[i]);
}
printf("\n");
return 0;
}
void test_char(){
char c[] ="abc";
}
/**
* 结构体
*/
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
void test_struct(){
struct Books book1;
struct Books book2;
strcpy(book1.title, "C Programming");
strcpy( book1.author, "Nuha Ali");
strcpy( book1.subject, "C Programming Tutorial");
book1.book_id = 6495407;
/* Book2 详述 */
strcpy( book2.title, "Telecom Billing");
strcpy( book2.author, "Zara Ali");
strcpy( book2.subject, "Telecom Billing Tutorial");
book2.book_id=11;
printf( "Book 1 title : %s\n", book1.title);
printf( "Book 1 author : %s\n", book1.author);
printf( "Book 1 subject : %s\n", book1.subject);
printf( "Book 1 book_id : %d\n", book1.book_id);
/* 输出 Book2 信息 */
printf( "Book 2 title : %s\n", book2.title);
printf( "Book 2 author : %s\n", book2.author);
printf( "Book 2 subject : %s\n", book2.subject);
printf( "Book 2 book_id : %d\n", book2.book_id);
};
/**
*位域有些信息在存储时,
* 并不需要占用一个完整的字节,而只需占几个或一个二进制位。
* 例如在存放一个开关量时,只有 0 和 1 两种状态,用 1 位二进位即可。为了节省存储空间,并使处理简便,C 语言又提供了一种数据结构,称为"位域"或"位段"。
* 所谓"位域"是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。这样就可以把几个不同的对象用一个字节的二进制位域来表示。
*/
/**
* 在不同的时刻把不同的东西存储与相同的内存位置(使几个不同类型的变量共占一段内存(相互覆盖))
*/
void test_file(){
FILE *fp= NULL;
fp=fopen("/Users/yangtaotao/Documents/c/day/tmp.txt","w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
void test_def(){
printf("file:%s \n",__FILE__); //当前文件名。 预定义宏
printf("date:%s \n",__DATE__);// 预定义宏,当前日期
printf("time:%s \n",__TIME__);//
printf("Line 当前行号: %d \n",__LINE__);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。