Input Functions In C use in programs scanf() getch() getche() gets()
Input Functions In C #include< stdio.h > #include < conio.h > i nt main() { i nt a,b,c ; printf(“Enter the first number:”); scanf (“% d”,&a ); printf(“Enter the second number:”); scanf (“% d”,&b ); c= a+b ; printf(“the sum is % d”,c ); getch(); } s canf Always after printf statement
Input Functions In C #include< stdio.h > #include < conio.h > int main() { char ch; printf(“\n Enter a single character :”); ch = getche (); printf(“the character you typed is % c”,ch ); getch(); } g etche() g et means it gets something from input device. c h means character and e means echoes(displays) the character to the screen User does not press enter key after typing the letter
Input Functions In C #include< stdio.h > #include < conio.h > int main() { char ch; printf(“\n Enter a single character :”); ch = getchar (); printf(“the character you typed is % c”,ch ); getch(); } getchar()
Input Functions In C #include< stdio.h > #include < conio.h > int main() { char name[20]; printf(“\n Enter your name :”); gets(name); printf(“ my name is % s”,name ); getch(); } g ets(name)
Important points Remember the following getch() is always placed at the end of program after closing { curly brackets. Never place getch() inside main x Other input functions are the part of main body of function means they are present inside curly brackets . The purpose of getche() and getchar() is same but you will not press enter key after typing the letter in getche()
Typecasting in C
Typecasting in C Method of converting variable of one to another data type during program execution. There are two types of typecasting in C Implicit Typecasting and Explicit Typecasting
Typecasting in C Implicit Typecasting and Explicit Typecasting Implicit typecasting is performed automatically by compiler without programmer intervention. Example
Compiler converts sum of two float numbers into integer #include< stdio.h > #include< conio.h > i nt main() { float value1=2.5; float value2=5.3; int result; result =value1+value2; printf(“Result :% d”.result ); } Implicit Typecasting
Explicit Typecasting Explicit Typecasting is performed by programmer. The programmer explicitly define data types in parenthesis before variable or expression
Compiler converts division of two int numbers into float #include< stdio.h > #include< conio.h > i nt main() { int value1=30; int value2=7; float result; result =(float)value1/value2; printf(“Result :%f “,result); } Explicit Typecasting