• scanf()


    Description

    The C library function int scanf(const char *format, ...) reads formatted input from stdin.

    Declaration

    Following is the declaration for scanf() function.

    int scanf(const char *format, ...)

    Parameters

    • format − This is the C string that contains one or more of the following items −

      Whitespace character, Non-whitespace character and Format specifiers. A format specifier will be like [=%[*][width][modifiers]type=] as explained below −

    Sr.No.Argument & Description
    1

    *

    This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.

    2

    width

    This specifies the maximum number of characters to be read in the current reading operation.

    3

    modifiers

    Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g)

    4

    type

    A character specifying the type of data to be read and how it is expected to be read. See next table.

    fscanf type specifiers

    typeQualifying InputType of argument
    c Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char *
    d Decimal integer: Number optionally preceded with a + or - sign int *
    e, E, f, g, G Floating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 float *
    o Octal Integer: int *
    s String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). char *
    u Unsigned decimal integer. unsigned int *
    x, X Hexadecimal Integer int *
    • additional arguments − Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

    Return Value

    If successful, the total number of characters written is returned, otherwise a negative number is returned.

    Example

    The following example shows the usage of scanf() function.

    #include <stdio.h>
    
    int main () {
       char str1[20], str2[30];
    
       printf("Enter name: ");
       scanf("%s", str1);
    
       printf("Enter your website name: ");
       scanf("%s", str2);
    
       printf("Entered Name: %s
    ", str1);
       printf("Entered Website:%s", str2);
       
       return(0);
    }

    Let us compile and run the above program that will produce the following result in interactive mode −

    Enter name: admin
    Enter your website name: www.tutorialspoint.com
    
    Entered Name: admin
    Entered Website: www.tutorialspoint.com
  • 相关阅读:
    9.内存的了解
    8.时钟初始化
    3.2Linux的模块驱动
    3.1Linux内核的配置和编译
    5.10TCP客户端服务器
    5.9UDP客户端服务器-基于OK6410
    5.8fork父子进程
    4.NFC前台调度系统
    3.非标准的NDEF格式数据解析--IsoDep
    Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline
  • 原文地址:https://www.cnblogs.com/Davirain/p/10255449.html
Copyright © 2020-2023  润新知