C Language Programming Case Tutorial (2nd edition) code note (iii)

Source: Internet
Author: User

Scattered knowledge points:
    • variables : In the C language, each variable must first be defined and then referenced. The so-called variable existence refers to the system to allocate a piece of storage space for the variable, at this time the operation of the variable is to the variables corresponding to the storage space stored in the data to operate. The time when a variable occupies a storage space is called the lifetime of the variable, and the area that the variable can refer to is called the scope of the variable. The scope of a variable depends on where the variable is defined.
    • Global Variables : variables defined outside the function are called global variables. The lifetime of a global variable is the program file that defines the variable, starting at the definition and ending at the end of the program file. If some local variables in the program file have the same name, the scope of the global variable should be subtracted from this part of the area.
    • Local Variables : variables defined inside a function, including formal parameters defined in the parameter table, are called local variables. The lifetime and scope of local variables are functions that define these variables. If a block variable with the same name as a local variable appears in the function, the scope of the local variable is the area where the block variable exists, but its life cycle does not end. (variables that can have the same name between different functions)
    • block variable : A variable defined in a compound statement is called a block variable. The lifetime of a block variable is only a compound statement block that defines it. Advantage: You can increase the utilization of storage space.
    • static variable : A variable allocates storage space to the system when it starts running, and all of its allocated storage space is recycled until the program ends.
    1. Format: Static data type variable name;
    2. Description
    • Global variables have the properties of static variables
    • By default, variables defined inside a function are automatically variable
    • A variable defined as a static variable that is initialized only when it is first used, and subsequent calls are no longer initialized
    • The formal parameter of the function must be an automatic variable. Therefore, in the C language, it is not allowed to use the storage class specifier when defining formal parameters
    • Variables with automatic (auto) storage classes coexist with the function, and the variables with static (static) storage classes die.
    • struct : is a composite data type that can combine variables of several different data types. It is often used to encapsulate different properties that represent the same object.

    1. Advantages: Can improve the program's clarity, reduce the complexity of the program, improve the maintainability of the program.
    2. A struct's variable is called a member, (in a struct, a member can belong to any data type). The syntax format for its type declaration:

struct < struct type name > {

< data types > < members 1>;

< data types > < members 2>;

......

< data types > < members n>

};

    • in the In the C language, the user is allowed an alias for the data type that already exists in the format:typedef Original data type name new data type name ;

For example: typedef int INTEGER;

Advantages: Can improve the clarity of the program, increase the readability of the program

After combining:

typedef struct POINT_TYPE {

Int x;

Int y;

} point

Point P1, p2

Equivalent to:

struct Point_type p1, p2;

Equivalent to:

struct point_type{

Int x;

Int y;

} p1, p2;

Initialization of struct type variables: struct Point_type p1 = {10,20};

Reference to struct type variable: p1.x; P1.y

Input of struct type variable: scanf ("%d%d", &p1.x,&p1.y);

Output of struct type variables: printf ("%d%d", p1.x,p1.y);

Assignment of struct type variables: p1.x=6; p1.y=8;

    • struct code example :

function Description: Enter The basic information of the students via the keyboard and output on the above. Then, enter a month and date through the keyboard to find and output the student information for the year after this given date.

code example:

1#include <stdio.h>2 #pragmaWarning (disable:4996)3 #defineNUM 204typedefstruct{5     intYear/*years*/6     intMonth/*Month*/7     intDay/*Day*/8 } DATE;9 Tentypedefstruct{ One     intNum/*School Number*/ A     Charname[ -];/*name*/ -DATE birthday;/*Date of birth*/ -     Chardepartment[ -];/*Affiliated Faculties*/ the     Charmajor[ +];/* Major in major*/ - } studentinfo; - voidInputinfo (Studentinfo s[]); - voidOutinfo (Studentinfo s[]); + voidSearchInfo (Studentinfo s[], date date); -  + Main () A { at Studentinfo S[num]; - date date; - Inputinfo (s); - Outinfo (s); -printf"\ n Enter a date (month,day): \ n"); -scanf"%d%d", &date.month, &date.day); in SearchInfo (S, date); - } to  + voidInputinfo (studentinfo s[]) - { the     inti; *printf"Enter%d studennts information (name,birthday,department,major): \ n", NUM); $      for(i =0; i < NUM; i++){Panax NotoginsengS[i].num = i +1; -scanf"%s", s[i].name); thescanf"%d%d%d", &s[i].birthday.year, &s[i].birthday.month, &s[i].birthday.day); +scanf"%s", s[i].department); Ascanf"%s", s[i].major); thePutchar ('\ n'); +     } - } $  $ voidOutinfo (studentinfo s[]) - { -     inti; theprintf"\ n Num Name Birthday Department major\n"); -      for(i =0; i < NUM; i++){Wuyiprintf"%4d%14s%4d/%2d/%2d%16s%16s", the S[i].num, S[i].name, S[i].birthday.year, S[i].birthday.month, S[i].birthday.day, - s[i].department, s[i].major); WuPutchar ('\ n'); -     } About } $  - voidSearchInfo (Studentinfo s[], date date) - { -     inti; A      for(i =0; i < NUM; i++){ +         if(s[i].birthday.month>date.month) { theprintf"\n%4d%16s%2d/%2d", S[i].num,s[i].name, S[i].birthday.month, - s[i].birthday.day); $             Continue; the         } the         if(S[i].birthday.month = = Date.month && s[i].birthday.day>date.day) { theprintf"\n%4d%16s%2d/%2d", S[i].num, S[i].name, S[i].birthday.month, the s[i].birthday.day); -         } in     } the}

C Language Programming Case Tutorial (2nd edition) code note (iii)

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.