[go: up one dir, main page]

0% found this document useful (0 votes)
54 views6 pages

Basic Question On Printf Scanf

The document provides multiple examples of C programming demonstrating the use of various format specifiers in printf and scanf functions. It covers printing and reading different data types, handling special characters, and formatting output, including width, justification, and precision. Each example includes code snippets, sample outputs, and explanations of the concepts being illustrated.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

Basic Question On Printf Scanf

The document provides multiple examples of C programming demonstrating the use of various format specifiers in printf and scanf functions. It covers printing and reading different data types, handling special characters, and formatting output, including width, justification, and precision. Each example includes code snippets, sample outputs, and explanations of the concepts being illustrated.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Example 1: Printing Multiple Data Types

#include <stdio.h>

int main() {
int i = 10;
float f = 23.45;
char c = 'A';

printf("Integer: %d, Float: %.2f, Character: %c\n", i, f, c);


return 0;
}

Output:

Integer: 10, Float: 23.45, Character: A

Explanation: This example demonstrates using different format specifiers: %d for integers,
%.2f for floating-point numbers with two decimal places, and %c for characters.

Example 2: Reading Different Data Types


#include <stdio.h>

int main() {
int age;
float salary;
char grade;

printf("Enter your age, salary, and grade: ");


scanf("%d %f %c", &age, &salary, &grade);

printf("Age: %d, Salary: %.2f, Grade: %c\n", age, salary, grade);


return 0;
}

Output (sample input):

Enter your age, salary, and grade: 25 50000.50 A


Age: 25, Salary: 50000.50, Grade: A

Explanation: The scanf function reads values for an integer, a float, and a character using
appropriate format specifiers.

Example 3: Width Specifier with printf


#include <stdio.h>

int main() {
int num = 123;
printf("Number with width 5: %5d\n", num);
printf("Number with width 10: %10d\n", num);
return 0;
}
Output:

Number with width 5: 123


Number with width 10: 123

Explanation: The numbers are printed with specified minimum field widths. If the number
of digits is less than the specified width, spaces are added.

Example 4: Left Justification with - Flag


#include <stdio.h>

int main() {
int num = 123;
printf("Left justified with width 10: %-10d\n", num);
return 0;
}

Output:

Left justified with width 10: 123

Explanation: The - flag left-justifies the output within the specified width.

Example 5: Reading a String Using scanf


#include <stdio.h>

int main() {
char str[50];
printf("Enter a string: ");
scanf("%49s", str);
printf("You entered: %s\n", str);
return 0;
}

Output (sample input):

Enter a string: Hello


You entered: Hello

Explanation: The scanf function reads a string, stopping at the first whitespace character.
The format specifier %49s ensures that no more than 49 characters are read, avoiding buffer
overflow.

Example 6: Printing Pointers


#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num;
printf("Address of num: %p\n", (void *)ptr);
return 0;
}
Output (sample output):

Address of num: 0x7ffee3b12d3c

Explanation: The %p format specifier is used to print a memory address. It's cast to (void
*) to avoid warnings.

Example 7: Printing in Scientific Notation


#include <stdio.h>

int main() {
double num = 12345.6789;
printf("Scientific notation: %e\n", num);
return 0;
}

Output:

Scientific notation: 1.234568e+04

Explanation: The %e format specifier prints numbers in scientific notation.

Example 8: Reading Hexadecimal Input


#include <stdio.h>

int main() {
int hex;
printf("Enter a hexadecimal number: ");
scanf("%x", &hex);
printf("Decimal equivalent: %d\n", hex);
return 0;
}

Output (sample input):

Enter a hexadecimal number: 1A


Decimal equivalent: 26

Explanation: The %x specifier reads a hexadecimal input and stores it as an integer.

Example 9: Printing with Leading Zeros


#include <stdio.h>

int main() {
int num = 5;
printf("Padded with zeros: %05d\n", num);
return 0;
}

Output:
Copy code
Padded with zeros: 00005

Explanation: The 0 flag pads the number with leading zeros.

Example 10: Printing Strings with a Precision


#include <stdio.h>

int main() {
char str[] = "Hello, World!";
printf("%.5s\n", str);
return 0;
}

Output:

Hello

Explanation: The precision .5 in the format specifier limits the number of characters printed
from the string.

Example 11: Reading Formatted Input Using a Field Width


#include <stdio.h>

int main() {
char str[20];
printf("Enter a word (up to 10 characters): ");
scanf("%10s", str);
printf("You entered: %s\n", str);
return 0;
}

Output (sample input):

Enter a word (up to 10 characters): Programming


You entered: Programmi

Explanation: The field width 10 in the scanf restricts the number of characters read from
the input to prevent buffer overflow.

Example 12: Handling Special Characters in Strings


#include <stdio.h>

int main() {
char str[50];
printf("Enter a string with spaces: ");
scanf("%[^\n]", str);
printf("You entered: %s\n", str);
return 0;
}

Output (sample input):


Enter a string with spaces: Hello World!
You entered: Hello World!

Explanation: The format specifier %[^\n] reads the input until a newline character is
encountered, allowing spaces in the input.

Example 13: Printing Escape Sequences


#include <stdio.h>

int main() {
printf("Newline character: \\n\n");
printf("Tab character: \\t\tTab Space\n");
printf("Backslash character: \\\n");
return 0;
}

Output:

Newline character: \n
Tab character: \t Tab Space
Backslash character: \

Explanation: Escape sequences are used to print special characters like newlines, tabs, and
backslashes.

Example 14: Using printf to Print a Percentage Sign


#include <stdio.h>

int main() {
float rate = 7.5;
printf("Success rate: %.2f%%\n", rate);
return 0;
}

Output:

Success rate: 7.50%

Explanation: To print a literal % character, you use %% in the format string.

Example 15: Reading and Printing Floating-Point Numbers in Different


Formats
#include <stdio.h>

int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num);

printf("Fixed-point notation: %.2f\n", num);


printf("Exponential notation: %.2e\n", num);
return 0;
}

Output (sample input):

Enter a floating-point number: 123.456


Fixed-point notation: 123.46
Exponential notation: 1.23e+02

Explanation: The code demonstrates how to print floating-point numbers using both fixed-
point (%.2f) and exponential notation (%.2e).

You might also like