输出打印数组---printf函数

本文探讨了C语言中如何使用printf函数输出不同类型的数组,包括字符串数组和整数数组,并通过实例展示了如何正确地输出这些数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输出打印数组---printf函数

printf(“%s\n”,str);语句可将字符指针所指的字符串一次性的输出,除了字符类型,其他类型的数组不能用数组名来整体性的输出它的全部元素,只能逐个元素输出,用for循环

下面我们来一个验证:

#include<stdio.h>
#include<windows.h>

void  f(char **p)
{
	*p += 2;
}

int main()
{
	char *a[] = { "123", "456", "7891" };
	int b[] = { 1, 2, 3, 4, 5, 6 };
	char **p;
	p = a;
	f(p);
	//printf("%s", *p);
	printf("%s", b);//若将非字符型数组用%s这种形式输出,则我们编译没问题
	system("pause");
	return 0;
}

以上代码编译成功

运行后为:

以下为测试代码,关于字符串和数组的输出:

#include<stdio.h>
#include<windows.h>

void  f(char **p)
{
	*p += 2;
}

int main()
{
	char *a[] = { "123", "456", "7891" };
	int b[6] = { 1, 2, 3, 4, 5, 6 };
	char *c ="abcdef";
	char d[4] = { '1', '2', '6', '5' };
	char **p;
	p = a;
	f(p);
	printf("*p: %s\n", *p);

	printf("b数组:");
	for (int i = 0; i < 6; i++)
	{
		printf(" %d ", b[i]);
	}
	printf("\n");
	printf("指针变量c: %s \n", c);
	printf(": %s\n", d);
	system("pause");
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值