Notes On C Programming Printf() Function

Back To Page


  Category:  C LANGUAGE | 10th August 2023, Thursday

techk.org, kaustub technologies

The printf() Function Is A Fundamental And Widely-used Function In The C Programming Language. It Is Part Of The Standard I/O Library (stdio.h) And Is Used To Output Formatted Text To The Console Or Other Output Streams. The printf() Function Enables Developers To Display Variables, Strings, And Other Data Types In A Controlled And Formatted Manner.

Syntax:

int Printf(const Char *format, ...);

  • The printf() Function Returns The Number Of Characters Printed On Success And A Negative Value On Error.
  • The format Parameter Is A String That Specifies The Format Of The Output. It Can Contain Plain Text And Format Specifiers.
  • Format Specifiers Start With A % Symbol, Followed By A Character That Indicates The Data Type To Be Printed.

Common Format Specifiers:

  • %d, %i: Print An Integer.
  • %f: Print A Floating-point Number.
  • %c: Print A Character.
  • %s: Print A String.
  • %x, %X: Print An Integer In Hexadecimal Format.
  • %o: Print An Integer In Octal Format.

Usage:

1. Printing Variables:

int Age = 25;
printf("My Age Is %d Years.\n", Age);

2. Printing Floating-Point Numbers:

double Pi = 3.14159;
printf("The Value Of Pi Is %.2f.\n", Pi);

3. Printing Characters And Strings:

char Grade = 'A';
char Name[] = "John";
printf("Student %s Scored %c Grade.\n", Name, Grade);

4. Printing In Hexadecimal And Octal Formats:

int Num = 42;
printf("Decimal: %d, Hexadecimal: %x, Octal: %o\n", Num, Num, Num);

5. Formatting Width And Precision:

float Value = 123.45678;
printf("Value: %.2f\n", Value); // Prints With 2 Decimal Places.

6. Escape Sequences:

printf("New Line: \n");
printf("Tab: \t");
printf("Backslash: \\");

Conclusion: The printf() Function Is A Versatile Tool For Formatting And Outputting Data In C. It Allows Developers To Create Well-organized And Human-readable Output, Making It An Essential Part Of C Programming. Understanding Format Specifiers And Their Usage Is Crucial For Effectively Utilizing The Power Of printf() In Various Scenarios.

Tags:
C Programming Language, C Programming Language, C Tutorials, C Tutorial For Beginners

Links 1 Links 2 Products Pages Follow Us
Home Founder Gallery Contact Us
About Us MSME Kriti Homeopathy Clinic Sitemap
Cookies Privacy Policy Kaustub Study Institute
Disclaimer Terms of Service