Professional Documents
Culture Documents
PC Unit 1
PC Unit 1
PC Unit 1
UNIT I - INTRODUCTION TO C
OVERVIEW OF C
CONSTANTS
COMPILING A C PROGRAM
OPERATORS PRECEDENCE
TYPE CONVERSION
1
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
PART – A (2 Marks)
1. What is an algorithm?
“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.
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
Communication
Effective analysis
Proper documentation
Efficient Coding
Proper Debugging
Proper testing
Efficient Program Maintenance
Complex logic
Alteration or Modification
Reproduction
Unknown
Cost
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.
3
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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 (!).
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).
4
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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.
(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
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
6
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
(ii) \t - tab
(iii) \a - alert
(iv) \0 - null
Syntax :
(type-name) expression;
7
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
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
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
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);
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.
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.
The modulo operation finds the remainder after division of one number by another
(sometimes called modulus).
Example
15%2=1;
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;
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.
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.
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
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?
Only one flow line should enter into I/O symbols and only one should exit from it.
Only one flow line should enter into decision symbol and one or two may exit from it. (in
14
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
15
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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
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.
16
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
WRITE or OUTPUT or PUT or
PRINT IF, THEN, END IF
-------------
-------------
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?
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.
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.
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.
18
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
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.
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.
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
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 */
Example:
#define PI 3.14
#define TRUE 1
#define FALSE 0
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.
Example Program
void main()
{
float r; /*declaration part*/
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.
3.Open a command prompt and go to the directory where you saved the file.
If there are no errors in your code, the command prompt will take you to the next line
and would generate executable file.
4. C CHARACTER SET
The character set is the fundamental raw material of any language and they are used to
represent information.
22
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
C CHARACTER SET
Alphabets digits special Char. White space Character spaces Escape sequence
ESCAPE
CHARACTER RESULT
SEQUENCE
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
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.
ii) Identifiers
Example
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
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
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
25
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
Example: ۥmۥ ۥ=ۥ ۥA
Defining Constants
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;
When the above code is compiled and executed, it produces the following result:
value of area : 50
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 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 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 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 10 and count is 0
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);
}
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;
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
Example:
func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
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
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;
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;
Example:
typedef int number;
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;
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;
};
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.
a) Arithmetic operators
C allows basic Arithmetic operations like addition, subtraction,
multiplication and division.
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
c) Logical operator:
Logical operators are used to combine the results of two or more conditions.
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.
37
Sri Manakula Vinayagar Engineering College Unit - 1 Programming in C
left operand
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:
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:
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
B = 0000 1101
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)
#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.
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
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
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
gets() puts()
INPUT OUTPUT
getchar() putchar()
getc() putc()
gets() puts()
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
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
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();
}
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.
Output
10
10
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.
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
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.
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i integer
%lf Double
%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:
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.
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
#include <stdio.h>
int main()
{
charblogName[] = "aticleworld";
printf("%s\n", blogName);
return 0;
Output: aticleworld
#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("%d\n", x);
printf("%i\n", y);
float f =12.67;
int a =67;
charstr[]="Hello World";
printf("%s\n",str);
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
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
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.
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
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
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.
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.
4. Download the C/C++ compilers. There are some popular compilers are:
1. GCC on Linux
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.
Key Benefits:
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.
In this image, click on the Install button to install the C/C++ 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