c language - a programming language into the first chapter

Created in the editor c ++ vs empty project, the source file name under which to create the file .cpp file, the file is c ++, you can start learning c language!

// define the program: In order for the computer to perform certain actions or to solve a problem, but a collection of instructions written order.

// c language belongs to the high-level language: The computer uses a binary system, c language want to run on your computer, you need a compiler to translate c language code into the binary 0 and 1 this process is called code compiler
 // c during operation of language: source code to ".c" end of the file, first compile source code into an object file, the suffix ".o" or ".obj", and the connector c language library, to generate an executable final the ".exe" file.

// Features c language: 1. modular program design, the level of clear concise language 2. 3. powerful, can develop system software, application software can be developed 4. portability, and can run on different operating systems.

// the include include, to the end of the .h file called header files. 
#include <stdio.h> // preprocessing instructions, also called the first reference document, the following compiled language code c must be introduced

// desired random number: it requires the introduction of two headers 
#include <stdlib.h> // random number rand () function where the file header, to give a random integer int, a predetermined value, the time necessary to introduce desired changes 
#include <time.h> // time: let the random numbers change over time, although this does not introduce random number generation, but will not change, that value has been

#define PI 3.14   // the DEFINE defined constants: the program has maintained a constant amount called constants. Syntax: #define constant name value 
int R & lt; // define global variables, global variables defined outside the called method body function body, which is called a local variable, the difference (can get local to global, topical not put out outside the body to use)

// Since prototype declaration defined function: the function body does not need to write {} while statement 
void jjxfb (); // if custom function written on the back when the main function, the main function needs to be called, must be declared to the front to find him, if this is defined in front of the main function, it need not be declared.


// main function entry must have all c language entry function, corresponding to entrance of the building 
void main () // void no return value represents mean 
{
     // generates a random number of 1-3 
    srand (time (NULL)) ;
     int COM = RAND ()% . 3 + . 1 ; // random seed 
    the printf ( " random number:% D \ n- " , COM);

    printf ( " Please enter the radius of the circle: " );
    scanf_s("%d", &r);

    double s = PI * r * r;
    the printf ( " radius of% d park area: 2Lf% \ n-. " , R & lt, S);

    // define the variable 
    int A = 2 , B = . 3 ; Double C = 5.23 ;
     // B = A ++; // first assignment from Canada. 3 B = 2 = A 
     @ B = A ++; // first increase since after the assignment. 3 = B =. 3 A
     // A = A + B +; // A = A + B =. 5. 7 + = A + 2 =. 5. 7. 5 =
     // B = (A + =. 5, A -. 5, *. 5 A, A /. 5); // comma operator 
    B = ( int ) (A + C); // cast 
    the printf ( " A% = D, B = D% " , A, B);

    // data size of the sort: char. 1 <Short 2 <= a float int. 4 = Long <Double. 8 
    the printf ( " Get the size of a data type: " , the sizeof (A)); // can also write or sizeof a sizeof (int) three kinds of data types can not be written in parentheses omitted variable name

    int width, height, area;
    the printf ( " Please enter the length and width: " );
     // fflush (stdin); // action: empty the cache, clear ENTER 
    scanf_s ( " % D% D " , & width, height &); // without No. acquiescence with spaces and carriage returns instead of

    if (width>0 && height>0)
    {
        area = width * height;
        the printf ( " Area is:% d \ n newline " , Area);
    }
    the else  IF (width == 0 ) // multiple conditional branch statement 
    {
         // similar switch (int / char) {case value 1: case Value 2: ... break; case value 3: ... break; default: } ... 
    }
     the else
    {
        printf ( " make a mistake \ t escape character print 8 spaces! " );
    }

    / * Basic data types are lowercase% placeholder
    Plastic
        Short or short int shaping short% h 2byte represents 2 bytes
        * Shaping int% d 4 bytes
        Long integer or long int bytes long% ld 4

    Float
        Single precision float% f 4-byte Reserved 7 // most significant digits
        Double-byte double% lf 8 // 16 can remain significant figures

    Single quote character '' as a single byte char% c: '2' + '2' = results of d

    */


    //switch语法**********************************************************************
    int year, month, day;
    printf ( " Please enter the year - month: " );
    scanf_s("%d-%d", &year, &month);

    // determine how many days the month 
    Switch (month The)
    {
        // Otsuki 
        Case  1 :    
         Case  3 :
         Case  5 :
         Case  7 :
         Case  8 :
         Case  10 :
         Case  12 :
            Day = 31 is ;
             break ; // jump out of the structure without meaning to break later in the program continues executing code
         // Satsuki 
        Case  . 4 :
         Case  . 6 :
         Case  . 9 :
         Case  . 11 :
            Day = 30 ;
             BREAK ;
         // February this leap year 29 days 28 days average year 
        Case  2 :
            Day = (% year . 4 == 0 && year% 100 ! = 0 || year% 400 == 0 )? 29 : 28 ;
             BREAK ;
         // time in front of all these conditions are not met long code 
        default :
             BREAK ;
    }
    printf ( " % d of% d% d months there have been days \ the n-. " , year, month The, Day);

    // the while (condition) circulating 
    the while ( to true ) // Condition: true represents the endless loop can be changed to other conditions 
    {
         // loop: the code need to loop operation, to the condition determination, in the loop body 
        BREAK ; // using break; to exit the loop 
    }

    // do-while (condition) loop 
    do {
         // loop body: whether while conditions are met, the first time through the loop body to judge the conditions are not satisfied exit, continue to meet on the next cycle. 
    } The while ( to false );

    int cont = 1;
    for (int i = 1; i <= 10; i++)
    {
        IF (I == . 5 ) Continue ; // out of the current loop to continue the next cycle

        IF (I> . 8 ) BREAK ; // out the whole cycle 

        CONT ++; // external variable external definitions need to use 
        the printf ( " % D " , I);
         // for loop can also be put inside the for loop, loop are It can be nested 
    }
    printf("\n");

    // one-dimensional array syntax: variable name Data type [length limitation] = {the number of elements depends on the length assignment} 
    int SS [ . 3 ] = { . 1 , 2 , . 3 }; // corresponds to the same hotel 3 a one room, no room fourth, the fourth element is written on the error 
    int Score [] = { . 1 , 2 , . 3 , . 4 }; // [not assigned] indicates the length is not fixed, depending on the number of elements , how many elements, length is the number
     // get the array index value syntax: variable name [index] where the index value is the standard small elements, 0 represents the first element, the first element 1 represents ..... so the length of the index can not be greater than -1


    jjxfb (); // call the function, the function part of the code of good packaging, take over the main function is executed, do not call him will not be implemented
    
    // If the function has a return value can be used to receive variables, such as: variable name = data type function name (); // receiver functions return value 
}

// Define a function Syntax: return type function name (parameter) {function body block} 
void jjxfb () // multiplication table (no reference function without return) 
{
     for ( int I = . 1 ; I <= . 9 ; I ++ )
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d*%d=%d \t", i, j, i * j);
        }
        printf("\n");
    }
}
/ * Four kinds of types of functions
Without return no parameters: void function name () {}
There are no parameters return: return type function name () {return a return value;}
There are no parameters return: void function name (parameter) {}
There have parameters return: return type function name (parameter) {return a return value;}

Either a function does not return a value or a maximum of one return value.
*/

Guess you like

Origin www.cnblogs.com/longxinyv/p/12161141.html