New line in c
In C, there are a few ways to insert a new line:
-
Using
\n
: You can use the\n
character to insert a new line. This is a special character called a newline character. For example:printf("Hello\nWorld");
This will print "Hello" followed by a new line, and then "World".
-
Using
printf
with a format string: You can use theprintf
function with a format string that includes a newline character. For example:printf("Hello %s\nWorld", "there");
This will print "Hello there" followed by a new line, and then "World".
-
Using
puts
: Theputs
function is similar toprintf
, but it automatically appends a newline character to the end of the string. For example:puts("Hello World");
This will print "Hello World" followed by a new line.
-
Using
fprintf
: If you're writing to a file instead of the console, you can usefprintf
to insert a new line. For example:FILE *fp = fopen("example.txt", "w"); fprintf(fp, "Hello\nWorld"); fclose(fp);
This will write "Hello" followed by a new line, and then "World" to the file "example.txt".
Note that in all cases, the \n
character is used to insert a new line. This is because the \n
character is a special character that tells the terminal or file to move to the next line.