日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scanf读取字符_在C语言中使用scanf()读取整数时跳过字符

發布時間:2023/12/1 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scanf读取字符_在C语言中使用scanf()读取整数时跳过字符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

scanf讀取字符

Let suppose, we want to read time in HH:MM:SS format and store in the variables hours, minutes and seconds, in that case we need to skip columns (:) from the input values.

假設,我們要讀取HH:MM:SS格式的時間 ,并將其存儲在小時 , 分鐘和秒的變量中,在這種情況下,我們需要從輸入值中跳過列(:)。

There are two ways to skip characters:

有兩種跳過字符的方法:

  • Skip any character using %*c in scanf

    在scanf中使用%* c跳過任何字符

  • And, by specifying the characters to be skipped

    并且,通過指定要跳過的字符

  • 1)在scanf中使用%* c跳過任何字符 (1) Skip any character using %*c in scanf)

    %*c skips a character from the input. For example, We used %d%*c%d and the Input is 10:20: will be skipped, it will also skip any character.

    %* c跳過輸入中的字符。 例如,我們使用%d%* c%d ,并且Input is 10:20 – :將被跳過,也將跳過任何字符。

    Example:

    例:

    Input Enter time in HH:MM:SS format 12:12:10Output:Time is: hours 12, minutes 12 and seconds 10

    Program:

    程序:

    #include <stdio.h>int main () {int hh, mm, ss;//input timeprintf("Enter time in HH:MM:SS format: ");scanf("%d%*c%d%*c%d", &hh, &mm, &ss) ;//print printf("Time is: hours %d, minutes %d and seconds %d\n" ,hh, mm, ss) ;return 0; }

    Output

    輸出量

    Enter time in HH:MM:SS format: 12:12:10 Time is: hours 12, minutes 12 and seconds 10

    2)通過指定要跳過的字符 (2) By specifying the characters to be skipped)

    We can specify the character that are going to be used in the input, for example input is 12:12:10 then the character : can be specified within the scanf() like, %d:%d:%d.

    我們可以指定將在輸入中使用的字符,例如,輸入為12:12:10,則可以在scanf()中指定字符:,如%d:%d:%d 。

    Example:

    例:

    Input Enter time in HH:MM:SS format 12:12:10Output:Time is: hours 12, minutes 12 and seconds 10

    Program:

    程序:

    #include <stdio.h>int main () {int hh, mm, ss;//input timeprintf("Enter time in HH:MM:SS format: ");scanf("%d:%d:%d", &hh, &mm, &ss) ;//print printf("Time is: hours %d, minutes %d and seconds %d\n" ,hh, mm, ss) ;return 0; }

    Output

    輸出量

    Enter time in HH:MM:SS format: 12:12:10 Time is: hours 12, minutes 12 and seconds 10

    翻譯自: https://www.includehelp.com/c/skip-characters-while-reading-integers-using-scanf.aspx

    scanf讀取字符

    總結

    以上是生活随笔為你收集整理的scanf读取字符_在C语言中使用scanf()读取整数时跳过字符的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。