Search code examples
cvariablesprintfnewline

New line after variable in C


In C, I try to do \n after a variable.

Here is my source code:

int main() {
    int xD = 21;
    printf(xD\n);
}

And I receive this while compiling it:

new 1.c: In function ‘main’:
new 1.c:5:11: error: stray ‘\’ in program
printf(xd\n);
       ^
new 1.c:5:12: error: expected ‘)’ before ‘n’
printf(xd\n);
        ^
new 1.c:5:9: warning: format not a string literal and no format
arguments [-Wformat-security]
printf(xd\n);
       ^~

How can I fix it?


Solution

  • The printf function requires a format parameter to identify the type of variable xD is. Since xD is an integer, "%d" is required as the format parameter.

    printf("%d\n", xD);