Strings in C Programming Language
A string in C is a series of characters in a group that occupy contiguous memory. A group of characters (Alphabets, digits and special characters) is called as a string.
Example 1:
“Thank you for visiting www.learnconline.com” “www.learnconline.com is excellent site for beginners. This is example of strings in C.”
A string in C should always be enclosed with in double quotes (“).
If single quote is used then it is treated as character. Hence ‘A’ is different from “A”. Every string ends with a null character (‘\0’). Note that \0 is a single character. So when it has to be explicitly assigned to a string, it has to be written with in single quotes as ‘\0’.
A null character occupies 1 byte of memory.
Syntax:
char str[num_of_characters];
Example 2:
char name[20];
The above statement declares an array named “name” capable of holding 20 characters. The 20th character is null character. Hence we can store 19 characters. The 20th character is reserved for null character.
Example 3:
char name[15]=”LearnCOnline”;
Here,
‘L’ is stored at name[0]
‘e’ is stored at name[1]
‘a’ is stored at name[2]
‘r’ is stored at name[3]
.
.
.
.
‘e’ is stored at name[11]
And finally,
‘\0’ is stored at name[12].
name[14] and name[15] will contain garbage value.
Example 4:
char name[]=”Learn C Online”;
Here the size of an array is calculated automatically (15 characters).
We can also declare string as character pointer as follows:
char *name = “Learn C Online”;
Printing Strings:
Using Character pointer:
char *name = "Learn C Online"; printf(name); printf("%s", name); printf("%s",&name[0]);
Using Character Array:
char name[]="Learn C Online"; printf(name); printf(“%s”, name); printf(“%s”,&name[0]);
printf function can be used to print a string in C. Similarly, puts function can be used to print a string as shown below.
char name[]=”Learn C Online”; puts(name);
Accepting String as input in C language:
We can use scanf or gets to accepts string from the user.
Using scanf in C:
scanf(“%s”,name);
The above statement passes the base address implicitly.
scanf(“%s”,&name[0]);
In the above statement, we are passing the address of the first element.
Using gets in C:
gets(name); gets(&name[0]);
Although scanf and gets seems to be the same, there is a big difference between them. Let’s understand this difference.
Program using scanf in C language:
char *str; printf("Enter the string: "); scanf("%s",str); printf("\n%s",str);
Output:
Enter the string: Learn C Online Learn
Program using gets in C language:
char *str; printf("Enter the string: "); gets(str); printf("\n%s",str);
Output:
Enter the string: Learn C Online Learn C Online
When a string in C is accepted using scanf function, if a space is encountered then it will treat it as null character(‘\0’). In the above example, a space encountered after “Learn” is treated as null character. Hence while printing, only “Learn” is printed.
This problem can be resolved using gets function.
In example 3, if the length of the string is 15, shouldn’t the index of the last position of the string be 14, since it starts with 0?
In the example 3 we have defined string name as
char name[15]=
very nice teaching
In example 4
Using Character pointer:
char *name =
#include
int main()
{
char *str;
gets(str);
printf(“\n%s”,str);
return 0;
}
when i run the above code i had seen a another dialog box close the program. why?
@Rachana :-
It will print the actual string . . .
When I do this:
char *str;
printf(
could you also use the following because I learned it is unsafe to use gets because it doesn’t have a buffer, and scanf doesn’t return the whole string.
#include
#include
#define MAX_NAME_SZ 256
int main(){
//allocate some memory
char *str = malloc(MAX_NAME_SZ);
//check if it is OK
if(*str == NULL){
printf(“Sorry, there is not enough memory \n”);
return 1;
}
//Use fgets to get the string
fgets(str, MAX_NAME_SZ, stdin);
//Remove trailing newline characters
if((strlen(str) !=0) && (str[strlen(str)-1] == ‘\n’)){
str[strlen(str)-1] = ‘\0’;
}
printf(” You Entered: %s”, str);
return 0;
}
in example 4 printing strings using character array
what is the use of three printf statements?
good
char *str;
what the above statement mean???
@sanjay
char *str;
here str is character type pointer which can store the address of some character variable. i.e. it points to a character.
@sanjay
its a pointer which can store adress of some other character variable.
Example 4:
char name[]=
can c online teach me from basics ?? i have chosen computer engineering and am in 1st year nd not able to copeup everyone else in class so plz do advice something to fix me
Learn C Online can help you learn C programming language right from the scratch. All you need to do is go through each and every topics of c language and you should be able to excel C programming easily. Many readers have gained benefits from this website and you can be the another one.
All the best and if you have any query then definitely post your comments and we shall try to resolve your queries as quick as possible.
Thanks,
Learn C Online Team
Thank you
very nice