C基础
来源:互联网 发布:java md5 32加密算法 编辑:程序博客网 时间:2024/05/15 09:00
一.C语言关键字符
二.gcc 编译C程序
hello.c
#include <stdio.h>int main(){ /* 我的第一个 C 程序 */ printf("Hello, World! \n"); return 0;}gcc hello.c -o hello.out
三. gcc 编译两个c文件 两个文件相互extern引用
1.c
#include <stdio.h> int count ;extern void write_extern(); main(){ count = 5; write_extern();}
#include <stdio.h> extern int count; void write_extern(void){ printf("count is %d\n", count);}
gcc 1.c 2.c -o 1.out // 生成 1.out
三. 测试传递数组给函数
4.c
#include <stdio.h>double getAverage(int arr[], int size);int main (){ int balance[5] = {1000, 2, 3, 17, 50}; double avg; avg = getAverage( balance, 5 ) ; printf( "Average: %f \n", avg ); return 0;}double getAverage(int arr[], int size){ int i; double avg; double sum; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = sum / size; return avg;}gcc 4.c -o 4.out
四: 测试 从函数返回数组
5.c
#include <stdio.h>/* 要生成和返回随机数的函数 */int * getRandom( ){ static int r[10]; // C 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量 int i; /* 设置种子 */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { r[i] = rand(); printf( "r[%d] = %d\n", i, r[i]); } return r;}/* 要调用上面定义函数的主函数 */int main (){ /* 一个指向整数的指针 */ int *p; int i; p = getRandom(); for ( i = 0; i < 10; i++ ) { printf( "*(p + %d) : %d\n", i, *(p + i)); } return 0;}gcc 5.c -o 5.out
五: 指向数组的指针
6.c
#include <stdio.h>int main (){ double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; double *p; int i; p = balance; printf( "Use Point Get Value:\n"); for ( i = 0; i < 5; i++ ) { printf("*(p + %d) : %f\n", i, *(p + i) ); } printf( "Use Array Get Value:\n"); for ( i = 0; i < 5; i++ ) { printf("*(balance + %d) : %f\n", i, *(balance + i) ); } return 0;}gcc 6.c -o 6.out
六: 屏幕字符输入输出 getchar() putchar() scanf() 和 printf() 函数
7.c
#include <stdio.h>int main( ){ int c; printf( "Enter a value :"); c = getchar( ); printf( "\nYou entered: "); putchar( c ); printf( "\n"); return 0;}gcc 7.c -o 7.out
8.c
#include <stdio.h>int main( ){ char str[100]; int i; printf( "Enter a value :"); scanf("%s %d", str, &i); printf( "\nYou entered: %s %d ", str, i); return 0;}gcc 8.c -o 8.out
七:文件输入输出
9.c
#include <stdio.h>main(){ FILE *fp; fp = fopen("/mnt/fdisk/zwx320975/C/helloworld/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp);}
gcc 9.c -o 9.out
test.txt 文件包含
This is testing for fprintf...This is testing for fputs...
10.c
#include <stdio.h>main(){ FILE *fp; char buff[255]; fp = fopen("/mnt/fdisk/zwx320975/C/helloworld/test.txt", "r"); fscanf(fp, "%s", buff); printf("1 : %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("2: %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("3: %s\n", buff ); fclose(fp);}gcc 10.c -o 10.out
1 : This2: is testing for fprintf...3: This is testing for fputs...首先,fscanf() 方法只读取了 This,因为它在后边遇到了一个空格。其次,调用 fgets() 读取剩余的部分,直到行尾。最后,调用 fgets() 完整地读取第二行。
八:指针
#include<stdio.h>void main(){const int a = 2;int b = 1;const int * tempa = &a; // 指向 int常量的 指针int * const tempb = &b; // 指向 int 类型的 指针常量int * const tempb = &a;// 指向 int 类型的 指针常量 可以指向 int 常量 也可以是变量printf(" valueA = %d\n",*tempa);printf(" valueB = %d\n",*tempb);const int c = 2;//*tempa = 4; // 报错编译不过 因为 tempa 只能指向 const int ,int类型常量//tempa = &b; //报错编译不过 因为 tempa 只能指向 const int , b 不是 int类型常量 是变量tempa = &c; //OK//tempb = &a; // 报错编译不过 因为 tempb 是常量 第一次初始化赋值后 就不能再赋值*tempb = 3; //OK 能通过指针修改Value}
const int d = 2;
const int * const tempc= &d;// 指向 int常量 的 指针常量 必须指向 int 常量
所有对tempc 和 *tempc 都编译不过
0 0
- C #基础
- c基础
- C基础
- c基础
- C 基础
- c基础
- C基础
- c基础
- C基础
- C基础
- C基础
- c基础
- C 基础
- c基础
- C基础
- C#基础
- C基础
- C基础
- 程序设计竞赛训练手册
- 利用SpringMvc和MyBatis实现员工列表显示页面
- 准备在CSDN安家啦。
- Codeforces Problem 707C Pythagorean Triples(数学)
- Codeforces Round #364 (Div. 2) A. Cards
- C基础
- 命令熟悉
- POJ 2186 Popular Cows Korasaju .
- Expedition(优先队列)
- debian sudo自动补全
- 不能打开到主机的连接, 在端口 1521: 连接失败
- 内部排序
- 比特率和波特率的关系
- Debian 8.5设置静态IP