Programming C: Posts 2 – Import and export data types in C

Caution: The article has been updated and revised to more fully Posts 2: The components in C and Posts 3: Import and export of C

Today all look relatively long, however, when the practice will see very simple. This is the index Posts

The. The data type in C

1. The control characters

  • \n : Jump down to the next line in the first column soup.
  • \t : Soup column 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 the document.
Easy to understand controls the characters you try running the following and evolved their own comments.

#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 %%");

	// day la mot dong ghi chu

	/*
	  Day la mot doan ghi chu
	  Doan ghi chu nay co 2 dong
	 */

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

2. Tag

Is the word that C has built, we should not define them.

Bảng các từ khóa trong C

3. Style and variable

the. Type of data

The data type is the same as the container, items to store our belongings. Eg for storing drinking water ca, baskets for storing vegetables,…

Each data type has different sizes and corresponding domain values ​​and value types that it can be done. VD type int occupy 2 byte memory and to contain the integer,…

The data type in C
The data type in C

b. Transformers - constant
Corresponding to each type of data we variables, the constants of this type and the corresponding domain values ​​as above is used to store values. You need to differentiate types and variables.
A VD basket to put spinach, B for containing vegetable basket corresponds to a stored value of the variable 5, also store the value of the variable b 9 although they are the same type

Variables can be changed in the process of implementing the program can not be constant.

How to declare variables: kieu_du_lieu VARIABLE;
– Valid variable name is a string constant consists of: Alphabetic character, numbers and underscores. First character of the name must be letters or underscores. When a name is not set to coincide with the tukhoa.
Example 1 :
The correct name: delta, a_1, Num_ODD, Case
The wrong name: 3a_1 (the first character is a number); a-odd (use a hyphen); int (name coincides with tukhoa) ; the ta (spaces); f(x) (round brackets)
Noted: In C, name is case sensitive, lowercase
For du2 : number khác Number ; case khác Case (case is the keyword, so you have correctly named Case)
Syntax: type danh_sach_cac_bien;
CEO:

#include <stdio.h>

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

	int e = 4, f = 6;

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

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	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. !

Location declare 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.
Declare variables outside (Global variables): Location variables set outside all functions, structure… These variables can affect the entire program. Its life cycle is started to run the program until the program ends.
Declaring variables in (local variables): Location set variables inside the function, structure .... Only internal influence within the function, structure that .... Its life cycle begins at the function, structure is called to perform at're done.
Sometimes vague theory, the following example you will see better and run more.

#include <stdio.h>

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

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; 

	{
		int e = 6, d = 8;
		c = 7;
		printf("gia tri cac bien trong khoi:\n");
		printf("e = %d \t d = %d \t c = %d\n", e, d, c);
	}

	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);

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

You run the program, see results and comments themselves drawn nhá, unclear if can ask directly on the blog.
How to declare constants: Declaring constants you can report from anywhere in the program, Where the declaration of constants which will be determined.
Syntax: #define ten_hang gia_tri (There is no semicolon at the end nhá)
Note we do not use const structure as some users for using const in some cases we have to change the value of the constant.

#include <stdio.h>

#define a 6 	// hang so
#define c 'a'  // hang ky tu
#define s "nguyenvanquan7826" // hang chuoi

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

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

You run and feel nhá

B. Enter, Export in C

1. Format string data

Prior to the introduction, data for the variables I will talk about some format to import and export. Here are the signs described format:

  • %c : Single character
  • %with : Chain
  • %d : Integer Us 10 marked
  • %f : Number of floating (CEO 5.54 will print out 5.540000)
  • %and : Number of floating (with exponential notation)
  • %g : Number of floating (CEO 5.54 printing will print out 5.54)
  • %x : Hex unsigned integer (Contacts 16)
  • %the : Unsigned octal integer (Contacts 8)
  • the : The prefix used with% d, %x, %o to indicate long integer (eg% ld)

2. Export data: printf();

We use the function printf to export the data to the console.

#include <stdio.h>

int main(){
	int a = 12;
	float b = 13.5;
	char c = 'Q';
	long d = 3454;
	char* s = "nguyenvanquan7826"; // khai bao kieu chuoi

	printf("tong cua %d va %f la %f \n", a, b, a+b);
	printf("tich cua %d va %ld la %ld \n", a, d, a*d);
	printf("ky tu c la: %c \n", c);
	printf("chuoi s la: %s \n", s);
	printf("dinh dang so mu cua b la %e \n", b);
	printf("so he 16 va he 8 cua %d la %x va %o \n", a, a, a);
	printf("ma ASCII cua %c la %d", c, c);

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

I would like to explain 1 statement to clarify the publication of our.
xuatdulieu

You also note that for integers and characters have with each other via the ASCII code so we can print the character code in the format% d and also in character numeric code through the format% c. However, the nature of the variables do not change. In Vd on command in ASCII code of c will integer c nature but still is a variable of type char.
You could run and feel !

Next nhá, a few export format:

  • %5c : Export character width 5
  • %5d : Width integer 5
  • %20with : Export chain width 20
  • %5.3f : Export the actual width 5 including 3 number after comma
  • %-5d : Width integer 5 but aligns the left

Running and feel eg !

#include <stdio.h>

int main(){
    int a = 12;
    float b = 13.5;
    char c = 'Q';
    long d = 3454;
    char* s = "nguyenvanquan7826"; // khai bao kieu chuoi
    
    printf("%6d %5.3f %.3f \n", a, b, a+b);
    printf("%-5d %5ld %5ld \n", a, d, a*d);
    printf("%5c \n", c);
    printf("%30s \n", s);
    
    // system("pause"); // su dung de dung man hinh neu ban dung dev-C
    return 0;
}

3. Enter data: scanf();

I use the function scanf to input from the keyboard

#include <stdio.h>

int main(){
	int a;
	float b;

	printf("Nhap so nguyen a = ");
	scanf("%d", &a);

	printf("Nhap so thuc b = ");
	scanf("%f", &b);

	printf("a = %d \t b = %.3f", a, b);

	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

From this example we can see the syntax to import: scanf (“format string”[, to 1, of 2, ...]);
Note Do not forget to character & before each variable. Without going wrong.
The format string is placed in quotes: "" And the format is similar to our data.
Corresponding to each format is a corresponding type, if other types of errors will gradually come.

4. Enter the string in C

If you use the scanf function to enter the string you see that the chain can not enter spaces before or if you do not enter then enter the string again. If you do not believe you can try to run the program after:

#include <stdio.h>

int main(){
	int tuoi = 0;
	// khai bao chuoi co toi da 30 ky tu
	char ten[30], tenNguoiYeu[30];

	printf("Ho va ten cua ban ten la gi?");
	scanf("%s", ten); // nhap chuoi khong can dau &

	printf("Ban bao nhieu tuoi roi?");
	scanf("%d", &tuoi);

	printf("Nguoi yeu cua ban ten la gi?");
	scanf("%s", tenNguoiYeu);

	printf("\n====\n");
	printf("%s \n%d \n%s", ten, tuoi, tenNguoiYeu);
	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

The result is that you will not enter the age and name of love, as shown below.
chain integration in c

The reason is that only read data scanf no spaces (how to play, tabs, enter, …) and the distance will be stored in the keyboard buffer so you only get the first string before the match only way (word Nguyen), after each space, the next value if consistent with the data type of the next turn, it will always assigned to them and you will not be entered again. Of your type of material should not receive, tenNguoiYeu will receive the next value in the value received is the word From.

5. The phenomenon of floating command

Phenomena such phenomenon is called drift command. If you now make to enter the string before and immediately after that this phenomenon also occurs because scanf not read when you press the enter key when entered the number, it is stored in the buffer and when reading input values ​​to string it finds in the buffer see characters enter the string so it is always assigned to the string.

To enter the string with spaces (spaces) we use the function gets.

To enter commands when not washed before and after the string we need to clear the keyboard buffer with the command fflush(stdin); immediately after entering the.

#include <stdio.h>

int main(){
	int tuoi = 0;
	// khai bao chuoi co toi da 30 ky tu
	char ten[30], tenNguoiYeu[30];

	printf("Ho va ten cua ban ten la gi?");
	gets(ten); // nhap chuoi khong can dau &

	printf("Ban bao nhieu tuoi roi?");
	scanf("%d", &tuoi);
	fflush(stdin);

	printf("Nguoi yeu cua ban ten la gi?");
	gets(tenNguoiYeu);

	printf("\n====\n");
	printf("%s \n%d \n%s", ten, tuoi, tenNguoiYeu);
	// system("pause"); // su dung de dung man hinh neu ban dung dev-C
	return 0;
}

If you use Linux, the fflush(stdin); will not work, please read fflush(stdin) in ubuntu (linux) or gets() and fget() in C/C to know how to fix it.

ASCII
ascii


Caution: The article has been updated and revised to more fully Posts 2: The components in C and Posts 3: Import and export of C