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:

有兩種跳過字符的方法:

  1. Skip any character using %*c in scanf

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

  2. 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:10
Output:
Time is: hours 12, minutes 12 and seconds 10

Program:

程序:

#include <stdio.h>
int main () 
{
int hh, mm, ss;
//input time
printf("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:10
Output:
Time is: hours 12, minutes 12 and seconds 10

Program:

程序:

#include <stdio.h>
int main () 
{
int hh, mm, ss;
//input time
printf("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讀取字符

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380082.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380082.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380082.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

An Algorithm Summary of Programming Collective Intelligence (3)

k-Nearest Neighbors kNN(不要問我叫什么)PCI里面用kNN做了一個價格預測模型&#xff0c;還有一個簡單的電影喜好預測。簡單來說就是要對一個東西做數值預測&#xff0c;就要先有一堆已經有數值的東西&#xff0c;從里面找出和要預測的東西相似的&#xff0c;再通過計算這些相似…

遠控免殺專題(24)-CACTUSTORCH免殺

轉載&#xff1a;https://mp.weixin.qq.com/s/g0CYvFMsrV7bHIfTnSUJBw 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/mete…

DOM快捷鍵

DOM所有的命令(CMD) 步驟/方法 cmd命令大全&#xff08;第一部分&#xff09;winver---------檢查Windows版本 wmimgmt.msc----打開windows管理體系結構(WMI) wupdmgr--------windows更新程序 wscript--------windows腳本宿主設置 write----------寫字板 winmsd---------系統…

病毒的手工排除與分析(更新完畢)

作者簡介楊京濤    8年以上的IT行業經驗&#xff0c;理解企業需求&#xff0c;有企業ERP軟件部署規劃能力&#xff0c;有綜合布線網絡規劃和管理能力。熟悉軟件以及各類硬件&#xff0c;電話程控設備&#xff0c;各類網絡設備的管理維護。有編程基礎,熟悉VBA、腳本、批處理…

JavaScript中的String substring()方法和示例

JavaScript | 字符串substring()方法 (JavaScript | String substring() Method) The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string. JavaScript中的Str…

Java——方法(練習九九乘法表)

public static void(int,byte…) xxx(int a,int b) 修飾符 返回值類型 方法名(參數類型 參數名1&#xff0c;參數類型 參數名2…)&#xff5b; 方法體語句&#xff1b; return 返回值&#xff1b; &#xff5d; public class fangfa {public static void main(String[] ar…

UVA 10405-Longest Common Subsequence

最長公共子序列&#xff0c;值得注意的是這道題不能用scanf讀字符串。 #include<stdio.h>#include<string.h>#define MAXD 1005char s1[MAXD], s2[MAXD];int dp[MAXD][MAXD];int max( int a, int b){return a > b ? a : b;}void solve(){int len1 strlen(s1 …

對初學者的幾點建議

http://www.cnblogs.com/thcjp/archive/2007/06/14/783157.html 天轟穿vs2005入門.net2.0系列視頻教程推出已經有接近8個月了&#xff0c;這期間我收到非常多的反饋&#xff0c;我只能用非常來形容&#xff0c;呵呵&#xff0c;當然也了解了很多人的心理和學習方法。但是很遺憾…

系統固件升級_固件和操作系統之間的差異

系統固件升級固件 (Firmware) Firmware is somewhere similar to software but it is not a software. Somehow it is a modified form of software. 固件與軟件相似&#xff0c;但不是軟件。 不知何故&#xff0c;它是軟件的修改形式。 Firmware is fixed data or code that …

cobalt strick 4.0 系列教程 (5)--- 獲取立足點

https://blog.ateam.qianxin.com/CobaltStrike4.0%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C_%E4%B8%AD%E6%96%87%E7%BF%BB%E8%AF%91.pdf 0x01 客戶端 System Profiler [即探針] System Profiler 是一個為客戶端攻擊提供的偵察工具。這個工具啟動一個本地的 web 服務器&#xff0…

[轉]sql,N/$/#/@的含義和作用

declare sql nvarchar(4000)set sql Nselect TotalRecordscount(*) from N( sqlFullPopulate N) a EXEC sp_executesql sql,NTotalRecords int output, TotalRecords output 問題&#xff1a;sql 后面有個N, N 起什么作用? 答案&#xff1a; 加上 N 代表存入數據庫時…

Java——方法重載(overload)(比較兩個數據是否相等)

重載&#xff1a;方法名相同&#xff0c;參數列表不同&#xff0c;與返回值類型無關 重載的分類&#xff1a; 1&#xff0c;參數個數不同 ①&#xff0c;④&#xff0c;⑤&#xff0c;⑥&#xff1b; 2&#xff0c;參數類型不同 ①&#xff0c;②&#xff0c;③、 ⑤&#x…

scala怎么做冪運算_Scala冪(冪)函數示例

scala怎么做冪運算Scala programming language has a huge set of libraries to support different functionalities. Scala編程語言具有大量的庫來支持不同的功能。 scala.math.pow() (scala.math.pow()) The pow() function is used for the exponential mathematical opera…

frame--轉載

所謂框架便是網頁畫面分成幾個框窗&#xff0c;同時取得多個 URL。只 要 <FRAMESET> <FRAME> 即可&#xff0c;而所有框架標記 要放在一個總起的 html 檔&#xff0c;這個檔案只記錄了該框架 如何劃分&#xff0c;不會顯示任何資料&#xff0c;所以不必放入 <…

cobalt strick 4.0 系列教程(6)Payload Artifact 和反病毒規避

0x01 哲學 Strategic Cyber 責任有限公司會定期回答有關規避的問題。Cobalt Strike 是否能夠繞過 AV 產品&#xff1f;它能繞過哪些 AV 產品&#xff1f;它多久檢查一次&#xff1f; Cobalt Strike 默認的 Artifact 可能會被大多數終端安全解決方案攔截。規避不是 Cobalt Str…

【轉】企業開發的困境與變局

原文&#xff1a;企業開發的困境與變局 文 / 劉江 算起來&#xff0c;《程序員》已經有幾年時間沒有大篇幅討論企業軟件開發這個話題了。這其實挺奇怪的。要知道&#xff0c;按類別來分&#xff0c;國內從事企業軟件開發的技術人員是最多的&#xff0c;從CSDN和《程序員》聯合舉…

c# 類對象和實例對象_C#類和對象能力問題 套裝4

c# 類對象和實例對象1) What are the correct statements about given code snippets? using System;public class Example{virtual private int X;private int Y;static void Main(string[] args){Console.WriteLine("Hello World");}}Hello WorldHelloWorldSyntax…

linkBar組件學習--設置linkBar子項的豎直間距.

效果&#xff1a;代碼&#xff1a; <?xml version"1.0" encoding"utf-8"?><!--http://blog.flexexamples.com/2008/04/20/setting-the-vertical-spacing-between-items-in-a-linkbar-control-in-flex/ --><mx:Application xmlns:mx"…

AES算法

算法簡介 AES本質是一種對稱分組密碼體制&#xff0c;采用代替/置換網絡。每輪由三層組成&#xff1a;線性混合層確保多輪之上的高度擴散&#xff0c;非線性層由16個S盒并置起到混淆的作用&#xff0c;秘鑰加密層將子秘鑰異或到中間狀態。 AES加密數據塊和秘鑰長度可以是128比…

C——結構體

例題1&#xff1a;(使用結構體) 輸入兩個學生的學號、姓名和成績&#xff0c;輸出成績較高的學生的學號、姓名和成績 解題思路&#xff1a; (1)定義連個結構相同的結構體變量student1,student2; (2)分別輸入兩個學生的學號、姓名、和成績&#xff1b; (3)比較兩個學生的成…