khuang8493
6/11/2018 - 11:35 PM

Input Output Strings using placeholders

To input a string and then print it out using placeholders to print value stored in variables.

int main(void)
{
	string s = get_string("Name: ");
	printf("hello, %s\n", s); //the "%s" is called a place holder. For print value stored in variable.
	
	//example of using a placeholder to print an integer stored in variable.
	
	printf("I have %i pets.\n", n);
}

%d print out digit

%i print out integer

%f print out float

%c print out character

%s print out string

%2d print out digit using at least 2 characters

%.2d specifies precision


Example:

printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);

Output:

Preceding with blanks:       1977
Preceding with zeros: 0000001977