30天学会C语言: Day 18-更多字串

格式化字串

格式化字串除了把变数转换成字串,还可以规定显示的位数

最小长度

% 的後面加上数字,可以设定变数显示的最小长度,如果长度不足会自动补空格,以达到对其的目的
前两个 printf() 没有规定最小长度,所以会以靠左对齐的方式显示
後两个 printf() 规定最小长度是4,所以显示的时候会自动补空格变成4个字,会变成靠右对齐

#include<stdio.h>
#include<stdlib.h>

int main(){
    int a=10, b=100;
    printf("%d\n", a);
    printf("%d\n", b);

    printf("%4d\n", a);
    printf("%4d\n", b);
	return 0;
}

在最小位数的前面加上0,会变成用0补齐长度

#include<stdio.h>
#include<stdlib.h>

int main(){
    int a=10, b=100;
    printf("%07d\n", a);
    printf("%07d\n", b);
	return 0;
}

string.h

包含各种便於处理字串的函式

strlen(s)

回传一个整数,是字串 s 的长度,长度的计算不包含字串结尾的 '\0'

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
    char str[10]="abc123";
	printf("%d", strlen(str));
	
	return 0;
}

和回圈一起使用可以遍历整个字串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
    char str[10]="abc123";
    for(int i=0; i!=strlen(str); i++)
        printf("%c\n", str[i]);
	
	return 0;
}

<<:  [Day 20] Reactive Programming - Spring WebFlux

>>:  Day 19 - 作业系统攻击 - Linux

Day 28 - 使用各种方式取得资料

这篇会分别使用前面几篇文章介绍的:XHR、Promise、fetch、async、axios 等 5...

从疫情聊聊 WFH 是福音还是地狱

习惯是可以半自动执行的行为,但很依赖节奏;修练则是终身的追求,所以可以灵活。节录自 刘轩《天上总会...

Android Studio初学笔记-Day2-LinearLayout介绍

对於初学着来说(像我本人),一开始如果没先了解布局的运作容易在布局的编排上产生问题,所以第一篇先来介...

提升 State ( Day12 )

之前在 State 和生命周期 在每一个元件里面都会有自己的 state,但如果遇到彼此需要共用的 ...

服务组织控制(Service Organization Control :SOC)

-服务组织控制(SOC) 服务组织控制(Service Organization Control ...