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

  • Home
  • SEARCH
  • 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 9116025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:34:25+00:00 2026-06-17T04:34:25+00:00

I know that every string in C ends with ‘\0’ character. It is very

  • 0

I know that every string in C ends with ‘\0’ character. It is very useful in cases when we need to know when the string ends. However, I am unable to comprehend its use in printing a string and printing a string without it. I have the following code:-

/* Printing out an array of characters */
#include<stdio.h>
#include<conio.h>
int main()
{
    char a[7]={'h','e','l','l','o','!','\0'};
    int i;
    /* Loop where we do not care about the '\0' */ 
    for(i=0;i<7;i++)
    {
        printf("%c",a[i]);
    }
    printf("\n");
    /* Part which prints the entire character array as string */
    printf("%s",a);
    printf("\n");
    /* Loop where we care about the '\0' */
    for(i=0;i<7&&a[i]!='\0';i++)
    {
        printf("%c",a[i]);
    }
}

The output is:-

hello!
hello!
hello!

I am unable to understand the difference. any explanations?

  • 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-06-17T04:34:26+00:00Added an answer on June 17, 2026 at 4:34 am

    In this case:

    for(i=0;i<7;i++)
    {
        printf("%c",a[i]);
    }
    

    You loop for a number of times (7) and then quit. That is the end condition of the loop. It terminates, no matter anything else.

    In the other case, you also loop for 7 times and no more and you just added another condition, which really serves no function as you already keeping a count of things. If you did the following:

     int index = 0;
     while (a[index] != '\0') { printf("%c", a[index]); index++; } 
    

    now you would depend on the zero termination character being there, if it wasn’t in the string, you while loop would go on forever until the program crashed or something terminated it forcedly. Probably printing garbage on your screen.

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

Sidebar

Related Questions

I know a little bit about TextWatcher but that fires on every character you
When i set lower_case_table_names = 1 in mysql i know that it converts every
I know that switch / select statements break automatically after every case. I am
I know that in OOP you want every object (from a class) to be
Is there a set of things that every JavaScript programmer should know to be
I have an object array, I know that the elements are type String, say
I know that you can either step into every property or not step into
I know that StringBuilder is more efficient than a normal string when processing code
i have a string that looks like this: /dir/location/test-load-ABCD.p and i need to parse
I know that at compile time when a String is created, that String will

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.