Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Hỏi về Modifier trong lập trình C

    Em mới tập tành học C nhưng kẹt cái vào chỗ bài về Input/Output có có cái phần về Modifier. E đọc mãi, làm ví dụ biết được sự khác biệt các Modifiers nhưng mà vẫn không hiểu nó là cái gì và để làm gì...Các tiền bối có kiến thức uyên thâm xin chỉ giáo em cái ah !

    Đây là phần lý thuyết.

    Modifiers for Format Commands in printf()
    The format commands may have modifiers, to suitably modify the basic conversion specifications.
    The following are valid modifiers acceptable in the printf()statement. If more than one modifier
    is used, then they must be in the same order as given below.
    ‘-’ Modifier
    The data item will be left-justified within its field; the item will be printed beginning from the leftmost
    position of its field.
    Field Width Modifier
    They can be used with type float, doubleor chararray (string). The field width modifier, which
    is an integer, defines the minimum field width for the data item. Data items for smaller width will be
    output right-justified within the field. Larger data items will be printed by using as many extra positions as required. e.g. %10fis the format command for a type float data item with minimum field
    width 10.
    Precision Modifier
    This modifier can be used with type float, doubleor chararray (string). The modifier is written as .mwhere mis an integer. If used with data type floator double, the digit string indicates
    the maximum number of digits to be printed to the right of the decimal. When used with a string, it
    indicates the maximum number of characters to be printed.

    If the fractional part of a type floator double data item exceeds the precision modifier, then the
    number will be rounded. If a string length exceeds the specified length, then the string will be truncated (cut of at the end). Padding with zeroes occurs for numbers when the actual number of digits
    in a data item is less than that specified by the modifier. Similarly blanks are padded for strings. e.g.
    %10.3f is the format command for a type float data item, with minimum field width 10 and 3 places
    after the decimal.
    ‘0’ Modifier
    The default padding in a field is done with spaces. If the user wishes to pad a field with zeroes, this
    modifier must be used.
    ‘1’ Modifier
    This modifier can be used to display integers as long int or a double precision argument. The corresponding format code is %ld.
    ‘h’ Modifier
    This modifier is used to display short integers. The corresponding format code is %hd.
    ‘*’ Modifier
    This modifier is used if the user does not want to specify the field width in advance, but wants the
    program to specify it. But along with this modifier an argument is required which tells what the field
    width should be.
    Let us now see how these modifiers work. First we see their effect when used with integer data
    items.
    Còn đây là phần Code ví dụ

    /* This program demonstrates the use of Modifiers in printf() */
    #include <stdio.h>
    void main()
    {
    printf(“The number 555 in various forms:
    ”);
    printf(“Without any modifier:
    ”);
    printf(“[%d]
    ”,555);
    printf(“With – modifier :
    ”);
    printf(“[%-d]
    ”,555);/* This program demonstrates the use of Modifiers in printf() */
    #include <stdio.h>
    void main()
    {
    printf(“The number 555 in various forms:
    ”);
    printf(“Without any modifier:
    ”);
    printf(“[%d]
    ”,555);
    printf(“With – modifier :
    ”);
    printf(“[%-d]
    ”,555);
    printf(“With digit string 10 as modifier :
    ”);
    printf(“[%10d]
    ”,555);
    printf(“With 0 as modifier :
    ”);
    printf(“[%0d]
    ”,555);
    printf(“With 0 and digit string 10 as modifiers :
    ”);
    printf(“[%010d]
    ”,555);
    printf(“With -, 0 and digit string 10 as modifiers:
    ”);
    printf(“[%-010d]
    ”,555);
    }
    Đây là phần giải thích của ví dụ trên


    The number 555 in various forms:
    Without any modifier:
    [555]
    With - modifier :
    [555]
    With digit string 10 as modifier :
    [555]
    With 0 as modifier :
    [555]
    With 0 and digit string 10 as modifiers :
    [0000000555]
    With -, 0 and digit string 10 as modifiers:
    [555]

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn post nhầm box rồi.

    Mình đọc sơ qua thì thấy đoạn english của bạn đang nói về modifier cho việc định dạng lại chuỗi được in ra ở hàm printf().
    Mình thấy nó rất rõ ràng và không có gì phức tạp sao bạn lại không hiểu nhỉ?

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •