Electronicsnotes-3 66947
Electronicsnotes-3 66947
2 Electronics
Synopsis
C is a general purpose, structured programming language also called mid level language as it combines the features of
high level & low level language.
]← Function body
} ← End of program
• main ( ) is a special function used to tell the complier where the program starts & the empty pair of paranthesis
indicate that the function main has no arguments.
• ‘{‘ indicates the starting of main function & ‘}’ indicates the end of the function & also end of the program.
• Function body between the braces contain a set of instruction that perform the given task.
• The lines beginning with /* & ending with */ are called comment lines used to enhance the readability &
understanding & these are not executable statements. // Can also be used for single line comment.
The C preprocessors: It is a program that process the source code before passing to complier.
It operates under the control of preprocessor directives and included in the program before main function.
Commonly used directives are
# include → Specifies the files to be included that required for the program.
# define → Define a macro substitution
# include directive: To include an external file containing functions & definition that required for the program so
that, need not require to rewrite them.
To include the file: # include < filename >
Or # include “file name”
Commonly include header files are #include “stdio.h” [standard input output header file]
#include “conio.h” [Consolidated input output header file]
An include file can include other files ‘but cannot include itself.
# define directive: Here preprocessor replaces the name by the equivalent where ever it occur in the program.
# define name equivalent
There are different forms of macro substitution.
• Simple macro substitution:
# define PI 3.1415
# define CAPITAL “BANGALORE”
• Argumented macro substitution:
# define SQUARE (x) (x * x)
• Nested macro substitution:
#define SQUARE (X) (X *X)
#define FOURTH(X) (SQUARE(X) * SQUARE(X))
C-Programming 12.3
→ Commonly include leader files are # include “stdio.h” [standard input output leader free]
# include “conio.h” [consolidated input output leader free]
Character set: These are used to form words, numbers & expression. The C character set can be classified int
• Letters: A………….Z
a ………… z
• Digits: 0 ………… 9
• Special characters: , . : ; ? “ ‘ # = & * > < / { [
• White space: ‘ ’
C Tokens: The basic & the smallest unit of C program. They are
• Keywords • Identifiers
• Constants • Strings
• Operators • Special symbols
Keywords
The special words used in C program which have specific meaning that already defined in C.
There are 32 keywords in C & they must written in lower case.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
They are the names given to the program elements such as variables, arrays & functions.
Rules for Identifier Name
• The first character must be an alphabet or an underscore.
• All succeding character must be either letters or digits.
• Only first 31 character. are considered.
• Keyword should not be used as identifier.
• No special character. are considered except ‘ ’(underscrore)
• No two successive underscores are allowed.
• Must not contain white space.
Variables: The quantity that change during the execution of program.
• They represent a particular memory location where data be scored.
• There is no limit on number of character is variable name.
• All variables must be declared before they used in C program.
• Key words should not be used as variable name.
Syntax for declaration: Datatype variablename;
Syntax for assigning value: Variablename = value;
Datatype: It tell the type of data a variable can hold. The data types are classified into
• Built in data type:
Keyword Size in byte Range
1. Integer int 2 –32, 765 to 32,767
2. Floating-point (Real) float 4 3.4 e – 38 to 3.5e + 38
3. Character char 1 –128 to 127
4. Double precision double 8 1.7e – 308 to 1.7ef 308
5. Non specific void - -
12.4 Electronics
Note
Precision floating point: Float stores maximum of 6 dig. after decimal point but double stores 16 digits after decimal
point.
Constants:
The quantity that do not change during the execute in of program.
In order to keep a value constant the keyword “const” in used.
Example: Const int P = 10;
Const float p =10.7842;
C constant
Character constant: There a single character enclosed with a pair of single quotes.
Example: ‘a’, ‘ ‘, ‘?’, ‘ y’
String constants: Here a sequence of character enclosed with pair of double quotes.
Example: “Hi”, “y”
The string constant is terminated by ‘\0’ [null character] (hence ‘y’ ≠ ‘‘y’’).
Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical operations.
• Arithmetic Operators:
+ → unary plus
– → unary minus
* → multiplication
/ → division gives quotient
% → modulo division gives remainder
• Relational operators: These are used to compare two operands (const or variables) in an expression & gives
result as True (1) or false (0).
Relational operation are <, < =, >, > =, = =, ! =
• Logical operator: These operators make logic decision & gives result as Ture(1) or false (0).
Operators are
Logical AND &&
Logical OR ||
Logical NOT ! → [It is a unary operator]
Ex.: If a = 2, b = 4, c = 7
a && b || c && (!b)
= 2 && 4 || 7 && (!4)
= 2 && 4 || 7 && 0
= 1 || 7 && 0 = 1 || 0 = 1
C-Programming 12.5
Note
2. If-else statement: This is used to execute a set of statements if the conditional is true else to execute another set.
It is also called two way branching.
Syntax:
If (condition)
{
Statement 1;
}
else
{
Statement 2;
}
Write a program check whether the entered number is even or odd.
# include <stdio.h>
# inclue <conio.h>
Void main ( )
{
int p;
printf (“ enter the number\n”);
If ((p%2) = = 0)
{
printf(“The number is even\n”);
}
else
{
printf(“The number is odd\n”);
}
}
C-Programming 12.7
3. Nested if statement: This is used to check if the conditions are or then two.
Syntax:
if ( condition 1)
{
if (condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else if ( condition 3)
{
statement 3;
}
else
{
statement 4;
}
write a program to find largest if three number;
# include < stdio. h >
# include < conio. h >
viod main ()
{
int x,y,z;
print f (“ enter the value of x,y,z\ n”);
scanf (“ % .d% d% d% “, & x, &y, &z);
if (x >y )
{
if (x > Z)
{
print (“ x is largest \ n”);
}
else
{
}print (“ Z is largest \ m”);
else if (y > Z)
{
print (“ y is largest \n”);
}
else
{
printf (“ Z is largest \n”);
}
}
Switch Statement:
This is used to execute particular case among several tasks depending on user choice for expression. This is multiple
way of branching.
Syntax:
Switch ( expression)
{
case lable 1: statement 1;
break;
case lable 2: statement 2;
break ;
⫶
case default: statement n;
break ;
}
12.8 Electronics
Break indicates the end of particular case lable, & thereby switch statement terminated & case default is executed if the
selected value for expression is not matched with any of the case lable. The break statement for default case in optional.
Here the type of expression is int or char
write a program to find the area of geometrical figures as per user requirement.
# include < stdio. h>
# include < conio.h>
void main ( )
{
int choice;
float radius; breadth , base , height, area;
printf (“ 1 : circle 2: rectangle 3 : square);
printf ( “ enter your choice \n”);
scanf (“ % d”, & choice);
switch (choice)
{
case 1: print (“ welcome to find the area of circle\n’’);
print (“ enter the radian \n”);
scanf (“ %d”, & radius);
area= 3.14* radius * radius;
print (“ Area of circle = % f “ , area);
break;
case 2: print (“ welcome to find the area of rectangle\n”);
print (“ enter length & breadth \n”);
scanf (“ % d % d’’ , & length, & breadth);
area = length * breadth ;
print (“ Area of rectangle = % f “, area);
break ;
case 3: printf (‘‘welcome to find the area of Square\n’’);
print (“ enter the length\n”);
scanf(“ % d”, & length);
area = length * length ;
print (“ Area of square = % f’’, area);
break ;
Case default: printf (“ sorry, the choice is wrong \n”);
}
}
Loop (Iterative ) control statements: In some program a group of statements should execute repeatedly until
specified condition is satisfied, which can be achieved by Iterative or loop control statements
The loop control statements are
• while loop
• Do – while
• for
1. While loop: This is used to execute a set of statements until the specified condition is true or satisfied.
Syntax:
while (condition)
{
Statement ;
}
write a program to find sum of first n integers.
2. Do while loop: This is used to execute a set of statements until the specified condition becomes false.
Do while in called post- test loop where the statement will execute first then check for the condition. So it is the
only looping statement execute atleast once even if the condition is false.
C-Programming 12.9
Syntax:
do
{
statement ;
} while (condition);
Write a C program to find the sum of all even number between 1 to 100.
3. For statement: This is used when the programmer knows how many times does the set of statements to be
executed. It is also known as “fixed exaction”.
Here initialization, condition & Increment / decrement is mentioned in single line
Syntax:
for (intilization ; condition; increment/ decrement)
{
statement ;
}
Debugging of C Program
The process of detecting & correcting errors in the program in called debugging.
Generally programmers do 3 types of errors:
• Syntax error: This Type of errors occur due to grammatical mistake in the program. This errors are encountered
while compiling & displayed with line number where the error has occurred.
Ex.: Missing of semicolon, opening of brackets & double quote & missing of closing them.
• Logical error: This type of errors occur during coding process. if the logic used by the programmer for his problem
is not correct, the logical error occur where the program will be executed giving unwanted result.
It is difficult to debug such errors as the computer will not display them.
It can be eliminated by running the program for sample data & by inserting print statement in appropriate location.
• Run Time Errors : This type of error occur when we try to run ambiguity instruction like infinite looping.
Some run time errors are
• Divide by zero.
• Null printer assignment
• Data overflow
Testing: The process of executing the program to test the correctness of the outputs of the problem. The program is
tested with different set of data.
Input-Output Operations: In order to make the program or a system user friendly to enter his choice, input functions
are used & to display result or to help the user to choose the appropriate function output function are used.
To perform input-output operation C provides a standard input-output library. The header file that contain this
functions is stdio.h
Standard input function Standard output functions
scanf( ) printf ( )
getchar ( ) putchar ( )
gets ( ) puts ( )
getch ( )
The standard input output function are classified into two types
• Formated Input - output functions
• Unformated Input - output functions
12.10 Electronics
14. Which arithmetic operator used to represent remainder obtained in integer division.
Ans. % called modulo division.
22. What is the meaning of the symbol ‘∼’ in character set of C? 2015 (A)
Ans. Tilde which results in 1’s complement.
24. Mention the size of memory allocated for a character (char) type data in C-programming. 2016 (A)
Ans. One byte
Section - B
21. Write any two types of errors that occur in C-program. 2016 (S)
Ans. Logical error
Run time error
Section - C
14. For a 8-bit computer, If A = 0000 0101 and B = 0011 0100. Compute the following bitwise operators.
(i) A & B (ii) A|B (iii) A ^ 8
Ans. A = 0000 0101
B = 0011 0100
A & B = 0000 0100
A | B = 0011 0101
A ^ B = 0011 0001
Section - D
IV. Five Mark Questions with Answers
4. If a = 25 and b = 5, find
(a) a + b (b) a – b (c) a * b (d) a / b (e) a%b (f) –a % b (g) a % –b
Ans. a. 31 b. 19 c. 150 d. 4 e. 1 f. –1 g. 1
5. Write C program to accept the three integers & print the largest among them. 2015 (A)
Ans. # include < stdio. h >
# include < conio. h >
viod main ()
{
int x,y,z;
print f (“ enter the value of x,y,z\ n”);
scanf (“ %d%d%d”, & x, &y, &z);
if (x >y )
{
if (x > Z)
{
print (“ x is largest \ n”);
}
else
{
}print (“ Z is largest \ m”);
else if (y > Z)
{
print (“ y is largest \n”);
}
else
{
printf (“ Z is largest \n”);
}
}
6. Write a C program to accept two numbers & Compute their sum & average. 2015 (S)
Ans. # include < stdio. h >
# include < conio. h >
viod main ()
{
int a, b, c;
printf (“ enter any 2 values to find sum & average\n”);
scanf (“%d%d”, &a, &b);
c = a + b;
printf (“the sum is %d”, c);
c = c/2;
printf (“the average is %d”, c);
}
12.18 Electronics