22
DecEscape Sequences and Comments in C
Lines and Comments in C: An Overview
Do you know how to insert new lines in your code? Have you ever heard ofComments
in C? If not, just go through this article. You will not only understand what they are but also how to use them. If you want to deepen your understanding of C programming skills, consider enrolling in our C Online Training. In this article on C Tutorial, we'll explore lines
, escape sequences
, and comments
in C.Lines in C
In the previous tutorial, First C Program and Syntax of C, we saw twoprintf()
functions printing the output in a single line. Why so? We used two printf()
so, there must be two lines of output. But, we got only one.
This is because to insert a new line, \n
is used in C.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
printf("This is my first program");
return 0;
}
Output
Hello, World!
This is my first program
- If you want to print multiple lines with the help of a single
printf()
, you can do so like this in the C Compiler#include <stdio.h> int main() { printf("Hello, World!\nThis is my first program.\nI have started programming"); return 0; }
Every
\n
indicates a new lineOutput
Hello, World! This is my first program. I have started programming
- If you want to give spacing between the lines, you can use simultaneous
\n
like this#include <stdio.h> int main() { printf("Hello, World!\n\n"); printf("This is my first program"); return 0; }
Output
Hello, World! This is my first program
Read More - Top 50 Mostly Asked C Interview Questions and Answers
Escape Sequences
In the above examples, we demonstrated the use of\n
. Did you notice what this \n
is exactly? It is called an escape sequence
.
- one of the important characteristic features of C
- enable the inclusion of special characters and control patterns in strings and character constants that cannot be represented directly.
- Syntax
(backlash)\ character to represent a special character
Escape Sequence | Meaning |
| Alarm or Beep |
| Backspace |
| Form Feed |
| New Line |
| Carriage Return |
| Tab (Horizontal) |
| Vertical Tab |
| Backslash |
| Single Quote |
| Double Quote |
| Question Mark |
| octal number |
| hexadecimal number |
| Null |
Comments in C
It is important that even a novice having absolutely zero knowledge of programming also gets some idea about your code after looking at it.Comments are used for these purposes.
- It increases the readability of your code.
- It makes you understand what the program is all about without going through the source code.
- Not necessary to include it while writing the program but a good practice
- Does not affect your program compilation or execution
Two Types of Comments in C
- Single line comment
It starts with a double forward slash, “//
”. Whatever you write between //
and the end of the line will not be checked by the compiler so no role of this in program execution.
Example
//Program to print hello world
#include <stdio.h>
int main() {
printf("Hello, World!"); //Statement to be printed
return 0;
}
Here, you can see that one comment is used to describe the heading of the program in the C Editor. The other one is used at the end of the printf
statement. It describes the output on the output screen. Thus it enhances the readability of this code.
Output
Hello, World!
You can see that the comment does not affect the output.
- Multi-line comment
It is written in between /*.....*/
. The text in between /*....*/
has no role in the compilation and execution of the program.
Example
#include <stdio.h>
int main() {
/* This is the first program
in the C language */
printf("Hello, World!"); //Statement to be printed
return 0;
}
Output
Hello, World!
Summary
After going through this article, you must have understood the lines
and comments
in C. Now, it's your time to practice whatever learned. When you do hands-on, your knowledge and understanding increase. To get certified in C, consider enrolling in our C Certification Course. It would be a great benefit for you.