C++入门经典-例2.3-在print函数中使用附加符号

1:代码如下:

// 2.3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


int main()
{
    long iLong=100000;                            /*定义长整型变量,为其赋值*/
    printf("the Long is %ld
",iLong);                /*输出长整型变量*/

    printf("the string is: %s
","LOVE");                /*输出字符串*/
    printf("the string is: %10s
","LOVE");            /*使用m控制输出列*/
    printf("the string is: %-10s
","LOVE");            /*使用-表示向左靠拢*/
    printf("the string is: %10.3s
","LOVE");            /*使用n表示取字符数*/
    printf("the string is: %-10.3s
","LOVE");    
    return 0;
}
/* %ms 表示输出字符串占m列,如果字符串本身长度大于m,,则突破m的限制,将字符串全部输出;若字符串小于m,则用空格键进行左补齐。可以看到在字符串“LOVE”前后存在
6个空格。*/
/* %-ms 表示如果字符串长度小于m,则在m列范围内,字符串向左靠,右补空格。*/
/* %m.ns 表示输出占m列,但是只取字符串左端n个字符,这n个字符输出在m列的右侧,左补空格。*/
/* %-m.ns ,其中m、n的含义同上,n个字符输出在m列的左侧,右补空格。如果n>m,则m自动取n值,即保证n个字符正常输出。*/
View Code

运行结果:

原文地址:https://www.cnblogs.com/lovemi93/p/7494958.html