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

String Functions - Lengths of strings

/* String Functions - Lengths of strings */ #include <stdio.h> void main() { char str1[40] = "C Programming Language"; char str2[40] = " | Happy Codings :)"; int count = 0; while (str1[count] != '\0') count++; printf("\nThe length of the string \"%s\" is %d characters.", str1, count); count = 0; while (str2[count] != '\0') count++; printf("\nThe length of the string \"%s\" is %d characters.\n", str2, count); }

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.

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.

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions. A function can also be referred as a method or a sub-routine or a procedure, etc.

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

C program code to input binary number from user and find twos complement of the binary number. Twos complement of N-bit number is defined as the complement with respect to





A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. C program to demonstrate working


C Programming code sample to compute difference between two dates. Enter first day, month, year. No. of days of first date from the Jan 1. Count for additional days due to...

Declare c recursive function to print natural numbers in a range. Lets give a meaningful name to function, "printNaturalNumbers()". Next we need to print Natural Numbers in...

C code example to input principle amount in some variable say principle and input time in some variable say time. Input rate in some variable say rate. Find simple interest using

Print "binary equivalent" of an integer. C code using recursion, finds the binary equivalent of a "Decimal Number". Decimal numbers are of base 10 while "Binary Numbers" are of base 2