Hi!
I find an odd situation where when I type “abc”, the console keeps showing “aabbcc”. May I know how I can eradicate this strange phenomenon?
*Yup, I had added “libreadline” as shown in the book.
My code is:
[code]#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
int main(int argc, const char * argv[]) {
//"readline"
printf(“Who is cool?\n”);
const char *name = readline(NULL);
printf(“My name is %s”, name);
return 0;
}[/code]
And the console is showing:
[code]Who is cool?
aabbcc
My name is abc[/code]
That’s an Xcode quirk, not your fault. And there’s nothing you can do about it in Xcode.
If you run your tool from Terminal.app / the command line, the output won’t be awkwardly echoed like that.
Alright Mikey! Thanks for telling me that!
what does const mean?
const char * name = readline(NULL);
i dont remember reading this in the book till now.
Thanks
Also where can i type the user input to test the readline function?
i am kind of lost with this
There is a chapter later in the book called Constants where we talk about it a bit, but for now it’s merely to satisfy the expected return type of the readline() function. Its declared return type is const char *, and so we must declare the variable that we use to store its returned value as being of the same type.
To satisfy curiosity, that particular type (const char *) is used by many C functions to represent a pointer to a constant char. Since we’re making a pointer to one, rather than storing a singular char, presumably the pointer is to the first char in an array of them. An array of chars is also referred to as a C String. The const keyword in particular means constant, which in this case marks the array of chars as immutable (cannot be changed/mutated). If you’re interested in learning more about C strings, there’s a chapter about them near the back of the book.
In order to actually provide the user input for your program, you will need to use Xcode’s debug console. It should appear automatically when you build/run your program, in the bottom area. If it does not, you can make it appear by using the View menu, then selecting Debug Area > Activate Console.
Edit: for what it’s worth, having a passing familiarity with the existence of C strings is sometimes handy, but once you get past these first few chapters and transition from programming in C to programming in Objective-C, you will not use C strings again except in rare cases.