C Programming - Module 1 - 14 Nov 2023
C Programming - Module 1 - 14 Nov 2023
Module 1
Module Coverage
• Introduction to C
• Algorithms, Flowchart and Pseudocode
• Environment setup
• Program Structure
• Header Files and Comments
• Input and Output
• Escape Sequences
• Format Specifiers
• Keywords and Identifiers
• Data Types
• Variables and Constants
Introduction to C
The C-language is a general-purpose and
procedural-oriented programming language.
It is a structured and machine-independent
programming language.
It was developed by Dennis Ritchie in 1972 at
the AT&T Bell Laboratories.
Output:
Note 3:
• When we execute program 3, the output may seem to be clumsy and
not in a good presentation.
• This is because there is no proper formatting in the output. In order to
achieve the program output in a neat format, we have to make use of
“Escape Sequences” in C programming language.
Escape Sequences
An Escape Sequence in C is used to conveniently format the strings and
outputs of a program.
The escape sequence does not appear in the output when written in a
character or string literal, rather it is represented through its
functionality.
The escape sequence consists of a backslash and any other character
from the C character set.
Below are the 15 types of escape sequences that C provides:
Escape Meaning Description
Sequence
\a Alarm or Beep Generates a beep that alerts the user when the
program is executed.
\n New Line Starts a new line in the output/string.
\t Horizonal Tab Adds a horizontal tab in the output/string.
Escape Meaning Description
Sequence
\r Carriage Return Places the cursor at the start of the current
line.
\f Form Feed Places the cursor at the start of the next
page.
\v Vertical Tab Adds a vertical tab in the output/string.
\\ Backslash Displays a backslash in the output/string.
\' Single Quote Displays a single quote in the output/string.
\" Double Quote Displays a double quote in the output/string.
\nnn Octal Number Represents an octal number.
\xhh Hexadecimal Represents a hexadecimal number.
number
\0 Null Adds a 0 character and usually denotes the
termination of a string.
\b Backspace Backspaces a single character.
\? Question Mark Displays a question mark.
Program
Aim: To enhance the program 3 and#display
4 the output in a neat format.
/
*****************************************************************************************
*******************************
File Name: program4.c
Author: vrsyrn
Date & Time : 08/11/2023; 04:15 PM
Description: a program to display the output in a neat format
*****************************************************************************************
*******************************/
#include<stdio.h> // Header file
#include<conio.h>
int main() // main function
{
clrscr();
printf("Student Name\t: Sooraj\n");
printf("Student ID\t: YE112\n");
printf("Designation\t: Trainee\n");
printf("Branch\t\t: Vijayawada\n");
printf("Qualification\t: B.Tech\n");
printf("Address\t\t: House No: 12-360\n");
printf("\t\t Modern Food Steet\n");
printf("\t\t Benz Circle\n");
printf("\t\t Vijayawada - 520010\n");
printf("\t\t Andhra Pradesh");
getch();
return 0;
}
/* End of Program */
Output:
Note 4:
• printf() statement is used to display any text on the screen.
• The Escape sequences \n and \t are used to align the text.
Format Specifiers
The Format Specifier in C is used to tell the compiler about the type of
data to be printed or scanned in input and output operations.
They always start with a % symbol and are used in the formatted string
in functions like printf(), scanf, sprintf(), etc.
The C language provides a number of format specifiers that are
associated with the different data types such as %d for int, %c for char,
etc. Below are the most commonly used format specifiers in C.
Format Description
Specifier
%d or %i It is used to print the signed integer value where signed integer
means that the variable can hold both positive and negative values.
%u It is used to print the unsigned integer value where the unsigned
integer means that the variable can hold only positive value.
Format Description
Specifier
%o It is used to print the octal unsigned integer where octal integer
value always starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where the
hexadecimal integer value always starts with a 0x value. In this,
alphabetical characters are printed in small letters such as a, b, c,
etc.
%X It is used to print the hexadecimal unsigned integer, but %X prints
the alphabetical characters in uppercase such as A, B, C, etc.
%f It is used for printing the decimal floating-point values. By default, it
prints the 6 values after '.'.
%e/%E It is used for scientific notation. It is also known as Mantissa or
Exponent.
%g It is used to print the decimal floating-point values, and it uses the
fixed precision, i.e., the value after the decimal in input would be
exactly the same as the value in the output.
Format Description
Specifier
%p It is used to print the address in a hexadecimal form.
scanf():
• When we want to take input from the user, we use the scanf() function
and store the input value into a variable. This function is defined in
the stdio.h header file.
• Examples: scanf("%x", &variable);
scanf("%d", &user_input);
getchar():
• The getchar() function reads a character from the terminal and returns
it as an integer. This function reads only a single character at a time.
• This function is defined in the stdio.h header file.
• Examples: getchar();
putchar():
• The putchar() function displays the character passed to it on the
screen and returns the same character. This function too displays
only a single character at a time. This function is defined in the
stdio.h header file.
• Examples: c = getchar();
putchar(c);
gets():
• In ‘C’ scanf( ) is not capable of receiving a multiword string, therefore,
names such as “Rajeev Sharma” would be unacceptable.
• gets() function is used to scan a line of text from input device and will
be terminated by a newline character.
• Example: printf (“enter your name");
gets (name);
puts():
• puts() writes a string to standard output which is previously read by
gets() upto but not including the null character. These are under
stdio.h header file.
• Example: printf (“enter your name");
gets (name);
puts (name);