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

Frequency Based Histogram

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
/* Frequency Based Histogram Histogram of Frequency of Different Characters in Input */ #include<stdio.h> #define TNOCHAR 128 /* Total Number of characters is 128: 0 - 127 */ int main(void) { int c,i,j; int character[TNOCHAR]; for(i=0;i<TNOCHAR;++i) character[i] = 0; while((c=getchar())!=EOF) ++character[c]; for(i=0;i<TNOCHAR;++i) { putchar(i); for(j=0;j<character[i];++j) putchar('*'); putchar(' '); } return 0; }

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.

Get character from stdin. Returns the next character from the standard input (stdin). It is equivalent to calling getc with stdin as argument. A getchar() function is a non-standard function whose meaning is already defined in the stdin.h header file to accept a single input from the user. In other words, it is the C library function that gets a single character (unsigned char) from the stdin. However, the getchar() function is similar to the getc() function, but there is a small difference between the getchar() and getc() function of the C programming language.

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.

Write character to stdout. Writes a character to the standard output (stdout). It is equivalent to calling putc with stdout as second argument. putchar() function is a file handling function in C programming language which is used to write a character on standard output/screen. The putchar() function is used for printing character to a screen at current cursor location. It is unformatted character output functions. It is defined in header file stdio.h.

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:

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 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.


Insert line in the Linked List. Create a new node. Save data into new node. First, we handle the case where 'data' should be the first element. Get soundex code for a string.



Str - str represents the array, in which string is stored. Fpw - FILE pointer to the output file, in which record needs to be written and opening the file FILEW.TXT in "w" mode for





C Program implements 'Cyclesort'. Cycle sort is an in-place, unstable 'Sorting Algorithm', a comparison sort that is theoretically optimal in terms of the total number of writes to the


C language program take the number of days as input. For the number of years, divide the input by 365 and obtain its quotient. For the number of weeks, divide the input by 365 and