Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6060655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:51:02+00:00 2026-05-23T08:51:02+00:00

I have three variables initialised below : char c1[] = Hello; char c2[] =

  • 0

I have three variables initialised below :

char c1[] = "Hello";
char c2[] = { 'H', 'e', 'l', 'l', 'o', '\0'};
char* c3 = "Hello";

I am aware that c1 and c2 are the same, and that they are both strings because they are terminated by \0. However, c3 is different from c1 and c2. Is this because c3 does not terminate with a \0? Does that mean that c3 is not a string? If c3 is not a string, then why does printf("%s", c3); not give an error? Thanks!

EDIT:

Is there a reason why c1 and c2 can be modified but c3 can’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:51:02+00:00Added an answer on May 23, 2026 at 8:51 am

    In terms of C, the most pertinent difference between c3 and the others is that you are not allowed to attempt to modify the underlying characters with c3. I often find it helpful to think of it like this:

    char *xyz = "xyz";
    

    will create a modifiable pointer on the stack and make it point at the non-modifiable character sequence {'x','y','z','\0'}. On the other hand,

    char xyz[] = "xyz";
    

    will create a modifiable array on the stack big enough to hold the character sequence {'x','y','z','\0'} and then copy that character sequence into it. The array contents will then be modifiable. Keep in mind the standard says nothing about stacks but this is commonly how it’s done. It is just a memory aid, after all.

    Formally, c3 is a pointer to a string literal while c1 and c2 are both arrays of characters which both happen to end with a null character. When they’re passed to functions like printf, they decay to a pointer to the first element of the array which means they’ll be treated identically to c3 within that function (actually they decay under quite a few circumstances, see third quote from c99 below for exceptions).

    The relevant sections of C99 are 6.4.5 String literals which explains why you’re not allowed to modify what c3 points to:

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

    and why it does have a null terminator:

    In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.

    And 6.3.2.1 Lvalues, arrays, and function designators under 6.3 Conversions states:

    Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PHP application that contain three small applications. Each application have own users
I have an NSTimer that I initialize with scheduledTimerWithTimeInterval: with a very short interval
First off, I'm using Access 2000 and DAO. I have code that executes a
Suppose I have the following struct: struct sampleData { int x; int y; };
I have a collection of worker classes, and I need to be able to
Lets say I have a fairly complex frag shader for figuring out just how
This is homework...I'm not asking for answers, I just have a bug I'm not
I'm using CakePHP's ACL component to manage permissions for my app. I have about
I would like my constructor to call another constructor with parameter but when I
My app is crashing occasionally and when I see the crash logs this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.