String Constants vs Char Arrays in C
C Programming

String Constants vs Char Arrays in C

 

A string constant is a series of characters(array of characters or group of characters) enclosed with double quotes(like “GOOD “). A string constant is a one-dimensional (str11[]) array of characters. A character constant is a character enclosed in single quotes. (like ‘G ‘). Each character in a string takes only 1 byte.

String and Character Array in C

#include<stdio.h>

#include<conio.h>

void main()

{

char str11[]={‘X’,’C’,’N’,’O’,’T’,’E’,’S’,’\0′};//unsized character

char str22[]=”I Love Programming”;

int ii;

clrscr();

/*print out str11*/

for(ii=0;str11[ii];ii++)

printf(“%c”,str11[ii]);

printf(“\n”);

/*print out str22*/

for(ii=0;str22[ii];ii++)

printf(“%c”,str22[ii]);

getch();

}

String Array and String Constant in C

Explanation…

In this example, there are two character arrays str11 and str22.

In the declaration of str11, a set of character constants, including a null character(‘\0’), is used to initialize the array.

For str22, a string constant is assigned to the array.

The compiler will append a null character to str22.

Both str11 and str22 are unsized arrays means the compiler will automatically figure out how much memory is needed.

The for loop prints out all the elements in str11. After the execution of the for loop, the string constant is shown on the screen.

Another for loop displays the string constant assigned to str22 by putting every element of the array on the screen.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment