Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to use printf() function to print formatted data to stdout in C

1 Answer

0 votes
#include <stdio.h>
   
int main(void)
{
	printf("Characters: %c %c \n", 'a', 98);
	printf("Signed decimal integer: %d %i \n", 1234, 9763);
	printf("Preceding with blanks: %10d \n", 1966);
	printf("Preceding with zeros: %010d \n", 1966);
	printf("Unsigned decimal integer: %u\n", 30000);
	printf("Unsigned octal: %o\n", 600);
	printf("Unsigned hexadecimal integer: %x %X\n", 0x13f, 0x13f);
	printf("Decimal floating point: %f\n", 3.14f);
	printf("Floats: %4.2f %+.0e %E \n", 3.14159, 3.14159, 3.14159);
	printf("Scientific notation: %e %E\n", 3.783e+2, 3.783e+2);
	printf("string: %s\n", "abcde string");
	
	int n = 10;
	int *p = &n;
	printf("Pointer address: %p\n", p);

	printf("The shortest representation: %%e or %%f: %g\n", 873.2357);
	printf("The shortest representation: %%E or %%F: %G\n", 873.2357);	
	printf("Decimals: %d %ld\n", 1977, 650000L);
	printf("Different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
	printf("Width trick: %*d \n", 5, 10);
	
    return 0;
}
  


/*
run:
  
Characters: a b
Signed decimal integer: 1234 9763
Preceding with blanks:       1966
Preceding with zeros: 0000001966
Unsigned decimal integer: 30000
Unsigned octal: 1130
Unsigned hexadecimal integer: 13f 13F
Decimal floating point: 3.140000
Floats: 3.14 +3e+000 3.141590E+000
Scientific notation: 3.783000e+002 3.783000E+002
string: abcde string
Pointer address: 000000000022FE44
The shortest representation: %e or %f: 873.236
The shortest representation: %E or %F: 873.236
Decimals: 1977 650000
Different radices: 100 64 144 0x64 0144
Width trick:    10

*/

 



answered May 5, 2016 by avibootz
edited Apr 26, 2024 by avibootz
...