0
char* a="HELLO WORLD";

IF ADDRESS of 'H' is 0x01 then the printf with %s prints to D but if the same code is written with manual printing routine

while(*a!=NULL) {printf("%c",n[a]);n++;}

this prints a few more characters.. but

printf("%s",a);

prints it perfectly.

while(*a++) printf("%c", *(a-1)); or 

for(;*a++;)printf("%c", *(a-1));

although work but i dont want solutions but the process mechanisms..

so the question coming to my mind is

whether printf gets the length of the string from some register(or any memory unit) or it performs character check.. then prints...

user409640
  • 21
  • 4

4 Answers4

7

The way you're indexing into the character string is odd. It works for the string, but won't stop because you never change the value of *a. What your program does is try to get the a offset of n, so for the first 11 positions they are the same, but the loop doesn't terminate because *a will always be 'H'. What you'd want the terminating condition to be is n < strlen(a).

However, the more succinct way to write that program would be:

int main(int argc, char **argv) {
    char *a = "HELLO WORLD";
    while(*a) printf("%c", *a++);
    return 0;
}

This works because a is an array of characters and as we're printing out each character (de-referencing the value stored at the position) we also increment to the next position. The string should terminate with a NULL reference, which will cause the loop to terminate sine *a == 0 at the NULL terminator.

mjschultz
  • 1,936
  • 1
  • 18
  • 20
  • 1
    +1 I think this is an elegant answer because it doesn't require another integer counting value and uses pointer arithmetic –  Aug 03 '10 at 13:13
6

did you mean or you have error:

int main() {
   int n = 0;
   char* a="HELLO WORLD";

   while(a[n] != NULL) {printf("%c",a[n]);n++;}
}

explanation about what is wrong:

while(*a!=NULL) printf("%c",n[a]);n++;
  1. a is not modified anywhere, so *a will not change it's value.
  2. Although n[a] is perfectly valid construct in C I strongly recommend not to use it, because it is semantically incorrect. You access array by index, not index by array.
  3. You increment index (n++) but check the pointer to array. You could possibly increment a inself like this: while(*a!=NULL) {printf("%c",*a);a++;}
Andrey
  • 59,039
  • 12
  • 119
  • 163
0

The corect way to do this is:

#include <iostream>
using namespace std;

int main() {
    char *a="HELLO WORLD";
    int n = 0;
    while(a[n]!=NULL){
        cout<<a[n];
        n++;
    }

   cout<<'\n';
   return 0;
}
XCS
  • 27,244
  • 26
  • 101
  • 151
  • @cristy:-please read the whole question first.. if I were to use C++... #include #include typedef std::string String; int main() { String afg="HELLO WORLD"; std::cout< – user409640 Aug 03 '10 at 13:51
  • @porgramming-tornado I don't get what you're saying? You didn't mention any programming language in your question and the tags say: "c++,c"... So I thought a C++ version would help. Your actual question makes no sense, because your program is not working properly cause of the strange iteration you make through the string letters. The "n" you use I think you wanted to use as an integer to iterate, but when you write "n[a]" makes no sense... – XCS Aug 03 '10 at 14:03
  • And pritf prints only what you ask it to print. If you use printf(%s) it will print the whole string. If you use printf(%c) it will print only a char, the reason your program doesn't output what it should is that your outputting algorithm is wrong, not the "printf" function... – XCS Aug 03 '10 at 14:05
  • the question had all the stuffs in the last two lines. if you had seen it totally.. my question was a rough pseudo code but u guys took it as program snippet.furthermore i never asked about that specific programs output....n[a] works look at the program again copy it and run it... nothing is strange ,everything is normal.. – user409640 Aug 03 '10 at 14:22
  • -1 for using `using namespace std;` instead of `using std::cout;`. – SigTerm Aug 03 '10 at 14:36
-1

As far as I remember, by default when you created char* a it will be something as {HELLO WORLD\0} in memory ('\0' is how %s know the end of the your string is)..

Not sure if '\0' == null will yield true.. but I doubt it

Shady M. Najib
  • 2,151
  • 2
  • 19
  • 30
  • @Phil: The integer value 0 is not the null pointer constant. In particular, even if he were incrementing `a` in his code, `*a!=NULL` is the wrong condition, since `*a` is not a pointer type. `0` is converted to the null pointer constant in pointer contexts by the compiler, but not the other way round. – Alok Singhal Aug 03 '10 at 13:53
  • @Alok: the OP is confusing C and C++. In C++, NULL is the null pointer constant, and it's a macro for a constant integral 0. Could be `0L`, could be `false`, but usually just `0`. So there it works. – MSalters Aug 03 '10 at 14:07
  • So, to summarize that, '\0' == null will yeild false? Did I get you right? – Shady M. Najib Aug 03 '10 at 20:57
  • In general, '\0' == NULL is true. However, it is not a requirement. This could be due to the fact that the system is not using ascii, or that the null pointer is not found at (void*)0. – Bill Lynch Aug 17 '10 at 03:45