博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scanf读取字符_在C语言中使用scanf()读取整数时跳过字符
阅读量:2532 次
发布时间:2019-05-11

本文共 1944 字,大约阅读时间需要 6 分钟。

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

翻译自:

scanf读取字符

转载地址:http://pfazd.baihongyu.com/

你可能感兴趣的文章
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>