
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is a string? Declare and initialize the strings in C language
An array of characters (or) collection of characters is called a string.
Declaration
Refer to the declaration given below −
char stringname [size];
For example - char a[50]; a string of length 50 characters.
Initialization
The initialization is as follows −
Using single character constant −
char string[20] = { ‘H’, ‘i’, ‘l’, ‘l’, ‘s’ ,‘\0’}
Using string constants −
char string[20] = "Hello":;
‘\0’ is called a null character. It marks the end of the string.
‘\0’ is automatically placed by the compiler, if a string is given as input. The user has to take care of placing ‘\0’ at the end if a single character is given.
Accessing − There is a control string "%s" used for accessing the string, till it encounters ‘\0’.
Example
Following is the C program for a string −
#include<stdio.h> main ( ){ char a[10] = "Hello"; clrscr ( ); printf ( " given string is %s",a) getch ( ); }
Output
When the above program is executed, it produces the following result −
Given string is Hello
- Related Articles
- How to declare and initialize constant strings in C#?
- How to declare and initialize a dictionary in C#?
- How to declare and initialize a list in C#?
- How to Initialize and Compare Strings in C#?
- How do you declare, initialize and access jagged arrays in C#?
- What are the input and output for strings in C language?
- Strings in C Language
- How do I declare and initialize an array in Java?
- How to initialize and compare strings?
- How to Initialize and Compare Strings in Java?
- How to declare, create, initialize and access an array in Java?
- How to initialize a string to an empty string in C#?
- What are string literals in C language?
- What are string searching functions in C language?
- Split a String in Balanced Strings in C++

Advertisements