HIDE NAV

C Variables Types and printf

A basic demostration of variable types and use of printf()

vars_and_printf.c


#include <stdio.h>
#include <stdlib.h>

int main() {

	/* example variable declarations for all c data types
	An initial assignment is also included*/
	char someCharacter = 'z';

	short someSignedShortInteger = -2;
	unsigned int someUnsignedShortInteger = 246;

	int someSignedInteger = -8046;
	unsigned int someUnsignedInteger = 22850;

	long someSignedLongInteger = -8908164680;
	unsigned long someUnsignedLongInteger = 4001680250;

	float someFloatingPointNo = 3.14159;

	double someDoublePrecisionFloatingNo = 6.022e23;


	/* example printf( ) formats for each type
	The format string contains text mixed with % formatting specifiers which mark things to be inserted in the output.
	Every % specifier must have a matching argument of the correct type after the format string.
	See the below for formatting specifiers and flags*/

	printf("Single character and its number code in decimal and hex: %c %d %x", someCharacter, someCharacter, someCharacter);
	printf("\nSigned short integer and long: %hd %d %ld", someSignedShortInteger, someSignedInteger, someSignedLongInteger);
	printf("\nUnsigned short integer and long: %hdu %du %ldu", someUnsignedShortInteger, someUnsignedInteger, someUnsignedLongInteger);
	printf("\nDouble and single precision floating point: %f %.2f", someDoublePrecisionFloatingNo, someFloatingPointNo);

	/* printf( ) specifiers
	d or i	Signed decimal integer	392
	o	Unsigned octal
	x	Unsigned hexadecimal integer
	X	same as x with uppercase characters
	f	Decimal floating point - double precision (float is always promoted to double)
	e	Scientific notation with e marking magnitude (e.g. 6.022e+23)
	E	same as e with upper case E's
	g	Use the shortest representation: %e or %f	392.65
	G	same as g with upper case characters
	a	Hexadecimal floating point, lowercase
	A	same as a with upper case characters
	c	Character
	s	String of characters
	p	Pointer address
	%	Putting %% in the print string will write a single % to the stream

	Flags
	h	short integer - use before specifier
	l	long integer - use before specifier
	u	Unsigned decimal integer - use after specifier
	.(number)	number of decimal places to print (floats) - use before specifier
	(number)	number of total characters to print (padded by spaces) - use before specifier
	*/

	return EXIT_SUCCESS;
}