當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ printf()用法及代碼示例

printf()函數最初是在<cstdio>頭文件下聲明的。它將格式化的字符串打印到標準輸出 stdout。

句法:

int printf(const char*word, .......)

參數:

  • word: 表示需要在標準輸出stdout上打印的字符串,
  • …….:表示可以傳遞給它的任何額外參數。

返回類型:它返回寫入的字符總數。

例子:

C++


// C++ Program to demonstrate
// use of printf()
#include <iostream>
using namespace std;
int main()
{
    char a[] = "geeksforgeeks";
    printf("The total no of characters returned by "
           "printf() is %d",
           printf("%s", a));
    return 0;
}
輸出
geeksforgeeksThe total no of characters returned by printf() is 13

要了解有關print()的返回類型的更多信息,請參閱return type of print() and scanf().

例子:

C++


// C++ Program to implement
// use of % with printf()
#include <iostream>
using namespace std;
int main()
{
    int a = 5;
    printf("%d", a);
}
輸出
5

注意:printf() 中出現的任何帶有“%”符號的內容都稱為格式說明符。

格式說明符的組成部分:

  • 一個牌子
  • width - 這是一個可選字段,決定寬度字段
  • Precision
  • Length
  • Specifier
  • Flags

標誌:

它負責兩個字段之間的轉換行為。它可以分為:

  • -:左證明答案。
  • +: 作為符號附加到結果的開頭。
  • space:默認情況下,如果不存在任何內容,結果將附加空格。
  • #:另一種轉換形式
  • 0:用於填充整數或浮點數中大部分尾隨零的數字。

格式說明符:

格式說明符用於獲取輸入並打印類型的輸出。

說明符 輸出 示例
d 或 i 有符號十進製整數 360
u 無符號十進製整數 7200
o 無符號八進製 555
x 無符號十六進製整數 7fb
e 尾數/指數表示法 4.567e+8
c Character k
s 字符串 geeks
% 將 % 符號打印到輸出屏幕

注意:

.x f denotes the precision of float variables up to  x decimal places

示例 1:

C++


// C++ Program to 
#include <bits/stdc++.h>
using namespace std;
int main() {
   char course[]="geeksforgeeks";
  printf("Owner of this article is %s",course);
  return 0;
}
輸出
Owner of this article is geeksforgeeks

示例 2:

C++


// C++ Program to demonstrate
// Use of %._f
#include <bits/stdc++.h>
using namespace std;
int main()
{
    float Self_paced_price = 3999.0,
          system_design_price = 10999.0;
    printf("the value of system design price / self paced "
           "price is %.3f",
           system_design_price / Self_paced_price);
    return 0;
}
輸出
the value of system design price / self paced price is 2.750

.3f 說明應打印在小數點後 3 位以內的值。

示例 3:

C++


// C++ Program implement
// Decimal to Octal
// Conversion using '%o'.
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int decimal_number = 13;
    printf("Decimal to Octal conversion of %d is %o",
           decimal_number, decimal_number);
    return 0;
}
輸出
Decimal to Octal conversion of 13 is 15

示例4:

C++


// C++ Program to implement
// Decimal to hexadecimal
// Conversion using '%x'
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int decimal_number = 13;
    printf("Decimal to Hexadecimal conversion of %d is %x",
           decimal_number, decimal_number);
    return 0;
}
輸出
Decimal to Hexadecimal conversion of 13 is d

實施例5:

C++


// C++ Program to
// Char to int Conversion
// and Vice Versa.
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a = 78;
    printf("%c\n", a);
    int b = 'g';
    printf("%d", b);
    return 0;
}
輸出
N
103


相關用法


注:本文由純淨天空篩選整理自raj2002大神的英文原創作品 C++ printf() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。