Apply for Zend Framework Certification Training

c



< Introduction to C Programming-2 Input output with different data types >



How to print in c in different ways
 
1. Print simple text
#include <stdio.h>
int main() {
    printf("Hello World");
    return 0;
}
Output:
Hello World
2. Print text with a new line
Use \n for a new line.
printf("Hello\nWorld");
Output:
Hello
World
You can also write:
printf("Hello World\n");
printf("Welcome to C Programming");
3. Print an integer using %d
int age = 25;
printf("Age = %d", age);
Output:
Age = 25
%d → integer (int)
4. Print a float using %f
float price = 25.50;
printf("Price = %f", price);
Output:
Price = 25.500000
Print only 2 decimal places
printf("Price = %.2f", price);
Output:
Price = 25.50
5. Print a character using %c
char grade = 'A';
printf("Grade = %c", grade);
Output:
Grade = A
6. Print a string using %s
char name[] = "Rajesh";
printf("Name = %s", name);
Output:
Name = Rajesh
7. Print a double using %lf
double salary = 45000.75;
printf("Salary = %lf", salary);
Output:
Salary = 45000.750000
For controlling decimal places:
printf("Salary = %.2lf", salary);
8. Print multiple values
You can print several variables in one printf().
#include <stdio.h>
int main() {
    char name[] = "Rajesh";
    int age = 25;
    float marks = 85.5;
    printf("Name = %s\nAge = %d\nMarks = %.2f",
           name, age, marks);
    return 0;
}
Output:
Name = Rajesh
Age = 25
Marks = 85.50
 
Different ways to print in C
1. printf() Used for formatted output.
printf("Hello");
printf("%d", 100);
printf("%f", 25.5);
2. puts() Used mainly to print a string.
#include <stdio.h>
int main() {
    puts("Hello World");
    return 0;
}
Output:
Hello World
puts() automatically adds a new line.
So:
puts("Hello");
puts("World");
Output:
Hello
World
3. putchar() Used to print one character.
#include <stdio.h>
int main() {
    putchar('A');
    return 0;
}
Output:
A
You can also do:
putchar('H');
putchar('i');
Output:
Hi
Quick comparison
Function Used for Example
printf() Formatted output printf("%d", age);
puts() String puts("Hello");
putchar() Single character putchar('A');
Format specifiers commonly used with printf()
Specifier Data type Example
%d int printf("%d", 10);
%f float printf("%f", 10.5);
%lf double printf("%lf", 10.5);
%c char printf("%c", 'A');
%s String printf("%s", "Hello");
%u Unsigned int printf("%u", 10);
%x Hexadecimal printf("%x", 255);
%o Octal printf("%o", 10);
 
For beginners, remember these three first:
 
printf()           → formatted output
puts()            → string output
putchar()       → single-character output

< Introduction to C Programming-2 Input output with different data types >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top