C語言的數據類型

C語言的數據類型
- (1)輸入輸出
#include <stdio.h>
int main(int args, const char *argv){
//單純字符串輸出
puts("hello world");
//格式化輸出
printf("hello %s\n","alicfeng");
//輸入
char username[10];
int age;
gets(username);//很危險的做法 推薦不使用
scanf("%d",&age);//參數-(類型,參數的地址)
printf("%s age is %d\n",username,age);
return 0;
}
make && run
? demo gcc -o main demoIO.c && ./main
demoIO.c: In function ‘main’:
demoIO.c:18:5: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
gets(username);//很危險的做法 推薦不使用
^
/tmp/cct0RsLf.o:在函數‘main’中:
demoIO.c:(.text+0x49): 警告: the `gets' function is dangerous and should not be used.
hello world
hello alicfeng
alic
22
alic age is 22
- (2)C語言方法的調用
#include <stdio.h>
// 方法的聲明和定義
int max(int a, int b) {
return a > b ? a : b;
}
int main() {
printf("the max is %d\n",max(10,20));
return 0;
}
make && run
? demo gcc -o main demoFunc.c && ./main
the max is 20
- (3)C語言的宏定義
#include <stdio.h>
//定義宏 編譯前已經準備好 因而速度很快
#define MATH_PI 3.14
int main(){
printf("the PI value is %f\n",MATH_PI);
return 0;
}
make && run
? demo gcc -o main demoDefine.c && ./main
the PI value is 3.140000
- (4)C語言的宏定義方法
#include <stdio.h>
//定義宏方法 對于多行可以使用反斜杠
#define MAX(A, B) A>B?A:B
int main() {
printf("the max value is %f\n",MAX(20.4,30.8));
return 0;
}
make && run
? demo gcc -o main demoDefineFunc.cpp && ./main
the max value is 30.800000
- (5)C語言的條件運算符
#include <stdio.h>
//if
void ifCondition(int score) {
if (score >= 90) {
printf("優秀\n");
} else if (score >= 80) {
printf("良好\n");
} else if (score >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
}
//switch
void switchCondition(int score) {
switch (score / 10) {
case 10:
case 9:
printf("優秀\n");
break;
case 8:
printf("良好\n");
break;
case 7:
case 6:
printf("及格\n");
break;
default:
printf("不及格\n");
break;
}
}
//三木運算
void simpleCondition(int score) {
puts(score >= 60 ? "及格" : "不及格");
}
int main(){
ifCondition(95);
switchCondition(88);
simpleCondition(38);
return 0;
}
make && run
? demo gcc -o main demoCondition.c && ./main
優秀
良好
不及格
- (6)C語言的循環
#include <stdio.h>
int main() {
/*第一:for*/
for (int i = 0; i < 10; ++i) {
printf("hello %d\n", i);
}
for (int i = 0; i < 10; printf("hello %d\n", i++)) {
printf("alicfeng\n");
}
/*第二:while*/
// int index = 10;
// do {
// printf("hello %d\n", index--);
// } while (index > 0);
int index=0;
while (index<10){
printf("hello %d\n", index++);
}
return 0;
}
- (7)C語言的結構體
#include <stdio.h>
/*定義一個結構體*/
struct User{
int age;
char *name;
};
int main(){
struct User user;
user.age = 20;
user.name = "alicfeng";
//復制一個結構體變量 但是新的變量已經分配新的內存空間
struct User user1 = user;
//對user的成員改變不會影響user1的值
user.age = 21;
printf("%s age is %d\n",user.name,user.age);
printf("%s age is %d\n",user1.name,user1.age);
return 0;
}
make && run
? demo gcc -o main demoStruct.c && ./main
alicfeng age is 21
alicfeng age is 20
- (8)C語言的結構體指針
#include <stdio.h>
#include <stdlib.h>
struct User{
int age;
char *username;
};
int main(){
//對結構體的變量需要開辟內存空間
struct User *user = malloc(sizeof(struct User));
user->age = 20;
user->username = "alicfeng";
struct User *user1 = user;
user->age = 22;
printf("%s age is %d\n",user1->username,user1->age);
return 0;
}
make && run
? demo gcc -o main demoStructPoint.c && ./main
alicfeng age is 22
- (9)C語言的指針函數
#include <stdio.h>
#include <stdlib.h>
//正常函數 指針調用
void sayPointer(){
printf("hello pointer\n");
}
//指針參數的方法
void changeValue(int *value){
*value = 100;
}
int main(){
//example one
void (*p)();
p = sayPointer;
p();
//example two
int value = 0;
changeValue(&value);
printf("the value is %d\n",value);
return 0;
}
make && run
? demo gcc -o main demoFuncPointer.c && ./main
hello pointer
the value is 100
- (10)C語言的typedef關鍵字
#include <stdio.h>
//定義一個People的類型
typedef struct {
int age;
char *username;
} People;
void sayHello(){
printf("hello world\n");
}
//定義一個指針指向一個方法
typedef void (*Func)();
int main(){
People *people;
people->age = 22;
people->username = "alicfeng";
printf("%s age is %d\n",people->username,people->age);
//example two
Func pointer = sayHello;
pointer();
return 0;
}
make && run
? demo gcc -o main demoTypedef.c && ./main
alicfeng age is 22
hello world
- (11)C語言的頭文件
#header.h文件
#ifndef DEMO_DEMOHEADER_H
#define DEMO_DEMOHEADER_H
/**
* PS:這里只是函數的聲明,相當于接口的方法
*/
void sayHello();
#endif //DEMO_DEMOHEADER_H
//主體文件.c
#include <stdio.h>
#include "header.h"
/**
* 函數的定義
*/
void sayHello(){
printf("hello world\n");
}
# mainFunc
#include <stdio.h>
#include "header.h"
int main(){
sayHello();
return 0;
}
make && run
? demo gcc -o main demoHeader.c header.c && ./main
hello world
- C語言的字符串操作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
/*字符串的拼接 sprintf*/
char content[100];
//void *memset(void *s,int c,size_t n)
//將已開辟內存空間 s 的首 n 個字節的值設為值 c
memset(content,0,100);
//任意類型的值拼接成字符串
sprintf(content,"hello %s;the value is %f or %d","alicfeng",98.5,100);
printf("%s\n",content);
/*memcpy用來做內存拷貝*/
char *source = "hello world";
char des[5];
//開始拷貝
memcpy(des,source+6* sizeof(char),5* sizeof(char));
printf("%s\n",des);
/*字符串拷貝*/
char str1[10] = "alicfeng";
char *str2 = malloc(10);//char str2[10];也可以
strcpy(str2,str1);
printf("str2 value is %s\n",str2);
return 0;
}
make &run
? demo gcc -o main demoString.c && ./main
hello alicfeng;the value is 98.500000 or 100
world
str2 value is alicfeng
- C語言的文件操作
#include <stdio.h>
#include <stdlib.h>
int main() {
/*文件的寫入 覆蓋式寫入*/
//打開文件
FILE *file = fopen("log", "w");
//判斷文件是否打開失敗
if (file != NULL) {
for (int i = 0; i < 5; fprintf(file, "%d\n", ++i)) {
printf("寫 %d 入成功\n", i+1);
}
}
//關閉文件
fclose(file);
/*文件的寫入 追加式寫入*/
FILE *fileA = fopen("log", "a+");//以追加的方式寫入
if (NULL== fileA){
perror("open file error");
exit(1);
}
fputs("alic appending",fileA);
fclose(fileA);
/*文件的讀取*/
FILE *fileR = fopen("log", "r");
//獲取文件的大小
//要將指針放置文件流的最末端 參數0為偏移量
fseek(fileR, 0, SEEK_END);
long size = ftell(fileR);
//創建存放內容的緩沖區 并且還想在末端添加\0, 因而調大一個字節
char buf[size + 1];
//重新將指針置于文件的開始位置
fseek(fileR, 0, SEEK_SET);
fread(buf, sizeof(unsigned char), size, fileR);
buf[size] = '\0';
fclose(fileR);
printf("%s\n", buf);
/*文件重命名*/
int result = rename("log", "data");
printf("文件重命名%s\n", result == 0 ? "成功" : "失敗");
/*刪除文件*/
printf("刪除文件%s\n", remove("data") ? "失敗" : "成功");
return 0;
}
make && run
? demo gcc -o main demoFile.c && ./main
寫 1 入成功
寫 2 入成功
寫 3 入成功
寫 4 入成功
寫 5 入成功
1
2
3
4
5
alic appending
文件重命名成功
刪除文件成功
- C語言小游戲
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
//設置隨機數的機制 否則產生的隨機數是固定的值
srand(time(NULL));
//隨機生成兩位數的int
int randValue = rand()%100;
int userInput;
printf("Hello 請輸入兩位數:\n");
while(1){
scanf("%d",&userInput);
if (userInput>randValue){
printf("數值過大\n");
} else if (userInput<randValue){
printf("數值過小\n");
} else{
printf("恭喜您,答對了!\n");
break;
}
}
return 0;
}
make && run
? demo gcc -o main demoGame.c && ./main
Hello 請輸入兩位數:
50
數值過小
75
數值過小
85
數值過大
80
數值過小
82
數值過大
81
恭喜您,答對了!
源碼地址
****價值源于技術,貢獻源于分享****