Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views18 pages

ECE - C Unit-I

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

ECE - C Unit-I

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

LECTURE NOTES

EC-405 PROGRAMMING IN C

UNIT- I

C-Programming Basics
Course Contents: Structure of a C program - Character Set –keywords – Data types -Constants,
Variables – Arithmetic operators- evaluation of expression– Assignment statement –Nested assignment
statement – Compound assignment operators- Increment, Decrement operators- printf() and scanf()
functions – Operator precedence – Relational, Logical, Bitwsie logical operators

State the Importance of C Language:

 C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph) Laboratories.

 Generally, we use to call C as a middle level language. Because, the ‘C’ compiler combines the
capabilities of an assembly language with the features of a high-level language.

 C is highly portable, that is, ‘C’ programs written on one computer can be run on another with
little (or) no modification.

 ‘C’ language is best for structured programming, where the user can think of a problem in
terms of function modules (or) blocks.It has the ability to extend itself.

 ‘C’ Programs are efficient and fast.

Explain the basic Structure of C Programming

 A C program is divided into different sections. There are six main sections to a basic cprogram.

 The six sections are

1. Documentation section 4. Global Declaration section

2. Link section 5. Main function

3. Definition section 6. Subprograms

1. Documentation Section

 The documentation section is the part of the program where the programmer
givesthe details of the program.
 Programmer usually gives the name of the program, the details of the author
and other detailslike the time of coding and description. It gives anyone to read the code
and its overview.

Example /** *File Name: Helloworld.c


* Author: Manthan Naik
* date: 09/08/2019
* description: a program to display hello world */

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 1
LECTURE NOTES
EC-405 PROGRAMMING IN C

2. Link Section

 This part of the code is used to declare all the header files that will be used in the
program.

 This leads to the compiler being told to link the header files to the system
libraries.

Example #include<stdio.h>

3. Definition Section

 This section is used to define the symbolic constants.

Example #define PI=3.14

4. Global Declaration Section

 In this section where the global variables are declared. All the global variable
used are declared in this part.

 The user-defined functions are also declared in this part of the code.

Example float area(float r);

int a=7;

5. Main Function Section: Every C-programs need to have the main function. Each main
function contains 2parts.

1. Declaration part and

2. Execution part

 The declaration part is the part where all the variables are declared.
 The execution part begins with the curly brackets { and ends with the curly closebracket }.
Both the declaration and execution part are inside the curly braces.

Example

int main()
{
int a=10;
printf(" %d", a);
return 0;
}

6. Sub Program Section

 All the user-defined functions are defined in this section of the program. We can also say this
section as a user defined program section.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 2
LECTURE NOTES
EC-405 PROGRAMMING IN C

know the Programming style of ‘C’ language with sample Program:

C Basic commands Explanation

#include <stdio.h> This is a preprocessor command that includes standard input


output header file(stdio.h) from the C library before compiling a C
program

int main() This is the main function from where execution of any C program
begins.

{ This indicates the beginning of the main function.

/*_some_comments_*/ whatever is given inside the command “/* */” in any C program,
won’t be considered for compilation and execution.

printf(“Hello_World! “); printf command prints the output onto the screen.

getch(); This command waits for any character input from keyboard.

return 0; This command terminates C program (main function) and returns0.

} This indicates the end of the main function.

Simple ‘c’ program: /* program to calculate addition of two numbers */


1. #include<stdio.h>
2. int main()
3. {
4. int a=10,b=15,c;
5. c=a+b;
6. printf(“addition of a and b is =%d”,c);
7. }
Output: Addition of a and b is =25

Executing a ‘C’ program:

▪ The steps involved in executing a ‘c’ program are:

1. Creating the program.

2. Compiling the program.

3. Linking the program.

4. Executing the program.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 3
LECTURE NOTES
EC-405 PROGRAMMING IN C
1. Creating the program:

a) Write ‘c’ program (source code)


b) Same program with an extension.c
c) File saved with .c extension is called as source program.
2. Compiling the program: (Alt +F9)

a) Source program with an extension .c is given as input to compiler.

b) Compiler checks for errors if source code contains errors then goto sourcecode to
modify.

c) If source code is errors free then compiler comments into object with an
“extension .obj”.

3. Linking the program:

a) Object program with an extension .obj is given as input to linker.

b) Linkers checks for errors if object source code to modify.

c) If object code is errors free then linkers comments “.obj” file into executetable
file with an extension “.exe”.

4. Executing the ‘c’ program: (ctrl +F9)

a) Executable file .exe is sum, if any errors at run time then go to source code
tomodify.

b) If no runtime error occurs then the output will be displayed to the user and
theprogram is successful.

know about the Character Set

 A character set defines the valid characters that can be used in source programs or
interpreted when a program is running.

 The source character set is the set of characters available for the source text/source
code.The character set are classified into:

1. Alphabets / Letters
2. Digits
3. Special Symbols
4. White Spaces

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 4
LECTURE NOTES
EC-405 PROGRAMMING IN C

know about constants, variables, keywords & identifiers.


Constants

▪ Constants are the fixed values that do not change during the execution of a program.

▪ C supports several types of constants.

– Numeric Constants

▪ A numeric constant is a sequence of numeric digits. It required minimum size of 2bytes and
max 4 bytes. It may be positive or negative but by default sign is always positive. These are
two types

1. Integer constants
2. Real constants / Floating constants
– Character Constants

1. Single character constant

2. String Constants

Variables

▪ A variable is a name to a data value which is to be stored in memory.

▪ Unlike constants, the value of a variable can be changed during the execution of aprogram.

▪ The rule for naming the variables is same as the naming identifier. Before used in the
program it must be declared.

▪ Declaration of variables specifies its name, data types and range of the value thatvariables
can store depends upon its data types.

▪ The syntax for declaring a variable is –DataType IdentifierName ;

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 5
LECTURE NOTES
EC-405 PROGRAMMING IN C
▪ Example- int num; long int sum , a;
Rules for constructing variable names

1. First character in a variable name must be an alphabet or an underscore (_) andcurrency


character ($).

2. Variable name can have alphabet, digits and underscore.

3. No whitespace is allowed within the variable name.

4. C variables are case sensitive. Ex: name and Name are two different variables.

5. You can not use keywords / reserve words as variable name in C.

Declaring a Variable:

▪ A variable can be used to store a value.

▪ Syntax: Datatype variable1, variable2,……. variablen;

▪ variable1, variable2, are names of variables.

▪ Variables are separated by comma and ends with semicolon(;).

▪ Example: int sum; int a,b,c,d;

Assigning Values to Variables:

▪ Values can be assigned to Variables using asignment operator(=).

▪ Syntax:variable_name = value;

▪ Example: int a=10;


float b=12.55;
char p=‘E’;

Keywords

▪ Keywords are predefined, reserved words in C language and each of which isassociated
with specific features.

▪ These words help us to use the functionality of C language.

▪ Keywords cannot be used as a variable or function name.

▪ C language has reserved 32 words as auto double int struct continue for
keywords.
break else switch default do if
▪ All the Keywords are written in
case char goto return union float
lower-case letters.

▪ Since C is case-sensitive.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 6
LECTURE NOTES
EC-405 PROGRAMMING IN C
Identifiers

▪ Identifiers refer to the name of variables, functions and arrays. Each variable has a name by
which it is identified in the program. In C Language the identifier is allowedthe maximum
length of 31.

Identifier must follow some rules. Here are the rules:

▪ All identifiers must start with either a letter( a to z or A to Z ) or currencycharacter($) or an


underscore.

▪ They must not begin with a digit.

▪ After the first character, an identifier can have any combination of characters.

▪ C keywords cannot be used as an identifier.Identifiers in C is case sensitive.

List various Data Types with examples

 The kind of data variable holds in a programming language is called “Data type”.

 Data Types are classified into the following categories:

1. Primary Data Type

2. User defined Data Type

3. Secondary /Derived Data Type

 The data type of a variable tells us the following things


 Type of value that can be stored.
 Size of a variable ( number of bytes)
 Range of vales.
Primitive Data Types

 Almost all programming languages provide a set of primitive data types 


 C language supports four types of primitive Data Types. They are: 
 int
 float
 double
 char

Declaration of primary data types:

 Syntax of declaring variable of any primary data type is data-type var1, var 2, var n;
 Integer : Integers are defined by using the keyword “int”. The format specifier
for interger is “%d”. It occupies 2 bytes of memory. Range of integer is -32768 to +32767.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 7
LECTURE NOTES
EC-405 PROGRAMMING IN C
 Floating point : Float numbers are defined by using the keyword “ float”. ”. The format
specifier for floating point is “%f”. It occupies 4 bytes of memory. Range of float is -3.4e38 
to +3.4e38
 Double: Double numbers are defined by using the keyword “double”(long float). ”. The
format specifier for double is “%lf”. It occupies 8 bytes of memory. Range of double is -1.7e308
to +1.7e308 
 Character : Char data type is used to define a single character. It defined by using the
keyword “char”. The format specifier for interger is “%c”. It occupies 1 byte of memory. Range of
char is -128 to +127 

Secondary / Derived data types

 There are some derived data types which are supported by C such as arrays, functions,
structures, and pointers.

User defined data types

 These are data types defined by user. typedef and enumerated data types are two user defined
data types.

Example: Write a C program to display Datatypes using int, float,char, double.

#include <stdio.h>
void main() {
// datatypes Output:
Integer datatype : 10
int a = 10; char b = 'S'; float c = 2.88; Character datatype : S
double d = 28.888; Float datatype : 2.880000
Double Float datatype : 28.888000
printf("Integer datatype : %d\n",a);
printf("Character datatype : %c\n",b);
printf("Float datatype : %f\n",c);
printf("Double Float datatype : %lf\n",d);
getch();
}

Explain different arithmetic operators, relational operators, and logical operators with
their precedence
Operators:

 Operators are Symbols that tells the computer to perform certain mathematical or
logical manipulations.
 These operators are used in programs to manipulate data and variables.
Arithmetic Operator:
 The Arithmetic operators are some of the C Programming Operator, which are used to perform
arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division
and Modulus.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 8
LECTURE NOTES
EC-405 PROGRAMMING IN C
 All these Arithmetic operators in C are binary operators which means they operate on
two operands. 
 The below table shows all the Arithmetic Operators in C Programming with examples. 

Binary Operator Meaning Example If a= 4 & b=2, then

+ Addition or unary plus a+b=6


- Subtraction or unary minus a-b=2

* Multiplication a*b=8

/ Division a/b=2

% Modulus a%b=0
It returns the remainder after (Here remainder is zero). If it is
the division 10 % 3 then it will be 1).

Example: Write a C program to print Arithmetic operators using only two variables.

#include<stdio.h>
void main() Output:
{ addition of 2 numbers is 13
subtraction of 2 numbers is 7
int a=10,b=3;
multiplication of 2 numbers is 30
division of 2 numbers is 3
printf("\naddition of 2 numbers is %d",a+b);
modulo of 2 numbers is 1
printf("\nsubtraction of 2 numbers is %d",a-b);
printf("\nmultiplication of 2 numbers is %d",a*b);
printf("\ndivision of 2 numbers is %d",a/b);
printf("\nModulo of 2 numbers is %d",a%b);
getch();
}

 Within the above arithmetic operators in C, The value of a is 10 and the value of b is 3 then
the adition of two operands is 13.
 The value of a is 10 and the value of b is 3 then the subtraction of two operands is 7.
 The value of a is 10 and the value of b is 3 then the multiplication of two operands is 13.
RELATIONAL OPERATOR

 Relational Operators are used to compare two quantities and take certain decision depending on
their relation.
 All the operators are called as binary operators because they take two expressions as operands.
 If the specified relation is true it returns one. If the specified relation is false it returns zero.

Operator Meaning Example if a=5 & b=3, then

< is less than a<b (5<3 means False)0

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 9
LECTURE NOTES
EC-405 PROGRAMMING IN C
Operator Meaning Example if a=5 & b=3, then

<= Is less than or equal to a<=b (5<=3 means False)0

> Is greater than a>b (5>3 means True)1


>= Is greater than or equal to a>=b(5>=3 means True)1

== Is equal to a==b (5==3 means False)0


!= Is not equal to a!=b (5!=3 means True)1

 == Checks if the values of two operands are equal or not. If yes, then the condition
becomes true.
 != Checks if the values of two operands are equal or not. If the values are not equal, then the
condition becomes true.
 > Checks if the value of left operand is greater than the value of right operand. If yes, then the
condition becomes true.
 < Checks if the value of left operand is less than the value of right operand. If yes, then the
condition becomes true.
 >= Checks if the value of left operand is greater than or equal to the value of right operand.
If yes, then the condition becomes true.
 <= Checks if the value of left operand is less than or equal to the value of right operand. If
yes, then the condition becomes true.

Example: Try the following example to understand all the relational operators available
in C
#include<stdio.h>
void main()
{
int a=9, b=4;
printf("\n The < value of a is %d", a<b);

printf("\n The > value of a is %d", a>b);

printf("\n The >= value of a is %d", a>=b);

printf("\n The <= value of a is %d", a<=b);

printf("\n The == value of a is %d", a==b);

printf("\n The != value of a is %d", a!=b);


} Output:
The < value of a is 0
The > value of a is 1
The >= value of a is 1
The <= value of a is 0
The == value of a is 0
The != value of a is 1

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 10
LECTURE NOTES
EC-405 PROGRAMMING IN C
LOGICAL OPERATORS

 These operators are used for testing more than one condition and making decisions. It
always produce single result in the form of true or false but at a time only one result is valid.

 'C' has three logical operators they are: 

Operator Meaning Example Result


If c = 5 & d = 2 then,

&& Logical AND ((c==5) && (d>5)) False


It returns true only
if aboth expressions are true
|| Logical OR ((c==5) || (d>5)) True
It returns true only if either
expression is true
! Logical NOT !(c==5) False
It returns true ,if the operand is
false and
If the operand is false, the
result is true.
Example:
#include<stdio.h>
void main()
{
int a=5, b=2;

printf("\n&& Operator : Both conditions are true %d",(a<b)&&(a<=b));

printf("\n || Operator : Only one condition is true %d",(a<b)||(b<a));

printf("\n! reverses the state of the operand %d", !(a!=b));

getch();
Output:
&& operator : Both conditions are true 0
}
|| operator : Only one condition is true 1
! reverses the state of the operand 0

Evaluation of Logical expression ((a!=b)||(b>c)&&(a<b)||(b>c)), if a=3, b=4 and c=5.

 ((a!=b)||(b>c)&&(a<b)||(b>c))
 ((4!=4) || (4>5)) && ((3<4) || (4>5))

 (True || False) && (True || False)


 True || True
 True

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 11
LECTURE NOTES
EC-405 PROGRAMMING IN C
Operator Precedence

 Operator precedence is used to determine the order of operators evaluated in an expression.

 In c programming language every operator has precedence (priority).

 When there is more than one operator in an expression the operator with higher precedence is
evaluated first and the operator with the least precedence is evaluated last.

Order of Evaluation of Operators Precedence of Arithmetic, Relational and Logical Operators


Operators Type Priority
() Parentheses 1
!, –(unary) Logical NOT and Unary minus 2

*, /, % Arithmetic and modulus 3

+,- Arithmetic 4
(binary)
< ,<=, >, >= Relational 5
==, != Relational 6
&& Logical AND 7
|| Logical OR 8

Operator Precedence of Arithmetic Operator

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the


expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same
level” (i.e., not nested), they’re evaluated left
to right.
*,/,% Multiplication, Evaluated second. If there are several, they’re
Division,Remainder evaluated left to right.

+,- Addition, Evaluated third. If there are several,


Subtraction they’re evaluated
left to right.
= Assignment Evaluated last.

Explain the assignment statements


 These operators are used for assigning the result of an expression to a variable. It is also known
as an assignment operator in C.
 Assignment operators store a value in the object specified by the left operand.
 Syntax:
Variable = constant / variable/ expression;

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 12
LECTURE NOTES
EC-405 PROGRAMMING IN C
 Example:
b=c; /* b is assigned the value of c */
a=9; /* a is assigned the value 9*/
b = c+5; /* b is assigned the value of expr c+5 */

Example: Write a C Program using Assignment operator.


#include<stdio.h>
void main()
{
int a=2, b=3, c;
printf("\n The values of = is: %d",c=a+b);
getch();
}
Output:
The values of = is: 5

Nested Assignment(Multiple Assignment)

 Since C language does not support chaining assignment like a=b=c; each assignment
operator (=) a=b
 operates on two operands only. Then how expression a=b=c evaluates? According to
operators associativity assignment operator (=) operates from right to left, that means
associativity of assignmentoperator (=) is right to left.
 Expression a=b=cis actually a=(b=c), see how expression a=(b=c)evaluates?

 Value of variable c will be assigned into variable bfirst.


 Then value of variable bwill be assigned into variable a.
 Finally value of variables a and b will be same as the value of variable c.

Example: Consider the following program


#include<stdio.h> Syntax for nested assignment:
void main() variable1=variable2 =variable3= value
{
int a,b,c;
a=0; b=0; c=100;
printf("Before Multiple Assignent a=%d,b=%d,c=%d\n",a,b,c);

//Multiple Assignent a=b=c;


printf("After Multiple Assignent a=%d,b=%d,c=%d\n",a,b,c);
getch();
}
Output:
Before Multiple Assignment a=0,b=,c=10
After Multiple Assignment a=10,b=10,c=10

Explain the increment & decrement operators


 Two most useful operators which are present in 'C' are increment and decrement operators. 
 These operators are represented as ++ and --. ++ increments operand by 1 and –- decrements
operand by 1. 

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 13
LECTURE NOTES
EC-405 PROGRAMMING IN C

 Both are unary operators and can be used as pre or post increment/decrement. 

Operator Meaning Example


If a = 5, then

a ++ Post increment a++;


// a becomes 7

++a Pre increment ++a;


// a becomes 6
a-- Post decrement a--;
// a becomes 5

--a Pre decrement --a;


// a becomes 6

Simple example to understand the prefix and postfix increment

int a=9,b; But, if we execute this code…


b=a++; int a=9,b;
printf(“%d\n”,a); b=++a;
printf(“%d”,b); printf(“%d\n”,a);
printf(“%d”,b);
The output would be
The output would be
10 10
9 10

Simple example to understand the prefix and postfix decrement:

int a=9,b; But, if we execute this code…


b=a--; int a=9,b;
printf(“%d\n”,a); b=--a;
printf(“%d”,b); printf(“%d\n”,a);
printf(“%d”,b);
The output would be
8 The output would be
9 8
8

Increment & Decrement Operators Sample Program

#include <stdio.h>
void main()
{
Department of Computer Science Engineering
Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 14
LECTURE NOTES
EC-405 PROGRAMMING IN C

int b=10;
printf("\nThe value of b is %d",b);
printf("\npost increment of b is %d",b++);
printf("\npost increment of b is %d",++b);
printf("\npre decrement of b is %d",--b);
printf("\npost decrement of b is %d",b--);
printf("\nThe value of b is %d", b);
getch();
}

Output:
The value of b is 10
post increment of b is 10
post increment of b is 12
pre decrement of b is 11
post decrement of b is 11
The value of b is 10

Identify the compound assignment operators


 A compound assignment operator is an operator that performs a calculation and an
assignment at the same time.
 The compound-assignment operators combine the simple-assignment operator with another
binary operator.
 Compound-assignment operators perform the operation specified by the additional operator,
then assign the result to the left operand. For example, a compound-assignment expression
such as.
 expression1 += expression2 is same as expression1 = expression1 + expression2
 Note that the expression:
a *= b + c is equivalent to a = a * (b + c) and not a=a *b+c

Explain the I/P functions printf and scanf

 The printf() function is used to display aoutput and the scanf() function is used to take input
from users.
 The printf() and scanf() functions are commonly used functions in C Language.These functions
are inbuilt library functions in header files of C Programming.
printf() function:-
 In C Programming language, the printf() function is used for output.
 printf() function can take any number of arguments.Frist argument must be enclosed
within the double quotes "hello" and every other argument should be separated by
comma(,) within the double quotes.

Important points about printf():


 printf() function is defined in stdio.h header file.By using this function, we can print the
data or user-defined message on monitor (also called the console).
 printf() can print a different kind of data format on the output string.

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 15
LECTURE NOTES
EC-405 PROGRAMMING IN C

 To print on a new line on the screen,we use "\n" in printf() statement.


 Syntax:-
printf("format specifier", argument_list);
 The format string for output can be %d (integer), %c (character), %s (string), %f (float)
and %lf (double) variable.
 Example:
#include <stdio.h>
void main()
{
// using printf()
printf("Welcome to Polytechnic College");
getch();
}
 Output: Welcome to Polytechnic College

scanf() Function:
 The scanf() function is used to read input data from the console.
 The scanf() function is in built function available in the C library.
 scanf() function can read character,string,numeric and other data from keyword in C
language.
 Syntax:
scanf("format specifier", argument_list);
 Example:
#include <stdio.h>
void main()
{
// using scanf()
int number;
printf("Please enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
getch();
}
 Output:
Please enter a number: 7
You entered: 7

Know various type conversion techniques

 Type Casting or Type Conversion is a process of converting one type of data intoanother type
of data.

 The conversion is done only between those datatypes where in the conversion ispossible i.e..
char to int and vice versa. Type conversion are two types:

1. Implicit type conversion 2. Explicit type conversion

1. Implicit type conversion: This type of conversion is usually performed by thecompiler


when necessary without any commands by the user. Thus it is also called "Automatic Type
Conversion".

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 16
LECTURE NOTES
EC-405 PROGRAMMING IN C

 The compiler usually performs this type of conversion when a particular expression
contains more than one data type. In such cases either type promotion or demotion takes
place.
 bool->char->int->long->float->double

Example:Write a C Program to demonstrate Implict type conversion.


#include<stdio.h>
void main()
{
int x=10; //integer x
char y='a'; //character y
//y implicitly converted to int
//ASCII value of 'a' is 97
x=x+y;
//x is implicitly converted to float
float z=x+1.0;
printf("X=%d, Z=%f",x,z);
Output: X=107, Z=108.000000
getch();
}

Explicit type Conversion : Explicit type conversion rules out the use of compiler for converting one

data type toanother instead the user explicitly defines within the program the datatype of the

operands in the expression.

▪ Example:

1. a= (int) 21.3/(int)4.5 Evaluated as 21/4 and the result is 5.

2. b=(int) (a+b) The result of a+b is converted into integer.

3. c=int(a+b) a is converted into integer and then add a


to b
4. The example below illustrates how explicit conversion is done by the user.

Example 2: Write a C Program demonstrate explict type conversion.

#include<stdio.h>
void main()
{
double x=1.2;
int sum=(int)x+1; // Explict conversion from double to int
printf("Sum=%d",sum);
getch();
} Output: Sum=2

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 17
LECTURE NOTES
EC-405 PROGRAMMING IN C

BITWISE OPERATORS:
 Bitwise operators do manipulation on bits stored in memory.
 These operators work with char and int type data. They cannot be used with floating
point numbers.
Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

 The & (bitwise AND) in C takes two numbers as operands and does AND on every it of two
numbers. The result of AND is 1 only if both bits are 1.
 The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of two
numbers. The result of OR is 1 if any of the two bits is 1.
 The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit of
two
numbers. The result of XOR is 1 if the two bits are different.
 The ~ (bitwise NOT) in C takes one number and inverts all bits of it.

Define an expression and show how to evaluate an Arithmetic Expression


 An expression in C is defined as 2 or more operands are connected by one operator and
which can also be said to a formula to perform any operation.
 An operand is a function reference, an array element, a variable, or any constant. An
operator is symbols like “+”, “-“, “/”, “*” etc.
 Example: A*B
 In the above expression multiplication symbol (*) is said to be an operator and A and B are
said to be 2 operands.
 Evaluate an Arithmetic Expression if a=10 and b=5 then (a/b*a+b)+a-b expression is
 (a/b*a+b)+a-b
 (10/5*10+5)+10-5
 (2*10+5)+10-5
 (20+5)+10-5
 25+10-5
 35-5
 Answer is 30

******

Department of Computer Science Engineering


Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 18

You might also like