PC Unit 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

UNIT I - INTRODUCTION TO C

 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

1
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

PART – A (2 Marks)

Two mark questions with answer

1. What is an algorithm?

Algorithm means the logic of a program. It is a step-by-step description of how to arrive


at a solution of a given problem.
2. What are the characteristics of an algorithm?

(or) Advantages of algorithm


 In algorithms each and every instruction should be precise.
 In algorithms each and every instruction should be unambiguous.
 The instructions in an algorithm should not be repeated infinitely.
 Ensure that the algorithm will ultimately terminate.
 The algorithm should be written in sequence.
 It looks like normal English.
 The desired result should be obtained only after the algorithm terminates.

3. How many types the Algorithm can be represented?


 Normal English
 Program
 Flowchart
 Pseudo code
 Decision table

4. What is Flowchart? Why is flowchart required?

Flowchart is a pictorial representation of an algorithm. It is often used by programmer as


a program planning tool for organizing a sequence of step necessary to solve a problem by a
computer.

5. What is pseudo code?

“Pseudo” means imitation or false and “code” refers to the instruction written in the
programming language. Pseudo code is programming analysis tool that is used for planning
program logic.

6. What are the rules for drawing a flowchart?

• The standard symbols should only be used.


• The arrowheads in the flowchart represent the direction of flow of control in the
problem.
• The usual direction of the flow of procedure is from top to bottom or left to right.
• The flow lines should not cross each other.
• Be consistent in using names and variables in the flowchart.
• Keep the flowchart as simple as possible.
• Words in the flowchart symbols should be common statements and easy to understand.
• Chart main line of logic, and then incorporate all the details of logic.
• If a new page is needed for flowcharting, then use connectors for better representation.

7. What are the rules for writing pseudo code?

• Write on statement per line.


• Capitalize initial keywords.
• Indent to show hierarchy.
• End multi line structure.
• Keep statements language independent.

2
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

8. List out the basic design structure? (or) basic logic structures.
• Sequence structure
• Selection structure
• Loop structure

9. List out the advantages of flowchart.

 Communication
 Effective analysis
 Proper documentation
 Efficient Coding
 Proper Debugging
 Proper testing
 Efficient Program Maintenance

10. What are all the disadvantages of flowchart or limitations?

 Complex logic
 Alteration or Modification
 Reproduction
 Unknown
 Cost

11. List out the advantages of Pseudo code.

 It can be done easily in any word processor.


 It can be easily modified as compared to flowchart.
 Its implementation is very useful in structured design element.
 It can be written easily.
 It can be read and understood easily.

12. List out the disadvantages of Pseudo code.

 It is not visual.
 We do not get a picture of the design.
 There is no standard style or format.
 One pseudocode may be different from another.
 For a beginner, it is more difficult to follow the logic or write pseudocode as compared
to flowchart.

13. Write two characteristics of pseudo code.

(i) Write one statement per line.


(ii) Capitalize initial keyword.
(iii) Indent to show hierarchy.
(iv) End multiline structure.
(v) Keep statements language independent.

14. Draw a flowchart to find the bigger of two numbers.

3
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

15. Differentiate flowchart, pseudocode and program.

FLOWCHART PSEUDOCODE PROGRAM


A flowchart is a Pseudo code is made up of two A program consists of a
pictorial representation words: pseudo and code. Pseudo series of instructions that a
of code, using box means imitation, code refers to computer processes to
shapes and arrows. instructions, written in a perform the required
programming language operation

16. Distinguish between Logical and bitwise operators.

Logical Operators
These operators are used to perform logical operations on the given expressions.
 There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and
logical NOT (!).

S.no Operators Name Example Description

1 && logical (x>5)&&(y<5) It returns true when both conditions


AND are true

2 || logical (x>=10)||(y>=10) It returns true when at-least one of


OR the condition is true

3 ! logical !((x>5)&&(y<5)) If “((x>5) && (y<5))” is true,


NOT logical NOT operator makes it false

Bit wise Operators

 These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
 Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^
(XOR), << (left shift) and >> (right shift).

OPERATOR MEANING DESCRIPTION


& Bitwise AND Binary AND Operator copies a bit to the result if it exists in
both operands.
| Bitwise OR Binary OR Operator copies a bit if it exists in either
operand.
^ Bitwise XOR Binary XOR Operator copies the bit if it is set in one
operand but not both.
<< Shift left Binary Left Shift Operator. The left operands value is moved
left by the number of bits specified by the right operand.
>> Shift right Binary Right Shift Operator. The left operands value is
moved right by the number of bits specified by the right
operand.
~ One‟s Binary Ones Complement Operator is unary and has the
complement effect of 'flipping' bits.

17. What is meant by operator precedence?


 Outermost parenthesis is evaluated first.
 Then innermost parenthesis.
 If there is two or more parenthesis, then the order of execution is from left to right.
 Next multiplication and division are performed.
 Finally addition and subtraction.

4
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

18. Explain briefly about void type.


The void type, in several programming languages derived from C and Algol68, is the type for
the result of a function that returns normally, but does not provide a result value to its caller.

19. What is expression?


An expression represents a combination of variables, constants, and operators arranged as
per the syntax of the language.
Expressions are evaluated using an assignment statement of the form
variable = expression;
Example : 3.14 * r * r
s * (s - a) * (s - b) * (s - c)

20. Write the basic structure of C program.


Documentation Section
Header File Section / Link Section
Definition Section
Global (Variable) Declaration Section
main()
{
Declaration(local variable) part
Executable part
}
sub program section
function 1
function 2


Function n

21. What do you mean by structural programming?


 C is a structured language.
 A structured language allows variety of programs in small modules.
 It is easy for debugging, testing and maintenance if a language is a structured one.

22. What is the difference between the two operators = and ==?
The Assignment Operator evaluates an expression on the right of the expression and
substitutes it to the value or variable on the left of the expression. The general form is
Syntax: identifier = expression
Eg: x = a + b;

Equality operator ( == ) is used together with condition. The value of the expression is one or
zero. If the expression is true the result is one, if false result is zero.

Syntax: if ( identifier == expression)


Example : if (x == 1) { statement }

23. Give any two programming languages ?

(1) C Language:
C is a popular general purpose programming language. We can write the codes for operating
system, application programs and assembly language programs in C language
(2) PASCAL:
Pascal is an influential imperative and procedural programming language, designed in 1968–
1969 and published in 1970 by Niklaus Wirth as a small and efficient language intended to
encourage good programming practices using structured programming and data structuring.

5
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

24. Write about increment and decrement operators?

Increment Operators
It is used to increase the value of the Operand by 1. There are two types of Increment
Operators in C Language. They are pre Increment operator and post Increment operator.
Eg: ++g -> pre increment
g++ -> post increment

Decrement Operators
It is used to decrease the value of the Operand by 1. There are two types of Decrement
Operators in C Language. They are pre decrement operator and post decrement operator.
Eg: --g ----> pre decrement
g-- ----> post Decrement

25. List out the various stages in evolution of C by a Table form?

Language year Founder


ALGOL 1960 International group
BCPL 1967 Martin Richards
B 1970 Ken Thompson
C 1972 Dennis Ritchie
K & RC 1978 Kernighan and Ritchie
ANSI C 1989 ANSI committee
ANSI / ISO C 1990 ISO Committee

26. What is the purpose of main ( ) in C?


Main ( ) is a special function used by the c system to tell the computer where the
program starts. Every program must have exactly one main function. The empty parenthesis
immediately following main indicates that the function main has no arguments.

27. What is difference between a constant and a variable?


The constants are data items, the value of a constant cannot be changed during
execution of the program. But we can assign different values to a variable when every necessary
in our program.

28. What is importance of keywords in C?


Certain reserved words are called as keyword. It has standard and predefined meaning in
„C‟ language. Some of them are
 auto
 double
 float
 void

29. Is „main‟ a keyword in C language? Justify your answer.


 Yes main is a keyword.
 It is used to mention main function of C program.
Example
main()
{
----
----
}
30. Write any four escape sequences in C.
(i) \n - new line

6
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
(ii) \t - tab
(iii) \a - alert
(iv) \0 - null

31. What is type casting?


C allows programmer to convert the type of an expression from one form to another by
placing the desired type in parentheses is before the expression. This is known as type casting.

Syntax :
(type-name) expression;

32. What are the features of C program?


„C‟ programs are fast and efficient.
„C‟ is highly portable
„C‟ is powerful, efficient, compact, and flexible.
„ C‟ has got rich set of operators.
It is a middle level language.
33. List out the rules to be followed in forming in identifier?
Rules:
 First letter should be alphabets.
 Both numeric and alphabets are permitted.
 Both lower case and upper case are allowed.
 No special symbols are permitted except underscore (_).
 Keywords are not permitted.
 Blank space is not allowed.

Eg: STDNAME, sub , TOT_MARKS.

34. What is identifier? Give any two examples for an identifier.


Or
What is a Variable ? illustrate with an example
Identifiers are names given to various program elements such as variables, functions and
arrays etc.
Eg: STDNAME, sub , TOT_MARKS.

35. How to declare the variables?


Variables should be declaring inside the main function. It tells the compiler what is the
name of the variable, and type of the data that variable hold.
Syntax :
data_type v1 . v2. vn;

36. List out the rules for constructing a variable?


 A variable name can be any combinations of 1 to 8 alphabets, digits
 No commas or blank spaces are allowed.
 First character must be alphabet.

37. What is meant by constants?


The items whose values cannot be changed during the execution of program
Eg:
marks=90;
Discount = 15;

38. What is operator? What are the different types of operators in C?


An operator is a symbol that specifies an operations to the performed on operands
operators are used in programs to manipulate data and variables.
 Arithmetic operators
 Relational operators
 Logical operators

7
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
 Assignment operators
 Increment and decrement operators
 Conditional operators
 Bitwise operators
 Special operators

39. What is an Operator and Operand?


An operator is a symbol that specifies an operation to be performed with the help of
operands.
E.g:
a+b here a, b  operands; (+) indicates operator

40. What is Ternary operator?


It is also known s conditional operators. It checks the conditions and executes the
statement.
Syntax : condition? exp1: exp2;

Description: “?: ” operator acts as a ternary operator


o It first evaluates the condition
o If it is True then „exp1‟ is evaluated
o If it is False then „exp2‟ is evaluated

41. Give two examples for logical and relational expressions.


Relational Expressions:
(i) (a>b)
(ii) (a==b)
Logical Expressions:
(i) if((a>b)&&(a>c))
(ii) if((a>b)||(a>c))

42. What are the Bitwise operators available in C ?

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

42. What is meant by unary operator?


“ C “ language has two useful operators generally not found in any other programming
languages. They are

 Increment ( ++ )
 Decrement ( -- )
These operators are generally known as unary operators.
Operator Meaning
++a Pre increment
--a Pre decrement
a++ Post increment
a-- Post decrement

8
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
43. What you mean by C tokens?
„C‟ language contains the individual units called C tokens . They are:
 Identifiers
 Keywords
 Constants
 Strings
 Operators
 Special symbols

44. What is the purpose of assignment operator?


Assignment operator are mainly used to assign a value or expression or value of a
variable to another variable.
Syntax :
variable = expression;
E.g :
x = 10;
X= a+b;

45. List different data types available in C?


Data types is a name refer to the kind of data in a program. C++ data types can be
classified as.
Four fundamental data types are
 Primary data types
 User defined data types
 Derived data types
 Empty
46. What are expressions?
An expression represents data item such as variables, constants, which are
interconnected with operators as per the syntax. An expression is evaluated using assignment
operator
Syntax : variable = expression;
Example : x=a*b-c;

47. What is meant by control string? Give some examples?


It is the type of data that the user going to accept via input statements.
Examples:
% c  single character
% s  Strings
% f  float values
% d  decimal integer
48. Give a note about 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) ;

E.g: int n;
scanf(“%d”,&n);
49. Give a note about 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.
printf( ) function is mainly used to display information from the standard output device
Monitor.

Syntax:
printf( “control string “ , &var1,&var2,…&varn);

9
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
E.g:
int n;
printf(“%d”,&n);

50. Write the rules to be followed in „C‟ language ?


 All the statements should be written in lower case.
 White spaces are not allowed while declaring variables ,keyword, function.
 All the statement should be ended with semi-colon ( ;)
 Upper case letters are used only for Symbolic statements.
 User can write one or more statements in a single line by separating them with a
semicolon (;).

51. What are all the characteristics of a „C‟ program ?


 Clarity
 Integrity
 Simplicity
 Efficiency
 Modularity
 Generality ( overview)

52. How to initialize a variable? Give example?


Initialisation of a variable can be done using assignment operator( = )
Syntax :
variable = constant;
E.g:
i= 29; j=50;
int i=25;float k = 45;

53. List the Various input and output statements in C.

Unformatted Input and output statement


 getc()
 getchar()
 gets()
Output:
 putc()
 putchar()
 puts()
Formatted input/output statements
 scanf()
 fscanf()
Output
 printf()
 fprintf()

54. Difference between scanf() and printf() functions with examples.


Scanf() Printf()
It is standard input „C‟ library function. It is standard output‟C‟ library function.
It is a function used to read information It is a function used to display the
from the keyboard. information through monitor.
Scanf() function starts with string Printf() function starts with string
arguments and may contains additional arguments and may contains additional
arguments. arguments
E.g: int n ; E.g: int n ;
Scanf(“%d”,&n); printf(“Result is……%d”,&n);

55. Compare Global variable and local variable with examples.


Global variable Local variable
Variables that are declared before the Variables that are declared after the

10
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
main ( ) function is known as global main( ) function is known as local
variables. variables.
The scopes of the variables are The scopes of the variables are
throughout the program. representing only in one block ( main
block )
E.g: int a,b; E.g: main ( )
main( ) {
{ int a, b;
……… ………
} }

56. Difference between high level language and low level language.
Low Level High level
It is representing in 0‟s and 1‟s at bit It is represent in terms of normal
level. English.
Programmers can carry out operations at Programmers are design in such a way
bit level for better efficiency.

57. Give the steps for executing a „C‟ program?


Execution is the process of running the program to execute a program. There are four
steps to be followed;
 Creating the program
 Compiling the program
 Linking the program with system library
 Executing the program

58. What are delimiters? List out the delimiters.


These are the special symbols which have syntactic meaning and have got significance.
It will not specify any operations.
Symbol Name Meaning
# Hash Pre-processor directive
: Colon Label Delimiters
; Semicolon Statement Delimiters
() Paranthesis Used in functions
{} Curly braces Used for „c‟ block structure

59. Write a „C‟ program to implement the expression ((m+n)/(p-m)*m), where


m=4,n=6,p=8.
main()
{
int m=4,n=6,p=8;
float e;
e=(m+n)/(p-m)*m;
printf(“%f”,e);
}
60. Write the following conditions using “?” operator.

4x + 100 for x<40


Salary =
300 for x=40

4.5x + 150 for >40


Salary = x<40?4*x+100:x>40?4.5*x+150:300;

11
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

61. Write the limitations of using getchar() and scanf() functions for reading strings.

getchar(): It is written in standard I/O library. It reads a single character only from a
standard input device. This function is not use for reading strings.

scanf(): It is use for reading single string at a time. When there is a blank was typed,
the scanf() assumes that it is an end.
62. What is compilation Process?

Compilation translates the source program into assembly instructions, which are then
converted to machine instructions. Finally, the linking process establishes a connection to the
operating system for primitive.

63. Discuss the working modulo operator.

The modulo operation finds the remainder after division of one number by another
(sometimes called modulus).
Example
15%2=1;

64.What are variables? Give examples.

A variable is the reference of the memory location. A variable may change in value at the
time of program execution. Depending on conditions or on information passed to the program.
Example

int m1,m2,tot;
float avg;

65. Define implicit type conversion.

The ability of some compilers to automatically insert type conversion functions where an
expression of one type is used in a context where another type is expected.
66. Problem solving with computers involves several steps.

 Clearly define the problem.


 Analyze the problem and formulate a method to solve it
 Describe the solution in the form of an algorithm.
 Draw a flowchart of the algorithm.
 Write the computer program.
 Compile and run the program (debugging).
 Test the program (debugging)
 Interpretation of results.

67. Define Algorithm.

An algorithm is a part of the plan for the computer program.


An algorithm is an effective procedure for solving a problem in a finite number of steps.

Definition
 It is “step by step logical description of a program”.
 It is one of the “Program Designing Tools”.
 It is used in “formal design”

12
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
68. List the Characteristics of an Algorithm
 The algorithm should be in sequence manner.
 It can be constructed in normal English
 Each and every instruction should be precise and unambiguous. (each and every
instruction should be clear and should have only one meaning)
 Each instruction should be performed in finite time.
 One or more instructions should not be repeated infinitely.
 After the instructions are executed, the user should get the required results.

69. List out Qualities of a Good Algorithm .

 Time: The better algorithm is to execute a program within lesser time.


 Memory: The better algorithm is to execute a program that takes lesser amount of
memory storage.
 Accuracy: Multiple algorithms may provide correct solutions to a given problem, some of
these may provide more accurate results than others.
 Sequence: The procedure of an algorithm must form in a sequence.
 Generability: The designed algorithm must solve isolated problem and more often
algorithms are designed to handle a range of input data to meet this criteria.

70. Define Flowchart.

Definition
 It is “Pictorial or diagrammatic representation of an algorithm or logic of
the program”.
 It is one of the “Program Designing Tools”.
 It is used in “formal design”.

Types of Flowchart
 Document flowcharts, showing controls over a document-flow through a system
 Data flowcharts, showing controls over a data-flow in a system
 System flowcharts showing controls at a physical or resource level
 Program flowchart, showing the controls in a program within a system

71. Draw and explain the various symbols of flowchart.

Flowchart Symbols

13
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

72. What are the various guidelines to be followed while drawing a flowchart?

Rules for Drawing A Flowchart


 The standard symbols should only be used.
 The flow lines should be from top to bottom or left to right.
 Only one flow line should enter into process and only one should exit from it.

 Only one flow line should enter into I/O symbols and only one should exit from it.

 Only one flow line is used in conjunction with terminal symbol.

 Only one flow line should enter into decision symbol and one or two may exit from it. (in

 some case three lines are also exit from it)


 The flow lines should not intersect each other.
 Make the flowchart as simple.
 Use simple & common word in the flowchart symbols

73. Discuss about basic design structures in flowchart


Basic design structures in flowchart
In general, there are three types of designs available, they are
 Sequence
 Selection
 Loop
Top tested loop
Bottom tested loop
(i) Sequence Structure
It is a sequence or linear structure because the flow always continues in same direction
every time the structure is executed.

(ii) Selection Structure


• This structure includes the decision and the operations to be performed in response to
the decision.
• This is also known as “two way branching structure”.

14
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

(iii) Loop Structure


• The loop structure is used to execute a sequence of steps in a no. of times or until a
specific condition going to occur false.
• There are two concepts available, they are
(a) Top tested loop.
(b) Bottom tested loop.
(a) Top tested loop
The condition is appearing always at the first statements of the loop. If it is true, the
control will transfer into body of the loop otherwise it will terminate.

(b) Bottom tested loop


The condition is appearing always at the last statements of the loop. If it is true, the
control will transfer into body of the loop otherwise it will terminate.

15
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

74. Discuss the advantages and limitation of flowcharting

Advantages of Flowchart
 Communication
 Effective analysis
 Proper documentation
 Efficient Coding
 Proper Debugging
 Proper testing
 Efficient Program Maintenance
Disadvantages of Flowchart
 Complex logic
 Alterations and Modifications
 Reproduction
 Cost
 Unknown

75. Define Pseudocode.

Definition
 It is one of the “design tool”.
 Pseudocode is combination if „Pseudo‟ and „Code‟
 Pseudo Imitation (False)
 Code Instruction (Program)
 It is half – way communication between design and development phase.

76. Rules for Writing Pseudo code.


There are some rules available that should be following while writing psudocode. They
are given below.
(vi) Write one statement per line.
(vii) Capitalize initial keyword.
(viii) Indent to show hierarchy.
(ix) End multiline structure.
(x) Keep statements language independent.
(i) Write one statement per line.
Write Pseudocode for each and every task and sub-task. But only condition is use one
action or statement per line then writes another statement in next new line.
Eg:
Tasks are:

Read name and


marks. Calculate
total and average.
Print the outputs.
Pseudocode:

READ name, m1, m2, m3, m4, m5


Tot = m1+m2+m3+m4+m5
Avg = Tot / 5

WRITE Tot, Avg


(ii) Capitalize initial keywords.
The keywords in the pseudocode should be written in all capital letters that give specific
meaning to the operation.
Eg:
The keywords are
READ or INPUT or GET

16
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
WRITE or OUTPUT or PUT or
PRINT IF, THEN, END IF

WHILE, UNTIL, LOOP


(iii) Indent to show hierarchy.
Indentation is the process of showing the “boundaries” of the structure. It is used for
readability.
Eg:
READ name, m1, m2, m3, m4, m5
Tot = m1+m2+m3+m4+m5
Avg = Tot / 5
IF Avg > 75 THEN
Rank = Distinction
END IF
WRITE Tot, Avg, Rank.

(iv) End Multiline Structure.


The multiline structure must be ended for readability and clarity. It is also helps to make
sure that you know where the loop or selection is completed or ended.
Eg:
IF a>b THEN
WRITE A is greater
ELSE
WRITE B is
greater END IF;
(or)
WHILE(n > 0)

-------------
-------------
END WHILE
(v) Keep statements language independent.
Here languages refer programming languages. Our pesudocode is not dependent to the
program coding.

77. What is the difference between the = operators and the == operator?

Assignment Operator (=)


= assigns the value of right side expression‟s or variable‟s value to the left side variable.
Example: x = (a +b) y=x

Equal To Operator (==)


while == is an Equal to Operator and it is a relation operator used for
comparison (to compare value of both left and right side operands).
Example: x=5 y=2 if(x==y)

78. What is a Program?

A program consists of a series of instructions that a computer processes to

perform the required operation. In addition, it also contains some fixed data,

required to perform the instructions, and the process of defining those instructions and

data.

A programmer must determine three basic requirements:

17
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
1. The instructions to be performed
2. The order in which those instructions are to be performed.
3. The data required to perform those instructions.
To perform a task using a program, a programmer has to consider various inputs of the
program along with the process, which is required to convert the input into desired output.
Suppose we want to calculate the sum of two numbers, A and B, and store the sum in C, here A
and B are the inputs, addition is the process, and C is the output of the program.

79. List out the characteristics of a program.

Any computer program should have the following characteristics

 Accuracy of calculations: Any calculation made in the program should be correct and
accurate.
 Clarity: It refers to the overall readability of the program which helps the user to
understand the underlying program logic easily without much difficulty.
 Modularity: When developing any program, the task is sub divided into several modules
or subtasks. These modules are developed independently (i.e.) each task does not
depend on the other task.
 Portability: Portability is defined as the ability to run the application program on
different platforms.
 Flexibility: Any program written should be flexible (i.e.) the program should be
developed in such a way that it can handle the most of the changes without rewriting the
entire program.
 Efficiency: Any program needs certain memory and processing time to process the data.
This memory and processing unit should be of least amount. This is the efficiency of the
program.
 Generality: The program should be in general. If a program is developed for a specific
task then it can be used for all the similar tasks in the same domain.
 Documentation: any application program developed should be well documented such
that even in the absence of the developer and the author, the programmers could be able
to understand the concept behind it.

80. List problem solving tools.


1. Algorithm
2. Flowchart
3. Pseudo code

18
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

PART – B ( 5 Marks & 10 Marks)

1. 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 from
the 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 a
high 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.
 „C‟ has got rich set of operators.
 It can be applied in systems programming areas like compilers, interpreters and
assemblers, etc…

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

19
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

DOCUMENTATION SECTION

PREPROCESSOR SECTION

DEFINITION SECTION & GLOBAL DECLERATION


SECTION

main()

{
DECLARATION PART

EXECUTEABLE PART

}
SUB PROGRAM SECTION

{
BODY OF THE SUB
PROGRAM
}

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

20
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 in
the main () function.
 User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.

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*/


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

3. EXECUTING THE PROGRAM:

COMPILATION & LINKING PROCESSES

21
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Execution is the process of running the program, to execute a „c‟ program, we need to
follow the steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library.
4. Executing the program.

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, which
tells 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 line
and 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

4. C CHARACTER SET

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

„C‟ program are basically of two types, namely


 Source character set
 Execution character set

22
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

C CHARACTER SET

SOURCE CHARCTER SET EXECUTION CHARACTER SET

Alphabets digits special Char. White space Character spaces Escape sequence

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
CHARACTER RESULT
SEQUENCE

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

Back slash \l Present back slash

Null \0 Null. (end of the string)

23
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

5. 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 to
construct 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
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.

24
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

‘C’ constants

Numeric constants Character constants

Integer Real constants Single Character String constants


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

25
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Example: ‫ۥ‬m‫ۥ‬ ‫ۥ=ۥ‬ ‫ۥ‬A

ii) String constants:


A string constant is a sequence of characters enclosed in double Quotes, the characters
may 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

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

26
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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;

6. C STORAGE CLASSES

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 be
used 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;
}

27
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 of
that member to be shared by all objects of its class.
#include <stdio.h>
/* 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 of
defined 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 the
same global variables or functions as explained below.

28
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
#include <stdio.h> extern int count;
void write_extern(void)
{
count = 5;
printf("count is %d\n", count);
}

7. 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 during
the 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 variables
are 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”

29
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

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 of
a 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 );
}

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

30
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

(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
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 for
the number. It contains 8 bytes.

Syntax:

31
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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:
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:

32
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
int a[10];
char chi [20];

 Pointer
A pointer is a special variable that holds a memory address (location in memory) of
another 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 data
types under a single name. In memory, the entire structure variable is stored in sequence.

Syntax:
struct < structure name>
{
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 < tag name>
{
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,

33
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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.

9. OPERATORS AND EXPRESSIONS


Operator is a symbol that is used to manipulate arithmetic, logical and relational
expressions.
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
- 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()
{

34
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 VALUE
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
{
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b )
{
printf("Line 2 - a is less than b\n" );

35
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
}
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));
}

Output:
Condition : Return Values
5>3 && 5>10: 1
8>5 II 8<2 : 0
!(8==8) : 0
Example:2

36
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
#include <stdio.h>

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 of
a 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 C -= A is equivalent to C
-= operand from the left operand and =C-A
assign the result to left operand
Multiply AND assignment operator,
It multiplies right operand with the C *= A is equivalent to C
*= left operand and assign the result =C*A
to left operand
Divide AND assignment operator,
It divides left operand with the C /= A is equivalent to C
/= right operand and assign the result =C/A
to left operand
Modulus AND assignment operator,
It takes modulus using two C %= A is equivalent to C
%= operands and assign the result to =C%A

37
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 =
>>=
operator C >> 2
Bitwise AND assignment operator C &= 2 is same as C = C
&=
&2
bitwise exclusive OR and C ^= 2 is same as C = C
^=
assignment operator ^2
bitwise inclusive OR and C |= 2 is same as C = C |
|=
assignment operator 2

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 );

38
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

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 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
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 programming
Line 7 -They
languages. <<= are,
Operator Example, Value of c = 44
1. IncrementExample,
Line 8 - >>= Operator (++) Value of c = 11
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
a--=11
--a=11
f) Conditional operators

39
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Conditional operators itself check the condition and executes the statement depending
upon 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 it is false yhen‟exp2‟ is evaluated.
Example:
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 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101


A^B = 0011 0001

40
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
~A = 1100 0011

The Bitwise operators supported by C language are listed in the following table. Assume
variable 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.

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

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

41
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & Right to left
sizeof
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

11. INPUT AND OUTPUT STATEMENTS

 In C language there are 2 types of I/P & O/P statements are available. They are : several
functions are available for input/output operations in C. These functions are collectively
known as standard I/O library.

42
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

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 of
data.
 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 library
function 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”);
else
if(isdigit(ch)>0)
printf(“It is a digit”);

43
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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.

44
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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()
{
int m,n,a;
clrscr();
printf(“Enter the 2 numbers:”);
scanf(“%d%d”,&m,&n);

45
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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

12. TYPE CASTING OR TYPE CONVERSION

Typecasting is converting one data type into another one. It is also called as data conversion or
type 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.
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

46
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 type
promotion.

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

13. TYPES OF C QUALIFIERS:


There are two types of qualifiers available in C language. They are,

1. const
2. volatile

CONST KEYWORD:
 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
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 elements
that affect the format specifier.

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

47
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
2. A number after % specifies the minimum field width. If the string is less than the width, it will
be 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
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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:

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

Explanation:

50
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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-point
number 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

51
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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;

52
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C

Single program

#include<stdio.h>

main()

Char ch='B';

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

//print decimal or integer data with d and i

int 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 notation

int 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 string
up to 5 character

printf("%-20.5s\n",str);//left align and print string up to 5 character

53
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Output

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

14. 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
 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.
 These environments make the unit test‟s running easy

Best IDE‟s for C

54
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
1. Visual Studio Code
2. Eclipse
3. NetBeans
4. Sublime Text
5. Atom
6. Code::Blocks
7. CodeLite
8. CodeWarrior

15. 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 needs
for a quick code-build-debug cycle and leaves more complex workflows to fuller featured such as
VisualStudioCode.

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. It
is 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

55
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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.

56
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 is
software that supports only the window operating system.

57

You might also like