Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

printf not printing out the given parameters

The code is supposed to print out 13:Hello World!, but the output is only 13:.

#include <stdio.h>
   int main(){

  char *str = "Hello World!\n";

 int Laenge= 0;

  while (*str++){

   ++Laenge;
   }  
  printf(" %d:%c\n", Laenge, *str);
  return 0;
   }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can interate till you find the null character!

#include <stdio.h>

int main()
{
    char *str = "HELLO WORLD!\n";
    int len = 0;
    
    while(*(str + len) != '\0'){
        len++;
    }
    printf("%d:%s", len, str);

    return 0;
}

Your code is not working because your are incrementing the original string and converting it to ‘\0’. Use a walker.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading