C语言学习笔记[第10天]

本文详细介绍了C语言中使用printf()和scanf()进行格式化输入输出的方法。包括如何正确使用&来传递变量地址,如何指定宽度和精度进行输出,以及如何通过*修饰符跳过输入中的特定部分等内容。

四、字符串和格式化输入/输出

4.printf() 和scanf()
4.5 使用scanf()

​ 如果用scanf()读取基本变量类型的值,在变量名前加上一个&,如果用scanf()把字符串读入字符串数组中,不用使用&

// input.c -- 何时使用&
#include <stdioh>
int main(void)
{
    int age;        //变量
    float assets;   //变量
    char pet[30];   //字符数组,用于储存字符串
    
    printf("Enter your age, assets, and favorite pet.\n");
    scanf("%d %f", &age, &pet);
    scanf("%s", pet);
    printf("%d $%.2f %s\n", age, assets, pet);

    return 0;
}
//Enter your age, assets, and favorite pet.
//38
//92360.88 llama
//38 $92360.88 llama

\\\\\\\\\\\
三个表格
\\\\\\\\\\\\

#从scanf()角度看输入
#格式字符串中的普通字符
#scanf()的返回值
4.6 printf()和scanf()的*修饰符
/* varwid.c -- 使用变宽输出字段 */
#inlude <stdio.h>
int main(void)
{
    unsigned width, precision;
    int number = 256;
    double weight = 242.5;

    printf("Enter a field width:\n");
    scanf("%d", &width);
    printf("The number is :%*d:\n", width, number);
    printf("Now enter a width and a precision:\n");
    scanf("%d %d", &width, &precision);
    printf("Weight = %*.*f\n", width, precision, weight);
    printf("Done!\n");

    return 0;
}
//Enter a field width:
//6
//The number is :  256:
//Now enter a width and a precison:
//8 3
//Weight = 242.500
//Done!
/* skiptwo.c -- 跳过输入中的前两个整数 */
#inlude <stdio.h>
int main(void)
{
    int n;
    
    print("Plese enter three integers:\n");
    scanf("%*d %*d %*d", &n);
    printf("The last interger was %d\n", n);

    return 0;
}
//Please enter three integers:
//2013 2014 2015
//The last integer was 2015
4.7 printf()的用法提示
//对齐
printf("%9d %9d %9d\n", val1, val2, val3);
### 关于C语言学习笔记第二的内容 虽然当前未提供具体针对“C语言学习笔记第二”的内容,但从已有参考资料中可以推测可能涉及的基础知识点。以下是基于现有引用材料整理的相关信息: #### 1. C语言的历史背景 C语言由丹尼斯·里奇和肯·汤姆逊在1970年至1973年间开发完成,隶属于美国贝尔实验室[^2]。它最初是为了编写UNIX操作系统而设计的语言,因此具有强大的底层操作能力。 #### 2. 基础语法回顾 根据已有的资料,“C语言基础语法”可能是第二学习的重点之一。这通常包括变量声明、数据类型以及基本控制流语句等内容[^1]。例如: ```c int main() { int a = 10; // 整型变量声明 float b = 3.14f; // 浮点数变量声明 char c = 'A'; // 字符变量声明 if (a > 5) { // 条件判断语句 printf("a is greater than 5\n"); } else { printf("a is not greater than 5\n"); } return 0; } ``` #### 3. 进一步扩展到堆内存概念 如果第二学习已经涉及到更深入的主题,则可能会引入动态内存分配的概念。尽管具体内容出现在第十笔记中[^3],但初步了解`malloc`及其用途可以帮助理解后续高级主题。例如: ```c #include <stdio.h> #include <stdlib.h> int main() { int *ptr = (int *) malloc(sizeof(int)); // 动态分配整型空间 if (ptr != NULL) { *ptr = 10; // 赋值给指针指向的空间 printf("Value stored at allocated memory: %d\n", *ptr); free(ptr); // 释放动态分配的内存 } else { printf("Memory allocation failed!\n"); } return 0; } ``` 以上代码展示了如何通过 `malloc()` 函数申请一块存储区域,并最终调用 `free()` 将其归还给系统。 --- #### 总结 由于缺乏明确标注为“C语言学习笔记第二”的直接引用,上述分析综合了基础知识与稍高阶话题之间的过渡关系。实际教学安排应视个人进度调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ykp想睡觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值