Escape Sequences and Comments in C

Escape Sequences and Comments in C

31 Mar 2024
Beginner
652 Views
9 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Lines and Comments in C: An Overview

Do you know how to insert new lines in your code? Have you ever heard of Comments 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 two printf() 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 \nindicates a new line

    Output

    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
    

  • Some important escape sequences
    Escape SequenceMeaning

    \a

    Alarm or Beep

    \b

    Backspace

    \f

    Form Feed

    \n

    New Line

    \r

    Carriage Return

    \t

    Tab (Horizontal)

    \v

    Vertical Tab

    \\

    Backslash

    \'

    Single Quote

    \"

    Double Quote

    \?

    Question Mark

    \nnn

    octal number

    \xhh

    hexadecimal number

    \0

    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

types of comments in c programming language

  1. 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.

  1. 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.
Share Article
Batches Schedule
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this