instagram youtube
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
logo
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C++ Printf Serve as: A Information with Examples

- Team

Rabu, 9 Oktober 2024 - 22:50

facebook twitter whatsapp telegram line copy

URL berhasil dicopy

facebook icon twitter icon whatsapp icon telegram icon line icon copy

URL berhasil dicopy


C++ printf is a formatting serve as this is used to print a string to stdout. The fundamental thought to name printf in C++ is to supply a string of characters that wish to be revealed as it’s in this system. The printf in C++ additionally accommodates a structure specifier this is changed via the real worth all over execution.

Need a Most sensible Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

The C++ Printf Prototype

The C++ printf prototype is:

int printf(const char * structure, …);

The printf prototype is explained within the <cstdio> header report. While you use the printf() serve as, it prints the string identified via the structure to the usual output stdout. The structure too can include some specifiers that get started with a % and exchange values of variables with the printf() serve as. To position it merely, they paintings as further arguments to the printf() serve as.

The C++ Printf Parameters

The C++ prototype accommodates the next parameters:

  • const char: Any textual content to be imprinted on the console as is
  • structure: A pointer to the string with the non-compulsory structure specifier beginning with the %d signal.

Go back Worth of Printf in C++

C++ printf returns:

  • The selection of characters written if a success
  • A unfavorable worth, if failed

Our Unfastened Lessons with Certificates

Normal Type of Layout Specifier

The overall type of a C++ printf structure specifier is as underneath:

%[flags][width][.precision][length]specifier

Within the above structure:

  • %: That is the main signal that denotes the start of the structure specifier
  • Flags: An non-compulsory a number of values that modifies the conversion conduct. The to be had values are:
    • -: The results of printf serve as is via default proper justified. This worth left justifies the effects
    • +: This will likely upload an indication to the values of the end result, even for the sure values
    • Area: If there’s no signal hooked up to a worth, this will likely connect an area to the start of the end result
    • #: This results in another type of conversion being carried out
    • 0: You employ this flag for integers and floating-point numbers to pad the values with zeros as a substitute of house
  • Width: An non-compulsory box that accepts * or an integer worth to decide the minimal width box
  • Precision: An non-compulsory argument specifies precision via accepting a *, integer worth, or not anything after a ‘.’ image
  • Period: An non-compulsory parameter that defines the dimensions of the argument
  • Specifier: This box accepts a conversion structure specifier

Be informed From The Best possible Mentors within the Trade!

Automation Trying out Masters ProgramDiscover Program

Learn From The Best Mentors in the Industry!

Layout Specifiers Utilized in C++ Printf

Printf in C++ can settle for the next structure specifiers.

Specifier

Description

%

Prints the % signal

c

Writes a unmarried personality to stdout

s

Writes a string to stdout

d or i

Transforms a signed integer to a decimal illustration

o

Presentations an unsigned integer the usage of an octal illustration

x or X

Transforms an unsigned integer to a hexadecimal illustration

u

Presentations an unsigned integer the usage of a decimal illustration

f or F

Turns a floating-point quantity right into a decimal illustration

e or E

Transforms a floating-point quantity to a decimal exponent notation

a or A

Presentations a floating-point quantity the usage of a hexadecimal exponent

g or G

Presentations a floating-point quantity the usage of decimal or decimal exponent notation

n

Returns the selection of characters written via a long way the usage of this name serve as. It writes the end result to the worth pointed via the argument.

p

A pointer that issues to the implementation-defined personality collection

Get ready Your self to Solution All Questions!

Automation Trying out Masters ProgramDiscover Program

Prepare Yourself to Answer All Questions!

Examples of C++ Printf() Serve as

Now that you recognize the basics of C++ printf, have a look at some examples that will provide you with a greater working out of ways the printf() serve as in C++ works.

Instance 1

On this instance, you are going to merely show the worth of an integer the usage of %d and write a string to stdout the usage of the %s structure specifiers.

#come with <cstdio>

int primary(){

    int i = 9;

    char title[] = “Simplilearn”;

    printf(“i = %d n”, i);

    printf(“Welcome to %s n”, title);    

    go back 0;

}

Output:

C_Plus_Plus_Printf_Function_1.

Instance 2

On this instance, you are going to use more than a few structure specifiers from the above desk to outline the conversion conduct and get the end result within the desired structure.

#come with <cstdio>

int primary(){

    char c = ‘S’;

    drift x = 7.0, y = 9.0;

    double d = 6.548;

    int i = 50;

    printf(“The drift department is : %.3f / %.3f = %.3f n”, x,y,x/y);

    printf(“The double worth is : %.4f n”, d);

    printf(“Environment the width of c : %*c n”,3,c);

    printf(“The octal similar of %d is %o n”,i,i);

    printf(“The hex similar of %d is %x n”,i,i);

    go back 0;

}

Output:

C_Plus_Plus_Printf_Function_2

Need a Most sensible Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

C++ Printf Vs. Sprintf

The Sprintf() serve as in C++ is sort of very similar to the printf() serve as with a unmarried primary distinction. Whilst the C++ printf writes to the usual output, the sprintf writes the output to a string buffer. The sprintf() serve as prototype is:

int sprintf(char * buffer, const char * structure, …);

Within the above prototype, the parameters are:

  • Buffer: Pointer to a buffer the place the effects can be written
  • Layout: Pointer to a null-terminated string written to the report flow

Instance of C++ Stringf() Serve as

Within the underneath instance, you are going to write a string to a buffer the usage of the sprintf serve as and output the formatted string. You’ll additionally show the whole selection of characters written to the buffer.

#come with <cstdio>

#come with <iostream>

the usage of namespace std;

int primary(){

    char buf[100];

    int return_Val;

    char my_name[] = “Simplilearn”;

    char subject[] = “tutorials”;

    return_Val = sprintf(buf, “Welcome to %s you’ll be able to learn more than a few programming %s right here!!”, my_name, subject);

    cout << buf << endl;

    cout << “Overall characters written are: ” << return_Val << endl;

    go back 0;

}

Output:

C_Plus_Plus_Printf_Function_3

Printf Vs. Cout in C++

The C++ printf() serve as is most often utilized in C programming however too can run in C++. Then again, the cout is particularly an addition in C++. The desk underneath shows the numerous variations between the C++ printf and cout.

C++ Printf

C++ Cout

Maximum utilization

C language

C++ language

Object of

<cstdio> header report

<iostream> header report

Layout specifier

Takes specifier

It does now not take specifier

Go back worth

Returns the selection of characters if a success and a unfavorable worth if unsuccessful

It does now not go back any worth

Sort secure

Now not sort secure in enter parameters

Sort secure in enter parameters

Unharness a Prime-paying Automation Trying out Task!

Automation Trying out Masters ProgramDiscover Program

Unleash a High-paying Automation Testing Job!

Instance of C++ Cout

Within the instance underneath, you are going to see using C++ printf and cout concurrently to peer the adaptation.

#come with <iostream>

the usage of namespace std;

int primary() {

   char c = ‘S’;

   cout<< “The worth of c : ” << c;

   printf(“nThe worth of c : %c”, c);  

   go back 0;

}

Output:

C_Plus_Plus_Printf_Function_4.

Conclusion

You discovered the entirety concerning the C++ printf() serve as on this article. You’ve gotten additionally noticed other structure specifiers that you’ll be able to use to structure the effects and use them in examples. Now it is time to use the opposite specifiers too. If you wish to be told extra such basic ideas of C++ programming, you’ll be able to confer with Simplilearn’s C++ Educational for Freshmen. The academic is designed for novices and covers all of the fundamental ideas intimately. 

If you wish to pursue a occupation in internet building, our Complete-Stack Internet Building Route would possibly fit you. The route provides hours of carried out studying and on-line studying fabrics that can assist you snatch more than one internet building programming languages that permit you to excel within the box.

Have any questions for us? Point out them as feedback, and our mavens gets again to you at the identical ASAP!

supply: www.simplilearn.com

Berita Terkait

Most sensible Recommended Engineering Tactics | 2025
Unfastened Flow Vs General Flow
Be told How AI Automation Is Evolving in 2025
What Is a PHP Compiler & The best way to use it?
Best Leadership Books You Should Read in 2024
Best JavaScript Examples You Must Try in 2025
How to Choose the Right Free Course for the Best Value of Time Spent
What Is Product Design? Definition & Key Principles
Berita ini 9 kali dibaca

Berita Terkait

Selasa, 11 Februari 2025 - 22:32

Revo Uninstaller Pro 5.3.5

Selasa, 11 Februari 2025 - 22:21

Rhinoceros 8.15.25019.13001

Selasa, 11 Februari 2025 - 22:12

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Februari 2025 - 22:08

RoboDK 5.9.0.25039

Selasa, 11 Februari 2025 - 22:05

RoboTask 10.2.2

Selasa, 11 Februari 2025 - 21:18

Room Arranger 10.0.1.714 / 9.6.2.625

Selasa, 11 Februari 2025 - 17:14

Team11 v1.0.2 – Fantasy Cricket App

Selasa, 11 Februari 2025 - 16:20

Sandboxie 1.15.6 / Classic 5.70.6

Berita Terbaru

Headline

Revo Uninstaller Pro 5.3.5

Selasa, 11 Feb 2025 - 22:32

Headline

Rhinoceros 8.15.25019.13001

Selasa, 11 Feb 2025 - 22:21

Headline

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Feb 2025 - 22:12

Headline

RoboDK 5.9.0.25039

Selasa, 11 Feb 2025 - 22:08

Headline

RoboTask 10.2.2

Selasa, 11 Feb 2025 - 22:05