Programming C: Posts 2 – The components in C

1. The control characters

  • \n : Jump down to the next line in the first column soup.
  • \t : horizontal tab.
  • \r : Jump to surrender, not unloaded.
  • \the : Beep.
  • \\ : In the Forums \
  • \” : In the Forums “
  • \’ : In the Forums ‘
  • %%: In the Forums %

These are just some familiar characters control, or use, in addition to some other control characters you can see in other documents. To understand the control characters you run the test program later and manually draw your own comment.

#include <stdio.h>

int main()
{
    printf("\a");
    printf("Hinh nhu vua co tieng gi keu @@\n");
    printf("Ban dang o dong thu 2\nBay gio xuong dong 3 roi ne ^^\n");
    printf("\tDong nay cach ra 1 tab thi phai?\n");
    printf("\t\t\t\t\t\tCach ra nhieu tab qua \rVe dau dong thoi\n");
    printf("Dau \\ \nDau \'\nDau \" \nDau %%\n");

    // day la mot dong ghi chu va chi co tac dung chu thich khi viet code cho de hieu

    /*
      Day la mot doan ghi chu
      Doan ghi chu nay co 2 dong
     */
    return 0;
}

2. Scripts

Scripts in C language consists of characters, following symbols: (distinguish uppercase and lowercase):

  • 26 A Latin alphabet flowers,B,C…From
  • 26 Latin alphabet is often a,b,c …from.
  • 10 decimal 0,1,2…9.
  • The mathematical symbols: +, -, *, /, =, <, >, (, )
  • The special symbols: :. , ; ” ‘ _ @ # $ ! ^ [ ] { } …
  • Spaces or gaps, down the line (\n) and tabs (\t)

3. Tag

Are words that C has built and there are certain functions, later made many of you will know gradually. We should not redefine these keywords.

asm • auto • break • case • cdecl • char • class • const • continue • _cs • default • delete • do double • _ds • else • enum • _es • extern • _export • far • _fastcall • float • for •friend • goto • huge • if • inline • int • interrupt • _loadds • long • near • new • operator • pascal • private • protected • public • register • return • _saveregs • _seg • short •signed • sizeof • _ss • static • struct • switch • template • this • typedef • union • unsigned • virtual • void • volatile • while

4. Type of data

In life there are many types of objects to contain the different things such as baskets for storing vegetables, The cup (that) holds water. Likewise, in programming the data types to be performed (storage) the corresponding components.

Each data type has a size (occupied area), type of domain values ​​and different values ​​in computer memory when using. Here are some basic data types commonly used in C.

Data types Type of data Size (byte) Domain values
Style Character tank 1 -128 to 127
unsigned char 1 0 to 255
Type Integer short 2 -32768 to 32767
int 4 -2147483648 to 2147483647
long 4 -2147483648 to 2147483647
long long 8 -9223372036854775808 to 9223372036854775807
Style Number real float 4 1.175494-38 to 3.40282338
double 8 2.225074308 to 1.797693308
long double 12 3.362103-4932 to 1.1897314932

Noted:

  • Above is the information table of some common data type or use. Also, many types of data, but do not take into, you can consult in here for the type integer, Here the real data type
  • Do C (or C ++) there are many different versions and used on different operating systems should be able to value and size of data types also form different. The table above is I check on win 7, Dev-C 5.11.

You can use this program to run and test results:

#include <stdio.h>
#include <limits.h>  // limits for interger
#include <float.h>   // limits for float

int main()
{
    printf("TYPE                %6s %20s %20s\n", "SIZE", "MIN VALUE", "MAX VALUE");
    printf("char:          %6ld byte %20d %20d\n", sizeof(char), CHAR_MIN, CHAR_MAX);
    printf("unsigned char: %6ld byte %20d %20d\n", sizeof(unsigned char), 0, UCHAR_MAX);
    printf("short:         %6ld byte %20d %20d\n", sizeof(short), SHRT_MIN, SHRT_MAX);
    printf("int:           %6ld byte %20d %20d\n", sizeof(int), INT_MIN, INT_MAX);
    printf("long:          %6ld byte %20ld %20ld\n", sizeof(long), LONG_MIN, LONG_MAX);
    printf("long long:     %6ld byte %20lld %20lld\n", sizeof(long long), LLONG_MIN, LLONG_MAX);
    printf("float:         %6ld byte %20e %20e\n", sizeof(float), FLT_MIN, FLT_MAX);
    printf("double:        %6ld byte %20e %20e\n", sizeof(double), DBL_MIN, DBL_MAX);
    printf("long double:   %6ld byte %20Le %20Le\n", sizeof(long double), LDBL_MIN, LDBL_MAX);
    return 0;
}

5. Name

Each of us who has name, so in C programming components will also be named. For example, the function name, variable name, constant name, label name,…

Invalid name is a consecutive sequence composed of alphanumeric characters, some or underscores. The name is case sensitive and does not coincide with keywords.

  • Chemical name is case-sensitive, so: the other The, offices other offices.
  • VD The correct name: the, offices, nguyenvanquan7826, quan_7826, _7826, _nhung, _123huong.
  • **VD The wrong name: **
Wrong name Reason
1 Begins with
1the Begins with
offices 7826 Contains spaces
office-7826 Contain dashes
f(x) Contains curly braces
int Coincides with keywords

6. Variable

Corresponding to each type of data we have the variables of those types and corresponding domain values ​​as above is used to store values. You need to differentiate types and variables.

A VD basket to put spinach, basket B to hold them together as parsley and basketball, respectively, the values ​​of variables a note 5, also store the value of the variable b 9 although they are the same type integer.

Variable values ​​can be changed during the running program.

6.1 Declare variable

To declare a variable we use the syntax:

Kieu_du_lieu Danh_sach_ten_bien;

Theory, longish, you see and run the following program will see more detail:

#include <stdio.h>

int main()
{
    int a, b; // khai bao 2 bien kieu so nguyen
    float c; // khai bao 1 bien kieu so thuc

    // Gan gia tri cho cac bien

    a = 1;      
    b = 2;
    c = 3.4;

    // vua khai bao bien vua gan gia tri
    int e = 4, f = 6;

    printf("a = %d; b = %d\n", a, b);
    printf("c = %f\n", c);
    printf("e = %d; f = %d\n", e, f);

    return 0;
}

As you see on the structure and declared that. As for the command output to display the value, there are some other things that are using the% d, %f . The thing I will say later in detail, So, now that you keep writing in order to see how to declare variables is ok roài. !

6.2 The range of variables

When programming, you have to understand the scope of the variable. If the declaration and use of improper, obviously will not lead to errors difficult to control, so you need to determine the correct position, scope of use before using variable variables.

Each variable is declared where they will be used and the corresponding operating range from where it declared to brace } with its horizontal level.

For example the following program.

#include <stdio.h>

int a = 1, b = 5; // khai bao bien toan cuc, no se duoc dung o bat ky dau ke tu dong nay.

int main()
{
    // khai bao 2 bien trong ham main, no se duoc dung trong toan bo ham main ke tu dong nay
    int c = 4, d = 6; 

    if(c < d)
    {
        int e = 6, d = 8;  // khai bao bien e va d, no duoc dung trong doan nay.
        c = 7;
        printf("gia tri cac bien trong khoi:\n");
        printf("e = %d \t d = %d \t c = %d\n", e, d, c);
    } // den day bien e, d vua khai bao khong con hoat dong nua.

    // printf("gia tri bien e = %d\n", e);  // lenh nay sai vi bien e khong con ton tai nua

    printf("gia tri cac bien trong ham main:\n");
    printf("c = %d \t d = %d\n", c, d);

    printf("gia tri cac bien toan cuc:\n");
    printf("a = %d \t b = %d\n", a, b);

    return 0;
}

You run the program, see results and comments themselves drawn nhá.

In this there is a small explanation with you after running the program that we have report 2 variable d, and 2 variable d due within 2 various command blocks (1 what lies in the main function, 1 what lies in the if statement ) Should we completely different, not affect each other. But then just declare variables c 1 time, so when used in paragraph c if statement, the variable c is used by the main function and the declaration has been changed c variable values.

7. Hang

Hang like variables but can not change the value. If you deliberately assign new values ​​to the constants will be wrong.

Constant name often written uppercase to recognizable and distinguishable from variables.

Syntax constant declarations: #TEN_HANG define VALUE (Note no semicolon at the end point).

You run and see the results of the following examples:

#include <stdio.h>

#define AGE_MAX 150     // hang so
#define C 'a'            // hang ky tu
#define NICK_NAME "nguyenvanquan7826" // hang chuoi

int main()
{
    printf("hang AGE_MAX = %d\n", AGE_MAX);
    printf("hang C = %c\n", C);
    printf("hang NICK_NAME = %s\n", NICK_NAME);

    // AGE_MAX = 10; // lenh nay sai vi hang khong the thay doi duoc gia tri
    return 0;
}

Exercise: You run the examples in the article and think to draw their own comments.