Các Lệnh Trong C++



2. Export data using printf3. Enter data using scanf4. Enter the string in C5. Explain a little bit about printf, scanf and stdio.h

In the previous post, we became acquainted with a number of programs that print to the screen some information. This article we will learn more about how lớn enter production in C.

Bạn đang xem: Các lệnh trong c++

1. Format string data

Prior khổng lồ the introduction, data for the variables I will talk about some format to lớn import và 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 using printf

We use the function printf lớn export the data khổng lồ the console (from the print means print). Combined with the above sầu format string, we consider a simple example follows.

Example 1:

#include int main() int a = 12; float b = 13.5; char c = 'Q'; long d = 3454; char* s = "nguyenvanquan7826"; // knhị bao kieu chuoi printf("Vi du ve sầu su dung lenh printf "); printf("Tong cua %d va %f la %f ", a, b, a+b); printf("Tich cua %d va %ld la %ld ", a, d, a*d); printf("Ky tu c la: %c ", c); printf("Chuoi s la: %s ", s); printf("Dinch dang so mu cua b la %e ", b); printf("So he 16 va he 8 cua %d la %x va %o ", a, a, a); printf("Ma ASCII cua %c la %d", c, c); return 0;Above sầu I have used some control characters ( – down the line) mentioned in the previous post, you can nhận xét if not Me. You run the program and draw your own comment.

His explanation 1 statement khổng lồ clarify the publication of our.


Printf statement to lớn print the corresponding variable

The format string is placed in quotes: “”. Corresponding lớn each format is a variable of type respectively, if another type will result in errors.

You also note that for integers and characters have sầu 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 bởi vì not change. In Vd on commvà in ASCII code of c will integer c nature but still is a variable of type char.

Example 2:

#include int main() int a = 12; float b = 13.5; char c = 'Q'; long d = 3454; char s<> = "nguyenvanquan7826"; // knhì bao kieu chuoi printf("%6d %5.3f %.3f ", a, b, a+b); printf("%-5d %5ld %5ld ", a, d, a*d); printf("%5c ", c); printf("%30s ", s); return 0;You see this example we use the format% d, %f but insert in the middle of a% 6d, %5.3f. The meaning of it is as follows:

%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

You run the program, view to lớn see more results. TH if the breadth of our smaller kích thước, the length of the number of stars? For example, we have sầu some int a = 1234 but only in the% 2d, clear for 2 the space is less than a can 4 digits but the result is still in full. You can try.

Example 3:

#include int main() int ngay = 12; int thang = 8; int nam giới = 1992; printf("KQ: %02d/%02d/%04d ", tức thì, thang, nam); return 0;Above, You notice we used %02d ie stars? Nhiá is the number printed will be 2 number, if the original number with less than 2 will add 0 front to lớn enough 2 number. Example No. 8 will succeed 08. If you use% 05d will be 00008.

3. Enter data using scanf

In the previous example, we just declare variables and assign values ​​lớn variables such as but not flexible because the program only works with the predetermined value, want khổng lồ work with different data we have khổng lồ revise the code, so it"s not a good program. A software program is made lớn be for many different sets of values ​​can be used.

Therefore, the data will be included in the software used is specific and simple data entry inlớn the keyboard. We use the comm& scanf to import data from the keyboard (from scanning means scanned và the data we use to scan lệu from keyboard).

Example 1:

#include 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 b = %.3f", a, b); return 0;The use of commvà scanf combined with the string format for data entry will be similar khổng lồ the data by the command printf.

Xem thêm: 【Download】 Tải Lightroom Cc 2017 Free Download, Adobe Lightroom Classic Cc 2018 Full Version Pc

Note Do not forget lớn character và before each variable entering. Without going wrong.

Example 2:

/* Tinch dien tich hinch chu nhat */#include int main() int a, b; printf("Nhap chieu dai, chieu rong: "); scanf("%d%d", &a, &b); printf("Dien tich HCN: %d ", a * b); return 0;In the example above sầu, You see we can use 1 scanf commvà khổng lồ enter data for several variables, each corresponding variable 1 certain format.

4. Enter the string in C

4.1 Error importing string scanf

If you use the scanf function lớn enter the string you see that the chain can not enter spaces before or if you vày not enter then enter the string again. If you do not believe you can try khổng lồ run the program after:

#include int main() int tuoi = 0; // khai bao chuoi co toi domain authority 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 và printf("Ban bao nhieu tuoi roi? "); scanf("%d", &tuoi); printf("Nguoi yeu cua ban ten la gi? "); scanf("%s", tenNguoiYeu); printf(" ==== "); printf("Ten: %s Tuoi:%d NY:%s ", ten, tuoi, tenNguoiYeu); return 0;The result is that you will not enter the age & name of love, as shown below.


Results when used to enter the string scanf

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 lớn them và you will not be entered again. Of your type of material should not receive sầu, tenNguoiYeu will receive sầu the next value in the value received is the word From.

4.2 The phenomenon of floating command

Phenomemãng cầu such phenomenon is called drift command. If you now make to lớn enter the string before and immediately after that this phenomenon also occurs because scanf just read the number in the correct format that is unreadable when you press the enter key when entered the number (enter characters or also can be considered a chain), it is stored in the buffer & 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 lớn clear the keyboard buffer with the commvà fflush(stdin); immediately after entering the.

#include int main() int tuoi = 0; // knhị bao chuoi co toi domain authority 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(" ==== "); printf("Ten: %s Tuoi:%d NY:%s ", ten, tuoi, tenNguoiYeu); return 0;If you use Linux, the fflush(stdin); will not work, you can fix by entering 1 temporary sequence immediately after entering the. This temporary chain entry only khổng lồ remove sầu characters, excess string in buffer as follows (this way can also be used when you bởi vì on windows).

#include int main() int tuoi = 0; // knhị bao chuoi co toi domain authority 30 ky tu char ten<30>, tenNguoiYeu<30>, temp<255>; printf("Ho va ten cua ban ten la gi? "); gets(ten); // nhap chuoi khong can dau và printf("Ban bao nhieu tuoi roi? "); scanf("%d", &tuoi); //fflush(stdin); gets(temp); printf("Nguoi yeu cua ban ten la gi? "); gets(tenNguoiYeu); printf(" ==== "); printf("Ten: %s Tuoi:%d NY:%s ", ten, tuoi, tenNguoiYeu); return 0;

5. Explain a little bit about printf, scanf and stdio.h

5.1 Printf & scanf in the letter f

As you all know print mean in, scan was sweep or is called in to enter. So why vày they have letters f bachồng to form printf & scanf ?

Word f This means that format (Format). As you can see we are importing or exporting the values ​​of the variables are formatted % Something eg% d is integer, %f are real numbers,… & letters f This is such meaningful.

5.2 Library stdio.h ?

In all the program ever, we always have #include , So what is it?

#include ie meant khổng lồ include our program will declare use something, but here is the use of the library stdio.h

So stdio.h what? std stands for standard , in stands for input, the stands for output, h stands for header (header – head, top – because it is declared in the beginning of the program). So it means standard input đầu vào output – Import standard output. We underst& it is a library service for the import & export of program standards. Enter the standard output is entered from the keyboard & output lớn the screen. There are also many sources as import from the tệp tin import và export, Import from cursor,… output to printer, output file,… but they considered keyboard & screen is a standard system of import và export.


Write a program in some any và print a value a2, the3, the4Write a program that reads from the keyboard 3 perform integer day, month, & output to the screen in the size of "dd / milimet / yyyy".Read & write program 2 integer và print out the result of the (+), Subtraction (-), multiplication (*), division (/). đánh giá results divided 2 integer.Write a program lớn the radius of the sphere, calculate & print area, volume of the sphere which. Guide: S = 2 & V = ^ 4πR (4/3)πR^3.Enter a number is the number of seconds, Change the number of seconds the minute and hour format manufactured under gio:phut:Shine, each component 2 number. Example 3661 = 01:01:01.