Strings : Why is this program not printing the first element?

Why is this program not printing the first element?
#include 
#include 
#include 

int main(void)
{
    char names[][5] = {
        "aadit",
        "jobs",
        "mark",
    };



    printf("%s\n",names[0]);
    getch();
}

when I printf(%s",names[0]) it shows aaditjobs.

Replies

  • [Prototype]
    [Prototype]
    The first name you specified actually requires 6 byte of space. So what's effectively happening is, at the run time, aadit took 5 byte with \0 not coming into picture because it's beyond the memory being held by the array. This made the program to think the string has not yet finished and making it jump to the next memory location pointing to jobs\0. This time, since jobs is only 4 bytes it finds the 5th byte as terminating char thus finishing the print.

    If you make jobs as jobss, it'll move forward to print mark as well.
  • Aadit Kapoor
    Aadit Kapoor
    Thank you!

You are reading an archived discussion.

Related Posts

Google has created a stir in the wearable technology market by announcing Android Wear. Android Wear is a project that will extend Android to the wearable form factor. While Google...
Just as Google finished rattling the smartwatch competition by announcing Android Wear, LG and Motorola were quick to announce their plans to adopt this platform by announcing the existence of...
Hi frnz, im doing the project "Automatic face detection using color based segmentation and template matching" in my last semister....Can anyone give me the code for this?. I have failed...
A recent Kickstarter campaign has planned to fund a new yet, unique project - LifePrint. LifePrint is a wireless, portable printer that lets you print pictures from your smartphone. Using...
Sean Ebersold submitted a new project: Carbon | Wearable Solar Charger + Analog Watch - Carbon is a high tech wearable solar charger and a analog watch. Carbon charges your...