Date
Jul. 21st, 2024
 
2024年 6月 16日

Post: C Primer 003 : printf() 函数

C Primer 003 : printf() 函数

Published 12:05 May 01, 2010.

Created by @ezra. Categorized in #Programming, and tagged as #C/C++.

Source format: Markdown

Table of Content

这篇博客主要介绍 C 语言的 printf()

printf() 函数是在 stdio.h 中定义的函数,可以把内容输出到控制台。

int printf(const char *format,...);

来看一些示例:

/*
  printf标准函数练习1
  打印Hello World~
*/
#include <stdio.h>
int main(){
  printf("Hello World~");
  return 0;
}
/*
  printf标准函数练习2
  转义符练习
  打印Hello World~并换行
*/
#include <stdio.h>
int main(){
  printf("Hello World~\n");
  return 0;
}
/*
  printf标准函数练习3
  占位符练习
  计算并打印3 X 2 + 10 = 16
*/
#include <stdio.h>
int main(){
  printf("3 X 2 + 10 = %d\n",3*2+10);
  return 0;
}
/*
  printf标准函数练习4
  占位符练习
  计算并打印3 X 2 + 10 = 16
*/
#include <stdio.h>
int main(){
  printf("%d X %d + %d = %d\n",3,2,10,3*2+10);
  return 0;
}
/*
在屏幕上打印如下内容:
    1
    12
    123
    1234
    12345
*/
#include <stdio.h>
int main(){
    int num1 = 0,num2 = 0;
    for (num1 = 1,num2 = 0;num1 <= 5;num1++){
        printf("%d\n",num2 * 10 + num1);
        num2 = num2 * 10 + num1;
    }
}
/*
  num1 num2   printf   num2*10+num1
    1   0      1       0*10+1=1
    2   1      12      1*10+2=12
    3   12     123     12*10+3=123
    4   123    1234    123*10+4=1234
    5   1234   12345   1234*10+5=12345
*/
/*
有如下一组数字,编写程序把前10个打印在屏幕上:
        1 1 2 3 5 8 13 21 ...
*/
#include <stdio.h>
int main()
{
    int num1 = 0,num2 = 1,num3 = 0;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    num1 = num2;
    num2 = num3;
    num3 = num1 + num2;
    printf("%d  ",num2);
    printf("\n");
    return 0;
}
Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF