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

Append substring to string: strncat

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Append substring to string: strncat */ #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; strcpy (str1,"happycodings.com"); strcpy (str2,"Superman"); strncat (str1, str2, 6); puts (str1); return 0; }
main() Function in C
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. Its first function or entry point of the program from where program start executed, program's execution starts from the main. So main is an important function in c , c++ programming language.
Syntax for main() Function in C
void main() { ......... // codes start from here ......... }
is a keyword in C language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value. In place of void we can also use int return type of main() function, at that time main() return integer type value.
is a name of function which is predefined function in C library. • An operating system always calls the main() function when a programmers or users execute their programming code. • It is responsible for starting and ends of the program. • It is a universally accepted keyword in programming language and cannot change its meaning and name. • A main() function is a user-defined function in C that means we can pass parameters to the main() function according to the requirement of a program. • A main() function is used to invoke the programming code at the run time, not at the compile time of a program. • A main() function is followed by opening and closing parenthesis brackets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* basic c program by main() function example */ #include <stdio.h> #include <conio.h> main() { printf (" It is a main() function "); int fun2(); // jump to void fun1() function printf ("\n Finally exit from the main() function. "); } void fun1() { printf (" It is a second function. "); printf (" Exit from the void fun1() function. "); } int fun2() { void fun1(); // jump to the int fun1() function printf (" It is a third function. "); printf (" Exit from the int fun2() function. "); return 0; }
strncat() Function in C
Append characters from string. Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied. Appends a number of characters from one string to another. The strncat() function copies up to num characters of the string addressed by the second pointer argument, source, to the location following the string addressed by the first pointer, destination. the first character of source is copied over the null character that terminates the string addressed by destination.
Syntax for strncat() Function in C
#include <string.h> char * strncat ( char * destination, const char * source, size_t num );
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character.
C string to be appended.
Maximum number of characters to be appended. size_t is an unsigned integral type. The function returns the value of its first argument, which points to the resulting string. The locations that strncat() reads from and writes to must not overlap. Unlike strcat(), strncat() has a third parameter, num, to limit the length of the string written. The strncat() function stops copying when it has copied num characters, or when it reaches a terminating null character in the source string, whichever occurs first. In either case, strncat() appends a null character to the concatenated string addressed by destination. The string's length thus increases by at most num characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* append characters from string by strncat() function code example */ #include <stdio.h> #include <string.h> int main () { char str1[50], str2[50]; //destination string strcpy(str1, "This is my initial string"); //source string strcpy(str2, ", add this"); //displaying destination string printf("String after concatenation: %s\n", strncat(str1, str2, 5)); // this should be same as return value of strncat() printf("Destination String str1: %s", str1); return 0; }
#include Directive in C
#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: • Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. • User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor.
Syntax for #include Directive in C
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. */ // C program to illustrate file inclusion // <> used to import system header file #include <stdio.h> // " " used to import user-defined file #include "process.h" // main function int main() { // add function defined in process.h add(10, 20); // mult function defined in process.h multiply(10, 20); // printf defined in stdio.h printf("Process completed"); return 0; }
strcpy() Function in C
Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
Syntax for strcpy() Function in C
#include <string.h> char * strcpy ( char * destination, const char * source );
Pointer to the destination array where the content is to be copied.
C string to be copied. Destination is returned. strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string.h header file. Function copies the string pointed by source (including the null character) to the destination.
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 32 33 34
/* copy one string to another by strcpy() function example */ #include<stdio.h> #include<string.h> int main() { char ch_arr1[20]; char ch_arr2[20]; printf("Enter first string (ch_arr_1): "); gets(ch_arr1); printf("Enter second string(ch_arr_1): "); gets(ch_arr2); printf("\nCopying first string into second... \n\n"); strcpy(ch_arr2, ch_arr1); // copy the contents of ch_arr1 to ch_arr2 printf("First string (ch_arr_1) = %s\n", ch_arr1); printf("Second string (ch_arr_2) = %s\n", ch_arr2); printf("\nCopying \"Greece\" string into ch_arr1 ... \n\n"); strcpy(ch_arr1, "Greece"); // copy Greece to ch_arr1 printf("\nCopying \"Slovenia\" string into ch_arr2 ... \n\n"); strcpy(ch_arr2, "Slovenia"); // copy Slovenia to ch_arr2 printf("First string (ch_arr_1) = %s\n", ch_arr1); printf("Second string (ch_arr_2) = %s\n", ch_arr2); // signal to operating system program ran fine return 0; }
puts() Function in C
Write string to stdout. Writes the C string pointed by str to the standard output (stdout) and appends a newline character ('\n'). The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream. Notice that puts not only differs from fputs in that it uses stdout as destination, but it also appends a newline character at the end automatically (which fputs does not). The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1.
Syntax for puts() Function in C
#include <stdio.h> int puts(const char *str)
C string to be printed. On success, a non-negative value is returned. On error, the function returns EOF and sets the error indicator (ferror).
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
/* write string to stdout by puts() function example */ #include <stdio.h> #include <string.h> int main() { char name[50]; printf("Enter your name "); gets(name); int age[50]; printf("Enter your age "); gets(age); char address[50]; printf("Enter your address "); gets(address); int pincode[50]; printf("Enter your pincode "); gets(pincode); printf("Entered Name is: "); puts(name); printf("Entered age is: "); puts(age); printf("Entered address is: "); puts(address); printf("Entered pincode is: "); puts(pincode); getch(); return 0; }

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented
Program sample to copy its input to its output replacing string of one or more blanks by a single blank. String will be terminated if you press ENTER. Press Ctrl-Z & then ENTER
Despite of easiness, the above code is messy and less readable. printf("The Triangle is not valid."); statement is unnecessarily repeated for various conditions. You can cut the extra
C Program coding input cost price and selling price of a product & check profit or loss. Logic to find profit or loss. In primary mathematics classes, you all have learned about profit and
"Gets()" is used to accept string with Spaces and we are "Passing accepted String" to the function. Inside function we have stored this string in pointer. Inside "While Loop" we are