Understanding Strings in C

Previous
Next

“%s”:

  • It is format specifier
  • %c represents single character.
  • %s represents complete string(collection of characters)
  • It is used to process(read & display) strings.
#include<stdio.h>
int main()
{
	char s1[20] = "Hello" ;
	printf("%s all \n" , s1);	
	return 0;
}
#include<stdio.h>
int main()
{
	char s1[4] = {'a','b','c'};
	printf("String is : %s\n" , s1);	
	return 0;
}

Duplicate strings get different memory locations:

#include<stdio.h>
int main()
{
	char s1[10] = "abc";
	char s2[10] = "abc";
	
	printf("s1 addr : %u\n" , s1);
	printf("s2 addr : %u\n" , s2);	
	return 0;
}
#include<stdio.h>
int main()
{
	char s1[10] = "abc";
	char s2[10] = "abc";
	
	if(s1==s2)
		printf("Strings are equal \n");
	else
		printf("Strings are not equal \n");	
	return 0;
}
Previous
Next

Add Comment

Courses Enquiry Form