Programming C: Posts 3 – Enter made in c

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 to enter production 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 using printf

We use the function printf to export the data to the console (from the print means print). Combined with the above format string, we consider a simple example follows.

Example 1:

#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("Vi du ve su dung lenh printf\n");
    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);

    return 0;
}

Above I have used some control characters (\n – down the line) mentioned in the previous post, you can review if not Me. You run the program and draw your own comment.

His explanation 1 statement to clarify the publication of our.

Export data

Printf statement to print the corresponding variable

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

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.

Example 2:

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

    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 see more results. TH if the breadth of our smaller size, the length of the number of stars? For example, we have 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:

Put the problem is assuming we have the variables shown any birth date (eg right = 12, thang = 8, male = 1992). Please print out the format is dd / mm / yyyy. Means that we have to print out the results 12/08/1992.

#include <stdio.h>

int main()
{
    int ngay = 12;
    int thang = 8;
    int nam = 1992;

    printf("KQ: %02d/%02d/%04d\n", ngay, 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 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 ​​to variables such as but not flexible because the program only works with the predetermined value, want to work with different data we have to revise the code, so it's not a good program. A software program is made to 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 into the keyboard. We use the command scanf to import data from the keyboard (from scanning means scanned and the data we use to scan lệu from keyboard).

Example 1:

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

    return 0;
}

The use of command scanf combined with the string format for data entry will be similar to the data by the command printf.

Note Do not forget to character & before each variable entering. Without going wrong.

Example 2:

/* Tinh dien tich hinh chu nhat */
#include <stdio.h>

int main()
{
    int a, b;
    printf("Nhap chieu dai, chieu rong: \n");
    scanf("%d%d", &a, &b);

    printf("Dien tich HCN: %d\n", a * b);

    return 0;
}

In the example above, You see we can use 1 scanf command to 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 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("Ten: %s \nTuoi:%d \nNY:%s \n", ten, tuoi, tenNguoiYeu);

    return 0;
}

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

Troi command in C

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 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.

4.2 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 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 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("Ten: %s \nTuoi:%d \nNY:%s \n", 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 to remove characters, excess string in buffer as follows (this way can also be used when you do on windows).

#include <stdio.h>

int main()
{
    int tuoi = 0;
    // khai bao chuoi co toi da 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 &

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

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

    printf("\n====\n");
    printf("Ten: %s \nTuoi:%d \nNY:%s \n", ten, tuoi, tenNguoiYeu);

    return 0;
}

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

5.1 Printf and scanf in the letter f

As you all know print mean in, scan was sweep or is called in to enter. So why do they have letters f back to form printf and 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,… and letters f This is such meaningful.

5.2 Library stdio.h ?

In all the program ever, we always have #include <stdio.h>, So what is it?

#include ie meant to 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 output – Import standard output. We understand it is a library service for the import and export of program standards. Enter the standard output is entered from the keyboard and output to the screen. There are also many sources as import from the file import and export, Import from cursor,… output to printer, output file,… but they considered keyboard and screen is a standard system of import and export.

Exercise

  1. Write a program in some any and print a value a2, the3, the4
  2. Write a program that reads from the keyboard 3 perform integer day, month, and output to the screen in the form of "dd / mm / yyyy".
  3. Read and write program 2 integer and print out the result of the (+), Subtraction (-), multiplication (*), division (/). Reviews results divided 2 integer.
  4. Write a program to the radius of the sphere, calculate and print area, volume of the sphere which.
    Guide: S = 2 and V = ^ 4πR (4/3)πR^3.
  5. 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.