#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
描述一个学生.一些数据
名字
年龄
电话
性别
struct 结构体关键字 Stu - 结构体标签 struct Stu - 结构体类型
struct Stu
{
//成员变量
char name[20];
short age;
char tele[12];
char sex[5];
}s1,s2,s3;//是3个全局的结构体变量
typedef struct Stu
{
//成员变量
char name[20];
short age;
char tele[12];
char sex[5];
}Stu;//是3个全局的结构体变量
int main()
{
Stu s1 = {"张三",20,"48355456532","男"};//局部的结构体变量
Stu s2 = {"旺财",30,"13451521255","保密"};
return 0;
}
struct S
{
int a;
char c;
char arr[20];
double d;
};
struct T
{
char ch[10];
struct S s;
char* pc;
};
int main()
{
char arr[] = "hello bit\n";
struct T t = { "hehe",{1,'w',"hello world",3.14},arr};
printf("%s\n", t.ch);//hehe
printf("%s\n", t.s.arr);//hello world
printf("%lf\n", t.s.d);//3.14
printf("%s\n", t.pc);//hello bit
return 0;
}
typedef struct Stu
{
char name[20];
short age;
char tele[12];
char sex[5];
}Stu;
void print1(Stu tmp)
{
printf("name: %s\n", tmp.name);
printf("age: %d\n", tmp.age);
printf("tele: %s\n", tmp.tele);
printf("sex: %s\n", tmp.sex);
}
void print2(Stu* s)
{
printf("name: %s\n", s->name);
printf("age: %d\n", s->age);
printf("tele: %s\n", s->tele);
printf("sex: %s\n", s->sex);
}
int main()
{
Stu s = { "李四",40,"11544845315","男" };
//打印结构体数据
//print1和print2 哪个更好?
//print2更优
print1(s);
print2(&s);
return 0;
}
Add(int a, int b)
{
int z = 0;
z = a + b;
return z;
}
int main()
{
int a = 20;
int b = 10;
int ret = 0;
ret = Add(a, b);
printf("%d\n", ret);
return 0;
}
栈:先进后出,后进先出
插入一个元素:压栈
删除一个元素:出栈
int main()
{
int i = 0;
for (i = 0; i < 100; i++)
{
printf("%d ", i);
}
return 0;
}
Debug 调试版本
Release 发布版本
F5 - 启动调试 - 和F9配合使用
断点 F9切换断点
int main()
{
int i = 0;
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
for (i = 0; i <= 12; i++)
{
printf("hehe\n");
arr[i] = 0;
}
return 0;
}
int main()
{
int i = 0;
for (i = 0; i < 100; i++)
{
printf("%d ", i);
}
for (i = 0; i < 100; i++)
{
printf("%d ",10-i);
}
return 0;
}
int Add(int x, int y)
{
return x + y;
}
int main()
{
printf("hehe\n");
int a = 10;
int b = 10;
int c = Add(a, b);
return 0;
}
int main()
{
{
int tmp = 0;
printf("tmp = %d\n", tmp);
}
int arr[10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
arr[i] = i;
}
return 0;
}
void test2()
{
printf("hehe\n");
}
void test1()
{
test2();
}
void test()
{
test1();
}
int main()
{
test();
return 0;
}
void my_strcpy(char* dest,char* src)
{
if (dest != NULL && src != NULL)
{
while (*dest++ = *src++)
{
;//优化
//*dest = *src;
//src++;
//dest++;
}
*dest = *src;
}
}
好代码
char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;
assert(dest != NULL);//断言
assert(src != NULL);//断言
//把src指向的字符串拷贝到dest指向的空间,包含‘\0’字符
while (*dest++ = *src++)
{
;//优化
//*dest = *src;
//src++;
//dest++;
}
return ret;
}
int main()
{
//strcpy
//字符串拷贝
char arr1[] = "###";
char arr2[] = "bit";
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}
int main()
{
const int num = 10;
const int* p = #
//*p = 20;
//error const 放在指针变量的*左边时,修饰的是*p,也就是说:不能通过p来改变*p(num)的值
//const 放在指针变量的*右边时,修饰的是指针变量p本身,p不能被改变
int n = 100;
p = &n;
printf("%d\n", num);
return 0;
}
int my_strlen(const char* str)
{
int count = 0;
assert(str != NULL);//断言指针 - 保证指针的有效性
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
int len = my_strlen(arr);
printf("%d\n", len);
return 0;
}