Javatpoint Logo
Javatpoint Logo

Add 2 Strings in C

In C programming language, a string is an array of characters terminated by a null character ('\0'). It is a common data type used to represent textual data, such as words, sentences, and paragraphs. Strings in C are very important because they are used extensively in writing programs that deal with text. A string is a sequence of characters stored in a contiguous block of memory. The size of the block depends on the number of characters in the string plus one for the null character. The null character serves as a marker that indicates the end of the string. It has a value of zero and is represented by the '\0' character.

C provides several functions to manipulate strings, such as strcpy(), strcat(), strlen(), strcmp(), and strstr(). These functions operate on strings and allow programmers to perform various operations, such as copying, concatenating, finding the length, comparing, and searching for specific characters or substrings within a string. Strings in C are implemented as character arrays, which means that they are mutable. This means that the characters in a string can be changed at any time. However, it is important to be careful when modifying strings to avoid overwriting other data stored in memory. In addition, strings should be null-terminated to ensure that the program knows when the string ends. When declaring a string in C, the programmer can choose to initialize it with a value or leave it uninitialized. If the programmer chooses to initialize it, they can do so by assigning a value to the first element of the array, or by using a string literal. If they choose to leave it uninitialized, the string will contain garbage values, which could lead to undefined behavior when accessed.

C also allows the programmer to use pointers to manipulate strings. Pointers are variables that store the memory address of another variable. When used with strings, pointers can be used to pass strings to functions, manipulate strings, and return strings from functions. In conclusion, strings in C are a fundamental data type used to represent textual data. They are implemented as character arrays terminated by a null character, and can be manipulated using a variety of functions. It is important to be careful when working with strings to avoid overwriting other data in memory, and to ensure that strings are null-terminated. Pointers can also be used to manipulate strings, and are a powerful tool for working with strings in C.

C Code


Enter string 1: Hello
Enter string 2: world
Result: Helloworld

The output of the above code will depend on the input provided by the user. When the program is run, it will prompt the user to enter two strings. Let's say the user enters "Hello" for the first string and "world" for the second string. Then, the program will concatenate the two strings and store the result in the result array. Finally, it will print the concatenated string using the printf function. Note that there is no space between "Hello" and "world" in the output, because we did not include a space character in the result array. If we wanted to add a space between the two strings, we could modify the code to include a space character in the result array before concatenating the two strings.


In this code, we first declare three character arrays: str1, str2, and result. We then prompt the user to enter the first and second strings using the scanf function. Next, we use the strcat function to concatenate the two strings into the result array. The strcat function appends the second string to the first string, so we call it twice to add str1 and str2 to result. Finally, we print the concatenated string using the printf function. Note that this code assumes that the two strings entered by the user do not exceed the size of the character arrays (str1 and str2). If the strings are longer than 100 characters, the program may encounter a buffer overflow. To avoid this, you can either increase the size of the arrays or use a dynamic memory allocation function like malloc.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA