Monday, February 23, 2009

Placeholders

Placeholders are used to determine when what type of values are going to be input or displayed, depending on what type of functions that you use

Input Placeholders
scanf() function requires input placeholders to allow data to be transferred to the specific variable depending on which placeholder you will use.

Data type..............Placeholder
int.......................... %d
char....................... %c
double................... %lf
float....................... %f
string.................... %s

Note: for data that contains spaces in character arrays(strings) we use the %[^\n] symbol instead of %s because %s will only store strings without spaces.


Output Placeholders
Output placeholders are used to display data onto the screen. printf() function uses the output placeholder when necessary. The only difference with the input placeholders is the double data type placeholder for input function is %lf, while the double data type placeholder for output function is %f.

Data type..............Placeholder
int.......................... %d
char....................... %c
double................... %f
float....................... %f
string.................... %s (can also be used for strings with spaces)

Examples:

#include< stdio.h>
int main()
{
int number1 = 10;
int number2 = 20;
int number3 = 30;

printf("number1 contains %d \n", number1);
printf("number2 contains %d \n", number2);
printf("number3 contains %d \n", number3);

return 0;
}

Output:
number1 contains 10
number2 contains 20
number3 contains 30

#include< stdio.h>
int main()
{
int number1 = 10;
double number2 = 20.532;
double number3 = 30.88812;

printf("number1 contains %d \n", number1);
printf("number2 contains %f \n", number2);
printf("number3 contains %f \n", number3);

return 0;
}

Output:
number1 contains 10
number2 contains 20.532000
number3 contains 30.888120

#include< stdio.h>
int main()
{

char letter = 'A';
char letter2 = 'B';
char letters[] = "ABC";

printf("first letter = %c\n", letter);//character
printf("second letter = %c\n", letter2);//character
printf("letters contain %s", letters);//string
return 0;
}

Output:
first letter = A
second letter = B
letters contain ABC
Examples of inputs that uses placeholders:

#include< stdio.h>
int main()
{
int integernumber;

printf("Integernumber = ");
scanf("%d", &integernumber);
printf("\n variable integer contains: %d", integernumber);
}

Output:
Integernumber = 200
variable integer contains: 200


#include< stdio.h>
int main()
{
double lengthOfWall;

printf("Enter length of wall:");
//note the difference btw %lf for scanf
//and printf, %f for double
scanf("%lf", &lengthOfWall);
printf("\n lengthOfWall is %f", lengthOfWall);
}

Output:
Enter length of wall:35.886
lengthOfWall is 35.886000


#include< stdio.h>
int main()
{
char letter;

printf("enter a letter: ");
scanf("%c", &letter);
printf("\n letter is %c", letter);
}

Output:
enter a letter: E
letter is E

#include< stdio.h>
int main()
{
char fullname[21];

printf("enter full name: ");
//note: %s not used because fullname might
//be of a name with spaces.%[^\n] is used instead.
scanf("%[^\n]", fullname);

//for display, always use %s for string
//eventhough there are spaces
printf("welcome %s", fullname);
}


Output:
enter full name: Kelvin Chan
welcome Kelvin Chan

Note: that the ampersand symbol , & is required in front of the variable name in the input list of scanf() function, on int, double, float and char data types. Strings does not require &.

No comments: