-5

I was wondering how to fix this printf statement to not print the character outside the brackets;

printf("[ %c ]\n", display(Room1,5,5));

So as you can see i want to the returned character to be where the %c, the function that i am calling code is; (this code has been editted from suggestions)

if(Room1[i][j] == blue){
            return COLOR_blue "." COLOR_RESET;
        }           
    return " ";
}

so if it is equal to blue it will print a blue '.' otherwise it will just create a space, however it is just printing to console as;

.[
  ]

- with the dont being blue, not sure why it does this as i want it inside the brackets

  • `display()` what does it return? %c writes one character so display should *return* one character - it should not print one character – AndersK Aug 05 '17 at 06:03
  • @AndersK. display is supposed to return a coloured character to go inbetween the brackets, however from what you have said i have clearly not done it correctly – javair peter Aug 05 '17 at 06:10
  • 1
    @AndersK. has a good point about what you are returning and how you are using that return. You also need to realize that escape sequences on a TTY are usually (I think always) more than one character. Here's another SO question that might help: [List of ANSI color escape sequences](https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences). – Mike Aug 05 '17 at 06:18
  • @Mike I have changed it so now that the return statement is "return COLOR_blue "." COLOR_RESET; and it is saying "warning: return makes integer from pointer without a cast" and then outputs 3 random lines in console - i.imgur.com/en2YDu6.png – javair peter Aug 05 '17 at 06:21
  • `return printf(" ");` will return `1` which you then pass to the `%c` format. Probably not what you want. If the other `printf` prints 10 characters it will return `10` and you will get a newline output. – Weather Vane Aug 05 '17 at 06:21
  • Also, I'm not sure if you understand there is a difference between a character and a string. A character is a single symbol, like this: '1'. Whereas a string is multiple characters strung together, like this: "string". What @AndersK is telling you is that in the `printf` statement, you are trying to print a character `%c` and your function returns a string. You need them to match, either both use string `%s` or both use character. – Mike Aug 05 '17 at 06:22
  • 1
    No, the function does not return a string, it returns the number of characters output by `printf`. – Weather Vane Aug 05 '17 at 06:22
  • However the question's code has now been changed so a downvote. Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem. – Weather Vane Aug 05 '17 at 06:25
  • Sorry, @WeatherVane is completely correct, I was in the process of correcting myself when he pointed this out. `printf` will actually write to your output stream, i.e. your screen. If you `return printf` you are going to be returning the length of what was printed. [printf](http://www.cplusplus.com/reference/cstdio/printf/) You want to use [sprintf](http://www.cplusplus.com/reference/cstdio/sprintf/) and you don't return it either. – Mike Aug 05 '17 at 06:28
  • 1
    The code can only compile if `COLOR_blue` and `COLOR_RESET` are `#define` string constants. It appears that your `display()` function is returning a character pointer to either a blank or a complex string with a `.` in the middle. You should, therefore, be printing the string with `%s` and not `%c`. When you use `%c`, one byte of the address of the string is treated as a character — not what you had in mind at all. The code you show cannot, on its own, produce the output you claim. You should really create an MCVE ([MCVE]) that can be compiled by those trying to help you. – Jonathan Leffler Aug 05 '17 at 07:29

2 Answers2

1

Because you return 'char *'

//to print first char in the string
printf("[ %c ]\n", *display(Room1,5,5));  

Or to print the complete string

//to print first char in the string
printf("[ %s ]\n", display(Room1,5,5));  
0___________
  • 60,014
  • 4
  • 34
  • 74
0

You are already printing the . inside the function, if you want to return the value use return statement I.e don't do printf in the function instead return the . like return '.'

deepakl
  • 172
  • 8
  • Okay i have changed it to "return COLOR_blue "." COLOR_RESET; and it is saying "warning: return makes integer from pointer without a cast" and then outputs 3 random lines in console - http://i.imgur.com/en2YDu6.png @deepakl – javair peter Aug 05 '17 at 06:19