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 7910369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:50:42+00:00 2026-06-03T12:50:42+00:00

char a[10]; scanf(%s,a); int i=0; while(a[i]!=’\0′) printf(\n%c,a[i++]); //similar to printf(%s,a); char *b; b=malloc(10*sizeof(char)); scanf(%s,b);

  • 0
char a[10];
scanf("%s",a);
int i=0;
while(a[i]!='\0')
     printf("\n%c",a[i++]);  //similar to printf("%s",a);               


char *b;
b=malloc(10*sizeof(char));
scanf("%s",b);
i=0;
while((b+i)!='\0')
     printf("\n%c",*(b+i++));   //not similar to printf("%s",a);

For input “abcd”, the first loop prints a[] is it would be with printf(). But the same is not true for *b.

Second loops continues for too many until it encounters a ‘\0’.

So, does this mean ‘\0’ is appended at the end of character strings automatically but not at the end of char type pointers?

And whose job is it to append this ‘\0’? Compiler’s?

  • 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-03T12:50:44+00:00Added an answer on June 3, 2026 at 12:50 pm

    You forgot to dereference the pointer you get with b+i. It should be:

    while (*(b + i) != '\0') // or while (b[i] != '\0') or while(b[i])
    

    b + i just gets you an address, you have to dereference it to actually look at what the memory is pointing at and see if it’s the NUL-terminator. The x[y] notation is equivalent to *(x + y).

    Also don’t forget to free the memory you allocated with malloc.

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

Sidebar

Related Questions

while(1){ //Command prompt char *command; printf(%s>,current_working_directory); scanf(%s,command);<--seg faults after input has been received. printf(\ncommand:%s\n,command);
#include <stdio.h> int main() { char TurHare; while(1) { scanf(%c,TurHare); printf(\nCharacter :%c, TurHare); }
int num_arrays; char *p[20]; char tempc; int i=0; do { p[i]=malloc(sizeof(int)); scanf(%s,p[i]); tempc=*p[i]; ++i;
#include <stdio.h> int main () { char loop='y'; while(loop != 'n') { printf(loop? );
Code: #include <stdio.h> int main(void) { char i[50]; while(scanf(%s , i)){ printf(You've written: %s
int main( ) { char str[200]; int n,tc; scanf(%d,&tc); while(tc--) { scanf(%d,&n); gets(str); puts(str);
#include <stdio.h> #include <string.h> int main(){ char *command=0; do { printf([A]dd, [P]rint, [Q]uit\n); scanf(%s,
I read input from the keyboard using scanf() in a while loop and print
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
I have a program that has this code : #include<stdio.h> main(){ int input; char

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.