Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Small Programs Code Examples

Print several types of alphabets

/* Print several types of alphabets */ #include <stdio.h> #include <ctype.h> #include <getopt.h> #define PACKAGE "alphabet" #define VERSION "0.0.5" /* // `0' westeren, `1' phonetic, `2' greek `3' hebrew // `4' phoenician `5' arab */ static int type = 0; /* all output upper case */ static int upper = 0; /* recursive, output until killed */ static int recur = 0; /* phonetic alphabet */ const char *palpha[]= { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliet", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whisky", "x-ray", "yankee", "zulu" }; /* lower case greek alphabet */ const char *galpha[]= { "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega" }; /* lower case hebrew alphabet */ const char *halpha[]= { "tet", "chet", "zayin", "vav", "he", "dalet", "gimel", "bet", "alef", "samech", "nun", "nun", "mem", "mem", "lamed", "khaf", "kaf", "yod", "tav", "shin", "resh", "qof", "tzade", "tzade", "fe", "pe", "ayin" }; /* lower case phoenician alphabet */ const char *nalpha[]= { "aleph", "beth", "gimel", "daleth", "he", "waw", "heth", "yodh", "kaph", "lamedh", "mem", "nun", "ayin", "pe", "qoph", "resh", "sin", "taw", "waw", "samekh", "zayin" }; /* lower case arab alphabet */ const char *aalpha[]= { "xaa", "haa", "jiim", "th!aa", "taa", "baa", "alif", "saad", "shiin", "siin", "zaay", "raa", "thaal", "daal", "qaaf", "faa", "ghayn", "ayn", "th:aa", "taa", "daad", "yaa", "waaw", "haa", "nuun", "miim", "laam", "kaaf" }; /* status epilepticus, and die with `exval' */ void print_help(int exval); /* print the specified alphabet */ void print_alphabet(void); int main(int argc, char *argv[]) { int opt = 0; /* option parser */ while((opt = getopt(argc, argv, "hvpgwiaru")) != -1) { switch(opt) { case 'h': /* print help and exit */ print_help(0); case 'v': /* print version and exit */ exit(0); case 'p': /* print the phonetic alphabet */ type = 1; break; case 'g': /* print the lower case greek alphabet */ type = 2; break; case 'w': /* print the lower case hebrew alphabet */ type = 3; break; case 'i': /* print the lower case phoenician alphabet */ type = 4; break; case 'a': /* print the lower case arab alphabet */ type = 5; break; case 'r': /* print the alphabet repeatedly until killed */ recur = 1; break; case 'u': /* print all output upper case */ upper = 1; break; case '?': /* unkown option */ fprintf(stderr, "%s: Error - no such option `%c'\n\n", PACKAGE, optopt); print_help(1); break; } /* switch */ } /* while */ /* prints the selected alphabet */ print_alphabet(); return 0; } /* prints the selected alphabet */ void print_alphabet(void) { const char *ptr = NULL; int i = 0; /* western alphabet */ if(type == 0) { if(upper == 0) for(i = 97; i <= 122; i++) printf("%c\n", i); else for(i = 65; i <= 90; i++) printf("%c\n", i); /* phonetic alphabet */ } else if(type == 1) { if(upper == 0) { for(i = 0; i < 26; i++) printf("%s\n", palpha[i]); } else { for(i = 0; i < 26; i++) { ptr = palpha[i]; while(*ptr) printf("%c", toupper(*ptr++)); printf("\n"); } /* for */ } /* else */ /* greek alphabet */ } else if(type == 2) { if(upper == 0) { for(i = 0; i < 24; i++) printf("%s\n", galpha[i]); } else { for(i = 0; i < 24; i++) { ptr = galpha[i]; while(*ptr) printf("%c", toupper(*ptr++)); printf("\n"); } /* for */ } /* else */ } else if(type == 3) { if(upper == 0) { for(i = 0; i < 27; i++) printf("%s\n", halpha[i]); } else { for(i = 0; i < 27; i++) { ptr = halpha[i]; while(*ptr) printf("%c", toupper(*ptr++)); printf("\n"); } /* for */ } /* else */ } else if(type == 4) { if(upper == 0) { for(i = 0; i < 21; i++) printf("%s\n", nalpha[i]); } else { for(i = 0; i < 21; i++) { ptr = nalpha[i]; while(*ptr) printf("%c", toupper(*ptr++)); printf("\n"); } /* for */ } /* else */ } else if(type == 5) { if(upper == 0) { for(i = 0; i < 28; i++) printf("%s\n", aalpha[i]); } else { for(i = 0; i < 28; i++) { ptr = aalpha[i]; while(*ptr) printf("%c", toupper(*ptr++)); printf("\n"); } /* for */ } /* else */ } /* else if */ if(recur == 1) print_alphabet(); else return; } /* status epilepticus, and die with `exval' */ void print_help(int exval) { printf("%s,%s prints different alphabets\n", PACKAGE, VERSION); printf("Usage: %s [-h] [-v] [-p] [-g] [-w] [-i] [-a] [-r] [-u]\n\n", PACKAGE); printf(" Startup:\n"); printf(" -h print this help and exit\n"); printf(" -v print version and exit\n\n"); printf(" Alphabet:\n"); printf(" -p print the phonetic alphabet\n"); printf(" -g print the greek alphabet\n"); printf(" -w print the hebrew alphabet\n"); printf(" -i print the phoenician alphabet\n"); printf(" -a print the arab alphabet\n\n"); printf(" Output:\n"); printf(" -r print the alphabet repeatedly until killed\n"); printf(" -u print all output in upper case\n\n"); printf(" Per default the Westeren alphabet is printed\n"); exit(exval); }

The getopt() function is a builtin function in C and is used to parse command line arguments. The getopt() function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines. The parameters argc and argv are the argument count and argument array as passed to main() (see exec() ). The argument optstring is a string of recognized option characters; if a character is followed by a colon, the option takes an argument. All option characters allowed by Utility Syntax Guideline 3 are allowed in optstring. The implementation may accept other characters as an extension.

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.

Convert lowercase letter to uppercase. Converts c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, a lowercase letter is any of: a b c d e f g h i j k l m n o p q r s t u v w x y z, which translate respectively to: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

Write formatted data to stream. Writes the C string pointed by format to the stream. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. After the format parameter, the function expects at least as many additional arguments as specified by format.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.


C language encrypting and decrypting files. So as to change enter file path. Enter password. Open a file to store decoded data. Until end of file to be encrypted read each...

First, create a file to sort, write unsorted data to disk and now, sort the file. Sorting disk file. A Quicksort for files. First read in record i and j and then write them back in opposite slots.







C Language program Find 'LCM' of a Number using Recursion. The following C Program to use recursion, finds the 'LCM'. An 'LCM' is the 'Lowest Common Multiple' of any 2 numbers.

C program converts a binary tree into a singly Linked List by Breadth first search algorithm. We use this algorithm for Traversing Level by Level. Structure type to point to the nodes of