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 > If Else and Switch Case Code Examples

Program to check vowel or consonant using switch...case statement

/* Program to check vowel or consonant using switch...case statement Observe the above program carefully. I have repeated printf("Vowel"); for many cases. I already explained in code sample to print number of days in months, if a switch...case contains same action for multiple cases. Then group all cases performing same action together. Arrange all similar cases together and remove break statement from all similar cases other than the last case. So you can group all cases to print vowel together as. switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("Vowel"); break; } In above code, if program control switches to any of the case in a, e, i, o, u, A, E, I, O, U it executes all below statements till break is found. Hence, for any of the case in a, e, i, o, u, A, E, I, O, U it prints "Vowel". */ #include <stdio.h> int main() { char ch; /* Input alphabet from user */ printf("Enter any character: "); scanf("%c", &ch); /* Switch ch value */ switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("Vowel"); break; default: printf("Consonant"); } return 0; }

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.

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.

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.

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,

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

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



Program to Concatenate two strings in single string. Concatenation of two strings is simple copying a string to another. To concatenate 2 strings str1 & str2, you will copy all characters

This C Program calculates the mean, variance and standard deviation. The formula which is used in this c program are mean = average of the numbers. var=(summation( ( Xj - average





Sum the integers from 1 to a user-specified number in C programming language. Declare the number of integers to be summed. The loop counter. Sum integers from 1 to count.


C Program to count number of digits without using loop. The second logic uses logarithms to count number of digits in a given integer. Total number of digit in an integer is equal to