tags:

views:

206

answers:

4
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...

+6  A: 

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
missing `{}` for while loop.
Naveen
@Naveen: Good catch. Otherwise it prints H forever until it fails.
0A0D
+1 for good answer, but you should explain **why** your code is correct.
Evan Teran
question is not syntax error related I Know upto C Graphics development ,so i am not a beginner,but i asked this questiopn cause i wanted to know about printf...
except in step 3 you need two curly braces.
0A0D
+8  A: 

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 I think this is an elegant answer because it doesn't require another integer counting value and uses pointer arithmetic
0A0D
A: 

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;
}
Cristy
+1: C++, not just C answers.
DeadMG
@cristy:-please read the whole question first..if I were to use C++...#include <iostream>#include <string>typedef std::string String;int main() { String afg="HELLO WORLD"; std::cout<<afg; return 0;}
@porgramming-tornadoI 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...
Cristy
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...
Cristy
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 programsnippet.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..
-1 for using `using namespace std;` instead of `using std::cout;`.
SigTerm
@sigterms:-+1 using is the most stupid thing one uses in c++ code.....
A: 

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
It will. Also, it's NULL.
Phil
@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
@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
So, to summarize that, '\0' == null will yeild false? Did I get you right?
Shady M. Najib
@Shady: 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.
sharth