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 > Conversions Code Examples

Convert Binary Code of a Number into its Equivalent Gray's Code using Recursion

/* Convert Binary Code of a Number into its Equivalent Gray's Code using Recursion This C program using recursion, evaluates the gray code equivalent of a binary number. A gray is also represented using 0s and 1s. The speciality of gray code is that only one bit is changed in 2 consecutive numbers, say 3 and 4. */ /* - C Program to Convert Binary Code of a Number into its Equivalent - Gray's Code using Recursion */ #include <stdio.h> int bintogray(int); int main () { int bin, gray; printf("Enter a binary number: "); scanf("%d", &bin); gray = bintogray(bin); printf("The gray code of %d is %d\n", bin, gray); return 0; } int bintogray(int bin) { int x, y, result = 0, i = 0; if (!bin) { return 0; } else { x = bin % 10; bin = bin / 10; y = bin % 10; if ((x && !y) || (!x && y)) { return (1 + 10 * bintogray(bin)); } else { return (10 * bintogray(bin)); } } }

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.

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,

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.

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

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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.


Program code to find out how many strings there are. Read string length. Check buffer is large enough and increase if necessary. Now get the position for the beginning of each...



C Programming print Fibonacci series up to n terms using loop. Fibonacci series is a series of numbers where the current number is the sum of previous two terms. For Example: 0, 1,

C programming converting example. Decimal number to Binary number, Octal number and HEX number converter. Enter any number to be converted. The Number has to be in the...



C program code example Initialize random number generator: how to use rand/srand. Generating random numbers. Print a number between 0 and 50, print a number between



The above approach to list Even Numbers is not optimal. It unnecessarily iterates for odd numbers which is a performance issue and to overcome this start the loop with first even...