本文目錄一覽:
c語言編程怎樣入門
任何知識的學習沒有太多捷徑,但有經驗、方法及教訓
(1)基礎教材選擇-系統又通俗易懂,最好有該書配套免費視頻
建議選擇系統正統的大學教材,盡量不要選擇“多少天精通C語言”等吸引眼球的教程,相信一點C語言學習沒有速成。這裡給大家推薦一本不錯的入門教程:清華大學出版社-孫海洋-C語言程序設計,講解很透徹、知識點很全面、例程較多且通俗易懂。優酷“孫海洋課題”還有全書免費教學視頻,便於自學。
(2)動起手來–立馬安裝VC++6.0或VS開發環境
C語言是特別注重動手實操能力的課程!!動起手來,現在開始安裝VC++6.0開發環境,從第一個經典程序“Hello,world!”開始,每一個例題及知識點均通過開發環境驗證、理解深化。多做每一章小型實驗操作(網上多得很)。提升代碼調試能力。
(3)有了基礎後,一般可以有兩個發展方向可供選擇
(i)轉向項目實戰
建議購買一本C語言項目教程,在實踐項目中強化理論知識的學習。
(ii)繼續深入理論學習
建議購買國外經典深入學習C語言的教程,人民郵電出版社-C Primer Plus(第5版),或者 機械工業出版社-C程序設計語言(第2版.新版)
下定信心,堅持下去!希望對你有所幫助。
下面是轉載的 孫海洋 版 C語言程序設計 部分內容截圖。
怎麼用c語言寫一個 程序。實現從鍵盤輸入字符並寫入一個文件。
1、C語言標準庫提供了一系列文件操作函數。文件操作函數一般以f+單詞的形式來命名(f是file的簡寫),其聲明位於stdio.h頭文件當中。例如:fopen、fclose函數用於文件打開與關閉;fscanf、fgets函數用於文件讀取;fprintf、fputs函數用於文件寫入;ftell、fseek函數用於文件操作位置的獲取與設置。一般的C語言教程都有文件操作一章,可以找本教材進一步學習。
2、例程:
#includestdio.h
char c;
int main(){
FILE * fp2 = fopen(“output.txt”, “w”);//打開輸出文件
if (fp2==NULL) {//若打開文件失敗則退出
puts(“不能打開文件!”);
rturn 0;
}
c=getchar();//從鍵盤讀取一個字符
fputc(c,fp2);//向輸出文件寫入一個字符
fclose(fp2);//關閉輸出文件,相當於保存
return 0;
}
c語言遊戲編程實例
生命遊戲
/* —————————————————— */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway’s */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* —————————————————— */
#include stdio.h
#include stdlib.h
#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0
char cell[MAXSIZE][MAXSIZE]; /* the board */
char workcopy[MAXSIZE][MAXSIZE]; /* a working copy */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/
/* —————————————————— */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* —————————————————— */
void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];
gets(line); /* read in gens, row and col*/
sscanf(line, “%d%d%d”, generations, row, column);
for (i = 0; i row; i++)/* clear the board */
for (j = 0; j column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != ‘\0’; i++)
if (line[i] != ‘ ‘)
cell[max_row][i] = OCCUPIED;
max_col = (max_col i) ? i : max_col;
}
row_gap = (row – max_row)/2; /* the moving gap */
col_gap = (column – max_col)/2;
for (i = max_row + row_gap – 1; i = row_gap; i–) {
for (j = max_col + col_gap – 1; j = col_gap; j–)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j = 0; j–)
cell[i][j] = UNOCCUPIED;
}
for ( ; i = 0; i–)
for (j = 0; j column; j++)
cell[i][j] = UNOCCUPIED;
}
/* —————————————————— */
/* FUNCTION display : */
/* Display the board. */
/* —————————————————— */
#define DRAW_BOARDER(n) { int i; \
printf(“\n+”); \
for (i = 0; i n; i++) \
printf(“-“); \
printf(“+”); \
}
void display(int gen_no)
{
int i, j;
if (gen_no == 0)
printf(“\n\nInitial Generation :\n”);
else
printf(“\n\nGeneration %d :\n”, gen_no);
DRAW_BOARDER(column);
for (i = 0; i row; i++) {
printf(“\n|”);
for (j = 0; j column; j++)
printf(“%c”, (cell[i][j] == OCCUPIED) ? ‘*’ : ‘ ‘);
printf(“|”);
}
DRAW_BOARDER(column);
}
/* —————————————————— */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* —————————————————— */
void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;
display(0); /* display initial config. */
done = NO;
for (iter = 1; iter = generations !done; iter++) {
memmove(workcopy, cell, MAXSIZE*MAXSIZE); /*copy*/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i row; i++) { /* scan each cell…*/
top = (i == 0) ? 0 : i – 1;
bottom = (i == row – 1) ? row-1 : i + 1;
for (j = 0; j column; j++) {
left = (j == 0) ? 0 : j – 1;
right = (j == column – 1) ? column-1 : j + 1;
/* compute number of neighbors */
neighbors = 0;
for (p = top; p = bottom; p++)
for (q = left; q = right; q++)
neighbors += workcopy[p][q];
neighbors -= workcopy[i][j];
/* determine life or dead */
if (workcopy[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable (workcopy[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf(“\n\nAll cells die out.”);
done = YES;
}
else if (stable) {
printf(“\n\nSystem enters a stable state.”);
done = YES;
}
else
display(iter);
}
}
/* —————————————————— */
void main(void)
{
read_in();
game_of_life();
}
用C語言編寫一個程序: 從鍵盤輸入 10 個整數,求出其中的最大值。
程序:
#includestdio.h
int main()
{
int arr[10] = {0};
int i = 0;
int max = 0;
int min = 0;
printf(“請輸入10個整數:”);
for (i = 0; i sizeof(arr)/ sizeof(arr[0]); i++)
{
scanf(“%d”,arr[i]);
}
max = arr[0];
for (i = 0; i sizeof(arr) / sizeof(arr[0]); i++)
{
if (max arr[i])
{
max = arr[i];
}
}
min = arr[0];
for (i = 0; i sizeof(arr) / sizeof(arr[0]); i++)
{
if (min arr[i])
{
min = arr[i];
}
}
printf(“max=%d\n”, max);
printf(“min=%d\n”, min);
return 0;
}
結果:
請輸入10個整數:1 2 3 56 23 6 767 32 11 567
max=767
min=1
請按任意鍵繼續. . .
擴展資料:
編寫過程分為三部分:源代碼文件 —— 目標代碼文件——可執行文件。
用到兩個組件:編譯器、鏈接器。編譯器的作用是將源代碼轉換為中間代碼,產生中間文件。鏈接器將此中間代碼與其他代碼相結合來生成可執行文件。
中間文件的形式有多種,一般就是將源代碼文件轉換為機器語言代碼,將其結果放置在一個目標代碼文件中。雖然目標代碼文件包含機器代碼文件,但是該文件還不能運行。目標文件包含源代碼的轉換結果,但它還不是一個完整的程序,也就是不是一個完整的可執行文件,它還需要與一些基本元素。
目標代碼文件中所缺少的第一個元素是一種叫做啟動代碼的東西,這個代碼相當於程序跟操作系統之間的接口。所缺少的第二個元素是庫例程的代碼,幾乎所有c程序都利用標準c庫中所包含的例程,例如printf。
而鏈接器的作用就是將這三部分結合在一起,並將它們存放在單個文件,即可執行文件中,這樣,一個完整的可執行文件就產生了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/290828.html