0% found this document useful (0 votes)
8 views7 pages

Format Specifier Programs in C

The document explains various format specifiers used in C programming for input and output operations, including %d for integers, %f for floating-point numbers, %lf for doubles, %c for characters, %s for strings, and others. Each specifier is accompanied by example code demonstrating its usage. Additionally, it covers how to read and print different data types, including hexadecimal, octal, and scientific notation formats.

Uploaded by

leelarani
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)
8 views7 pages

Format Specifier Programs in C

The document explains various format specifiers used in C programming for input and output operations, including %d for integers, %f for floating-point numbers, %lf for doubles, %c for characters, %s for strings, and others. Each specifier is accompanied by example code demonstrating its usage. Additionally, it covers how to read and print different data types, including hexadecimal, octal, and scientific notation formats.

Uploaded by

leelarani
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

1.

%d – Integer (Decimal)

Explanation:

 %d is used to print or read signed integers (both positive and negative whole numbers).
 It reads an integer input from the user and outputs the integer in decimal format.

Code:

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading integer input from the user
printf("You entered: %d\n", num); // Printing the integer entered by the
user
return 0;
}

 scanf("%d", &num) is used to read an integer.


 printf("%d", num) is used to print the integer.

2. %f – Floating-point Numbers

Explanation:

 %f is used for floating-point numbers (numbers with a decimal point). This specifier
works for float data types.
 It reads a decimal number and prints it in the default floating-point format.

Code:

#include <stdio.h>

int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num); // Reading floating-point number input
printf("You entered: %f\n", num); // Printing the floating-point number
return 0;
}

 scanf("%f", &num) is used to read a floating-point number.


 printf("%f", num) prints the floating-point number.
3. %lf – Double (Floating-point)

Explanation:

 %lf is used for double precision floating-point numbers (double data type).
 double allows for greater precision compared to float.

Code:

#include <stdio.h>

int main() {
double num;
printf("Enter a double: ");
scanf("%lf", &num); // Reading double precision floating-point number
printf("You entered: %lf\n", num); // Printing the double precision
floating-point number
return 0;
}

 scanf("%lf", &num) is used to read a double-precision floating-point number.


 printf("%lf", num) prints the double value.

4. %c – Character

Explanation:

 %c is used to read and print a single character.


 It works with char data types.

Code:

#include <stdio.h>

int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch); // Reading a single character
printf("You entered: %c\n", ch); // Printing the character entered
return 0;
}

 scanf("%c", &ch) is used to read a character input.


 printf("%c", ch) prints the entered character.
5. %s – String

Explanation:

 %s is used to read and print a string (a sequence of characters).


 Note that scanf("%s", str) will read a string without spaces. If you want to input a
string with spaces, you should use fgets().

Code:

#include <stdio.h>

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // Reading a string (no spaces)
printf("You entered: %s\n", str); // Printing the string
return 0;
}

 scanf("%s", str) reads a string of characters without spaces.


 printf("%s", str) prints the string entered by the user.

6. %x / %X – Hexadecimal (Lowercase/Uppercase)

Explanation:

 %x prints integers in lowercase hexadecimal format (using a-f for 10-15).


 %X prints integers in uppercase hexadecimal format (using A-F for 10-15).

Code:

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading integer input
printf("Hexadecimal (lowercase): %x\n", num); // Printing in lowercase
hexadecimal format
printf("Hexadecimal (uppercase): %X\n", num); // Printing in uppercase
hexadecimal format
return 0;
}

 scanf("%d", &num) reads an integer.


 printf("%x", num) prints the integer in hexadecimal format (lowercase).
 printf("%X", num) prints the integer in hexadecimal format (uppercase).

7. %o – Octal

Explanation:

 %o is used to print integers in octal format (base 8).

Code:

#include <stdio.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading an integer
printf("Octal format: %o\n", num); // Printing the number in octal format
return 0;
}

 scanf("%d", &num) reads an integer.


 printf("%o", num) prints the number in octal format.

8. %e / %E – Exponential Notation

Explanation:

 %e prints a floating-point number in exponential (scientific) notation with a lowercase 'e'.


 %E prints the number with an uppercase 'E'.

Code:

#include <stdio.h>

int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num); // Reading floating-point number
printf("Scientific notation (lowercase): %e\n", num); // Printing in
scientific notation (lowercase)
printf("Scientific notation (uppercase): %E\n", num); // Printing in
scientific notation (uppercase)
return 0;
}
 scanf("%f", &num) reads a floating-point number.
 printf("%e", num) prints the number in scientific notation (lowercase).
 printf("%E", num) prints the number in scientific notation (uppercase).

9. %g / %G – Shortened Exponential or Floating-point

Explanation:

 %g prints the number in either exponential or floating-point format, depending on the


magnitude of the number. It uses lowercase letters for the scientific notation.
 %G does the same but uses uppercase letters for the scientific notation.

Code:

#include <stdio.h>

int main() {
double num;
printf("Enter a floating-point number: ");
scanf("%lf", &num); // Reading a floating-point number
printf("Shortened (lowercase): %g\n", num); // Printing the number in the
shortest format (lowercase)
printf("Shortened (uppercase): %G\n", num); // Printing the number in the
shortest format (uppercase)
return 0;
}

 scanf("%lf", &num) reads a floating-point number as double.


 printf("%g", num) prints the number in the shortest form (using
either scientific or
decimal format depending on the number's magnitude).
 printf("%G", num) does the same as %g, but with uppercase formatting.

10. %p – Pointer Address

Explanation:

 %p is used to print the memory address stored in a pointer variable.

Code:

#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num; // Pointer holding the address of num
printf("Address of num: %p\n", (void*)ptr); // Printing the memory
address stored in ptr
return 0;
}

 scanf() is not used here because the program is just printing the pointer's address.
 printf("%p", (void*)ptr) prints the address of the pointer variable.

11. %ld – Long Integer

Explanation:

 %ld is used to print long integers. It is typically used when the size of an integer is larger
than a standard int.

Code:

#include <stdio.h>

int main() {
long int num;
printf("Enter a long integer: ");
scanf("%ld", &num); // Reading a long integer
printf("You entered: %ld\n", num); // Printing the long integer
return 0;
}

 scanf("%ld", &num) reads a long integer.


 printf("%ld", num) prints the long integer.

12. %llu – Unsigned Long Long

Explanation:

 %llu is used to print unsigned long long integers, which are large positive numbers.

Code:

#include <stdio.h>

int main() {
unsigned long long num;
printf("Enter an unsigned long long integer: ");
scanf("%llu", &num); // Reading an unsigned long long integer
printf("You entered: %llu\n", num); // Printing the unsigned long long
integer
return 0;
}

 scanf("%llu", &num) reads an unsigned long long integer.


 printf("%llu", num) prints the unsigned long long integer.

13. %% – Literal Percent Sign

Explanation:

 %% is used when you want to print a literal percent sign (%).

Code:

#include <stdio.h>

int main() {
printf("This is 100%% correct!\n"); // Printing the literal percentage
sign
return 0;
}

 %% prints a literal percent sign. It's useful when you want to display the percent symbol
without it being treated as a format specifier.

You might also like