Friday, 26 October 2012

Inbuilt Functions in C language

getchar()

The C library function int getchar(void) gets a character (an unsigned char) from stdin. This is equivalent to getc with stdin as its argument.

Syntax: int getchar(int)
This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.

Example:
#include <stdio.h>
void main ()
{
   char c;

   printf("Enter character: ");
   c = getchar();

   printf("Character entered: ");
   putchar(c);
}
Output:
Enter character: a
Character entered: a

putchar()

The C library function int putchar(int char) writes a character (an unsigned char) specified by the argument char to stdout.

Syntax : int putchar(int char)
This function returns the character written as an unsigned char cast to an int or EOF on error.

Example
#include <stdio.h>
void main ()
{
   char ch;
   ch="A";
   putchar(ch);
}
Output:
A 

isalpha()


The C library function void isalpha(int c) checks if the passed character is alphabetic.

Syntax : int isalpha(int c);
This function returns nonzero value if c is a alphabet, else 0

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
    if(isalpha('a'))
        printf("It is an alphabet");
    else
        printf("It is not an alphabet");
    getch();
}
Output:
It is an alphabet

 
isdigit()

The C library function void isdigit(int c) checks if the passed character is a decimal digit character.
Decimal digits are 0 to 9

Syntax : int isdigit(int c);
This function returns nonzero value if c is a digit, else 0

Example
#include<stdio.h>
#include<ctype.h>
void main()
{
    if(isdigit('1'))
        printf("It is digit");
    else
        printf("It is not digit");
    getch();
}
Output:
It is digit 

 
isalnum()

The C library function void isalnum(int c) checks if the passed character is alphanumeric.

Syntax : int isalnum(int c);
This function returns nonzero value if c is a digit or a letter, else 0

Example:
#include<stdio.h>
#include<ctype.h>
void main()
{
    if(isalnum('3'))
        printf("It is alphanumeric");
    else
        printf("It is not alphanumeric");
    getch();
}
Output:
It is alphanumeric

 
islower()

The C library function int islower(int c) check whether the passed character is lowercase letter.

Syntax : int islower(int c);
This function returns a non zero value(true) if c is a lowercase alphabetic letter else, zero (false)

Example:
#include<stdio.h>
#include<ctype.h>
void main()
{
    if(islower('a'))
        printf("Entered character is in lower case");
    else
        printf("Entered character is not in lower case");
    getch();
}
output:
Entered character is in lower case
 
isupper()

The C library function int isupper(int c) check whether the passed character is uppercase letter.

Syntax : int isupper(int c);
This function returns a non zero value(true) if c is uppercase alphabetic letter else, zero (false)

Example:
#include<stdio.h>
#include<ctype.h>
void main()
{
    if(isupper('A'))
        printf("Entered character is in upper case");
    else
        printf("Entered character is not in upper case");
    getch();
}
Output:
Entered character is in upper case

 
ispunct()

The C library function int ispunct(int c) check whether the passed character is punctuation character.
A punctuation character is any graphic character (as in isgraph) that is not alphanumeric (as in isalnum).

Syntax : int ispunct(int c);
This function returns a non zero value(true) if c is a punctuation character else, zero (false)

Example:

#include <stdio.h>
#include <ctype.h>

void main()
{
   int var1 = 't';
   int var2 = '1';
   int var3 = '/';
    if( ispunct(var1) )
   {
       printf("var1 = |%c| is a punctuation character\n", var1 );
   }
   else
   {
       printf("var1 = |%c| is not a punctuation character\n", var1 );
   }
   if( ispunct(var2) )
   {
       printf("var2 = |%c| is a punctuation character\n", var2 );
   }
   else
   {
       printf("var2 = |%c| is not a punctuation character\n", var2 );
   }
   if( ispunct(var3) )
   {
       printf("var3 = |%c| is a punctuation character\n", var3 );
   }
   else
   {
       printf("var3 = |%c| is not a punctuation character\n", var3 );
   }
}   
Output:
var1 = |t| is not a punctuation character
var2 = |1| is not a punctuation character
var3 = |/| is a punctuation character

 
isspace()

The C library function int isspace(int c) check whether the passed character is white-space.
Standard white-space characters are:
' '     space (SPC)
'\t'    horizontal tab (TAB)
'\n'    newline (LF)
'\v'    vertical tab (VT)
'\f'    feed (FF)
'\r'    carriage return (CR)

Syntax : int isspace(int c);
This function returns a non zero value(true) if c is a white-space character else, zero (false)

Example
#include <stdio.h>
#include <ctype.h>
void main()
{
   int var1 = 't';
   int var2 = '1';
   int var3 = ' ';

   if( isspace(var1) )
   {
       printf("var1 = |%c| is a white-space character\n", var1 );
   }
   else
   {
       printf("var1 = |%c| is not a white-space character\n", var1 );
   }
   if( isspace(var2) )
   {
       printf("var2 = |%c| is a white-space character\n", var2 );
   }
   else
   {
       printf("var2 = |%c| is not a white-space character\n", var2 );
   }
   if( isspace(var3) )
   {
       printf("var3 = |%c| is a white-space character\n", var3 );
   }
   else
   {
       printf("var3 = |%c| is not a white-space character\n", var3 );
   }
}
Output:
var1 = |t| is not a white-space character
var2 = |1| is not a white-space character
var3 = | | is a white-space character


No comments:

Post a Comment