a crazy question for c programmers

diwana.nanda

diwana.nanda

@diwanananda-lUNv25 Oct 13, 2024
#include<stdio.h>
#include<string.h>
void main()
{
char name[5]="sajan";

printf("%s",name);

getch();

}

output:

sajan with a grbage value...................

que1:-where it store the null character if size is equal to the characters entered?

que2:-if we take even size nd same number of character then it wil genrate error in c, bt not in c++.why this heppens...

explain me logic plzzzzzz...................\

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Oct 8, 2011

    Thread moved from Introductions section to Computer Science & IT engineering section.
  • Pensu

    Pensu

    @pensu-8tNeGU Oct 8, 2011

    For the first one, u have answer in question itself, there is no place for null character. So, when compiler starts printing the string and it doesnt find any null character, it doesnt know where to stop, ergo garbage value.
    And i didnt get the second question, what error r u getting in C???
  • Rupam Das

    Rupam Das

    @rupam-das-wW5CvN Oct 8, 2011

    diwana.nanda
    #include<stdio.h>
    #include<string.h>
    void main()
    {
    char name[5]="sajan";

    printf("%s",name);

    getch();

    }

    output:

    sajan with a grbage value...................

    que1:-where it store the null character if size is equal to the characters entered?

    que2:-if we take even size nd same number of character then it wil genrate error in c, bt not in c++.why this heppens...

    explain me logic plzzzzzz...................\
    have you tried this c program in unix? my friend it is not c or c++, it is compiler. turbo c compiler does not consider appending a null once you press enter after scanf. so to avoid this, they have given the facility of 'gets'
  • diwana.nanda

    diwana.nanda

    @diwanananda-lUNv25 Oct 21, 2011

    guyz my second que is
    if i ll take thesize of array in even no. in c and c++ it will not generate error. but if i take size in odd numbers it will genrate error in c++ but not in c compiler..................
    this was the main question.