[go: up one dir, main page]

0% found this document useful (0 votes)
7 views61 pages

Programming in C

The document outlines the curriculum for the Programming in C course (U20EST201) under the Department of Information Technology, focusing on the vision, mission, and educational objectives of the institution and department. It provides an introduction to the C programming language, including its history, structure, character sets, tokens, and constants. The document also details the steps for executing a C program and includes examples to illustrate key concepts.

Uploaded by

btechit240937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views61 pages

Programming in C

The document outlines the curriculum for the Programming in C course (U20EST201) under the Department of Information Technology, focusing on the vision, mission, and educational objectives of the institution and department. It provides an introduction to the C programming language, including its history, structure, character sets, tokens, and constants. The document also details the steps for executing a C program and includes examples to illustrate key concepts.

Uploaded by

btechit240937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

U20EST201 – Programming in C Regulation: 2020

DEPARTMENT OF INFORMATION TECHNOLGY

Year: I Semester: II
IiIII
Regulation 2020

Department of Information Technology, SMVEC 2


U20EST201 – Programming in C Regulation: 2020

VISION, MISSION AND PROGRAM EDUCATIONAL OBJECTIVES

VISION OF THE INSTITUTION


To be globally recognized for excellence in quality education, innovation and research for
the transformation of lives to serve the society.
MISSION OF THE INSTITUTION
M1: Quality Education: To provide comprehensive academic system that amalgamates the
cutting edge technologies with best practices.
M2: Research and Innovation: To foster value-based research and innovation in collaboration
with industries and institutions globally for creating intellectuals with new avenues.
M3: Employability and Entrepreneurship: To inculcate the employability andentrepreneurial
skills through value and skill based training.
M4: Ethical Values: To instil deep sense of human values by blending societal righteousness
with academic professionalism for the growth of society.

VISION OF THE DEPARTMENT


To be a pioneer in the field of Information Technology by achieving academic excellence,
involving in research & development and promoting technical & professional expertise.
MISSION OF THE DEPARTMENT
Expertise: To impart quality education and create excellent engineers with strong
analytical, Programming and Problem solving skills to meet the ever changing demands of
IT industry
Eminence: To kindle creative thinking, innovation and foster value-based research in the
field of information technology
Complaisant: To enrich the employability skills, inculcate entrepreneurial ideology and
promote professional expertise
Exemplar: To instil moral values, ethical responsibilities and empowering graduates to be
socially responsible and technically competent
PROGRAM EDUCATIONAL OBJECTIVES
PEO1 Fortify: To prepare the students with fundamental knowledge in programming
languages and in developing applications
PEO2 Equip: To develop skill in understanding the complexity in networking, security, data
mining, web technology and mobile communication so as to develop innovative applications
and projects in these areas for the betterment of society, as well as to enable them to pursue
higher education
PEO3 Endow: To enable the students as full-fledged professionals by providing opportunities
to enhance their analytical, communication skills and problem solving skills along with
organizing abilities
PEO4 Conventional: To familiarize the students with the ethical issues in engineering
profession, issues related to the worldwide economy, nurturing of current job related skills
and emerging technologies

Department of Information Technology, SMVEC 2


U20EST201 – Programming in C Regulation: 2020

UNIT I INTRODUCTION TO C
C programming: Overview of C - Visual Studio code-Constants - Compiling a C Program - Variables
and Data Types - Technical Difference between Keywords and Identifiers - Types of C Qualifiers and
format specifies - Operators and Expressions - Operators Precedence - Type conversion - Input-Output
Statements.

INTRODUCTION TO C.
 C is a general-purpose high level language that was originally developed by Dennis
Ritchie at AT & T’s Bell Laboratories in 1972.
 Many of the important ideas of C stem fromthe language BCPL(Basic Combined
Programming Language), developed by Martin Richards.

 C is a general-purpose high level language that was originally developed by Dennis


Ritchie for the UNIX operating system. It was first implemented on the Digital
Equipments Corporation PDP-11 computer in 1972.
C has now become a widely used professional language for various reasons.
 Easy to learn
 Structured language
 It produces efficient programs.
 It can handle low-level activities.
 It can be compiled on a variety of computers.

History of C language
C is one of the most popular programming language, it was developed by Dennis
Ritchie at AT & T‟s Bell Laboratories of USA in 1972.

Programming Year Founder


Language
ALGOL 1960 International Group
BCPL 1967 Martin Richards
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&R C 1978 Brain Kernighan and Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee

C is a middle language
 Low level language-0 and 1‟s.
 High level language: eg FORTRAN, PASCAL, COBOL, BASIC, C, C++……etc.
 C stands in between these two categories. It is neither a low level language nor ahigh
level language. It is a middle level language.

Features and applications of „C‟ language


 C is a general purpose, structured programming language.
 It is powerful, efficient, compact and flexible.
 It is highly portable.
 It is a robust language.
 C is a middle level language, i.e. it supports both the low level language and high level
language features.
 C language allows dynamic memory allocation.
 „C‟ programs are fast and efficient.

Department of Information Technology, SMVEC 3


U20EST201 – Programming in C Regulation: 2020

 „C‟ has got rich set of operators.


 It can be applied in systems programming areas like compilers, interpreters and
assemblers, etc…

Department of Information Technology, SMVEC 3


U20EST201 – Programming in C Regulation: 2020

STRUCTURE OF A C PROGRAM

Every C program contains a no. of building blocks. These building blocks should be written in a
correct order and procedure, to execute without any errors. The structure of C is given below.

DOCUMENTATION
SECTION
PREPROCESSOR SECTION
DEFINITION SECTION & GLOBAL
DECLERATIONSECTION

main()

{
DECLARATION

PART

EXECUTEABLE

PART

}
SUB PROGRAM SECTION

{
BODY OF THE SUB

i) Documentation Section
 The general comments are included in this section.
 The comments are not a part of executable programs.
 The comments are placed between delimiters (/* and */).
Example:
/* Factorial of a given number */

ii) Preprocessor Section


 It provides preprocessor statements which direct the compiler to link functions from the
system library.
Example:
#include <stdio.h>
#include <math.h>
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

#include <string.h>
#include<conio.h>

iii) Definition Section


 This section defines all symbolic constants.
 It refers to assigning macros of a name to a constant.

Example:
#define PI 3.14
#define TRUE 1
#define FALSE 0

iv) Global Declaration Section


 It contains variable declarations, which can be accessed anywhere within the program.

The variables that are used in more than one function throughout the program are called
global variable.
 It should declare before the main() function.
 Global variable otherwise known as External variable or Public variable
Example:
int a;
main()
{

v) main() function
 Each and every C program should have only one main() function.
 Without main() function, the program cannot be executed.
 The main function should be written in lowercase only.
 It should not be terminated with semicolon.

vi) Declaration Part


 Each and every variable should be declared before going to use those variables in
execution part.

vii) Execution part


 Here, the logic of the program can be implemented.
 These statements are known as program statements or building blocks of a program.They
provide instructions to the computer to perform a specific task.

viii) Subprogram section


 The subprogram section contains all the user-defined functions that are called inthe
main () function.
 User-defined functions are generally placed immediately after the main () function,although
they may appear in any order.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Example Program

/*Documentation Section: program to find the area of circle*/

#include <stdio.h> /*Preprocessor section*/


#include <conio.h> /* Preprocessor section*/

#define PI 3.14 /*definition section*/

float area; /*global declaration section*/

void main()

float r; /*declaration part*/

printf("Enter the radius of the circle\n"); /*executable part starts here*/

area=PI*r*r;
printf("Area of the circle=%f",area);
getch();
}

EXECUTING THE PROGRAM: COMPILATION & LINKING PROCESSES:

 Execution is the process of running the program, to execute a „c‟ program, we need
tofollow the steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library.
4. Executing the program.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Let us look at a simple code that would print the words "Hello World":#include
<stdio.h>
int main()
{
/* my first program in C */ printf("Hello, World! \n");
return 0
}
1. The
first line of the program #include <stdio.h> is a preprocessor command, whichtells
C compiler to include stdio.h file before going to actual compilation.

2. The next line int main() is the main function where program execution begins.

3. The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the program.

4. The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.

5. The next line return 0; terminates main()function and returns the value 0.

Steps to execute a C Program

1. Open a text editor and add the code.

2. Save the file as hello.c using CTRL+S or Press F2

3. Open a command prompt and go to the directory where you saved the file.

4.Give CTRL+C or ALT+F9 to compile your code.

If there are no errors in your code, the command prompt will take you to the next lineand
would generate executable file.

5.Next type CTRL+R or CTRL+F9 to execute your program.You

will be able to see "Hello World" printed on the screen

C CHARACTER SET

The character set is the fundamental raw material of any language and they are used torepresent
information.

C program are basically of two types, namely


 Source character set
 Excution character set

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

SOURCE CHARACTER SET


These are used to construct the statements in the source program.
These are of four types.

SOURCE CHARACTER SET NOTATION


Alphabets A to Z and a to z.
Decimal Digits 0 to 9
White spaces Blank space
Horizontal tabs
Vertical tab
New line
Form feed.
Special characters + plus
* asterisk
, comma
; Semicolon
? Question mark
^ Caret
$ Dollar
~ tilde
< Less than

EXECUTION CHARACTER SET


Certain ASCII characters are unprintable, which means they are not displayed on the
screen or printer. Those characters perform other functions aside from displaying text.
“Examples are backspacing, moving to a new line or ringing a bell”

ESCAPE
CHARACTE RESULT
SEQUENC
R
E
Backspace \b Moves previous position
Horizontal tab \t Moves next horizontal tab
Vertical tab \v Moves next Vertical tab
New line \n Move next line
Form feed \f Moves initial position of next page

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Back slash \l Present back slash


Null \0 Null. (end of the string)

C TOKENS
 C-Tokens otherwise known as „lexical elements‟
 The smallest individual units of a C program are known as tokens.
 In C program, tokens are categorized into six, they are given below.
C Tokens

Keywords Identifiers Constants String Special Symbols Operators

i) Keywords
Keywords are reserved words whose meaning cannot be changed. They are used toconstruct
the program. They are written in lower case. There are 32 keywords are available.

auto double int struct const float short unsigned

break else long switch continue for signed void

case enum register typedef default goto sizeof volatile

char extern return union do if static while

ii) Identifiers

 It is used to refer the name of the variables, functions and arrays.


Rules

 An identifier must begin with a letter.


 Both upper case and lower cases are permitted.
 Keywords are not permitted.
 No special symbols are permitted except underscore (_).
 Some C compiler accept under score may appear in first letter.
 Maximum length of an identifier is 31.
 Blank spaces are not allowed in between an identifier.

Example

Valid Identifier Invalid Identifier


fact fact# -> Special symbols are not allowed
Fact 3fact -> first letter should be alphabet
FACT fac t -> No balnk space allowed

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

FACT3 int -> keywords not allowed

iii) Constants ( Write about Constants and its types with example.)
 The items whose values cannot be changed during the execution of program are called
constants. ‟C‟ constants can be classified as follows.

a) Numeric constants
There are two types of numeric constants,

1. Integer constants
2. Real or floating-point constants

1. Integer constants.
 An integer constant refers to a sequence of digits without a decimal point.

Example:marks90,per75

There are three types of integer constants namely,


a) Decimal integer constant (0 to 9) (Eg:10,-321)
b) Octal integer constant (0 to 7) (Eg.: 037,052)
c) Hexadecimal (0 to 9,A,B,C,D,D,E,F) (Eg : 0x4,0xBCF)

Rules for constructing Integer constants

* An integer constant must have at least one digit.


It must not have a decimal point.
It can be either positive or negative.
* If no sign precedes an integer constant, it is assumed to be positive.
* Commas or blanks are not allowed within an integer constant.
* The allowable range of integer constants is -32,768 to +32,767.

2. Real Constant
A Real constant is made up of a sequence of numeric digits with presence of a decimal
point.
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

Rules for defining real constants


 It must have at least one digit.
 It must have a decimal point which may be positive or negative.
 If it is negative, the sign is must or if it is positive, sign is not necessary.
 Use of blank space and comma is not allowed between real constants.
 Example: distance =126.0; Height = 5.6;
b) Character Constant
There are two types, they are given below.
i) Single Character constants
ii) String constants

i) Single Character constants


A single character constant or character constant is a single alphabet, a single digit or a single
special symbol enclosed within single inverted commas. Both the inverted commas should point to the
left

Example: ‫ۥ‬m‫ۥ‬ ‫ۥ=ۥ‬ ‫ۥ‬A

ii) String constants:


A string constant is a sequence of characters enclosed in double Quotes, the charactersmay
be letters, numbers, special characters and blank spaces etc.
Example: ”hi”, “325”.

 Declaring a variable as constant:


Syntax:
const datatype variable = constant
Description:
const -it is the keyword to declare constant.
Variable -it is the name of the variable.
Datatype - it is the type of data.
Constant - it is the constant.
Example:
const int dob = 3977;

 Declaring a variable as a volatile:


Syntax:
volatile datatype variable = constant
Description:
Volatile - it is the keyword to declare volatile
Variable -it is the name of the variable.
Datatype - it is the type of data.
Constant - it is the constant.
Example:

Defining Constants

There are two simple ways in C to define constants:

1.Using #define preprocessor.


2.Using const keyword.
The #define Preprocessor

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Following is the form to use #define preprocessor to define a constant:#define


identifier value

Following example explains it in detail:


#include <stdio.h>
#define LENGTH
10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:

value of area : 50

The const Keyword

 You can use const prefix to declare constants with a specific type
as follows:const type variable = value;

Following example explains it in detail:


#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH= 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:

value of area : 50

Note that it is a good programming practice to define constants in CAPITALS.

volatile int year=2007;

C STORAGE CLASSES

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. These specifiers precede the type that they modify. There are the following
storage classes, which can be used in a C Program

 auto
 register
 static
 extern

The auto Storage Class

The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with the same storage class, auto can only beused
within functions, i.e., local variables.

The register Storage Class

The register storage class is used to define local variables that should be stored in a register
instead of RAM. This means that the variable has a maximum size equal to the register size
(usually one word) and can't have the unary '&' operator applied to it (as it does not have a
memory location).
{
register int miles;
}

 The register should only be used for variables that require quick access such as
counters.
 It should also be noted that defining 'register' does not mean that the variable will
be stored in a register.
 It means that it MIGHT be stored in a register depending on hardware and
implementation restrictions.

The static Storage Class

 The static storage class instructs the compiler to keep a local variable in existence during
the life-time of the program instead of creating and destroying it each time it comes into
and goes out of scope.
 Therefore, making local variables static allows them to maintain their values between
function calls.

 The static modifier may also be applied to global variables. When this is done, it causes
that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a class data member, it causes only one copy ofthat
member to be shared by all objects of its class.
#include <stdio.h>
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

/* function declaration */
void func(void);
static int count = 5; /* global variable */ main()
{
while(count--)
{
func();
}
return 0;
}
/* function definition */ void func( void )
{
static int i = 5; /* local static variable */ i++;
printf("i is %d and count is %d\n", i, count);
}
 You may not understand this example at this time because I have used function and
global variables, which I have not explained so far.
 So for now, let us proceed even if you do not understand it completely. When the above
code is compiled and executed, it produces the following result:
i is 6 and count is 4 i is 7 and count is 3 i is
8 and count is 2 i is 9 and count is 1 i is 10
and count is 0

The extern Storage Class

 The extern storage class is used to give a reference of a global variable that is visible to
ALL the program files.
 When you use 'extern', the variable cannot be initialized as all it does is point the variable
name at a storage location that has been previously defined.

 When you have multiple files and you define a global variable or function, which will be
used in other files also, then extern will be used in another file to give reference
ofdefined variable or function.

 Just for understanding, extern is used to declare a global variable or function in another
file.

 The extern modifier is most commonly used when there are two or more files sharing
thesame global variables or functions as explained below.

#include <stdio.h> extern int count;


void write_extern(void)
{
count = 5;
printf("count is %d\n", count);
}

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

VARIABLES WITH EXAMPLE AND SCOPE OF THE VARIABLES.


In C, a variable is a data name used for storing a data value. Its value may be changed during
program execution. The value of variables keeps on changing during the execution of a program. In
other words, a variable can be assigned different values at different times duringthe execution of a
program.
The variable can be of different data types. They can be integer, float or character data types.
These data are stored in the memory and at the time of execution different operations are performed on
them.

Rules for naming the variables:


 A variable name can be any combination of alphabets, digits or underscore.
 But first character should be alphabets or an underscore (_).
 The length of the variable cannot exceed up to 8 characters long.
 Some of the c compilers can be recognized up to 31 characters long.
 The variable should not be a C keyword.
 Both lowercase and uppercase are permitted.
 The special symbols are not permitted except underscore.

Variable declaration:
Syntax:
datatype v1,v2,v3,….,vn;

Description:
data_type - It is the type of data.
V1, v2, v3 ,…, vn - list of variables.

Example 1:
int regno;
float cgpa;
char name[10];

Example 2:
Declaration of multiple variables of the same data types can be done in one statement.

int mark1;
int mark2;
int mark3;
int mark4;

can be written to become

int mark1, mark2, mark3, mark4;

Variables are declared at three basic places:


(i) When the variables are declared inside a function, they are called local variables.
(ii) When the variables are declared in the definition of function parameters, these variablesare
called formal parameters.
(iii) When the variables are declared outside all functions, they are called global variables.

“Variables used in expressions are also known as operands”

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Initializing variables:
Initialization of variables can be done using the assignment operator(==).

Syntax:
Variable = constant;
Or
Datatype variable = constant;
Example:
A=5;
B=8;
int i =23; float
s=3.14;

Scope of variables
Scope of a variable implies the availability of variables within the program.
Two types:
 Local variables
 Global variables

(i) Local variables:


The variables which are defined inside a function block or inside a compound statement ofa
function sub- program are called local variables.

Example:
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}

(ii) Global variables:

The variables that are declared before the function main ( ) are called the
global/external variables. These are available for all the functions inside the program.

Example:
int i=4; /* Global definition */
main()
{
i++; /* Global variable */
func();
printf( "Value of i = %d -- main function\n", i );
}
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

7. DATA TYPES
Data type is the type of the data, that are going to access within the program. C supports
different data types, each data may have predefined memory requirement and storage representation.
„C‟ supports the following 4 classes of data types.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

(i) Primary data types:


C has the following basic built-in primary datatypes.
 int
 float
 double
 char

 Int (Integer)
Integer data type is used to store numeric values without any decimal point e.g. 7, -101,
107, etc.
Syntax:
int variable name;
Example:
int roll, marks, age;
 Float

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Float data type is used to store numeric values with decimal point. In other words, float data
type is used to store real values, e.g. 3.14, 7.67 etc. e.g. percentage, price, pi, area etc. may contain real
values.

Syntax:
float variable name;

Example:
float per, area;
 Char (Character)
Char (Character) data type is used to store single character, within single quotes e.g.'a',
'z','e' etc. e.g. Yes or No Choice requires only 'y' or 'n' as an answer.

Syntax:
char variable name;

Example:
char chi='a', cha;

 Double
Double is used to define BIG floating point numbers. It reserves twice the storage forthe
number. It contains 8 bytes.

double variable name;

Example:
double Atoms;

S.No C Data types storage Range


Size
1 char 1 –127 to 127
2 int 2 –32,767 to 32,767
3 float 4 1E–37 to 1E+37 with six digits of precision
4 double 8 1E–37 to 1E+37 with ten digits of precision
5 long double 10 1E–37 to 1E+37 with ten digits of precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,767 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
13 unsigned long long int 8 2(power)64 –1

(ii) User defined data type:


User defined data type is used to create new data types. The new data types
formed are fundamental data types.

Typedef:

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

The 'typedef' allows the user to define new data-types that are equivalent to existing
data types. Once a user defined data type has been established, then new variables, array,structures,
etc. can be declared in terms of this new data type.

Syntax:
typedef type new-type;

Type refers to an existing data type.


New-type refers to the new user-defined data type.

Example:
typedef int number;

Declare integer variables as: number roll, age, marks;It is


equivalent to: int roll, age, marks;

(iii) Derived Data type:


Data types that are derived from fundamental data types are called derived data types. Derived data
types don't create a new data type; instead, they add some functionality to the basic data types. Two
derived data type are - Array & Pointer.

 Array
An array is a collection of variables of same type i.e. collection of homogeneous data referred by a
common name. In memory, array elements are stored in a continuous location.

Syntax:
Datatype arrayname[ ];

Example:
int a[10];
char chi [20];

 Pointer
A pointer is a special variable that holds a memory address (location in memory) ofanother
variable.
Syntax:
datatype *var_name;

* is a pointer variable.'var_ name' is the name where the variable is to be stored.

Example:
int a,*b;

variable 'b' stores the address of variable 'a'.

 Struct
A struct is a user defined data type that stores multiple values of same or different datatypes
under a single name. In memory, the entire structure variable is stored in sequence.

Syntax:
struct < structure name>
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

{
member1;
member2;

};
Structure name is the name of structure e.g. store details of a student as- name, roll, marks. struct
student
{
Char name [20];
Int roll,
Float marks;
};

 Union
A union is a user defined data type that stores multiple values of same or different data types
under a single name. In memory, union variables are stored in a common memory location.

Syntax:
union<tagname>

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

var1;var2;

};
Tag name is the name of union, e.g, store details of a student as- name, roll, marks.

Union student
{
Char name[20];
Int roll,

Float marks;
};

(iv) Empty data type

Void
Void data type is used to represent an empty value (or) null value. It is used as a return
type if a function does not return any value.

OPERATORS AND EXPRESSIONS


Operator is a symbol that is used to manipulate arithmetic, logical and relationalexpressions.
Operator is appearing in between operands.
Eg:
A + B;
TYPES OF OPERATORS:
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and Decrement operators
 Conditional operators
 Bitwise operators
 Special operators

a) Arithmetic operators
C allows basic Arithmetic operations like addition, subtraction,
multiplication and division.

OPERATOR MEANING EXAMPLES


+ Addition 2+9=11
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

- Subtraction 9-2=7
* Multiplication 3*5=15
/ Division 9/3=3
% Modulus 9%2=1

Example:1
#include<stdio.h>
#include<conio.h>
main ( )
{
int i,j,k;
clrscr( );
i=10;
j=20;
k=i+j;
printf(“values of k is %d”,k);
} getch( );
Output:

Value of k is 30
Example:2
#include <stdio.h>

main()
{

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

int a = 21; int b = 10; int c ;

c = a + b;
printf("Line 1 - Value of c is %d\n", c );c
= a - b;
printf("Line 2 - Value of c is %d\n", c );c
= a * b;
printf("Line 3 - Value of c is %d\n", c );c
= a / b;
printf("Line 4 - Value of c is %d\n", c );c
= a % b;
printf("Line 5 - Value of c is %d\n", c );c
= a++;
printf("Line 6 - Value of c is %d\n", c );c
= a--;
printf("Line 7 - Value of c is %d\n", c );

b) Relational operator
A relational operator is mainly used to compare two or more operands.
OPERATOR MEANING EXAMPLE RETURN
< Less than 2<9 1
> Greater than 2>9 0
== Equal to 2==2 0
!= Not equal to 2!=3 0

Example:1
#include<stdio.h>
#include<coio.h>
main( )
{
clrscr( );
printf(“\n Condition: Return values\n”);
printf(“\n 5!=5 :%5d”,5!=5);
} printf(“\n 5==5: %5d”,5==5);
Output:

Condition : Return Values


5! =5 0
5==5 1
Example:2
#include <stdio.h>

main()
{
int a = 21; int b = 10; int c ;

if( a == b )
{
printf("Line 1 - a is equal to b\n" );
}
else
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

{
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b )
{
printf("Line 2 - a is less than b\n" );

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

}
else
{
printf("Line 2 - a is not less than b\n" );
}
if ( a > b )
{
printf("Line 3 - a is greater than b\n" );
}
else
{
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */ a = 5;b =
20;
if ( a <= b )
{
printf("Line 4 - a is either less than or equal to b\n" );
}
if ( b >= a )
{
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
When you compile and execute the above program, it produces the following result:
Line 1 - a is not equal to b Line 2 - a is not less

than b Line 3 - a is greater than b

Line 4 - a is either less than or equal to b Line 5 - b

is either greater than or equal to b

c) Logical operator:
Logical operators are used to combine the results of two or more conditions.

OPERATOR MEANING EXAMPLE


&& Logical AND (9>2)&&(17>2)
|| Logical OR (9>2)!!17=17)
! Logical NOT 29!=29

Example:1
#include<stdio.h>
main()
{
printf("\n Condition : return values\n");
printf("\n5>3 && 5<10: %5d",5>3 && 5<10);
printf("\n8>5||8<2 : %5d",8>5||8<2):
printf("\n!(8==8) : %5d",!(8==8));
}

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Output:

Example:2

main()
{
int a = 5; int b = 20; int c ;

if ( a && b )
{
printf("Line 1 - Condition is true\n" );
}
if ( a || b )
{
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */ a = 0;b =
10;
if ( a && b )
{
printf("Line 3 - Condition is true\n" );
}
else
{
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) )
{
printf("Line 4 - Condition is true\n" );
}

d) Assignment operator
Assignment operators are mainly used to assign a value or expression or value ofa
variable to another variable.

Operat Description Example


or

= Simple assignment operator, C = A + B will assign


Assigns values from right side value of A + B into C
operands to left side operand
Add AND assignment operator, It
adds right operand to the left C += A is equivalent to C
+= operand and assign the result to =C+A
left operand
Subtract AND assignment operator,
It subtracts right operand from the C -= A is equivalent to C
-= left operand and =C-A
assign the result to left operand

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Multiply AND assignment operator,


It multiplies right operand with the C *= A is equivalent to C
*= left operand and assign the resultto =C*A
left operand
Divide AND assignment operator,
It divides left operand with the right C /= A is equivalent to C
/= operand and assign the result =C/A
to left operand
Modulus AND assignment operator,
It takes modulus using two operands C %= A is equivalent to C
%= and assign the result to =C%A

left operand

<<= Left shift AND assignment operator C <<= 2 is same as C = C <<


2

>>= Right shift AND assignment C >>= 2 is same as C = C >>


operator 2

&= Bitwise AND assignment operator C &= 2 is same as C = C & 2

^= bitwise exclusive OR C ^= 2 is same as C = C


and assignment operator ^2

|= bitwise inclusive OR C |= 2 is same as C = C | 2


and assignment operator

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Example:
#include<stdio.h>
#include<Conio.h>
main( )
{
int i,j,k;
clrscr( );
k=(i=4,j=5);
printf(“k=%d”,k);
} getch( );
Output:
K=5

Example:
#include <stdio.h>

main()
{
int a = 21; int c ;c
=a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );

c +=a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );

c -=a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );

c *=a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );

c /=a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );

c= 200;
c %=a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );

c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );

c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

c &=2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );

c ^=2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );

c |=2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );

}
When you compile and execute the above program, it produces the following result:
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %=Operator Example, Value of c = 11
Line 7 - << = Operat or Example, Value of c = 44
Line 8 - >> = Operator Example, Val ue of c = 11
9 - -&=
Line 10
Line Operatorr Example,
^= Operato Valu eofofcc==0 2
Example ,Value
Line 11 - |= Operator Example, Value of c = 2

e) Increment and Decrement operator


„C‟ languages have two useful operators generally not found in any other programminglanguages.
They are,
1. Increment (++)
2. Decrement (--)
These operators are generally known as Unary operators

OPERATOR MEANING
++a Pre increment
--a Pre decrement
a++ Post increment
a-- Post decrement

Example:
#include<stdio.h>
#include<conio.h>
main( )
{
int a=10;
printf(“a++=%d\n”,a++);
printf(“++a=%d\n”,++a);
printf(“a--=%d\n”,a--);
} printf(“--a=%d\n”,--a);
Output:

a++=10
++a=12
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

a--=11
--a=11
f) Conditional operators

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Conditional operators itself check the condition and executes the statement dependingupon
the condition.
Syntax:

Condition ? exp1 : exp2;

Description:
“? :” operator acts as a ternary operator.It
first evaluates the condition
If it is true then „exp1‟ is evaluated.If
Example: it is false yhen‟exp2‟ is evaluated.

main( )
{
int a=5,b=3,big;
big=a>b?a:b;
printf(“Big is……%d”,big);
Output: }

Big is 5
f) Bitwise operator:
It is used to manipulate the data at-bit level. It operates only integers.
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift Left
>> Shift Right
~ One‟s Complement

P q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume if A = 60; and B = 13; now in binary

format they will be as follows: A = 0011 1100B

= 0000 1101

A&B = 0000 1100

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

A|B = 0011 1101


A^B = 0011 0001

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

~A = 1100 0011

The Bitwise operators supported by C language are listed in the following table. Assumevariable A
holds 60 and variable B holds 13, then:

Example:
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
& 00011001
________
00001000 = 8 (In decimal)

#include <stdio.h>
main()
{
int a=12,b=39;
printf("Output=%d",a&b);
}
Output

Output=4

g) Special operator: „C‟ language supports some of the special operators they are
OPERATORS MEANING
, Comma operators
Size of Size of operators
& and * Pointer operators
. and -> Member section operators
Comma operators:
It is used to operate the statement such as variables, constants,expressin etc.
Example: val= (a=3,b=9,c=77);
Sizeof( ) operator:
IT is Unary operator.It returns the length in bytes of specified variable.It returns the
length in bytes of specified variable. It is very useful to find the bytes occupied by specified variables
in the memory.
Pointer operator:
&-> this symbol is used to specify the ADDRESS of the variables.
*-> this symbol is used to specify the VALUE if a variable.

OPERATORS PRECEDENCE IN C

 Operator precedence determines the grouping of terms in an expression.


 This affects how an expression is evaluated. Certain operators have higher
precedence than others; for example, the multiplication operator has higher
precedence than the addition operator.

 For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has


higher precedence than +, so it first gets multiplied with 3*2 and then adds into
7.
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

 Here, operators with the highest precedence appear at the top of the table, those
with thelowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* &sizeof Right to left

Multiplicative */% Left to right


Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= Right to left
&= ^= |=
Comma , Left to right

Example:
#include <stdio.h>
main()
{
int a = 20; int b = 10; int c = 15; int d = 5; int e;e =
(a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a +
b) * c / d is : %d\n", e );
e = ((a + b) * c) / d;// (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d);// (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5) printf("Value
of a + (b * c) / d is : %d\n" , e );
return 0;
}
When you compile and execute the above program, it produces the following result:
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is :50

INPUT AND OUTPUT STATEMENTS

 In C language there are 2 types of I/P & O/P statements are available.

 They are: severalfunctions are available for input/output operations in C.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

 These functions are collectively known as standard I/O library.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Input & Output functions

Unformatted I/O Statements Formatted I/O Statements


Input Output
scanf( ) printf( )
getc() putc()
fscanf() fprintf( )
getchar() putchar()

gets() puts()

a) Unformatted Input/Output statements


b) Formatted Input/Output statements

a) Unformatted Input/Output statements:


 These statements are used to single/group of characters.The user cannot specify the type ofdata.
 The following are the unformatted input/output statements available in C language.

INPUT OUTPUT
getchar() putchar()
getc() putc()
gets() puts()

Single character input – getchar() function:


 A single character can be given to the computer using C input libraryfunction
getchar().
Syntax:
char variable = getchar();
Eg:
char x;
x = getchar();

 This getchar() function is written in standard I/O library. It reads a single character from a
standard input device.

Program:
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(“Enter any character/digit…”);
ch=getchar();
if(isalpha(ch)>0)
printf(“It is a alphabet”);
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

else
if(isdigit(ch)>0)
printf(“It is a digit”);

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

else
printf(“It is alphanumeric”);
}
Output:
1. Enter any character/digit…a
It is alphabet
2. Enter any character/digit…1
It is a digit
3. Enter any character/digit…#
It is alphanumeric

Single character output – putchar() function:


 It is used to display one character at a time on the standard output device.
Syntax:
putchar(character variable);
Eg:
char x;
putchar(x);

Program:
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(“Enter any alphabet either in lower or uppercase..”)ch=getchar();
if(islower(ch))
{
putchar(toupper(ch));
}
else
{
putchar(tolower(ch));
} }
Output:

Enter any alphabet either in lower or uppercase…s S


Enter any alphabet either in lower or uppercase…M
m
getc() function:
 This is used to accept a single character from the standard input to a character variable.
Syntax:
character variable=getc();
Eg:
char c;
c = getc();

putc() function:
 This is used to display a single character in a character variable to standard output device.This
function is mainly used in file processing.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

Syntax:
putc(character variable);
Eg:
char c;
putc(c);

gets() function:
 The gets() function is used to read the string from the standard input device(keyboard).
Syntax:
puts(char type of array variable);
Eg:
puts(s);
Program:
#include<stdio.h>
#include<conio.h>
main()
{
char scientist[40];
puts(“Enter name:”);
gets(scientist);
puts(“print the name”);
puts(scientist);
}

OUTPUT:
Enter name: Risha
print the name: Risha

b) Formatted Input/Output statements:


The following are the input and output statements regarding formatted statements. They
are:

INPUT OUTPUT
scanf() printf()
fscanf fprintf()

scanf() function:
 Input data can be entered into the computer using standard input C library function called
scanf().This function is used to enter any combinations of input.
 scanf() function is mainly used to read information from the standard input device keyboard.
Syntax:
scanf(“control string”,&var1,&var2,…&varn);
Eg:
int n;
scanf(“%d”,&n);

Program:
#include<stdio.h>
#include<conio.h>
main()
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

{
int m,n,a;
clrscr();
printf(“Enter the 2 numbers:”);
scanf(“%d%d”,&m,&n);

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

if(m>n)
{
a=m;
m=n;
n=a;
}
printf(“the interchanged values are: “%d%d”,m,n);
getch();
}

printf() function:
 Output data can be displayed from the computer using standard output C
library function called printf().This function is used to display any combinations of
data.
 prints() function is mainly used to display information from the standard
output device(monitor).

Syntax:
printf(“control string”,&var1,&var2,…&varn);
Eg:
int n;
printf(“%d”,n);

Program:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“Engineering Students \n”);
getch();
}

OUTPUT: Engineering Students

TYPE CASTING OR TYPE CONVERSION

Typecasting is converting one data type into another one. It is also called as data conversion ortype
conversion. It is one of the important concepts introduced in 'C' programming.
'C' programming provides two types of type casting operations:
1. Implicit type casting
2. Explicit type casting
Implicit type casting
Also known as „automatic type conversion‟
1. Implicit type casting means conversion of data types without losing its original meaning. (i.e.)
without changing the significance of the values stored inside the variable.
2. Implicit type conversion happens automatically when a value is copied to its compatible data
type.

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

3. During conversion, strict rules for type conversion are applied. If the operands are of two
different data types, then an operand having lower data type is automatically converted into a
higher data type.

char -> short int ->int ->unsigned int -> long -> unsigned long ->longlong ->
unsigned long long-> float -> double -> long double

Department of Information Technology, SMVEC


U20EST201 – Programming in C Regulation: 2020

4. Implicit type of type conversion is also called as standard type conversion. We do not
require any keyword or special statements in implicit type casting.
5. Converting from smaller data type into larger data type is also called as typepromotion.

This type of type conversion can be seen in the following example.


#include<stdio.h>
int main(){
short a=10; //initializing variable of short data type
int b; //declaring int variable
b=a; //implicit type casting
printf("%d\n",a);
printf("%d\n",b);
}

Output
10
10

1. const
CONST
2. KEYWORD:
volatile
 Constants are also like normal variables. But, only difference is, their values can‟t be
modified by the program once they are defined.
 They refer to fixed values. They are also called as literals.
 They may be belonging to any of the data type.
 Syntax:
constdata_typevariable_name; (or) constdata_type *variable_name;
 Please refer C – Constants topic in this tutorial for more details on const keyword.

2. VOLATILE KEYWORD:
 When a variable is defined as volatile, the program may not change the value of the variable
explicitly.
 But, these variable values might keep on changing without any explicit assignment by the
program. These types of qualifiers are called volatile.
 For example, if global variable‟s address is passed to clock routine of the operating system
to store the system time, the value in this address keep on changing without any assignment
by the program. These variables are named as volatile variable.
 Syntax:
volatile data_typevariable_name; (or) volatile data_type *variable_name;

Format specifiers in C are used for input and output purposes. Using format specifier the

TYPES OF C QUALIFIERS:
There are two types of qualifiers available in C language.
They are,compiler can understand that what type of data is in input and output operation.
There are some elements that affect the format specifier. Below, I have mentioned elementsthat
Department of Information Technology, SMVEC
U20EST201 – Programming in C Regulation: 2020

affect the format specifier.

1. A minus symbol (-) sign tells left alignment

Department of Information Technology, SMVEC


2. A number after % specifies the minimum field width. If the string is less than the width, it willbe
filled with spaces

3. A period (.) is used to separate field width and precision.

Difference between %d and %i format specifier in C

When you are printing using the printf function, there is no specific difference between the %i and %d
format specifiers. But both format specifiers behave differently with scanf function.

The %d format specifier takes the integer number as decimal but the %i format specifier takes the
integer number as decimal, hexadecimal or octal type. it means the %i automatically identified the base
of the input integer number.

Format Specifier Type

%c Character

%d Signed integer

%e or %E Scientific notation of floats

%f Float values

%g or %G Similar as %e or %E

%hi Signed integer (short)

%hu Unsigned Integer (short)

%i integer

%l or %ld or %li Long

%lf Double

%Lf Long double

%lu Unsigned int or unsigned long

%lli or %lld Long long

%llu Unsigned long long

%o Octal representation

%p Pointer

%s String

%u Unsigned int

%x or %X Hexadecimal representation

48
Format Specifier Type

%n Prints nothing

%% Prints % character

#include <stdio.h>
int main()
{
int data = 65;
printf("%c\n", data);
return 0;
}

Output

Example
#include <stdio.h>
int main()
{
int data1, data2, data3;
printf("Enter value in decimal format:");
scanf("%d",&data1);
printf("data1 = %i\n\n", data1); printf("Enter
value in hexadecimal format:");
scanf("%i",&data2);
printf("data2 = %i\n\n", data2);
printf("Enter value in octal format:");
scanf("%i",&data3);
printf("data2 = %i\n\n", data3);
return 0;
}
49
Example
#include <stdio.h>
int main()
{
float data = 6.27;
printf("%f\n", data);
printf("%e\n", data);
return 0;
}
Output

6.270000
6.270000e+000

Example
#include <stdio.h>
int main()
{
float data = 6.276240;
printf("%f\n", data);
printf("%0.2f\n", data);
printf("%0.4f\n", data);
return 0;
}

Output:

6.276240
6.28
6.2762

Example
#include <stdio.h>
int main()
{
int pos = 14; float
data = 1.2;
printf("%*f",pos,data); return
0;
}

Ans:

50
The output of the above code will be 1.200000 with 6 space.

Explanation:

51
Here 1.200000 is printing with, 6 spaces, because by giving * in printf we can specify an additional
width parameter, here „pos‟ is the width and „data‟ is the value. if the number is smaller than the width
then rest is filled with spaces.

Differences between %f, %e and %g format specifiers in C language

Example
#include <stdio.h>
int main(void)
{
double data1 = 123456.0;
printf("%e\n", data1);
printf("%f\n", data1);
printf("%g\n", data1);
printf("\n");
double data2 = 1234567.0;
printf("%e\n", data2);
printf("%f\n", data2);
printf("%g\n", data2); return
0;

}
Output:

1.234560e+005
123456.000000
123456

1.234567e+006
1234567.000000
1.23457e+006

Explanation:

When using the G ( or g) conversion specifier, the double argument representing a floating-pointnumber
is converted in style f or e (or in style F or E ), depending on the value converted and the precision.

#include <stdio.h>
int main()
{
int data = 65;
printf("%o\n", data);
return 0;

Output: 101
52
Format specifier (Hexadecimal number): %x, %X

#include <stdio.h>
int main()
{
int data = 11;
printf("%x\n", data);
return 0;
}

Output: b

Format specifier (character array or string): %s

#include <stdio.h>
int main()
{
charblogName[] = "aticleworld";
printf("%s\n", blogName); return
0;

}
Output: aticleworld

Use of special elements with %s

#include <stdio.h>

int main()

charblogName[] = "aticleworld";

printf("%s\n", blogName);

printf("%24s\n", blogName);

printf("%-24s\n", blogName);

printf("%24.6s\n", blogName);

printf("%-24.6s\n", blogName);

return 0;

}
53
Single program

#include<stdio.h>

main()

Char ch='B';

printf("%c\n",ch);//printing character data

//print decimal or integer data with d and iint

x =45, y =90;

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

printf("%i\n", y);

float f =12.67;

printf("%f\n", f);//print float value

printf("%e\n", f);//print in scientific notationint

a =67;

printf("%o\n", a);//print in octal format

printf("%x\n", a);//print in hex format

charstr[]="Hello World";

printf("%s\n",str);

printf("%20s\n",str);//shift to the right 20 characters including the string

printf("%-20s\n",str);//left align

printf("%20.5s\n",str);//shift to the right 20 characters including the string, and print stringup to 5
character
54
printf("%-20.5s\n",str);//left align and print string up to 5 character

55
Output

B
45
90
12.670000
1.267000e+001
103
43
Hello
World Hello
World Hello
WorldHello
Hello

INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)


 Integrated Development Environment or IDE for short is an application or software which
programmers use for programming.
 It helps a programmer to program easily by providing all comprehensive facilities required for
the development of software.
 IDE can improve the productivity of a programmer or developer because of its fast setup and
various tools.
 Without this, a programmer takes a lot of time deciding various tools to use for their tasks.

 Mainly, an IDE includes 3 parts i.e. source code editor, build automation tool (compiler) and a
debugger.

 The source code editor is something where programmers can write the code, whereas, build
automation tool is used by the programmers for compiling the codes and the debugger is used
to test or debug the program in order to resolve any errors in the code.

 Furthermore, these IDEs also comes with additional features like object and data modeling,
unit testing, source code library, and a lot more.

 As of now, several IDEs are available for various programming languages like Python,
C++,Java, JavaScript, R and others. The modern IDEs even possess intelligent code completion
for maximizing the programmer‟s productivity.

Advantages of Using IDEs

 These are simple editing environments consisting of several features making coding
quick and efficient.
 Takes less time and effort- It includes various tools and features that help to prevent
mistakes, organizes resources and provide shortcuts.
 It allows quick navigation to the type
 Programmers can quickly navigate to other members by using hyperlinks
 IDEs organize imports and can add appropriate imports
56
 It can give warning in case of any errors or mistakes
 IDEs are great for generating code or completing the code depending upon previous
codes.

57
VISUAL STUDIO CODE
 Visual Studio Code is a streamlined code editor with support for development operations like
debugging, task running, and version control.
 It aims to provide just the tools a developer needsfor a quick code-build-debug cycle and leaves
more complex workflows to fuller featured such asVisualStudioCode.

 It is an open-source code editor developed by Microsoft for Windows, Linux and Mac OS.
Visual Studio Code is based on an Electron framework.
 According to a survey done in 2018 by Stack Overflow, it was ranked the most popular
developer environment tool among others.
 Furthermore, this IDE is also customizable which lets programmers change the theme, keyword
shortcuts and preferences.

How to run a C program in Visual Studio Code?

A visual studio code is a lightweight software application with a powerful source code editor that runs
on the desktop. It is a free source code editor developed by Microsoft for Windows, Mac OS and Linux.
It is a software editor that has a rich extension of various languages like C++,C+, C, Java,
Python, PHP, Go, etc. and runtime language extensions such as .NET and Unity. Itis easy to edit,
build, syntax highlighting, snippets, code refactoring and debugging. In visual studio code, we can
change the application's background theme, keyboard shortcuts set on our preferences, install an
extension and add additional functionality.

Prerequisites for running a C program in Visual Studio Code

1. We should have a basic knowledge of C programming.

2. The Visual Studio Code Editor must be installed in the system.

3. Download the C/C++ Extension. It is an extension provided by Microsoft that support visual

studio code. It helps in IntelliSence, debugging and code browsing of the programming code in
the visual studio.

4. Download the C/C++ compilers. There are some popular compilers are:

1. GCC on Linux

2. GCC via Mingw-w64 on Windows

3. Microsoft C++ compiler on windows

4. Clang for XCode on MacOS

58
code.visualstudio.com. Visual Studio Code is a free source-code editor made by Microsoft for
Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent
code completion, snippets, code refactoring, and embedded Git.

Visual Studio Code

Key Benefits:

 Support for Debugging


 Syntax highlighting
 Intelligent Code completion, snippets and code refactoring
 EmbeddedGit Control
 Completely portable
 Easy customization

Programming Languages Supported: C, C++, C#, CSS, Go, HTML, Java, JavaScript, Python, PHP,
TypeScript and much more.

59
Download & Install the C/C++ Extension

1. We need to click on the extension button that displays a sidebar for downloading and
installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension.

2. After that, click on the C/C++

In this image, click on the Install button to install the C/C++ extension.

3. After clicking the Install button, it shows the below image.

Download and Install Compiler Extension

A MinGW is an advanced GCC compiler software used to compile and execute code. It issoftware
that supports only the window operating system.

60

You might also like