this post was submitted on 07 Feb 2024
6 points (87.5% liked)

Code Review

155 readers
1 users here now

Welcome to the programming.dev code review community! This is a place where you can review other people's code or request reviews on your own

Before posting please read the sections below (click on one to expand it)

What is a code review?

What is a code review?

A code review is a process where people who did not write a piece of code look over it to find errors, suggest improvements, and make sure it follows coding standards

When doing a code review look for these aspects:

  1. Correctness: Does the code behave as expected and meet the requirements?
  2. Readability: Is the code easy to understand and maintain?
  3. Efficiency: Are there more efficient ways to accomplish the same task?
  4. Best Practices: Does the code follow established coding standards and best practices?
  5. Security: Are there potential security vulnerabilities?
  6. Scalability: Will the code perform well as the application scales?

Post Guidelines

Guidelines

  • Put the programming language you used for your code in square brackets ahead of your post title. e.g. [Python], [React], [C#], etc.
  • Provide a link to a spot where people can view your code (e.g. github, etc.) in your post body
  • In your post title put a small description of what the thing is that you want reviewed and then you can expand on that in more details if you want in your post body

Rules

Rules

Any code review requests you post after your first must be done after you have reviewed two other people's code (and then you need another two for each request after that as well).

Youre free to post your first code review request in this community though without needing to follow this


Credits

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient




Wormhole

[email protected]

founded 1 year ago
MODERATORS
 

Hello, I am new to this community, as well as to coding in general. I am having fun learning C, and I've generally been able to work through/slam my head into problems until they make sense, but I am confounded by this discrepancy, and I am hoping to have some help with it:

printf("%%c);

Output: %c


#include 

void textGreen(const char* text)
{
    printf("\033[32m%s\033[0m", text);
}

int main()
{
    textGreen("%%c\n");
    return 0;
}

Output: %%c.

Since printf is wrapped into the function, should the text not be outputting with the same behavior? Why is my terminal printing this code without escaping the percent sign? FWIW, the text is green, at the very least.

I am using Ubuntu 23.10, the code was written in KATE, it was compiled in GCC, and it was run on the basic GNOME terminal.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 9 months ago (1 children)

Thank you so much! I didn't realize that %s changed the runtime rules for escape characters. Is there somewhere that I can find a list of these rules? Because things like \n still work like normal, but then %% does not.

[โ€“] [email protected] 1 points 9 months ago

Nothing has changed. %-sequences only work in format string (first argument of printf, it is a feature of printf family of functions). Backslash escaped sequences work everywhere (it is a feature of C language itself).