C - Basic Input Output Library Function

 Basic Input Output Library Function:

A function is a self-contained block of code that performs a particular task.

1. strcpy():

To copies one string into another which is available in string.h header file.

Example:

main ()

{

char name[20], name1[10]="BCA II ";

strcpy(name,"C program");

puts(name);

strcpy(name,name1);

puts(name);

}

Output ->        C program  BCA II

 2. strcat():

To appends one string to another which is available in string.h header file.

Example:

main()

{

char name[20], name1[10]="BCA II ";

strcpy(name,"C program ");

strcat(name,name1);

puts(name);

}

Output->        C program  BCA II

3. abs():

To returns the absolute value of an integer which is available in math.h header file.

Example:

main()

{

int a= -3;

printf(" Absolute value of %d is %d ",a, abs(a));

}

Output ->        Absolute value of -3 is 3

4. getchar(): To read a character from the keyboard which is available in stdio.h header file.

Example:

main()

{

char ch;

printf("Enter a character ");

ch=getchar( );

printf("\n Entered character is %c ",ch);

}

Output ->

Enter a character

A Entered character is A

 5. strlen(): To calculate the length of a string, which is available in string.h header file.

Example:

main()

{

char name1[10]="BCA II ";

printf(" Length of name1 is %d ",strlen(name1));

}

Output -> Length of name1 is 6

 6. toupper(): To convert a character into uppercase letter which is available in ctype.h header file.

Example:

main()

{

char ch;

printf("Enter a character ");

ch=getchar( );

printf("\n Uppercase character is %c ",toupper(ch));

}

Output ->

Enter a character c

Uppercase character is C

 7. printf(): To print the values contained in a variable on the screen.

Example:

main()

{

char name1[10]="BCA";

int number=12;

printf(" name is %s number is %d ",name1,number);

}

Output -> name is BCA number is 12

8. pow(): To calculate power, which is available in math.h header file.

Example:

main()

{

int x,n;

printf("Enter a number");

scanf("%d",&x);

printf("Enter power ");

scanf("%d",&n);

printf (" Power is %d",pow(x,n));

}

Output ->

Enter a number 5

Enter power 2

Power is 25

 

9. exit(): To terminate the execution of program.

Example:

main()

{

int i;  for(i=0;i<1000;i++)

{

printf(" Num is %d",i);  if(i==3)

exit();

}

}

Output ->

Num is 0

Num is 1

Num is 2

Num is 3

 

10. clrscr(): To clears the screen.

Example:

main()

{

char name1[10]="BCA";

int number=12;

printf(" name is %s number is %d ",name1,number);

clrscr();

printf(" All clear !");

}

Output -> All clear

11. getch():

getch() function reads a single character that you types without echoing it on the screen and without waiting for the enter key to be hit. The getch() takes the following form:

variable-name = getch();

variable-name is a valid C name that has been declared as char type. When this statement is encountered, the computer does not wait a key is pressed and then assigns this character as a value to getch function.


12. getchar():

It reads a single character that you type with echoing it on the screen and waiting for the enter key to be hit. The getchar() takes the following form:

variable-name = getchar();

variable-name is a valid C name that has been declared as char type.  When this statement is encountered, the computer waits until enter key is pressed and then assigns this character as a value to getchar function.

 

13. putchar() :

Writing a Character there is a function putchar for writing characters one at a time to the terminal. It takes the form as shown below:

putchar(variable-name);

where variable-name is a type char variable containing a character. This statement is displaying the character contained in the variable-name at the terminal. For example, the statements

answer = 'Y';  putchar(answer);

will display the character Y on the screen. The statement

putchar('\n');

would cause the cursor on the screen to move to the beginning of the next line.

 

14. scanf() :

To read integer, float etc datatype from the keyboard in C by using the scanf function. The general form of scanf is

scanf("control string",&arg1,&arg2,....&.argn);

The control string specifies the field format in which the data is to be entered and the arguments arg1, arg2, ...argn specify the address of locations where the data is stored. Control string and arguments are separated by commas.

Example:

int age, float sal;

scanf("%d%f", &age, &sal);

Comments

Popular posts from this blog

Courses After 12th Science

MS Office Versions List

Computer - Network Topologies