String in C language for beginners
In C language string is
an one-dimensional array of characters terminated with a null character \0.
Even if
we do not place the null character at the end of a string constant. The C
compiler automatically places the '\0' at the end of the string when it
initializes the array. Look at the following example below.
Initializing a String: A
string can be initialized in different ways.
nitializing a String: A string can be initialized in different ways.
1. char str[] = "LAUNCHPADLWD";
2. char str[20] = "LAUNCHPADLWD";
3. char str[] = {'L','A','N','C','H','P','A','D','L','W','D'\0'};
4. char str[20] = {'L','A','N','C','H','P','A','D','L','W','D'\0'};
Example 3 printing name enter by user using gets() and puts()
#include <stdio.h> int main() { char ch[20]; printf(“Enter your name:”); gets(ch); puts(ch); return 0; } |
Output:
We should use gets in strings because if we use scanf and user enter a name rupesh ,it will print on screen rupesh but if user enters full name rupesh jha then only rupesh will print because scanf does not read character after space so , only rupesh will print , but if we use gets then rupesh jha full name will print because gets() read the character after space also.
0 Comments
Please do not enter any spam links in the comment box