C Language First chapter C language Introduction

Source: Internet
Author: User
Tags variable scope

Directory

    • Introduction of C language
    • Ii. Characteristics of C language
    • Third, Hello World
    • IV. Escape character
    • Five, placeholder
    • Six, Tetris Game
    • Vii. Download of documents
Introduction of C language

C is a general programming language, which is widely used in the development of system software and application software. From 1969 to 1973, in order to transplant and develop UNIX operating system, Dennis Ritchie and Ken Thompson, based on B language, was designed and developed at Bell Labs.
The C language is highly efficient, flexible, functional, expressive and highly portable, and has been favored by programmers for the most widely used programming language in the last 25 years [2]. Currently, C language compilers are ubiquitous in various operating systems, such as Microsoft Windows, Mac OS X, Linux, Unix, and so on. The design of C language affects many later programming languages, such as C + +, objective-c, Java, C #, and so on.
In the 1980s, in order to avoid the differences in C language grammar produced by each development manufacturer, a complete set of international standard grammar, called ANSI C, was established by the American National Standards Bureau as the standard of C language. The 1980s to date the relevant program development tools generally support the syntax to match ANSI C.

Ii. Characteristics of C language

C language is a procedural language with structured programming, variable scope (variable scope) and recursive function.
The C-language pass-through parameter is passed as value (pass by value), and the pointer (a pointer passed by value) can also be passed.
Different types of variables can be grouped together by struct (struct).
There are only 32 reserved words (reserved keywords), which makes the variable, function name more elastic.
Part of the variable type can be converted, such as Integer and character variables.
with pointers (pointer), the C language makes it easy to control low-level memory.
Compile preprocessing (preprocessor) makes the compilation of C language more resilient.

Third, Hello World

#include "stdio.h" void Main () {   printf ("Hello world!");}

IV. Escape character
Single-line comment/*1, multiline comment, single-line comment 2, C language is composed of function name () *///3, contains header file, printf function in Stdio.h#include "stdio.h"//4, define the main function, there is only one, is the program's entry void Main () {    ///5, Output information to the screen, call function    printf ("Hello world!\n");        6, escape character    //\n career    //\t tab    //\ "double quotes    //\ ' single quotation mark    //\b backspace    //\a beep    printf (" Zhang Guolao teacher said \t\t ");    printf ("\" Hello s1sr139!\ ');    printf ("\a\a\a");}

Five, placeholder
#include "stdio.h" void Main () {    /**          %c        single character          %d        decimal integer          %f        decimal floating-          point        number          %o octal %s        string          %u        unsigned decimal number          %x        hexadecimal number percent        output percent%        A format description can have several modifiers to specify the display width, Fractional-tailed and left-aligned, etc.:          -         left alignment          + "         +" or "-" numbers preceded by a signed number          0         field width with leading zero padding instead of white space        *                /printf ("% 10s%s's score was%lf, class%d, gender%c. \ n "," "," Zhang 3 ", 98.5,100, ' M ');                        printf ("%10s\n", "Hello");        printf ("%10s\n", "good");        printf ("%10s\n", "Boy");}

Six, Tetris Game

Game.cpp C + + code is as follows:

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> #include < conio.h> #define MOD 28#define size_n 19#define size_m 12int cur_x,cur_y;int score,mark,next,map[size_n][size_m], Gamespeed=300;int shape[28][6]={{0,-1,0,-2,1,0}, {0,1,1,0,2,0}, { -1,0,0,1,0,2}, {0,-1,-1,0,-2,0}, {0,-1,0,1 , -1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1}, { -1,1,0,1,1,0}, {0,-1,1,0,1,1}, { -1,0,0,-1,1,-1}, { -1,-1,-  1,0,0,1}, { -1,0,0,1,1,1}, {0,1,1,-1,1,0}, { -1,0,0,1,1,1}, {0,1,1,-1,1,0}, { -1,0,0,-1,0,-2}, { -1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0}, {0,1,1,0,1,1}, {0,-1,1,0,1,-1}, { -1,0,0,-1,-1,-1}, { -1,0,-1,1,0,1}, {0,1,0,2        , 0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, { -1,0,-2,0,-3,0}};void gotoxy (int x,int y) {COORD C; C.x=x-1;        C.y=y-1; SetConsoleCursorPosition (GetStdHandle (Std_output_handle), c);}        void Gameover () {int i,j,flag=0; for (j=1;j<size_m-1;j++) {if (map[1]                [j]!=0) {flag=1;break;                        }} if (Flag==1) {for (i=1;i<size_n-1;i++) {gotoxy (2,i+1);                        for (j=1;j<size_m-1;j++) {printf ("-");                }puts ("");                } gotoxy (7,9);                printf ("GAME over!");                Gotoxy (1,size_n+1);        Exit (0);        }}void showmap (int id) {int i,j;        Gotoxy (a);                                if (id!=-1) {for (i=0;i<size_n;i++) {for (j=0;j<size_m;j++) { if (i==0&&j==0 | | i==0&&j==size_m-1 | | j==0&&i==size_n-1 | | j==size_m-1&&i==size                                _n-1) printf ("");                                else if (i==0 | | i==size_n-1) printf ("--");                                else if (j==0 | | j==size_m-1) printf ("|");        else if (map[i][j]==2) printf ("");                        else if (i==cur_x+shape[id][0] && j==cur_y+shape[id][1] | |                                        I==CUR_X+SHAPE[ID][2] && J==cur_y+shape[id][3] | |                                        I==CUR_X+SHAPE[ID][4] && J==cur_y+shape[id][5] | |                                i==cur_x && j==cur_y) printf ("");                                else if (map[i][j]==0) printf ("");                        } if (i==1) printf ("Next:");                        if (i==11) printf ("Bisection:%d", score);                        if (i==14) printf ("Speed:%d", score/100+1);                Puts ("");                }} else {mark=1; for (i=0;i<size_n;i++) {for (j=0;j<size_m;j++) {if (I==0&&amp ; J==0 | | i==0&&j==size_m-1 | | j==0&&i==size_n-1 | | J==size_m-1&&i==size_n-1) printf ("");                                else if (i==0 | | i==size_n-1) printf ("--");                                else if (j==0 | | j==size_m-1) printf ("|");                                else if (map[i][j]==2) printf ("");                                else if (map[i][j]==0) printf ("");                        } if (i==1) printf ("Next:");                        if (i==11) printf ("Score:%d", score);                        if (i==14) printf ("Speed:%d", score/100+1);                Puts (""); }} gotoxy (30,6);        printf ("");        for (i=0;i<6;i=i+2) {gotoxy (30+2*shape[id][i+1],6+shape[id][i]); printf (""); } gotoxy (30,6);        printf ("");        for (i=0;i<6;i=i+2) {gotoxy (30+2*shape[next][i+1],6+shape[next][i]); printf (""); } Sleep (Gamespeed);}        void init (int id) {int i,j;        memset (map,0,sizeof (map)); for (i=0;i&Lt                                size_n;i++) {for (j=0;j<size_m;j++) if (i==size_n-1 | | j==0 | | j==size_m-1)        Map[i][j]=-1; } cur_x=0;        cur_y=5; Showmap (ID);}        int judge_in (int x,int y,int ID) {int i;        if (map[x][y]!=0) return 0;        for (i=0;i<6;i=i+2) {if (map[x+shape[id][i] [y+shape[id][i+1]]!=0) return 0; }return 1;}        void Fun_score () {int i,j,ii,jj;                for (i=1;i<size_n-1;i++) {int flag=0;                for (j=1;j<size_m-1;j++) {if (map[i][j]!=2) {flag=1;break;}                        } if (flag==0) {int k=3;                                while (k--) {gotoxy (2,i+1);                                                for (ii=1;ii<size_m-1;ii++) {if (map[i][ii]==2) {                                       if (k%2==1) printf ("");         else printf ("");                        }}sleep (100); } for (ii=i;ii>1;ii--) {for (jj=1;jj<size_m-1;jj++) map[ii][jj]=                        MAP[II-1][JJ];                        } showmap (-1);                        score+=10;                if (score%100==0 && score!=0) gamespeed-=50;                }}}int Main () {int i,id,set=1;        Srand (Time (NULL)); Id=rand ()%mod;        Id= (id+mod)%mod; Next=rand ()%mod;                next= (next+mod)%mod;                init (ID);                while (1) {here:mark=0;                        if (set==0) {id=next; Next=rand ()%mod;                        next= (next+mod)%mod;                        cur_x=0;cur_y=5;                Set=1;                     } while (!kbhit ()) {Gameover ();   if (judge_in (cur_x+1,cur_y,id) ==1) cur_x++;                                else {map[cur_x][cur_y]=2;                                for (i=0;i<6;i=i+2) map[cur_x+shape[id][i] [cur_y+shape[id][i+1]]=2;                                Fun_score ();                        set=0;                        } if (mark!=1) Showmap (ID);                goto here;                } char key;                                Key=getch ();                        if (key==72) {int tmp=id;                        id++;                        if (id%4==0 && id!=0) id=id-4;                if (judge_in (cur_x,cur_y,id)!=1) id=tmp;                } else if (key==80 && judge_in (cur_x+1,cur_y,id) ==1) cur_x++;                else if (key==75 && judge_in (cur_x,cur_y-1,id) ==1) cur_y--; else if (key==77 &&Judge_in (cur_x,cur_y+1,id) ==1) cur_y++;        else if (key==27) {gotoxy (1,size_n+1); exit (0);}        } getch (); return 0; }

Vii. Download of documents

Help and examples Click Download

Development tools Click to download

C Language First chapter C language Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.