Strings in C

📘 C 👁 53 views 📅 Dec 22, 2025
⏱ Estimated reading time: 1 min

A string in C is a sequence of characters stored in an array of type char and terminated by a null character ('\0').


Declaration of String

char str[20];

Initialization of String

char str[] = "Hello"; char str[6] = {'H','e','l','l','o','\0'};

Input and Output of Strings

Using scanf() and printf()

scanf("%s", str); printf("%s", str);

Note: scanf() stops reading at whitespace.


Using gets() and puts()

gets(str); puts(str);

Note: gets() is unsafe and not recommended.


Using fgets() (Recommended)

fgets(str, sizeof(str), stdin);

String Handling Functions

(Require #include )

FunctionDescription
strlen()Finds length
strcpy()Copies string
strcat()Concatenates strings
strcmp()Compares strings
strlwr()Converts to lowercase
strupr()Converts to uppercase

Example Program

#include #include int main() { char s1[20] = "Hello"; char s2[20] = "World"; strcat(s1, s2); printf("%s\n", s1); printf("Length = %d", strlen(s1)); return 0; }

Difference Between Character Array and String

  • Character array may not end with '\0'

  • String must end with '\0'


Summary

  • Strings are character arrays ending with null character

  • Stored using char type

  • string.h provides string functions

  • fgets() is safer for input



🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes

🤖 AI Quizer Assistant

📝 Quiz
📚 Categories
🏆 Leaderboard
📊 My Score
❓ Help
👋 Hi! I'm your AI quiz assistant for Quizer.in!

I can help you with:
• 📝 Finding quizzes
• 🏆 Checking leaderboard
• 📊 Your performance stats

Type 'help' to get started! 🚀
AI is thinking...