Question

What is the output of the following code segment and why? int main(void) { // Find...

What is the output of the following code segment and why?

int main(void)
{
    // Find the output of the following and explain it:
    char name[] = "Hello";
    name[2] = '\0';
    printf("%s\n", name);

    return 0;
}

why answer is he?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Strings in c ends with a special character '\0'

In other words, '\0' indicates the end of the string

char name[] = "Hello";
After this line, name is "Hello"
Internally It stores name as "Hello\0"

name[2] = '\0';
After this line
It changes name as "He\0lo\0"

So, it means string ends after "He"

So, output of the code is
He

Output :

He

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!
Add a comment
Know the answer?
Add Answer to:
What is the output of the following code segment and why? int main(void) { // Find...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • why is correct the output of this code? #include <stdio.h> int main( void ) { char...

    why is correct the output of this code? #include <stdio.h> int main( void ) { char val[] = "one"; if (val[1] = 'o') printf("correct"); else printf("incorrect"); }

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • should be in C language What is the output of the following segment of C code:...

    should be in C language What is the output of the following segment of C code: void CapsLetter (char* x, charl); int main() { char* text; text="This is some sample text."; CapsLetter(text, 'e'); printf("%s", text); return 0; سی void Caps Letter (char* x, char 1) { int i=0; while (x[i]!='\0') { if (x[i]==1) x[i]=1-32; } 1. This is som sample text. 2. THIS IS SOME SAMPLE TEXT. 3. This is some sample text. 4. this is some sample text. What...

  • What is the output of the PRINTLINE2 from the following code segment?      public static void Main(){...

    What is the output of the PRINTLINE2 from the following code segment?      public static void Main(){ int x = 15; int y = 55;        int z = DoIt(ref x, ref y);      PRINTLINE(x + “ “ + y + “ “ + z); // PRINTLINE 1   } static int DoIt(ref int a, ref int b) {   a += 22;   b -= 12;   PRINTLINE(a + “ “ + b); // PRINTLINE 2   return (a - b); }

  • 24. What will be the output of the following code #include<stdio.h> int main() { char *p;...

    24. What will be the output of the following code #include<stdio.h> int main() { char *p; p="%d\n"; p++; p++; printf(p-2, 100); return 0; } a. 100 b. 00 c. 10 d. compiler error e. none of the above 26. What will be the output of the following code #define TRIPPLE(x) (3*x) int x = 4; printf(“triple(%d) = %d \n”,x+1, TRIPPLE(x+1)); a. triple(5) = 12 b. triple(5) = 13 c. triple(4) = 14 d. triple(4) = 15 e. none of the...

  • Please help me debug the following code #include int main(void){ int a = 11, b =...

    Please help me debug the following code #include int main(void){ int a = 11, b = 5; int *ptr1 = &a, *ptr2 = &b; printf("%d %d\n", *ptr1, *ptr2); swap(&ptr1, &ptr2); printf("%d %d\n", *ptr1, *ptr2); minimum(ptr1, ptr2); printf("%d %d\n", a, b); return 0; } // Part A void swap(int **ptr1, int **ptr2){ int *temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; return; } // Part B void minimum(int *ptr1, int *ptr2){ if(*ptr1 > *ptr2){ *ptr1 = *ptr2; }else{ *ptr2 =...

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT