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 > Beginners Lab Assignments Code Examples

how to parse the command line arguments

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* how to parse the command line arguments This program is an example of how to parse the command line arguments. It sets up all the global variables for a real program, it just doesn't have any body. */ #include <stdio.h> #include <stdlib.h> int verbose = 0; /* verbose mode (default = false) */ char *out_file = "print.out"; /* output filename */ char *program_name; /* name of the program (for errors) */ int line_max = 66; /* number of lines per page */ /* * do_file -- dummy routine to handle a file * * * * Parameter * * name -- name of the file to print * */ void do_file(char *name) { printf("Verbose %d Lines %d Input %s Output %s\n", verbose, line_max, name, out_file); } /* * usage -- tell the user how to use this program and * * exit * */ void usage(void) { fprintf(stderr,"Usage is %s [options] [file-list]\n", program_name); fprintf(stderr,"Options\n"); fprintf(stderr," -v verbose\n"); fprintf(stderr," -l<number> Number of lines\n"); fprintf(stderr," -o<name> Set output filename\n"); exit (8); } int main(int argc, char *argv[]) { /* save the program name for future use */ program_name = argv[0]; /* * loop for each option. * Stop if we run out of arguments * or we get an argument without a dash. */ while ((argc > 1) && (argv[1][0] == '-')) { /* argv[1][1] is the actual option character. */ switch (argv[1][1]) { /* -v verbose */ case 'v': verbose = 1; break; /* * -o<name> output file * [0] is the dash * [1] is the "o" * [2] starts the name */ case 'o': out_file = &argv[1][2]; break; /* * -l<number> set max number of lines */ case 'l': line_max = atoi(&argv[1][2]); break; default: fprintf(stderr,"Bad option %s\n", argv[1]); usage(); } /* * move the argument list up one * move the count down one */ ++argv; --argc; } /* * At this point all the options have been processed. * Check to see if we have no files in the list * and if so, we need to process just standard in. */ if (argc == 1) { do_file("print.in"); } else { while (argc > 1) { do_file(argv[1]); ++argv; --argc; } } return (0); }

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,

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

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.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

#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:

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.

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.

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 string to integer. Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

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.

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.

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.

How to print Odd Numbers from 1 to N using loop in C programming. Input upper limit to print odd number from user. Store it in some variable N. Run a loop from 1 to N, increment

In this c program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 10 in this program. The user is asked to enter elements of the...

C program to read elements in a matrix and find transpose of the given matrix. How to find transpose of a given matrix in C. Input elements in matrix A from user. Declare...

C Programming code implements two Stacks using a Single Array & Check for Overflow & Underflow. A Stack is a linear data structure in which a data item is inserted and deleted...

Check Armstrong Number of n digits. In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. Example 407 = 4*4*4 + 0*0*0 + 7*7*7

This c program prompts user for entering any integer, finds the Factorial of input number & displays the output on screen. Use a recursive user defined function to perform the task. We

If a number is exactly divisible by 2 then its an even number else it is an odd number. In this we have shared 2 ways to check whether the input number is even or odd. Using Modulus