C Library Function - putc()
Description
C library function *int putc(int char, FILE stream) Converts the string pointed to by the argument char Write the specified character (an unsigned char) to the specified stream stream and advance the position indicator.
Declaration
The following is the declaration of the putc() function.
int putc(int char, FILE *stream)
Parameters
- char -- This is the character to be written. The character is passed as its corresponding int value.
- stream -- This is a pointer to a FILE object that identifies the stream to which the character is to be written.
Return Value
The function returns the character written, cast from unsigned char to int, or EOF if an error occurs.
Example
The following example demonstrates the use of the putc() function.
#include
int main ()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "w");
for( ch = 33 ; ch <= 100; ch++ )
{
putc(ch, fp);
}
fclose(fp);
return(0);
}
Let's compile and run the above program; this will create a file in the current directory file.txt, with the following content:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
Now let us use the following program to view the content of the file above:
#include
int main ()
{
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
C Standard Library -