Unit1-Class 3&4 PIC

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

C is called Middle Level Language.

C Programming language is called as Middle Level Language because it supports the


feature of High Level Language and Low Level Language.

C is Called Middle Level Language Justify?


C Programming language is called as Middle Level Language because
(i)it behaves as High Level Language through Functions - gives a modular
programming and breakup, increased efficiency for re-usability
(ii)it gives access to the low level memory through Pointers. Moreover it does support
the Low Level programming i.e, Assembly Language.
As its a combination of these two aspects, its neither a High Level nor a Low level
language but a Middle Level Language.

By Neetha S.S, Dept of Computer Applications


Basic Structure of C programming

C language is very popular language among all the languages. The structure of a C program is a
protocol (rules) to the programmer, while writing a C program. The general basic structure of C
program is shown in the figure below.

Program Code

/* Our first simple C basic program */

#include <stdio.h>

void main()

int A=5, B=10, C;

C=A+B;

printf(“The Value of C is %d”,C);

getch();

} By Neetha S.S, Dept of Computer Applications


• /* Our first simple C basic program */
• Whatever is given inside the comment line /* */ in any C program,
won't be considered for compilation and execution.
• #include <stdio.h>
• #include, is known as pre-processor, the main work of processor is to
link the program with the header file.
• Header file is a collection of built-in functions that helps us in our
program. Header files contain definitions of functions and variables
which can be incorporated into any C program by pre-processor #include
statement.
• To use any of the standard functions, the appropriate header file must
be included. This is done at the beginning of the C source file.
• This is a preprocessor command that includes standard input output
header file(stdio.h) from the C library before compiling a C program.
By Neetha S.S, Dept of Computer Applications
By Neetha S.S, Dept of Computer Applications
• Constants, variables and Datatypes:

Character Set : The characters that can be used to form words and expressions depends upon the computer to
which the program is run.
The Characters in C are
1. Letters: A-Z, a-z, both upper and lower case.
2. Digits: 0-9.
3. Special character: + - * / % ? < > ~ ! @ # $ & ( ) { } [ ] | \ etc
4. White spaces are newline, horizontal tab, blank space .

Keywords: Keywords are those words whose meaning is already defined by the compiler. Keywords cannot be used as
variable name. No header file is needed to include the keywords. Keywords should be written in lower case letters.

Keywords are also called has reserved words. There are 32 keywords in C.

By Neetha S.S, Dept of Computer Applications


By Neetha S.S, Dept of Computer Applications
• enumeration
• enum week{Monday, tues,wed,thu,fri,sat,sun };

• mon=3

• int a;
• char 1
• int 2
• float 4
• long
Identifiers: In C Programming, identifiers are names given to C entities, such as
variables, functions, structures etc. identifiers are created to give unique name to C
entities to identify it during the execution of program.

Example: #define NUM 10


#define A 20 int B;

“NUM”, “A” , B are user – defined identifiers.

Variables: A Variable is a name give to memory location where the actual data is stored.
To indicate the memory location, each variable should be given a unique name
called identifier.

By Neetha S.S, Dept of Computer Applications


Rules for writing variable name in C
1.Variable name can be any combination alphabets, digits or
underscores.
2.The first character in the variable name must be an alphabet.
3.No blanks are allowed within a variable name.
4.No special symbol other than underscore can be used in a variable name.
5.Variable name should not be a keyword.

By Neetha S.S, Dept of Computer Applications


Constants: Constants are the terms that can't be changed during the execution of a
program.
Different types of C constants

Constant Type of value stored

Integer Constant Constant which stores integer value.

Floating Constant Constant which stores float value.

Character Constant Constant which stores character value.

String Constant Constant which stores string value.

By Neetha S.S, Dept of Computer Applications


Integer Constants: Sequence of number 0-9 without decimal points, fractional part or any other
symbols. It requires two or four bytes, can be +ve, -ve or Zero the number without a sign is as
positive.
Example: -10, +20, 40

Floating Constants: floating constants contains decimal value. Example: 2.5, 5.521, 3.14
etc.

Character Constants: A single character constant is given within a pair of single quote.

String Constant: These are the sequence of character within double quote. Example: “GKMV”,
“India”, “4”

By Neetha S.S, Dept of Computer Applications


Escape Sequences
In C language there are some characters that are not treated as the same.
These character combinations that give different results such as a new line, a tab space, or a
backspace, are known as escape sequences.
Escape sequences in C language start with a backslash (\) and followed by a character.

By Neetha S.S, Dept of Computer Applications


Data types

In C data types are defined as the data storage format that a variable can store a data to perform a
specific operation.
Variable(data) should be declared before it can be used in program. Data types are the keywords,
which are used for assigning a type to a variable.
C language is rich of data types.

By Neetha S.S, Dept of Computer Applications


Basic data types in C:
a) Character data type:
Character data type allows a variable to store only one character.
char keyword is used to refer character data type.
Storage size of char data type is 1 byte. We can store only one character using character data type
The format specifiers used for char is %c
b) Integer data type:
Integer data type allows a variable to store numeric values.
int keyword is used to refer integer data type. The storage size of int data type is 2 byte.
The format specifiers used for int is %d
c)Floating point data type:
Floating point data type allows a variable to store decimal values.
float keyword is used to refer floating point data type. The storage size of float data type is 4
byte.
The format specifiers used for int is %f
We can use up-to 6 digits after decimal using float data type.
By Neetha S.S, Dept of Computer Applications
d) Void data type:
Void is an empty data type that has no value.
This can be used in functions and pointers.

By Neetha S.S, Dept of Computer Applications


Variable Declaration in C

A variable declaration provides assurance to the compiler that there is one variable existing with the
given type and name so that compiler proceed for further compilation. A variable declaration has its
meaning at the time of compilation only, compiler needs actual variable declaration at the time of
linking of the program.
A variable declaration has the form:
type variable_list;
Here type must be a valid C data type. The variable_list may consist of one or more identifier names
separated by commas.
Some of the valid declaration are shown below.
int a, b; char ch;
float sum, average;

In the above example,


a and b are the variable of integer data type. ch is a variable of character data
type.
sum and average are the variable of floating point data type.
By Neetha S.S, Dept of Computer Applications
Assigning values to variables

Variables may be initialized at the time of declaration by assigning a value to them as show in
the below example:

int a=10, b=5; char c h='y';


float sum=0.0, average=12.45;

In the above example,


a and b are the variable of integer data type with value of 10 and 5 respectively.
ch is a variable of character data type with a value ‘y' character value must be enclosed by
single quote.
sum and average are the variable of floating point data type with a value 0.0 and 12.45
respectively.

By Neetha S.S, Dept of Computer Applications


Symbolic constant in c Language
A symbolic constant is name that substitute for a sequence of character that cannot be changed.
The character may represent a numeric constant, a character constant, or a string. When the
program is compiled, each occurrence of a symbolic constant is replaced by its corresponding
character sequence.
They are usually defined at the beginning of the program. The symbolic
constants may then appear later in the program in place of the numeric constants, character
constants, etc., that the symbolic constants represent.
For example
#define PI 3.141593
#define X 45

#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is
pre-processed, all occurrences of the symbolic constant PI are replaced with the replacement text
3.141593.

By Neetha S.S, Dept of Computer Applications


Operators and Expressions

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides the following types of
operators.

a)Arithmetic operators

C provides all the basic arithmetic operators, they are +, -, *, /, % Integer division truncates
any fractional part. The modulus division produces the remainder of an integer division.
Assume variable A holds 7 and variable B holds 2 then:

By Neetha S.S, Dept of Computer Applications


Operator Description Example
A+B
+ Adds two operands
7 +2=9
A-B
- Subtracts second operands from
7-2=5
first operands
A*B
* Multiply both operands
7*2=14
A/B
/ Divide numerator by denominator
7/2=3
A%B
% Modulus Operator and remainder of
7%2=1
after an integer division

By Neetha S.S, Dept of Computer Applications


b) Relational operators

Relational operator‟s checks relationship between two operands. If the relation is true, it returns
value 1 and if the relation is false, it returns value 0.

Assume variable A holds 10 and variable B holds 15 then:

By Neetha S.S, Dept of Computer Applications


Operator Description Example
A==B
Checks if the value of two operands is equal or not, if yes then condition
== 10==15
becomes true.
returns False(0)

Checks if the value of two operands is equal A!=B


!= or not, if values are not equal then condition becomes true. 10!=15
returns True(1)

Checks if the value of left operand is greater A>B


> than the value of right operand, if yes then condition becomes true. 10>15
returns False(0)

Checks if the value of left operand is less than A<B


< the value of right operand, if yes then condition becomes true. 10<15
returns True(1)

Checks if the value of left operand is greater A>=B


>= than or equal to the value of right operand, if yes then condition becomes 10>=15
true. returns False(0)

Checks if the value of left operand is less than A<=B


<= or equal to the value of right operand, if yes then condition becomes true 10<=15
returns True(1)
By Neetha S.S, Dept of Computer Applications
c) Logical operators

Logical operators are used to combine expressions containing relation operators.

Assume variable A holds 5 and variable B holds 2 then:


Operator Description Example

((A ==5) && (B>5))


&& Called Logical AND operator. If both the operands
returns false.
are non zero then the condition becomes true.

((A ==5) ||(B>5))


|| Called Logical OR Operator. If any of the two
returns true.
operands is non zero then the condition becomes
true.

!(A==5)
! Called Logical NOT Operator. Use to reverses the
returns false.
logical state of its operand. If a condition is true
then Logical NOT operator will make false.

By Neetha S.S, Dept of Computer Applications


d) Assignment operators

The most common assignment operator is =


This operator assigns the value in right side to the left side.

Simple assignment operator


The variable or value of right hand side is assigned to left hand side variable.
Example:
A=B; (the variable B value will be assigned to variable A)
A=45; (the value 45 is assigned to variable A)
A=B=C;( the variable value C is assigned to B, the variable value B is
assigned to A)

By Neetha S.S, Dept of Computer Applications


Compound assignment operator

Operator Description Example


A= B
Simple assignment operator, Assigns values from right side
= The value of B is assigned to A.
operands to left side operand
now A get the new value.
Add AND assignment operator, A += B
+= It adds right operand to the left operand and assign the
is equivalent to A = A +B
result to left operand
Subtract AND assignment operator, A -= B
It subtracts right operand from the left operand and assign
-= is equivalent to A = A -B
the result to left operand

Multiply AND assignment operator, It A *= B


*= multiplies right operand with the left operand and assign is equivalent to
the result to left operand A = A *B
Divide AND assignment operator, It A /= B
/= divides left operand with the right operand and assign the is equivalent to

result to left operand A = A/B


Modulus AND assignment operator, It A %= B
%=
takes modulus using two operands and assign the result to is equivalent to A = A%B
left operand
By Neetha S.S, Dept of Computer Applications
Difference between = and ==
= is an assignment operator
== is a relational operator.
Example for =
A=B
The value of B will be assigned to A, A gets a new value of B

Consider A=5 and B=8; A=B


The value 8 will be assigned to A, A gets a new value as 8.

Example for ==
A==B
It checks whether the value of A and B are equal or not. Consider A=5 and
B=8;
A==B
It checks whether the value of A and BByare equal or not.
Neetha S.S, Dept of Computer Applications

You might also like