C 语言中的 printf 和 scanf 函数详解

在 C 语言中,printfscanf函数是非常重要的输入输出函数。它们可以让我们与程序进行交互,并以各种格式显示和读取数据。

一、printf 函数

printf函数用于将数据输出到标准输出设备(通常是控制台)。它的基本语法如下:

printf("格式化字符串", 表达式列表);

其中,“格式化字符串” 是一个字符串,其中包含了普通字符和格式说明符。格式说明符用于指定输出数据的格式。表达式列表是要输出的数据,可以是变量、常量或表达式。

1. 常见的格式说明符

  • %d:用于输出十进制整数。例如:

展开过程

输出结果为:The number is 10

  • %f:用于输出浮点数。默认情况下,%f会输出小数点后 6 位数字。例如:

展开过程

输出结果为:The float number is 3.140000

可以使用.n的形式来指定输出的小数位数,其中n是要输出的小数位数。例如:

展开过程

输出结果为:The float number is 3.14

  • %c:用于输出单个字符。例如:

展开过程

输出结果为:The character is A

  • %s:用于输出字符串。例如:

展开过程

输出结果为:The string is Hello, world!

2. 其他格式说明符

  • %o:用于输出八进制整数。
  • %x%X:用于输出十六进制整数。小写x输出小写字母,大写X输出大写字母。
  • %u:用于输出无符号整数。
  • %p:用于输出指针的值。

3. 转义序列

在格式化字符串中,还可以使用转义序列来输出特殊字符。例如:

  • \n:换行符。
  • \t:制表符。
  • \\:输出反斜杠。
  • \":输出双引号。

例如:

展开过程

二、scanf 函数

scanf函数用于从标准输入设备(通常是键盘)读取数据,并将其存储到变量中。它的基本语法如下:

展开过程

其中,“格式化字符串” 是一个字符串,其中包含了普通字符和格式说明符。格式说明符用于指定输入数据的格式。变量地址列表是要存储输入数据的变量的地址,可以使用&运算符来获取变量的地址。

1. 常见的格式说明符

printf函数类似,scanf函数也支持各种格式说明符。例如:

  • %d:用于读取十进制整数。例如:

展开过程

用户输入一个整数后,scanf函数会将其存储到num变量中。

  • %f:用于读取浮点数。例如:

展开过程

  • %c:用于读取单个字符。例如:

展开过程

需要注意的是,当使用%c读取字符时,输入缓冲区中的任何字符都会被读取,包括空格、制表符和换行符。

  • %s:用于读取字符串。例如:

展开过程

scanf函数在读取字符串时,会自动忽略开头的空白字符,并在遇到空白字符时停止读取。如果要读取包含空格的字符串,可以使用gets函数或fgets函数。

2. 输入格式控制

在使用scanf函数时,可以使用一些格式控制字符来控制输入的格式。例如:

  • *:用于跳过输入中的某些数据。例如:

展开过程

如果用户输入 “1 2 3”,则num1的值为 1,num2的值为 3,中间的数字 2 被跳过。

  • width:用于指定读取的最大宽度。例如:

展开过程

如果用户输入 “Hello, world!”,则str数组中只会存储 “Hello”,后面的字符被忽略。

3. 错误处理

在使用scanf函数时,需要注意错误处理。如果输入的数据与格式说明符不匹配,scanf函数可能会返回错误值,并将输入缓冲区中的数据留在缓冲区中,导致后续的输入操作出现问题。

为了避免这种情况,可以在调用scanf函数后检查其返回值。如果scanf函数返回的值不等于期望读取的变量个数,则表示输入出现了错误。例如:

展开过程

三、总结

printfscanf函数是 C 语言中非常重要的输入输出函数。通过使用不同的格式说明符和转义序列,可以以各种格式输出和读取数据。在使用这些函数时,需要注意格式说明符的正确使用和错误处理,以确保程序的正确性和稳定性。

/** * Interface to be implemented by @{@link org.springframework.context.annotation.Configuration * Configuration} classes annotated with @{@link EnableTransactionManagement} that wish to * (or need to) explicitly specify the default {@code PlatformTransactionManager} bean * (or {@code ReactiveTransactionManager} bean) to be used for annotation-driven * transaction management, as opposed to the default approach of a by-type lookup. * One reason this might be necessary is if there are two {@code PlatformTransactionManager} * beans (or two {@code ReactiveTransactionManager} beans) present in the container. * * <p>See @{@link EnableTransactionManagement} for general examples and context; * see {@link #annotationDrivenTransactionManager()} for detailed instructions. * * <p>Note that in by-type lookup disambiguation cases, an alternative approach to * implementing this interface is to simply mark one of the offending * {@code PlatformTransactionManager} {@code @Bean} methods (or * {@code ReactiveTransactionManager} {@code @Bean} methods) as * {@link org.springframework.context.annotation.Primary @Primary}. * This is even generally preferred since it doesn't lead to early initialization * of the {@code TransactionManager} bean. * * @author Chris Beams * @since 3.1 * @see EnableTransactionManagement * @see org.springframework.context.annotation.Primary * @see org.springframework.transaction.PlatformTransactionManager * @see org.springframework.transaction.ReactiveTransactionManager */ public interface TransactionManagementConfigurer { /** * Return the default transaction manager bean to use for annotation-driven database * transaction management, i.e. when processing {@code @Transactional} methods. * <p>There are two basic approaches to implementing this method: * <h3>1. Implement the method and annotate it with {@code @Bean}</h3> * In this case, the implementing {@code @Configuration} class implements this method, * marks it with {@code @Bean}, and configures and returns the transaction manager * directly within the method body: * <pre class="code"> * @Bean * @Override * public PlatformTransactionManager annotationDrivenTransactionManager() { * return new DataSourceTransactionManager(dataSource()); * }</pre> * <h3>2. Implement the method without {@code @Bean} and delegate to another existing * {@code @Bean} method</h3> * <pre class="code"> * @Bean * public PlatformTransactionManager txManager() { * return new DataSourceTransactionManager(dataSource()); * } * * @Override * public PlatformTransactionManager annotationDrivenTransactionManager() { * return txManager(); // reference the existing {@code @Bean} method above * }</pre> * If taking approach #2, be sure that only one of the methods is marked * with {@code @Bean}! * <p>In either scenario #1 or #2, it is important that the * {@code PlatformTransactionManager} instance is managed as a Spring bean within the * container since most {@code PlatformTransactionManager} implementations take advantage * of Spring lifecycle callbacks such as {@code InitializingBean} and * {@code BeanFactoryAware}. Note that the same guidelines apply to * {@code ReactiveTransactionManager} beans. * @return a {@link org.springframework.transaction.PlatformTransactionManager} or * {@link org.springframework.transaction.ReactiveTransactionManager} implementation */ TransactionManager annotationDrivenTransactionManager(); 完整的翻译
09-17
`TransactionManagementConfigurer` 接口是 Spring 框架中用于配置事务管理的接口。以下是其 JavaDoc 的完整中文翻译: ```java /** * 实现此接口允许参配置 Spring 的基于注解的事务管理基础架构, * 特别是指定默认的 {@link org.springframework.transaction.PlatformTransactionManager} * 用于 {@link org.springframework.transaction.annotation.Transactional} 注解。 * * <p>当使用 Java 配置时,用户可以实现此接口并在配置类中提供必要的方法实现。 * 例如: * <pre class="code"> * @Configuration * @EnableTransactionManagement * public class AppConfig implements TransactionManagementConfigurer { * * @Bean * public PlatformTransactionManager txManager() { * return new DataSourceTransactionManager(dataSource()); * } * * @Override * public PlatformTransactionManager annotationDrivenTransactionManager() { * return txManager(); * } * * // 其他 bean 定义和配置方法 * } * </pre> * * <p>当使用 XML 配置时,此接口通常不需要实现,因为可以直接在 XML 中配置事务管理器。 * * @author Chris Beams * @since 3.1 * @see org.springframework.context.annotation.EnableTransactionManagement * @see org.springframework.transaction.annotation.Transactional * @see org.springframework.transaction.PlatformTransactionManager */ public interface TransactionManagementConfigurer { /** * 返回用于支持 Spring 的基于注解的事务管理的默认事务管理器。 * * <p>当使用 {@link org.springframework.transaction.annotation.Transactional} 注解时, * 如果没有在注解中明确指定事务管理器(即没有设置 {@code value} 属性), * 则将使用此方法返回的事务管理器。 * * <p>此方法的返回值必须是一个有效的 {@link org.springframework.transaction.PlatformTransactionManager} * 实例,通常是通过 Spring 的配置机制创建的。 * * @return 默认的事务管理器实例 */ PlatformTransactionManager annotationDrivenTransactionManager(); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值