Tech Study

C strings


c strings

A collection of characters in a linear sequence can be called a string. It is treated as a single data by C even if it contains white spaces. A single character in a string is generally defined using single quotation representation. While the entire string is represented using double quotation marks.

For example, “Hey! There I am Rajshree Verma”

To perform complicated operations easily on Strings in C standard library <string.h> is provided by C that contains many functions.

Declaring a c strings:-

A C String can also be treated as a simple array with char as a data type. String is not generally supported by C language as a data type directly. Therefore , to display a c strings, we will need to make use of a character array.

Syntax for declaring a variable as a String in C can be found bellow,

char stringVarName[arraySize];


Another way of declaring string which is common:-

char stringzVarName[stringLength] = “ourString”;


We have to define the size of an array while declaring a c strings variable because it will be used in calculating the number of characters that are going to be stored inside the string variable in C. Some of the examples of string declaration can be found bellow,

char rishuStr[12];

char VermaStr[12];

The above example here are representing string variables with an array size of 12. This shows that the given C string array can hold up to 15 characters at most. The indexing of array generally begins from 0 hence characters will be stored from a 0-11 position. A NULL character ‘\0’ is automatically added by the C compiler to the character array created.

Initializing a c strings:-

Following example is showing the initialization of c strings,

char rishuStr[15] = "RISHUGAL";

char vermaStr[15] = {'R','I','S','H','U','G','A',’L’,’\0’}; 

char rishh[7] = "rajshree";/* string size = 'r'+'a'+'j'+'s'+'h'+’r’+’e’+’e’+"NULL" = 8 */

char rishh2 [ ] = "itsme"/* string size = 'i'+'t'+'s'+'m'+'e'+"NULL" = 6 */

Taking String inputs in C:-

Programs which ask the user for input where it is interactive, to find a line of text entered from the user ,the scanf(), gets(), and fgets() functions are provided by C.

As an array name acts as a pointer when we use scanf() to read, we have to use the “%s” format specifier avoiding the use of the “&” to access the variable address.

For reference:

#include <stdio.h>

int main() {

char rishVar[10];

int r_age;

printf("Tell me your age and name: \n");

scanf("%s %d", rishVar, &r_age); 

printf("You said: %s %d",rishVar,r_age);


c strings

If we want to read a string containing spaces, we can make use of the gets() function. It is supposed to ignore the whitespaces. When a newline is reached the reading will be stopped by it.

Yet another alternative to gets() is also present, that is, is fgets() function that will read a specified number of characters.

The arguments to fgets()can be found belllow:

  • The name of the required string ,
  • Number of characters to be read,
  • stdin means to read from the standard input which is the keyboard.

String outputs in C:-

For printing or displaying Strings in C on the desired output device the standard printf function can come into action. The format specifier used is %s


printf(“%s”, rishVar);

The fputs() and printf() functions also come into play for string outputs.

c strings

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more