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 > Small Programs Code Examples

Another one

/* Another one */ #include <stdio.h> #include <string.h> #include <error.h> #include <ctype.h> #include <gd.h> #include <gdfontg.h> #include <gdfontl.h> static int red = 0; static int green = 0; static int blue = 0; void setupcolor(char *str); int main(int argc, char *argv[]) { FILE *pngout = {0}; gdImagePtr img; int fgcol, bgcol; char *str = NULL; char *fname = NULL; if(argc != 5) { fprintf(stderr, "Usage: ./imstr \"STRING\" FFFFFF 000000 image.png\n"); return 1; } else { str = argv[1]; img = gdImageCreate((gdFontGiant->w * strlen(str)) + 2, gdFontGiant->h + 2); setupcolor(argv[2]); bgcol = gdImageColorAllocate(img, red, green, blue); setupcolor(argv[3]); fgcol = gdImageColorAllocate(img, red, green, blue); fname = argv[4]; } gdImageString(img, gdFontGiant, gdImageSX(img) / 2 - (strlen(str) * gdFontGiant->w / 2), gdImageSY(img) / 2 - gdFontGiant->h / 2, str, fgcol); if((pngout = fopen(fname, "w")) == NULL) error(1, 0, "Error - fopen(): %s", fname); else { gdImagePng(img, pngout); fclose(pngout); } gdImageDestroy(img); return 0; } void setupcolor(char *str) { char r[3] = {0}; char g[3] = {0}; char b[3] = {0}; char *ptr = NULL; if(strlen(str) != 6) error(1, 0, "Error - Invalid hex color length: %s", str); for(ptr = str; *ptr; ptr++) if(!isxdigit(*ptr)) error(1, 0, "Error - Non hexdigit: %s", str); else if(isupper(*ptr)) *ptr = tolower(*ptr); r[0] = str[0], g[0] = str[2], b[0] = str[4]; r[1] = str[1], g[1] = str[3], b[1] = str[5]; r[2] = '\0', g[2] = '\0', b[2] = '\0'; sscanf(r, "%X", &red); sscanf(g, "%X", &green); sscanf(b, "%X", &blue); return; }

Closes a file descriptor, fildes. This frees the file descriptor to be returned by future open() calls and other calls that create file descriptors. The fildes argument must represent a hierarchical file system (HFS) file. When the last open file descriptor for a file is closed, the file itself is closed. If the file's link count is 0 at that time, its space is freed and the file becomes inaccessible. When the last open file descriptor for a pipe or FIFO file is closed, any data remaining in the pipe or FIFO file is discarded. close() unlocks (removes) all outstanding record locks that a process has on the associated file. Behavior for sockets: close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.

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.

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,

Get string length. Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

Checks if parameter c is an uppercase alphabetic letter. The isupper() function checks whether a character is an uppercase alphabet (A-Z) or not. Function isupper() takes a single argument in the form of an integer and returns a value of type int. Even though, isupper() takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII for the check. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z. Other locales may consider a different selection of characters as uppercase characters, but never characters that returns true for iscntrl, isdigit, ispunct or isspace.

Checks whether c is a hexdecimal digit character. This function is used to check if the character is hexadecimal or not, the Function returns 1 if the character is hexadecimal and 0 if the character is not hexadecimal. a hexadecimal character, isxdigit() returns a non-zero integer. a non-hexadecimal character, isxdigit() returns 0.

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.

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.

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

Close file. Closes the file associated with the stream and disassociates it. All internal buffers associated with the stream are disassociated from it and flushed: the content of any unwritten output buffer is written and the content of any unread input buffer is discarded. Even if the call fails, the stream passed as parameter will no longer be associated with the file nor its buffers.

The open() function shall establish the connection between a file and a file descriptor. It shall create an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file. The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor shall not share it with any other process in the system. The FD_CLOEXEC file descriptor flag associated with the new file descriptor shall be cleared.

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

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.

Convert uppercase letter to lowercase. Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z, which translate respectively to: a b c d e f g h i j k l m n o p q r s t u v w x y z.

Read formatted data from string. Reads data from s and stores them according to parameter format into the locations given by the additional arguments, as if scanf was used, but reading from s instead of the standard input (stdin). The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

Open file. Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE pointer returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter. The returned stream is fully buffered by default if it is known to not refer to an interactive device (see setbuf). The returned pointer can be disassociated from the file by calling fclose or freopen. All opened files are automatically closed on normal program termination.





Check for Armstrong Number in C language. Armstrong Number: If sum of cubes of digits of number equal to same given number then the number is called as Armstrong Number.

Compare three integer variables is one of the simplest c program you can write at ease. In this program, you can either take input from user using scanf() function or statically define

Program Trim leading white space characters from a string. Remove "Leading Whitespace" characters from the string. Find Last index of whitespace character. White space character




Check if string is a 'Palindrome' without using the "Built-In function". "Take a string" as input & store it in the array 'string[]'. Then store the same string into the another array 'reverse_s'