Tuesday, February 24, 2009

Output Functions
There are 3 basic functions that produces output on the screen:
  • putchar()
  • puts()
  • printf()
putchar()
Function Syntax:
putchar(char variable);

The putchar() function displays a character of a character type variable. Example:

#include< stdio.h>
int main()
{
char letter = 'A'; //Initialize a char type variable with the value of 'A'.

putchar(letter); //display variable letter on screen.
return 0;
}

Ouput:
A

puts()
Function Syntax:
puts(char string[]); OR
puts("format string");

The puts() function displays a character array or a string.
Example:

#include< stdio.h>
int main()
{
//Initialize a char array (String)
char message[] = "hi! this is a character array!";

//display the string variable on screen.
puts(message);
//displays anything you put between the double quotes""
puts("Hello World!");
return 0;
}


Output:
hi! this is a character array!
Hello World!

printf()
Function syntax:
printf("format string"); OR
printf("format string", output list);

printf() function display anything you want it to display.
Format string is anything that you entered is a format string. Output list is the list of variables that its data will be displayed, but you need to specify the appropriate placeholders

Example:

#include< stdio.h>
int main()
{
//declaring and initializing variable studentNumber.
int studentNumber = 50;

printf("Hello World!\n");//Display hello world
//Displays the data of the variable studentNumber with placeholder(%d)
printf("The number of students is %d", studentNumber);

return 0;
}

Output:
Hello World!
The number of students is 50.

We will be using a lot of printf() functions because it can be used to display text and variables together.

No comments: