C语言 格式说明符(小技巧)

C语言基本类型声明、初始化、大小及格式说明符总结 C语言基本类型声明、初始化、大小及格式说明符总结 类型 声明及初始化 大小(byte) 格式说明符 char char a=’2’; char a=50,b=062,c=0x32; char a=’\062’,b=’\x32’; char a=’\n’,b=’\012’,c=’\x0a’; 字面 阅读详情

前些日子看书看到的一个C语言的格式说明符----%n ,蛮有意思的,拿来与大家分享下。

printf可不只是一个输出函数哦,这个%n就是把字符串的长度,写进一个变量。

看代码吧,好理解。

复制代码
#include <stdio.h>

void main()
{
    int lenA;
    int lenB;
    printf("Hello%n\n",&lenA);
    printf("Hello%n World!\n",&lenB);
    printf("%d\n",lenA);
    printf("%d\n",lenB);
}
复制代码

 

最后输出的lenA,lenB的都是5;那说明获得字符串的长度只是%n前面的。

蛮有意思的吧,以前以为printf只是个输出函数,没想到也可以当输入用。

------------------------------------------------------------------------------------------------------------------------

--------------错误记录---------------

当C用malloc或C++中new动态分配内存时,一定要在合适的时候释放掉,我今天就是没有释放动态的分配内存。

还有就是sizeof不能对于动态分配内存的指针使用,这可是要记得啊,我今天在这上面可是栽了个跟头啊,如果你这样写了,那可是调试都都调试不出来的,我就是这样,明明调试正确,一运行就出错。

写下自己的错误,也希望看过的人,以后可可以避免·····

C 语言:格式说明符 首先,我们声明了 3 个整数变量 var1,var2,var3,然后我们借助 %d 格式说明符使用 scanf 从用户那里输入这三个变量,因为这些都是 int 变量,之后,我们将用户输入的值打印到 printf 的控制台,还使用 %d 告诉编译器它将要打印的变量的数据类型。让我们也看看它的实现。- 对于扫描和打印有符号的短代码,我们使用 %hi 符号,对于无符号短代码,我们使用 %hu 符号,它们的实现与其他说明符相同。当数据类型为 signed int 类型时使用它,该类型存储整数值,如 1,-1,2。 阅读详情

相关推荐

C 语言字符型格式说明符与输入输出详解

在 C 语言中,char类型用于表示单个字符,占用一个字节。它的取值范围通常是-128到127或0到255,具体取决于编译器和系统。字符型数据可以通过字符字面量(如'A')表示。%c格式说明符用于输入输出单个字符。和 getchar()用法,如何精确读取字符。用于读取字符串,但不能包含空格。用于输出字符串,注意字符串的结束符。进阶使用scanf()同时读取多个数据类型。掌握这些技巧后,你将能够灵活地处理 C 语言中的字符输入输出操作!🎯🔥 赶快将它们应用到你的项目中吧!

m0_62599305的博客 1041

printf() 中 %n 的用途是什么?

    在C printf() 中,%n是一个特殊的格式说明符,它不打印某些内容,printf() 统计出现在 %n 之前的字符数,并将该值赋值给参数变量。 #include<stdio.h> int main() { int c; printf("geeks for %ngeeks ", &c); printf("%d", c); getchar(); return 0; }     上面的程序打印“geeks for geeks 10”。第一个 printf() 打印“geek

运维狗的备忘录 1万+

从零解析特斯拉线圈:谐振变压器的核心原理与DIY实践

本文深入解析特斯拉线圈的工作原理与DIY实践,从LC振荡电路到电磁耦合,详细介绍了谐振变压器的核心原理。通过材料清单、绕制技巧和安全指南,帮助爱好者从零开始打造迷你闪电发生器,体验高压电子实验的独特魅力。

weixin_33670786的博客 496

关于c语言中printf(“%n“)的问题

在C printf() 中,%n是一个特殊的格式说明符,它不打印某些内容,printf() 统计出现在 %n 之前的字符数,并将该值赋值给参数变量。在写scanf 的输入异常处理时遇到了一个从未见过的格式说明符%n_c语言%n。printf() 中 %n 的用途是什么?printf()中%n格式说明符_c语言%n-优快云博客。

Dear_JIANJIAN的博客 1567

基于BQ24610的24V电池充电板

转自我的博客 本文的主要目的在于传播基本的阅读datasheet的能力,以及PCB设计需要注意的基本问题。 本次需要设计的是24V动力锂电池充电电路。 原理图设计 参考官方参考电路 为了快速设计电路,自然是参考官方参考电路,选择Typical Application。 做细微的参数调整 由于我们的需求是给24V的电池充电,所以需要稍微修改一下原理图,接下来需...

greedyhao的博客 1万+

HFSS新手避坑指南:从零搭建T形波导功分器(含参数扫描与优化实战)

本文为HFSS新手提供T形波导功分器设计的详细指南,涵盖基础设置、建模技巧、参数扫描与优化实战。重点解析内场分析中的常见陷阱,如单位设置、波端口定义和布尔运算错误,并分享优化设计的关键步骤,帮助工程师快速掌握HFSS在微波器件设计中的应用。

weixin_27442001的博客 140

%n在C语言总的意思

%n与其他格式说明符号不同。%n不向printf传递格式化信息,而是令printf把自己到该点已打出的字符总数放到相应变元指向的整形变量中。因此%n对于的变元必须是整形指针。 对printf调用返回之后,%n对于变元指向的变量中将包含有一个整数值,表示出现%n时已经由该次printf调用输出的字符数。 printf("this%n is a test\n",&count);//

zhangxhappy的专栏 7058

Python画好看的雷达图,全网开源代码最好看的

【代码】Python画好看的雷达图,全网开源代码最好看的。

大模型天花板 2287

C语言中鲜为人知的格式说明符及今天的错误记录

前些日子看书看到的一个C语言的格式说明符----%n ,蛮有意思的,拿来与大家分享下。 printf可不只是一个输出函数哦,这个%n就是把字符串的长度,写进一个变量。 看代码吧,好理解。 #include <stdio.h> void main() { int lenA; int lenB; printf("Hello%n\n"...

332

从AAAI 2025被拒到ACM MM 2025录用:我的论文投稿经验与教训总结

本文分享了作者从AAAI 2025被拒到ACM MM 2025录用的论文投稿经验与教训。详细解析了格式要求、可复现性清单等关键细节,提供了精准转投策略和提升录用概率的实用技巧,帮助研究者避免常见错误并优化投稿策略。

weixin_29207533的博客 507

利用python语言进行相机标定

利用python语言进行相机标定 目录利用python语言进行相机标定一、相机标定相关原理1.相机标定简介2.坐标映射2.1图像坐标系1)图像坐标系(Pixel coordinate system)2)成像平面坐标系(Retinal coordinate system)3)摄像机坐标系(Camera coordinate system)4)世界坐标系(World coordinate system)二、相机标定算法流程1.镜头畸变1.1径向畸变1.2切向畸变2.相机标定算法求解外参内参和单应矩阵关系求解(张

qq_44818951的博客 4701

[tip] java中的%n和\n,printf(“%n“)参数的误区

java %n 解读

mentalhealing的博客 1966

Fastwhisper + Pyannote 实现 ASR + 说话者识别

最近在研究ASR相关的业务,也是调研了不少模型,踩了不少坑,ASR这块,目前中文普通话效果最好的应该是阿里的modelscope上的中文模型了,英文的话,还是非whisper莫属了,而且whisper很变态,粤语效果也还不错,因此,如果实际业务中需要涉及到不同的语言,还是更推荐whisper多一点faster-whisper是使用CTranslate2对OpenAI的Whisper模型的重新实现,CTranslate2是一个用于Transformer模型的快速推理引擎。

qq_44193969的博客 1万+

C 语言 数据类型所用格式说明符

类型格式说明符说明float%f%.Nf浮点数输出double%f%e双精度浮点数输出int8_t%d%hhd有符号 8 位整数uint8_t%u%hhu无符号 8 位整数int16_t%d%hd有符号 16 位整数uint16_t%u%hu无符号 16 位整数int32_t%d%ld有符号 32 位整数uint32_t%u%lu无符号 32 位整数这些格式说明符可以确保数据以正确的格式输出。在printf中%f和%lf可以互换,用于输出double或float。在scanf中%f。

2302_79290539的博客 2685

Python跨年烟花(完整代码)

Python语言实现跨年烟花动画的完整代码。

AIGC 3万+

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

C语言格式说明符全解:从%d到%n,掌握常用%用法

在C语言中,printf()和scanf()等函数通过格式说明符来指定数据的输入输出格式。这些说明符以百分号开头,后面跟一个或多个字符,用于指定数据类型、长度、精度等格式信息。格式说明符的基本结构为:格式说明符用途分类数据类型功能描述%d整数int输出有符号十进制整数%i整数int输出有符号十进制整数(与%d几乎相同)%u整数输出无符号十进制整数%o整数输出无符号八进制整数(无前缀0)%x整数输出无符号十六进制整数(小写字母,无前缀)%X整数。

2301_78626750的博客 5085

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

c语言双精度的格式说明符_C中的格式说明符

c语言双精度的格式说明符In this article you will get list of format specifiers in C. 在本文中,您将获得C语言格式说明符的列表。 Format specifier is used to specify the data type while reading or writing. For example if you want to ...

culing2941的博客 1757

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C语言基础之吃透格式说明符

一、格式说明符。

2403_87518966的博客 1732

梳理一下C语言中的格式说明符

请注意,随着C语言标准和库的发展,可能会有新的格式说明符被引入,但上述列表涵盖了C语言中最为常见和基本的格式说明符。

m0_59091453的博客 2866

c语言中的%u是输入输出格式说明符

c语言中的%u是输入输出格式说明符,表示按unsigned int格式输入或输出数据。 %d 有符号10进制整数 %i 有符号10进制整数 %o 无符号8进制整数 %u 无符号10进制整数 %x 无符号的16进制数字,并以小写abcdef表示 %X 无符号的16进制数字,并以大写ABCDEF表示 %F/f 浮点数 %E/e 用科学表示格式的浮点数 %g 使用%f和%e表示中的总的位数表示最短的来表示浮点数 G 同g格式,但表示为指数 %c 单个字符 %s 字符串 标题整型有无符号(unsigned)和有符

qq_45896330的博客 5万+

C语言中常用的格式说明符及其用途(用于指定 printf 和 scanf 等函数)

在C语言中,格式说明符用于指定 printf 和 scanf 等函数中数据的输入/输出格式。以下是常用的格式说明符及其用途。

hiccupppppp的博客 1588

C语言中格式说明符

C语言中格式说明符

致力于分享在各类技术领域的学习与实践经验,涵盖编程、虚拟化、开源工具、网络安全等。 3567

c语言中八进制输出的格式说明符,C 的输入&输出格式说明符讲解

C的输入&输出格式说明符讲解,方便你了解C的输入与输出格式的写法。%d整型输出,%ld长整型输出,%o以八进制数形式输出整数,%x以十六进制数形式输出整数,或输出字符串的地址。%u以十进制数输出unsigned型数据(无符号数)。注意:%d与%u有无符号的数值范围,也就是极限的值,不然数值打印出来会有误。%c用来输出一个字符,%s用来输出一个字符串,%f用来输出实数,以小数形式输出,默认情...

weixin_39677538的博客 1万+
上一篇: HDU 1.2.5 GPA(ACM step)
下一篇: HDU 1.2.6 (ACM steps)
Attacker2001
博客等级 码龄12年 101粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值