Programming in C
Programming in C
switch statement
switch( expression ) { case constant1: statement ... break; case constant2: statement ... break;
... default:
statement ... break; }
Example: A simple calculator.
#include
int number; char type; char line[80];
int main( void ) {
while(1) {
printf( "Enter conversion and number: "); fgets(line, sizeof(line), stdin); sscanf( line, "%c", &type );
if ((type == 'q') || (type == 'Q')) break;
switch (type) { case 'o': case 'O': sscanf(line, "%c %o", &type, &number); break;
October1, 2014
case 'x': case 'X':
sscanf(line, "%c %x", &type, &number); break; case 'd': case 'D': sscanf(line, "%c %d", &type, &number); break; case '?': case 'h': case 'H': printf( "Letter Conversion:\n"); printf(" o Octal\n"); printf(" x Hexadecimal\n"); printf(" d Decimal\n"); printf(" q Quit program\n");
continue;
default:
printf(" Type ? or h for help\n");
continue;
}
/* End of switch */
printf("Result is %d\n", number);
}
/* End of while */
return 0; }
MISC:
1) How to convert a number to a string.
int sprintf ( char *s, const char *format, ... ) sprintf takes three arguments.
The first has to be a char * variable, which means you can use a char array, but make sure it's big enough to hold the converted number.
The second argument is a string containing a format specifier, depending on the format of the number you want to convert.
The third argument is the number you want to convert into a string.
sprintf returns the number of characters in the string (not included the null character).
2) How to convert a string to a number:
double atof( const char *str) int atoi( const char *str )
They are in stdlib.h header.
3) How to generate random numbers in C:
#include rand();
Examples: ran.c, ran01.c, ran1.c
/* ran.c Shifted, scaled integers produced by 1 + rand() % 6 */
#include #include
int main() {
int i;
for ( i = 1; i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- macro programming in excel
- programming in excel
- programming in java pdf
- programming in matlab
- object oriented programming in matlab
- programming in basic
- functions in c programming language
- functions in c programming examples
- functions in c programming pdf
- database programming in java
- array in c programming language
- database programming in python