Document From :)
Document From :)
Document From :)
1. MACHINE LANGUAGE:
Machine language is the programming language in which all programming
instructions are in binary number system form. This language uses two
symbols “0” and “1” to represent the data which are also called binary
digits.
1
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
1. Compiler
2. Interpreter
3. Assembler
2
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
Compiler Interpreter
STEPS OF PROGRAMMING:
There are five main steps in programming project/ process.
3
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
ii. Algorithm:
Algorithm is a step by step method of designing a program using human
language. It describes the logic and processing of flow of a program.
iii. Flowchart:
Flowchart is a pictorial/ graphical representation of an algorithm of a
program.
3. Coding the program:
Once an algorithm has been developed and its flowchart has been
completed, the next step is to code the program. Coding is the process of writing
instructions in a programming language.
4
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
ERRORS
Bugs: Programming errors are known as bugs.
Debugging: The process of detecting and correcting these errors is called
debugging.
OR
Debugging is the process of locating and eliminating program errors.
TYPES OF ERROR:
1. Syntax Error
2. Logical Error
3. Runtime Error
1. Syntax Error:
The syntax of a programming language is a set of rules which have to be
followed by programmer when writing a program in that language. These rules
are similar to grammatical rules of English. When a program violates these errors,
computer generates a syntax error.
For example, when you are writing the output function in C- language, you cannot
write “print” instead of “printf”.
2. Logical Error:
A logical error will not stop the execution of a program, but result will not
be accurate.
For example, if a programmer wants to multiply A to B and put the result in the
variable C.
C = A*B
But while typing programmer just typed C = A/B, then the result will give the
division instead of multiplication
5
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
Such program will run properly but it will give the unexpected result.
3. Run-time Errors:
Run-time error occurs when a program is running on the computer and
results are not achieved due to misinterpretation of a particular instruction. This
could be something like dividing a number by zero which results in a very large
value of quotient.
2. Syntax errors are shown on screen. 2. Logical errors are not shown on
screen.
3. Syntax errors stop the execution of 3. Logical errors do not stop the
a program. execution of a program.
6
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
Questions
1. Define the term Program, Programmer, Programming and Visual
Programming? (Short)
7
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
8
CHAPTER # 1: CONCEPTS OF COMPUTER PROGRAMMING CLASS: XII
9
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
Example1:
Write an algorithm to compute the distance travelled by train in the
specific time.
Step1: Start the program.
Step2: Read value for SPEED.
Step3: Read value for TIME.
Step4: Set DISTANCE = SPEED * TIME
Step5: Write value of DISTANCE.
Step6: Exit.
Example2:
Write an algorithm to prepare the students’ mark sheet.
Step1: Start.
Step2: Read marks of all subjects.
Step3: Sum all subject’s marks as, Set TOTAL = ENG + MATH+ URDU +
PHY + ISL +COMP
Step4: Calculate percentage as, Set PERCENTAGE =
TOTAL*100/MAX_MARKS
Step5: Write marks of all subjects.
Step6: Write MAX_MARKS, TOTAL, and PERCENTAGE.
Step7: Exit.
PSEUDOCODE:
The prefix pseudo means fake; pseudo-code, therefore literally means fake
code- that is, not the code actually it is entered into the computer. It is considered
as the “First Draft”. In this we can concentrate on the logic of a specific language.
ALGORITHM NOTATION
OR
CERTAIN POINTS AND TERMINOLOGIES USED IN MAKING
ALGORTHM
1
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
Example:
Write algorithm that takes two numbers and generates output of their
sum:
Step1: Start
Step2: Read X [to get first number]
Step3: Read Y [to get second number]
Step4: Set SUM = X + Y [Adding two numbers]
Step5: Write SUM [displaying sum of two numbers]
Step6: Exit [Terminate of program]
2
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
1. TERMINAL SYBOL:
The terminal symbol is used to indicate the beginning (start), ending (Stop) and
pauses (Halt) in the program’s logic flow.
Oval
Halt Start Stop
Read X Write X
Parallelogram
4. PROCESS SYMBOL:
The process box indicates processing operations in the form of variables and
formulas.
OR
The process box is used for all arithmetic and data transfer operation
Rectangle
5. DECISION SYMBOL:
It is used whenever a decision of a condition is required in the form of yes or
no, true or false, less than or greater than, equal to or not equal to.
If Yes
A=10
No
Diamond
3
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
A
Circle
A
Pentagon
For
A=1 to 10
[FOR,
WHILE,
DO WHILE]
9. PRINTED DOCUMENT SYMBOL:
This symbol indicates printed output of data on paper as hard copy output.
4
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
ADVANTAGES OF FLOWCHART:
Communication: Flowcharts are better way of communicating the logic of
a system to all concerned or involved.
Effective analysis: With the help of flowchart, problem can be analyzed in
more effective way therefore reducing cost and wastage of time.
Proper documentation: Program flowcharts serve as a good program
documentation, which is needed for various purposes, making things more
efficient.
Efficient Coding: The flowcharts act as a guide or blueprint during the
systems analysis and program development phase.
Proper Debugging: The flowchart helps in debugging process.
Efficient Program Maintenance: The maintenance of operating program
becomes easy with the help of flowchart. It helps the programmer to put
efforts more efficiently on that part.
DISADVANTAGES OF FLOWCHART:
•Complex logic: Sometimes, the program logic is quite complicated. In that
case, flowchart becomes complex and clumsy. This will become a pain for the
user, resulting in a waste of time and money trying to correct the problem
•Alterations and Modifications: If alterations are required the flowchart may
require re-drawing completely. This will usually waste valuable time.
•Reproduction: As the flowchart symbols cannot be typed, reproduction of
flowchart becomes a problem.
5
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
CONTROL STRUCTURES
Control Structures control how the program executes.
Three types of control structures:
1. Sequence Logic or Sequential Flow
2. Selection Logic or Conditional Flow
3. Iteration Logic or Repetitive Flow
6
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
7
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
FOR:
The FOR loop uses an index variable, such as “K” to control the loop.
FOR K =1 to 10
Example of FOR loop:
2. WHILE LOOP…..
8
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
3. DO – WHILE LOOP….
The Do-WHILE loop tests condition after execution of at least once of
loop.
9
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
Example 1: Write an algorithm which reads and write your name, age,
height, and gender.
ALGORITHM:
Step1: Start
Step7: Exit
FLOWCHART:
10
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
Example 2: Write an algorithm for printing your name ten times using FOR
Statement.
ALGORITHM:
Step1: Start
Step2: FOR A=1 to 10
a) Write “Name”
b) A = A+1 [END OF FOR LOOP]
Step3: Exit
FLOWCHART:
11
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
ALGORITHM:
Step1: Start
Step2: FOR A = 1 to 10 by 1
a) Write A, A^2, A^3
b) Set A = A + 1 [END OF FOR LOOP]
Step3: Exit.
FLOWCHART:
12
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
ALGORITHM:
Step1: Start
Step2: Read NUM
Step3: FOR A = 1 to 10 by 1
a) Write NUM; “*” A; “=” NUM*A
b) Set A = A + 1 [END OF FOR LOOP]
Step4: Exit
FLOWCHART:
13
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
Questions
1. Define the term Algorithm with the help of examples. (Short)
2. Define the term Pseudocode. (Short)
3. What is an Algorithm? Write the names of Algorithm Notations. (Short)
4. Define algorithm and also define all the algorithm notations. (Long)
5. Define Flowchart. List all the symbols of a flowchart. (Short)
6. What is a Flowchart? List and also describe all the symbols of Flowchart.
(Long)
7. Describe all the Categories of flowchart. (Long)
8. Write the advantages and disadvantages of a flowchart (Short)
9. Describe the rules and guidelines while designing the flowchart. (Short)
10.What are the control structures? And name the types control structures.
(Short)
11.Describe the control structures with the examples of Algorithm and
Flowchart. (Long)
14
CHAPTER # 2: ALGORITHM AND FLOWCHART CLASS: XII
15
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
ADVANTAGES OF C – LANGUAGE:
1. C has many Advantages over other programming languages.
2. It is an efficient language, suitable for applications that run on PCs or
microcomputer, minicomputers, workstations, and other small machine.
3. It has large variety of operators and commands can be used.
4. It is capable of handling a wide variety of system applications.
5. C is a high level programming language because C uses English like
statements, which is easy to read and east to understand.
6. C enables programmers to have extensive control over computer hardware.
7. C is a portable language. A C-program which is written in one computer can be
run on any computer with little or no modification.
8. It has high execution speed.
9. C is the standard programming language in many industries and applications.
10.It is flexible, by creating your own functions and modules.
SOURCE CODE
The files we create with an editor are called “source file” and what we store
in this file in called “Source Code”. Your Source file is saved with an extension,
“.CPP”.
1
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
LIBRARY: A library is a collection of linkable files that are supplied with compiler.
Turbo C++ Compiler comes with a library of useful functions and classes that you
can include in your program.
Note: Executable codes have software library, which tells the meaning of
instruction’s words.
2
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
File Name
Menu Bar
Window Number
#include<stdio.h>:
3
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
‘#’ are called preprocessing directives. This # include directives tells the
compiler to use the information in the header file called “stdio.h”
The ‘stdio’ stands for “standard input/ output” and ‘h’ stands for header file.
The “stdio.h” file contains all the instructions and sends information to the
screen.
The angle brackets ‘<>’ around the <stdio.h> indicate that this file to be found
in the “usual place” which is system dependent.
Another header file is <conio.h> in which ‘conio’ stands for “console input/
output.
void main(void):
C programs are collection of “functions” designed to perform a specific task
and return a computed result.
All C programs must specify a function “main”.
‘( )’ parenthesis of main indicate the compiler that it is function.
The ‘void’ before ‘main’ specifies that the function main( ) will not return a
value.
The ‘void’ in the parenthesis, specifies that the function takes no arguments.
{ }:
This bracket called “Braces”
The left brace begins the body of function.
The right brace must end the function.
printf():
This is function from the library.
It is used to print on the screen.
It is accessible through the standard input/output (stdio.h) header file.
“string”:
A string constant in C language is a series of characters marked by the
quotation marks.
“This is my first C program”
This string is an “argument” to the function printf( ), which controls what is to
be printed.
printf(“ ”)
4
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
SAVE FILE:
UNDERSTANDING ARGUMENTS:
When we use a function in a statement, such as printf( ), we are to be “calling
the function”
Argument: An argument is an item of information that needs in order to complete
its tasks (arguments are also referred to as parameters).
For Example: printf( ) is a function in the library. The function contains instructions
that tell the computer to print a string of characters on the screen. But what string
does it display? You have to tell it, by placing what you want to display, in
parentheses.
COMMENTS:
A comment is a message to anyone reading the program.
Adding comments makes your program easier to understand.
Comments can appear in everywhere in a program.
They are ignored by compiler or linker.
They are never added to the object code or to the executable program.
5
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Example:
/* this program display a word Hello on the monitor screen and includes a void
main(void) function to make it compatible with Turbo C++ Compiler that required
it. */
ESCAPE SEQUENCES:
To control the way the cursor moves on your monitor screen, and some other
compiler functions, by using special codes, called “Escape Sequence”.
An escape sequence starts with backslash ‘\’
After the back slash ‘\’ use a single character. So that the character is having a
special meaning.
Escape
Description
Sequence
6
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
“\n”:
This is
my first
C program
“\t”:
The escape sequence ‘\t’ tab moves the cursor to the next present tab stop.
(tab = 9 spaces)
Example:
printf(“This is\n\t my first\n\t\t C program”);
This will generate the output like:
This is
7
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
my first
C program
“\r”:
The escape sequence ‘\r’ tab performs a carriage return, moving the cursor to
the start of the line.Example:
printf(“Left\rRight”);
This will generate the output like:
Right
The screen displays only the word Right. Here is why: After displaying the left, the
\r code moves the cursor back to the start of the line. The word Right is displayed,
replacing the characters of the word Left.
8
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Questions:
1. Give the Introduction of C-Language. (Short)
2. Write the advantages of C-Language. (Short)
3. Define the term Source Code. And also write the steps to compile the source
code. (Short)
4. Define the term Library and Function in C-Language. (Short)
5. Write the steps to create an executable file. (Short)
6. What is an IDE? What platform we use to write programs in C-Language?
Describe with the help of diagram. (Long)
7. Write the rules and regulations which a programmer has to follow while
writing a program. (Short)
8. Write the Basic Structure of C-Language Program. (Short)
9. Describe all the elements of the Basic structure of C-Language Program.
(Long)
10.What do mean by arguments in C language? Explain your concept by giving
an example of it. (Short)
11.Write the steps to save the file of C-Language source code. (Short)
12.What are the Comments in C and How many types of Comments? Name them.
(Short)
13.Explain the types of comments with the help of example. (Long)
14.What are the Escape Sequences? Name some escape sequences. (Short)
9
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
10
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Q1. State the errors in the following programs and rewrite them.
Rewrite:
#include<stdio.h>
void main(void)
{
printf(“I am a college student”);
}
Rewrite:
void main(void)
{
Printf(“I am a good programmer”);
}
11
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Output:
hello world
this is my first c language program
Output:
Name: alex
address: sindh, karachi
phone: +923334209211
12
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Output:
right
worldw
Output:
This is
my first
C Program
Output:
This is
my first
C Program
13
CHAPTER # 3: AN OVERVIEW OF C-LANGUAGE CLASS: XII
Source code:
void main(void)
{
printf(“Welcome to the Computer Lab”);
printf(“\nBoard of Intermediate Education Karachi”);
}
Source Code:
void main(void)
{
printf(“\“I Love my Pakistan\””);
}
Source Code:
void main(void)
{
printf(“To Continue,\tpress:\n\‘ENTER\’ key”);
}
14
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Chapter#4: C Fundamentals
DATA:
We refer to input given to the computer as data. You feed data into the computer,
the computer processes it following your instructions and give you information as output.
Before you input data your program, you have to tell it what type of data program is dealing
with.
THE C CHARACTER:
A character denotes any alphabet, digit or special symbol used to represent
information. The valid alphabets, numbers, and special characters allowed in C are listed
below:
Alphabets A, B, C, …, Z
a, b, c,…., z
Digits 0, 1, 2, …, 9
Special Characters = - * / = % & # @ $ ! ? ^ ~ “ ‘ \ | <> ( ) { } ; : , _ blank
space
Valid Identifiers:
x y1 y2 Sum_1 Sum2 TABLE table
tax_rate _auto _temperature
DATA TYPES:
The data items are classified into data type to prevent data values from being
manipulated with the wrong operations and to prevent their representations being
misinterpreted.
C supports several different types of data, each of which may be represented differently
within the computer’s memory. The basic data types, their description, and the memory
requirements for each data type is listed in the following table:
DATA MEMORY
DESCRIPTION
TYPES REQUIREMENT
2
CHAPTER # 4: C FUNDAMENTALS CLASS XII
An integer consists of an optional plus or minus sign followed by a series of digits and
cannot contain any other character. Commas, decimal points, or special symbols such as $
or e for exponential notations are not allowed.
BYTES
DATA TYPES RANGE FORMAT
WIDTH
short unsigned
0 to 65535 2 %u
int
-2147483648 to
long signed int 4 %ld
+2147483647
long unsigned
0 to 4294967295 4 %lu
int
2.73e + 3
Mantissa Characteristic
Base 10 —power to
Sign indicates
which base is
direction in
which point 10 is raised
is shifted
3
CHAPTER # 4: C FUNDAMENTALS CLASS XII
7.262e+4 72620.0
-2.1613e+2 -216.13
3.14E–3 0.00314
-729.0e–2 -7.29
The long double may refer to a separate “extra-large” double-precision data type. It stores
an 80-bit floating point number.
BYTES
DATA TYPES RANGE FORMAT
WIDTH
float 3.4e–38 to 3.4e+38 4 %f
BYTES
DATA TYPES RANGE FORMAT
WIDTH
4
CHAPTER # 4: C FUNDAMENTALS CLASS XII
CONSTANTS
CONSTANTS:
Constants are quantities whose values do not change during program execution.
TYPES OF CONSTANTS:
There are four basic types of constants in C.
1. Integer Constants 2. Floating Point Constants
3. Character Constants 4. String Constants
Integer Constants:
An integer constant is an integer-valued number. Thus it consists of a sequence of
digits.
Following rules must be observed while constructing integer constants:
An integer constant must have at least one digit.
It must not have a decimal point.
It could be either positive or negative.
If no sign precedes an integer constant it is assumed to be positive.
No commas or no blanks are allowed within an integer constant.
The allowable range for integer constant is -32768 to +32767.
Integer constants include decimal, octal, and hexadecimal constants.
Decimal Integer Constants: A decimal integer constant can consist of any
combination of digits from 0 to 9. If the decimal integer constant contains two or more
combination of digits, the first digit must not be a 0.
Examples of valid decimal integer constants are:
08 574-26-28 -36.28 67 29
Octal Integer Constants:
An octal integer constant can consist of any combination of digits from 0 to 7. However,
the first digit must be 0, in order to identify the constants as an octal number.
Examples of valid octal integer constants are:
0 01 -0573 0666
Examples of invalid octal integer constants are:
05927 264
Hexadecimal Integer Constants:
5
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Character Constants:
A character constant is a single character, enclosed in apostrophes (i.e.; single quotation
marks). They include the letters of alphabet (lower and uppercase), the numerals 0 to 9,
punctuation marks such as :, ; and ?, and special characters such as &, +, -, #, and $.
Some examples of valid character constants are:
‘B’ ‘m’ ‘5’ ‘*’ ‘?’ ‘ ’
The following character constants are written incorrectly for the reason stated.
‘3 missing right quotation mark
‘73’ more than one character inside quotation marks
# quotation marks missing
Virtually all the PCs make use of the ASCII character set to encode a character. The code
can represent 128 (i.e. 27 = 128) different characters. For example, the ASCII code for ‘A’
is 65.
String Constants:
6
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Example of variable names and their data types as they declared at the beginning of the
function look like:
void main(void)
{
int num;
float fl_num;
char ch;
}
If we have several variable names of same data type, separate each variable with comma
and end the declaration with semicolon.
void main(void)
{
int num, count, student, year;
float fl_num, discount;
7
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Variable Initialization:
In computer programming, initialization (or initialization) is the assignment of an initial
value for a data object or variable.
We can assign an initial value either in the declaration or as a separate instruction.
ARRAYS:
An array is an identifier that refers to a collection of data items that all have the same name.
C does not contain a string type and it doesn’t have any built-in string functions.
Fortunately, C allows us to work with string type data through the use of arrays.
To declare a string variable, we use the char variable type and specify the maximum
number of characters that the variable can hold.
Syntax:
char varname[n];
varname is name of the variable, and n is the maximum number of characters.
8
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Suppose we need a variable to store a name. Thus we can declare the variable like:
char name [10];
if we want to store a short paragraph as string thus, we need to declare a variable like:
Questions
1. Define the term Data. (Short)
2. Describe the “C-Character” and make some list of characters used by C. (Short)
3. Define the term Identifiers. State and also explain the rules while constructing
identifiers with examples. (Long)
4. Write some Valid and Invalid Identifiers and give reasons for Invalid Identifiers.
(Long)
5. What do you mean by the term reserved words or keywords? State why a keyword
cannot be used as identifier? Write standard keywords used by Turbo C. (Short)
6. What are the data types? How many data types are used by C-Language? Name
them. (Short)
7. Explain all the data types used by C-Language with the help of suitable examples.
(Long)
8. What are the constant? How many types of Constants in C? Name them. (Short)
9. What are the Integer constants? Explain in detail. (Short)
10.What are the Floating point constants? Explain them with examples. (Short)
11.What are the Character constants? Explain them with examples. (Short)
12.What are the String constants? Explain them with examples. (Short)
13.Explain all the Constants in C with the help of examples. (Long)
14.Define the term Variable and write the rules for constructing variable name.
(Short)
17.Define the term Array. Also write the syntax for Array in C. (Short)
9
CHAPTER # 4: C FUNDAMENTALS CLASS XII
10
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Q1. Determine which of the following valid or invalid identifiers are. Give the
reason for invalid variables.
(i) Array1
Valid variable
(ii) $tax
Invalid variable name i.e. Special character ‘$’ cannot be used.
(iii) name and address
Invalid variable name i.e. White spaces cannot be used.
(iv) Include
Valid variable name
(v) Stwenty
Valid variable name
(vi) 5-numbers
Invalid variable name, special character ‘-’ cannot be used.
(vii) y/s
Invalid variable name i.e. special character ‘/’ cannot be used.
(viii) integer
Valid variable name.
(ix) f_name
Valid variable name.
(x) getch
Invalid variable name i.e. keyword cannot be used as a variable name.
(xi) smallest
Valid variable name.
(xii) ono_o_no
Valid variable name.
(xiii) 1starit
Invalid variable name i.e. variable name cannot begin with number.
11
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Q3. Write appropriate declaration and assign the given initial values to following
variables and arrays.
(i) Integer variables: a = 485, b = 675, c = 890
int a=485;
int b=675;
int c=890;
(ii) Integer variable: students = 96854
int students = 96854;
12
CHAPTER # 4: C FUNDAMENTALS CLASS XII
Q4. Determine the following Constant type. And write the data type beside them:
(i) ‘t’
Character constant
(ii) “hellow”
String constant
(iii) 12345
Integer constant
(iv) “9”
String constant
(v) ‘1’
Character constant
(vi) 1
Integer constant
(vii) 4.567777
(viii) “I am Pakistani”
String Constant
(ix) “2”
String Constant
(x) -935
Integer Constant
13
CHAPTER # 4: C FUNDAMENTALS CLASS XII
(xi) 2
Integer Constant
(xii) ‘&’
Character Constant
(xiii) “&”
String Constant
(xiv) “1.7E+23”
String Constant
(xv) 1.7E+23
Q4) Determine the following that are valid or invalid integers, float, Character and
String.
(i) 15 Invalid
14
CHAPTER # 4: C FUNDAMENTALS CLASS XII
(v) z Invalid
(vi) ‘ ’ Valid
15
CHAPTER # 4: C FUNDAMENTALS CLASS XII
ASCII:
(American Standard Code for Information Interchange) Pronounced "ask-ee," it is the built-in binary code
for representing characters. ASCII was originally developed for communications and uses only seven bits
per character, providing 128 combinations that include upper and lower case alphabetic letters, the
numeric digits and special symbols such as the $ and %.
Since the common storage unit in a computer is an 8-bit byte (256 character combinations) and ASCII
uses only the first 128 (0-127), the second set of 128 characters (128-255) are technically not ASCII, but
are typically used for foreign language and math symbols.
ASCII is entered by typing in regular text, but because there are not enough keys on the keyboard to enter
256 distinct characters, the hexadecimal (hex) numbering system is used. Hex is entered by typing only
the digits 0 to 9 or the letters A to F, and it provides a precise way of defining any of the 256 possible
combinations in the byte, whether they be control codes (0-31) or the last 128 (128-255).
16
CHAPTER # 4: C FUNDAMENTALS CLASS XII
46 2E 00101110 . 80 50 01010000 P
47 2F 00101111 / 81 51 01010001 Q
48 30 00110000 0 82 52 01010010 R
49 31 00110001 1 83 53 01010011 S
50 32 00110010 2 84 54 01010100 T
51 33 00110011 3 85 55 01010101 U
Dec Hex Binary Char 86 56 01010110 V
52 34 00110100 4 87 57 01010111 W
53 35 00110101 5 88 58 01011000 X
54 36 00110110 6 89 59 01011001 Y
55 37 00110111 7 90 5A 01011010 Z
56 38 00111000 8 Dec Hex Binary Char
57 39 00111001 9 91 5B 01011011 [
58 3A 00111010 : 92 5C 01011100 \
59 3B 00111011 ; 93 5D 01011101 ]
60 3C 00111100 < 94 5E 01011110 ^
61 3D 00111101 = 95 5F 01011111 _
62 3E 00111110 > 96 60 01100000 `
63 3F 00111111 ? 97 61 01100001 A
64 40 01000000 @ 98 62 01100010 B
65 41 01000001 A 99 63 01100011 C
66 42 01000010 B 100 64 01100100 D
67 43 01000011 C 101 65 01100101 E
68 44 01000100 D 102 66 01100110 F
69 45 01000101 E 103 67 01100111 G
70 46 01000110 F 104 68 01101000 H
71 47 01000111 G 105 69 01101001 I
72 48 01001000 H 106 6A 01101010 j
73 49 01001001 I 107 6B 01101011 k
74 4A 01001010 J 108 6C 01101100 l
75 4B 01001011 K 109 6D 01101101 m
76 4C 01001100 L 110 6E 01101110 n
77 4D 01001101 M 111 6F 01101111 o
78 4E 01001110 N 112 70 01110000 p
79 4F 01001111 O 113 71 01110001 q
17
CHAPTER # 4: C FUNDAMENTALS CLASS XII
18
175 AF 10101111 » 209 D1 11010001 ╤
176 B0 10110000 ░ 210 D2 11010010 ╥
177 B1 10110001 ▒ 211 D3 11010011 ╙
178 B2 10110010 ▓ 212 D4 11010100 ╘
179 B3 10110011 │ 213 D5 11010101 ╒
180 B4 10110100 ┤ 214 D6 11010110 ╓
181 B5 10110101 ╡ 215 D7 11010111 ╫
182 B6 10110110 ╢ 216 D8 11011000 ╪
183 B7 10110111 ╖ 217 D9 11011001 ┘
184 B8 10111000 ╕ 218 DA 11011010 ┌
185 B9 10111001 ╣ 219 DB 11011011 █
186 BA 10111010 ║ 220 DC 11011100 ▄
187 BB 10111011 ╗ 221 DD 11011101 ▌
188 BC 10111100 ╝ 222 DE 11011110 ▐
189 BD 10111101 ╜ 223 DF 11011111 ▀
190 BE 10111110 ╛ 224 E0 11100000 α
191 BF 10111111 ┐ 225 E1 11100001 ß
192 C0 11000000 └ 226 E2 11100010 Γ
193 C1 11000001 ┴ 227 E3 11100011 π
194 C2 11000010 ┬ 228 E4 11100100 Σ
195 C3 11000011 ├ 229 E5 11100101 σ
196 C4 11000100 ─ 230 E6 11100110 µ
197 C5 11000101 ┼ Dec Hex Binary Char
198 C6 11000110 ╞ 231 E7 11100111 τ
199 C7 11000111 ╟ 232 E8 11101000 Φ
200 C8 11001000 ╚ 233 E9 11101001 Θ
201 C9 11001001 ╔ 234 EA 11101010 Ω
202 CA 11001010 ╩ 235 EB 11101011 δ
Dec Hex Binary Char 236 EC 11101100 ∞
203 CB 11001011 ╦ 237 ED 11101101 φ
204 CC 11001100 ╠ 238 EE 11101110 ε
205 CD 11001101 ═ 239 EF 11101111 ∩
206 CE 11001110 ╬ 240 F0 11110000 ≡
207 CF 11001111 ╧ 241 F1 11110001 ±
208 D0 11010000 ╨ 242 F2 11110010 ≥
19
243 F3 11110011 ≤ 250 FA 11111010 ·
244 F4 11110100 ⌠ 251 FB 11111011 √
245 F5 11110101 ⌡ 252 FC 11111100 ⁿ
246 F6 11110110 ÷ 253 FD 11111101 ²
247 F7 11110111 ≈ 254 FE 11111110 ■
248 F8 11111000 ° 255 FF 11111111
249 F9 11111001 ∙
20
CHAPTER # 5: Operators and Expressions CLASS: XII
EXPRESSIONS:
An expression is a collection of operands (one or more) and operators (zero or more) that
can be evaluated to a single value. An operator is a constant, variable, or another expression.
The most general form of expression is:
Operand-1 operator Operand-2
For Example:
2 + 6 evaluates to 8
4 * 5 evaluates to 20
48/ 8 evaluates to 6
10 - 3 evaluates to 7
18.4 – 4 evaluates to 14.4
TYPES OF OPERATORS:
C is very rich in operators and is sometimes called “the language of operators”. It has as
many as 45 different operators. Some commonly used operators are:
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical/ Boolean operators
5. Increment and Decrement operators
Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations. An arithmetic
expression is made up of constants, variable, a combination of both or a function call,
connected to arithmetic operators.
Following list provides the detail about arithmetic operators.
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
1
CHAPTER # 5: Operators and Expressions CLASS: XII
/ Division a/b
Assignment Operators:
The basic assignment operator is =. This is used to assign value of an expression to an
identifier. It has the general form:
2
CHAPTER # 5: Operators and Expressions CLASS: XII
Identifier = expression
Where “identifier” generally represents a variable and “expression” represents a constant, a
variable or combination of both.
Example:
a=5
x=y
a=b+c
area = length + breadth
Example:
Suppose i and j are integer variables whose values are 5 and 6, and f and g are floating
point variables whose values are 5.5 and -3.25, evaluate them:
3
CHAPTER # 5: Operators and Expressions CLASS: XII
Relational Operators:
Relational operators are used to compare two numeric operands. The operands can be
variables, constants or expressions that ultimately get evaluated to a numeric value. Characters
can be represented as integers using ASCII code; therefore, operands can be characters as well.
The '==' is the so-called equality comparison operator and is used to check whether the two
expressions on both sides are equal or not. It returns true of they are equal, and false if they
are not.
The '=' is the so-called assignment operator and is used to assign the result of the expression
on the right side of the operator to the variable on the left side.
Operator Operation
&& Logical AND
|| Logical OR
! Logical NOT
Example:
Suppose that i is an integer variable whose value is 7, f is a floating-point variable whose
value is 5.5, and c is a character value that represents a character ‘w’. Evaluate them:
Expression Interpretation Value
(i >= 6) && (c == ‘w’) true 1
(i >= 6) || (c == 119) true 1
(f < 11) && (i > 100) false 0
(c != ‘p’) || ((i + f) <= 10) true 1
Example: a++
5
CHAPTER # 5: Operators and Expressions CLASS: XII
Example: --a
Example: a--
Example:
Suppose i and j are integer variables whose values are 3 and 5 respectively. Evaluate
them:
6
CHAPTER # 5: Operators and Expressions CLASS: XII
LIBRARY FUNCTIONS:
Library functions in C language are built-in functions which are grouped together and placed
in a common place called library. Each library function in C performs specific operation.
Functions are available in a number of libraries. These are accessed by including header
files in your code; the header file is a pointer/reference to the library.
Some of the common library functions provided with most C compilers are listed in the table
below:
Function Description Header File
abs(i) Return the absolute value of i stdlib.
cos(d) Return the cosine of d math.h
exp(d) Raise e to the power d (e=2.7182818… is the math.h
base of natural logarithm).
fabs(d) Return the absolute value of d. math.h
getchar(c) Enter a character from standard input device stdio.h
putchar(c) Send a character to standard output device stdio.h
scanf(…) Enter data items from standard input device stdio.h
printf(…) Send data items to standard output device stdio.h
sin(d) Return the sine of d math.h
sqrt(d) Return the square root of d math.h
pow(d1, d2) Return d1 raised to the d2 power math.h
tan(d) Return the tangent of d math.h
tolower(c) Convert letter to lowercase ctype.h
toupper(c) Convert letter to uppercase ctype.h
Syntax: abs(i)
7
CHAPTER # 5: Operators and Expressions CLASS: XII
Questions:
1. Describe Operators and Operands with the help of example.
2. Define the term expression with few examples.
3. Write the classification of operators.
4. How many types of operators in C? Name them.
5. Define Arithmetic operators. Also explain them with examples.
6. What is Order of Precedence? Write the precedence of arithmetic operators.
7. Explain Assignment operator with the help of examples.
8. Explain Relational Operators with the help of examples.
9. Explain the difference between Equality(= =) and Assignment (=) Operators.
10. Explain Logical/Boolean Operators with the help of examples.
11. Explain Increment and Decrement Operators with their types.
12. What are the Library Functions in C-language? Make list of some of the common
library functions.
13. Define the following functions.
(a) pow( ) (b) sqrt( ) (c) abs( )
8
CHAPTER # 5: Operators and Expressions CLASS: XII
9
CHAPTER # 5: Operators and Expressions CLASS: XII
Suppose that a, b, and c are integer variables that have been assigned the values
8, 3, and -5 respectively. Evaluate each of the following arithmetic expressions:
Expressions Answers
o a+b+c 6
o 2 * b + 3 * (a – c) 45
o a/b 2.66
o a/c -1.6
o a%b 2
o a*b/c 4.8
o a * (b / c) 4.8
Suppose that a, b, and c are floating point variables whose values are 8.8, 3.5, and
-5.2 respectively. Evaluate each of the following arithmetic expressions:
Expressions Answers
o a+b+c 7.1
o 2 * b + 3 * (a – c) 143.0
o a/b 2.514285
o a/c -1.69230
o a / (b + c) -5.176470
o (a / b) +c -2.685714
o 2 * a /3 * b -20.533333
10
CHAPTER # 5: Operators and Expressions CLASS: XII
Expressions Answers
o i++ – j –– -1
o i––% j ++ 3
o j++ / i–– 2
o k++ * ––i 4
o i–1%j+1 3
Interpret the following expressions and write values (answers) as 0 or 1, Assume
that i = 1, j = 2, k = 3.
Expressions Values
o i<j 1
o (i + j)> = k 1
o (j + k) > (i + 5) 0
o k! = 3 0
o j==2 1
−𝑏+√𝑏2 −4𝑎𝑐
o x= x = (-b + sqrt((b*b)-(4*a*c)))/(2*a)
2𝑎
(2ab)2
o x= x = pow(2*a*b,2)/(3*c)
3𝑐
4
o y = π r3 y = (4.3)*3.142*pow(r,3)
3
(a2 – b2)2
o z= z = pow((a*a-b*b),2)/(a-b)
(𝑎−𝑏)
11
CHAPTER # 5: Operators and Expressions CLASS: XII
o x = (x + y)n x = pow(x+y , n)
o A = π r2 A = 3.142*r*r
b2 – c3
o x= x = ((b*b) – pow(c,3))/(2*b*c)
2𝑏𝑐
o x = √𝑥 + 𝑦 + z3 x = sqrt(x+y) + pow(z,3)
(4𝑎+3)
o (4*a+3)/(2*y*y+2*z+2)
(2𝑦 2 +2𝑧+2)
12
CHAPTER # 6: INPUT AND OUTPUT STATEMENT CLASS: XII
Formatted Functions
Unformatted Functions
Type Input Output
Type Input Output
char scanf( ) printf( )
getch( )
putch( )
int scanf( ) printf( ) char getche( )
putchar( )
Float scanf( ) printf( ) getchar( )
string scanf( ) printf( ) Int - -
Float - -
1
CHAPTER # 6: INPUT AND OUTPUT STATEMENT CLASS: XII
Format Specifier:
The format specifier is used during input and output. It is a way to tell the compiler
what type of data is in the variable during taking input using scanf( ) or printing using printf(
). Each format specifier starts with the percent symbol (%) followed by a letter indicating
the data type.
Specifier Meaning
%d Inputs or displays an integer number
%u Inputs or displays an unsigned integer
%f Inputs or displays a float
%e Inputs or displays number in scientific
notation
%c Inputs or displays a character
%s Inputs or displays a string
%o Inputs or displays an octal number
(unsigned)
%x Inputs or displays a hexadecimal number
(signed)
2
CHAPTER # 6: INPUT AND OUTPUT STATEMENT CLASS: XII
Example#3: Example#4:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main(void) void main(void)
{ {
clrscr( ); clrscr( );
int fnum; char name[10];
printf(“Enter any number : ”); printf(“Enter your name : ”);
scanf(“%f”,&fnum); scanf(“%s”,&name);
getch( ); getch( );
} }
Example#5: Example#5:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main(void) void main(void)
{ {
clrscr( ); clrscr( );
int a, b, c, d; int i;
printf(“Enter four integers: “); float b;
scanf(“%d %d %d %d”, &a, &b, &c, &d); char c;
printf(“%d, %d, %d, %d”, a,b,c,d); char string[10];
getch(); printf(“Enter all data types: “);
} scanf(“%d %f %c %s”, &i, &f, &c, string);
printf(“%d, %f, %c, %s”, i,f,c,string);
getch();
}
3
CHAPTER # 6: INPUT AND OUTPUT STATEMENT CLASS: XII
Example:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr( );
char ch;
// the character is displayed when pressed
printf(“Enter a character: ”);
ch = getche( );
getch( );
}
1. A char Constant:
putchar(‘T’);
2. A Variable Constant:
char ch = ‘p’;
putchar(ch);
4
CHAPTER # 6: INPUT AND OUTPUT STATEMENT CLASS: XII
Example: Example:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main(void) void main(void)
{ {
clrscr( ); clrscr( );
char ch=’P’ putchar(‘A’);
putch(ch); putch(‘\n’); //blank line
putchar(ch); putchar(‘B’’);
putch(‘A’); getch();
putchar(‘A’); }
getch();
} Output:
Output: A
PPAA B
String Input:
C contains a number of library functions to input text strings. The most common method is
the use of gets( ) function which is smaller and faster as compared to the formatted input
functions.
o The gets( ) Function:
The function gets( ) stands for “get string”. It reads a string from the keyboard and
assigns it to the required variable, which comes as an argument of the function.
char name[10];
gets(name);
String Output:
C contains a number of library functions to output text strings. The most common method
is the use of puts( ) function which is smaller and faster as compared to the formatted output
functions.
Turbo C++ perform a new-line command after the puts( ) function. This means that after
the data is displayed, the cursor will move to the start of the next line.
1. A string constant:
puts(“Best Wishes”);
2. A string variable:
char greet[30]= “Best Wishes”;
puts(greet);
5
Example:
void main(void) Output:
{
Best Wishes
char greet[30] = “Best Wishes”;
Best Wishes
puts(greet);
Hello World
puts(“Best Wishes”);
puts(“Hello World”); }
6
Questions:
1. What are the commonly used input/output functions in C language? Enlist them.
(Short)
2. Describe the formatted I/O functions with the help of examples. (Long)
3. Define Format specifier. Enlist the various format specifiers and also write their
descriptions. (Long)
4. How many unformatted single character input/output functions in C language?
(Short)
5. Define unformatted single character input functions used by C language with the
help of examples. (Long)
6. What is the main difference between getch( ) and getche( ) function? (Short)
7. Describe unformatted single character output functions used in C-language with the
help of example. (Long)
8. Describe Unformatted String input function with the help of example. (Short)
9. Describe Unformatted String output function with the help of example. (Short)
7
Multiple Choice Questions (MCQ’s)
8
Write printf( ) statements for the following output:
Hello
How are you?
I am fine, you tell.
Source Code:
printf(“Hello”);
printf(“\n\tHow are you?”);
printf(“\n\tI am fine, you tell,”);
1
2
3
A
Source Code:
putchar(‘1’);
putchar(‘\n’);
putchar(‘2’);
putchar(‘\n’);
putchar(‘3’);
putchar(‘\n’);
putchar(‘A’);
9
Find and write the error of the following program and also it in a debugged form:
Find and write the error of the following program and also write it in a debugged
form:
void main (void)
{
printf(“ “This is a string” ”) missing semi colon(;) and escape sequence (\”)
}
Find and write the error of the following program and also write it in a debugged
form:
Write the source code for the following output by using format specifiers.
Output: Source code:
34 printf(“%d”34);
35.666 printf(“\n%.3f”,35.666);
Hellow printf(“\n%s”,”Hellow”);
@ pritnf(“\v%c”,‘@’);
11
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
1
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
getch( );
}
The while loop allows the programmer to specify that an action is to be repeated while
some condition remains true. It is used when the exact number of loop repetitions are not
known before the loop execution begins.
The general form of the while statement is:
while(condition)
statement;
void main(void)
{
clrscr( );
char ch;
printf(“Enter a character: ”);
ch = getche( );
while(ch!=‘q’)
{
printf(“\nCharacter = %c”,ch);
printf(“\nEnter a Character: “);
ch = getche( );
}
getch( );
}
printf(“\n”);
row++;
}
getch( );
}
For Example:
void main(void)
{
clrscr( );
int count =1;
do
{
printf(“%3d”, count);
}
while(++count<=10)
getch( );
}
3
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Example # 1:
void main(void) Output:
{ 1 2 3 4
clrscr( );
int i;
for(i = 1; i<=10; i++)
{
if(i ==5)
break;
printf(“%3d”, i);
}
getch( );
}
Example # 2: Output:
void main(void)
{ 1 2 3 4 6 7 8 9 10
clrscr( );
int i;
for(i = 1; i<=10; i++)
{
if(i ==5)
continue;
printf(“%3d”, i);
}
getch( );
}
The main difference between a while loop and do while loop is that while loop check
condition before iteration of the loop, whereas do-while loop, checks the condition after the
execution of the statements inside the loop.
4
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Example # 2: Write a program to print the sum of odd numbers from 1 to 100, i.e.
sum = 1 + 3 + 5 + 9 + … + 99
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int i, sum=0;
for(i=1; i<100; i=i+2)
sum = sum + i;
printf(“\n\n\nThe sum is = %d”, sum)
}
getch( );
}
5
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int row, column;
for(row = 1; row< = 5; row++)
{
for(column = 1; column < = 5; column ++)
printf(“*”);
printf(“\n”);
}
getch( );
}
Example # 4: Write a program which reads a number and prints its factorial using for
loop:
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int num, fact = 1;
printf(“Enter number to find its factorial: ”);
scanf(“%d”, &num);
for(int i = 1; i<=num;i++)
fact = fact * i;
printf(“\n\nThe factorial of %d is %d”, num, fact);
getch( );
}
6
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Example # 1: Write a program which reads and writes a character until the inputted
character is ‘q’.
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
char ch;
printf(“Enter any character: ”);
ch = getche( );
while(ch != ‘q’)
{
printf(“\n Character = %c”, ch);
printf(“\n\n Enter a character: ”);
ch = getche( );
}
getch( );
}
Example # 2: Write a program which prints your name ten times using while loop.
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int i = 1;
while(i<=10)
printf(“\nyour name”);
getch( );
}
7
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Questions:
1. What is iteration Control structure? And enlist the iteration control structures in
programming languages? (Short)
2. What is for loop used for? Write its general form with example. (Long)
3. Define nested for loop with suitable example. (Short)
4. What is while loop? When is it used? Write its general form with example. (Long)
5. Write a program using nested while loop. (Short)
6. Define the ‘do-while loop’ and also write its general statement with suitable example.
(Long)
7. What is break and continue statement used for? Describe them with example. (Long)
8. What is the major difference between while and do-while loop? (Short)
8
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Conversion:
void main(void)
{
int a = 3;
while(a<=10)
{
printf(“%d\n”,a);
a = a+3;
}
}
What will be the output of the following C-language program?
for(i=5; i > 0; i--)
{
for(j=1;j<=i;j++)
printf(“%d”,i);
printf(“\n”);
}
Output:
55555
4444
333
22
1
9
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Write a program which prints all the ASCII characters from 0 to 255.
use two methods:
i. for-loop
ii. while-loop
for-loop:
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int i;
for(i=0; i<=255; i++)
printf(“%d\t%c\n”,i,i);
getch( );
}
while-loop:
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int i=0;
while(i<256)
{
printf(“%d\t%c\n”,i,i);
i++;
}
getch( );
}
10
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Source Code:
#include<stdio.h>
#inlcude<conio.h>
void main(void)
{
clrscr( );
int num, i;
printf(“Enter any number: ”);
scanf(“%d”,num);
for(i=1; i<=10; i++)
printf(“%d * %d = %d\n”, num, i, num*i);
getch( );
}
23
45
67
89
11
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Program:
void main(void)
{
int i=4;
do
{
printf(“%d\t%d\n”, i, i*i);
i++;
}
while(i<10);
}
while-loop:
int a=1;
while(a<=100)
{
printf(“%d\n”,a*a);
a++;
}
12
CHAPTER # 8: ITERATION CONTROL STRUCTURES CLASS: XII
Output:
12345
12345
12345
12345
12345
Output:
1 4 9 16 25 36 49 64 81 100
13
CHAPTER # 9: FUNCTIONS CLASS: XII
Chapter # 9: Functions
FUNCTIONS:
A function is a block of instructions that together perform a specific task. Every C
program has at least one function, main( ). You can also write your own functions and use
them just as you use functions in a C library.
Characteristics of function:
1. The complexity of the entire program can be divided into simple subtasks.
2. Functions help us to avoid unnecessary repetition of code. Function can be used many
times in the same program.
3. A function can be shared by other programs by compiling it separately and loading
them together.
4. Functions can have inputs and outputs and can process information.
5. In Turbo C++, a function can call itself again and again.
CLASSIFICATION OF FUNCTIONS:
Functions are of two types.
These are:
1. Built-in Functions/ Library Functions
2. User-defined Functions
Built-in Functions:
Many of the operations, like taking the square root of a number, sin value of a number,
etc., will be frequently used by many programmers in their programs. These functions are
called Library Function or Built-in Functions
User-defined function:
Apart from the library functions that are built-in, users can also define functions to do a
task relevant to their program; such functions are called user-defined functions. These
functions should be codified by the user, so that any call to the function can refer to it.
1
CHAPTER # 9: FUNCTIONS CLASS: XII
2
CHAPTER # 9: FUNCTIONS CLASS: XII
Function Definition:
The function definition is a function itself. The general structure of the function
definition is:
type function_name (type argument-1, argument-2, …)
{
body of function;
}
The function definition has two parts:
1. Function Header
2. Body of Function
Function Calling:
A user defined function is called from the main program simply by using its name,
including the parenthesis which follows the name.
Syntax: function_name( );
Example: add( );
If function returns a value, then we can store return value to a variable in the calling function.
Example: x = add( x , y)
Argument:
Sending data to a function is called passing arguments; sending variables, constant, or
expression whose values are needed by the function. The arguments are the input data that the
function needs to compute its result.
Some examples of prototypes of user-defined functions are:
void display(void);
int average(void);
void subset(int x);
float big_pow(double x, double y);
3
CHAPTER # 9: FUNCTIONS CLASS: XII
#include<stdio.h>
#include<conio.h>
void table(int num);
void main(void)
{
clrscr( );
int n;
printf(“\t\t\t******PRINTING TABLE********\n\n”);
printf(“Enter any number: ”);
scanf(“%d”,&n);
table(n);
printf(“Enter any number: ”);
scanf(“%d”,&n);
table(n);
getch( );
}
void table(int num)
{
for(int i=1;i<=10;i++)
printf(“%d * %d = %d\n”,num,i,num*i);
}
Example2: Write a program that prints area of rectangle by passing multiple arguments
to a user-defined function.
#include<stdio.h>
#include<conio.h>
void rectangle(float width, float height);
void main(void)
{
clrscr( );
float w, h;
printf(“Enter width of rectangle: ”);
scanf(“%f”,&w);
printf(“Enter height of a rectangle: ”);
4
CHAPTER # 9: FUNCTIONS CLASS: XII
scanf(“%f”,&h);
rectangle(w,h);
getch( );
}
void rectangle(float width, float height)
{
float area;
area = width*height;
printf(“Area of rectangle = %f”,area);
}
Example3: Write a program that prints different sizes of rectangle and square by passing
multiple arguments to a user-defined function.
#include<stdio.h>
#include<conio.h>
void rectangle(int width, int height);
void main(void)
{
clrscr( );
printf("rectangle(7,4)\n");
rectangle(7,4);
printf("\nrectangle(8,4)\n");
rectangle(8,4);
printf("\nrectangle(2,1)\n");
rectangle(2,1);
printf("\nrectangle(7,8)\n");
rectangle(7,8);
getch( );
}
void rectangle(int width, int height)
{
int i,j;
for(i=1;i<=height;i++)
{
for(j=1;j<=width;j++)
printf("\xDB");
printf("\n");
}
}
5
CHAPTER # 9: FUNCTIONS CLASS: XII
Write a program that passing three arguments and return the maximum value of
argument by using user-defined function.
#include<stdio.h>
#include<conio.h>
int max(int a, int b, int c);
void main(void)
{
clrscr( );
int x, y, z;
printf("Enter First Number: ");
scanf(“%d”,&x);
printf("Enter First Number: ");
scanf(“%d”,&y);
printf("Enter First Number: ");
scanf(“%d”,&z);
printf(“The maximum number: %d”, max(x,y,z));
getch( );
}
6
CHAPTER # 9: FUNCTIONS CLASS: XII
Write a function that returns the value of factorial of inputted number by using
function.
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main(void)
{
clrscr( );
int num;
printf("Enter a Number: ");
scanf(“%d”,&num);
int fact(int n)
{
int f=1;
for(i=n;i>=1;i--)
f = f*I;
return f;
}
7
CHAPTER # 9: FUNCTIONS CLASS: XII
Questions:
1. What is function? Write the characteristics/importance/advantages of functions. (Short)
2. What is a built-in function? Explain with examples. (Short)
3. What is a user-defined function? (Short)
4. Write the general form of a function. (Long)
8
CHAPTER # 9: FUNCTIONS CLASS: XII
1. A function is a:
a. Header file b. block of code c. library d. none of them
2. A global variable is defined in a declaration:
a. In main( ) only b. in the first function that uses it
c. in any function that uses it d. outside of any function
3. Use the ____________ data type when the function does not return a value.
a. int b. float c. char d. void
4. A function that calls itself is called a:
a. recursive function b. function calling
c. function d. all of them
5. Every C program must have this function:
a. scanf( ) b. getch( ) c. main( ) d. printf( )
6. All variables declared in function definition are called:
a. Local variables b. Instance variable
c. Static variables d. Global variables
7. Which is not a built-in function?
a. pow( ) b. sqrt( ) c. sin( ) d. average( )
8. A function prototype is also known as:
a. Function call b. Function definition
c. Function header d. Function declaration
9. int average(int a, int b, int c) In this function declaration, , how many parameters are
passed?
a. 1 b. 2 c. 3 d. 4
10. The functions that are defined by the programmer are called:
a. Built-in function b. User-defined Function
c. Sub-function c. Function
9
CHAPTER # 14: DATA FILES CLASS: XII
C files can be thought of as a list of characters that include newline, spaces, tabs, letters,
numerals punctuation marks, and control characters. Similar to the editor program in which the
<enter> key causes a newline character (presented by C as ‘\n’) to be placed in the file.
Declaring a File:
The header file <stdio.h> contains a number of constructs that pertain to files. Among there is
the identifier FILE. It is a constant and so it is written with uppercase letters.
FILE *file_pointer;
Where keyword FILE is a structure type that establishes the memory buffer and file_pointer is
a pointer variable that indicates the beginning of this buffer.
If you want to use more than one file simultaneously, use this syntax:
Opening a File:
The link between your program, the file, and the computer is established with fopen( ) function,
using the syntax:
Here “pointer” is a pointer variable, “fopen” is a keyword, “filename” is the name of the
file, and mode is the Mode of accessing the file.
The mode argument specifies the type of operation you will be performing. In C commands,
you also enclose the mode arguments in quotation marks.
1
CHAPTER # 14: DATA FILES CLASS: XII
It can be:
Mode Meaning
“r” Open a text file for reading
“w” Create a text file for writing
“a” Append (add at end) to a text file
“r+” Open a text file for read/write
“w+” Create a text file for read/write
“a+” Append or create a text file for read/write
"r":
Opens a file for reading. The file must exist.
For example, if your program will be reading from the file, instead of writing a to it, use this
syntax:
FILE *fptr;
fptr = fopen(“myfile.txt”, “r”);
"w":
Creates an empty file for writing. If a file with the same name already exists, its content is
erased and the file is considered as a new empty file.
For example, to create a file called myfile.txt in the current directory for writing, you would
use:
FILE *fptr;
fptr = fopen(“myfile.txt”, “w”);
"a":
Appends to a file. Append mode causes a file to be created if it does not already exist. If it does
exist, the data you write on it will be added to the end of the file, without erasing its content.
For example, you want to append a file, use a command such as:
FILE *fptr;
fptr = fopen(“myfile.txt”, “a”);
2
CHAPTER # 14: DATA FILES CLASS: XII
Write a C program to Open a file using File opening Modes i.e. “a”, “w”, “r”.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
clrscr();
FILE *fp;
char ch, flag='y';
char name[30];
/* Creates an empty file for writing. If a file with the same name already exists, its content is
erased and the file is considered as a new empty file. */
fp=fopen("file.text","w");
if(fp==NULL)
printf("Error!\n");
else
{
while(flag!='n')
{
printf("Enter a name: ");
gets(name);
fputs(name,fp);
fputs("\n",fp);
printf("Enter more name? ");
flag = getche();
printf("\n\n");
}
}
fclose(fp);
getch();
}
3
CHAPTER # 14: DATA FILES CLASS: XII
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
clrscr();
FILE *fp;
char ch, flag='y';
char name[30];
fp=fopen("file.text","r");
if(fp==NULL)
printf("Error!\n");
else
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
/* If file exists, the data you write on it will be added to the end of the file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
clrscr();
FILE *fp;
char ch, flag='y';
char name[30];
fp=fopen("file.text","a");
if(fp==NULL)
printf("Error!\n");
else
fputs("Hello world\nWelcome",fp);
fclose(fp);
getch();
}
4
CHAPTER # 14: DATA FILES CLASS: XII
Questions
1. Define data files.
2. Define the following file opening modes in C:
a. “r” b. “w” c. “a”
5
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
RECORD:
A record is a collection of related fields. An employee record would be a collection of fields
of one employee. These fields would include Employee Name, Employee pay, Tax-rate,
Employee ID, etc.
FILE:
A file is a collection of related records that are treated as a unit. For example, a collection of
all employee records for one company would be an employee file.
KEY FIELD:
A key field is a particular field chosen to uniquely identify a record so that it can easily
be retrieved and processed.
PRIMARY KEY:
A primary key is a column or a group of columns in a table that uniquely identifies the
rows in that table. For example, in the table Customer, Customer_No, which displays the ID
number assigned to different customers, is the primary key.
SECONDARY KEY:
A secondary key is an additional key, or alternate key, which can be used in addition to
the primary key to locate specific data.
FOREIGN KEY:
A Foreign Key creates a link between tables. It references the primary key in another
table and links.
1
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
Database management systems are necessary for businesses and organizations because
they provide a highly efficient method for handling multiple types of data. Some of the data
that are easily managed with this type of system include: employee records, student
information, payroll, accounting, project management, inventory and library books. These
systems are built to be extremely versatile.
ADVANTAGES OF A DBMS:
The advantages of DBMS software are:
Increase Security:
Through the use of passwords, a student’s financial, medical, and grade information in
a university database is made available only to those who need to know.
2
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
DDL:
Data Definition Language (DDL) have facilities for describing the structure of fields,
records, files and program accessing rights. CREATE, ALTER, DROP, TRUNCUATE are the
commands of DDL.
DML:
Data Manipulation Language (DML) is used for accessing and updating data records and
files contained within the database.
INSERT, UPDATE, DELETE, MERGE, etc are the commands of DML.
SQL:
The SQL (Structured Query Language) contains all the features of both the data
definition language and the data Manipulation language. Also the American National Standards
Institute’s (ANSI) recommended language for relational database definition and manipulation
is SQL.
DATA DICTIONARY:
As data is entered, the data dictionary checks data to make sure that it follows the rules
defined during data definition, such as field name, field size, type of data. The data dictionary
may also help protect the security of the database by indicating who has the right to gain access
to it.
Below are some of the chief responsibilities that make up the day-to-day work of a DBA.
Software installation and Maintenance
Data Extraction, Transformation, and Loading
Database Backup and Recovery
Security
Performance Monitoring
3
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
In the hierarchical database model, data is organized like a family tree, with lower-level
records to higher-level records. A lower-level record is called a child, and a higher level record
is called a parent. The parent record at the top of the database is called the root record.
4
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
PARTS TABLE
Part Cost SHIPMENTS TABLE
Name Part Supplier
Number (Rs.) Quantity
123 ½ inch screw 5 Number Number
234 Circuit Board 25 123 1 2000
345 Coiled cord 45 234 2 4000
456 Keyboard Casing 200 345 3 2300
567 Key Set 95 456 4 5400
567 5 2100
SUPPLIERS TABLE
Suppliers
Name Address Phone
Number
34 Ground
1 Pak Hardware 5872843
Street
2 Computer Company 3854732
21 Mall Road
3 Badar Hardware 4857839
14 Kashif Lane
4 Dataline Wiring 6758987
45 Silver
5 Electro Electronics 3547678
76 Green Street
RDBMS:
RDBMS stands for Relational Database Management System is a collection of programs
and capabilities that enable IT teams and others to create, update, administer and otherwise
interact with a relational database. RDBMS stores data in the form of tables, with most
commercial relational database management systems using Structured Query Language (SQL)
to access the database.
ADVANTAGES OF RDBMS:
Following are the benefits or advantages of RDBMS:
5
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
It is easy to use.
The data manipulation can be done.
It reduces redundancy and replication of the data.
It offers better data integrity.
It provides better physical data independence.
It offers logical database independence i.e. data can be viewed in different ways by the
different users.
It provides multiple interfaces.
It provides better backup and recovery procedures.
DBMS RDBMS
1. RDBMS applications store data in
1. DBMS applications store data as file
tabular form.
2. DBMS does not support distributed
2. RDBMS supports distributed database.
database.
3. DBMS is meant to be small organization 3. RDBMS is designed to handle large
and deal with small data. amount of data.
4. It supports single user. 4. It supports multiple users.
5. Examples of DBMS are file systems, 5. examples of RDBMS are mysql, sql
xml, etc. server, oracle, etc.
Questions:
1. What is database? (Short)
2. What is Entity, Field, Record, and file? (Short)
3. What is Key field, Primary Key, Secondary Key, and Foreign Key? (Short)
4. What is Database Management System (DBMS)? Describe it with examples. (Short)
5. Why is DBMS necessary for any big organization? (Short)
6. Describe advantages of DBMS. (Short)
7. What is query language? (Short)
8. What is DDL and DML? (Short)
9. What is Data Dictionary? (Short)
10. What are the components of DBMS? Describe them all. (Long)
11. What are the responsibilities of DBA?
12. What is database model? Name the various database models. (Short)
13. Describe Hierarchical Database Model with diagram? (Long)
14. Describe Network Database Model with diagram? (Long)
15. Describe Relational Database Model with diagram? (Long)
6
CHAPTER # 15: DATA MANAGEMENT SYSTEMS CLASS: XII
3. The specialized software necessary to create and process the database and modified as
needed is called:
a. Compiler b. Windows XP c. DBMS d. Interpreter
8. A foreign key:
a. Has nothing to do with primary key
b. has different values than the primary key
c. is found in a tables that do not have a primary key
d. is related to the primary key of a different table
7
CHAPTER # 16: BASICS OF MS ACCESSS CLASS: XII
1
CHAPTER # 16: BASICS OF MS ACCESSS CLASS: XII
Questions
1. What is MS-Access? (Short)
2. What are the main objects of database or MS-Access? (Short)
3. What are the main objects of database or MS-Access? Describe them. (Long)
4. What are the Data types in MS-Access? Name them. (Short)
5. Describe the Data types in MS-Access. (Long)
2
CHAPTER # 16: BASICS OF MS ACCESSS CLASS: XII
7. Which of the following data types allows you to specify a field size?
a. Text b. number c. Date/ Time d. Currency
3
Chapter # 17: Advanced MS Access 2000
Relationship:
A Relationship is a link that is created and maintained between two tables that enables
data to be accessed from both the tables simultaneously, and is technically called a join.
Types of Relationship:
Access recognize three kinds of relationships which are as follow
1. One-to-one
2. One-to-many
3. Many-to-Many
One-to-one:
Each record in table A can have only one matching record in table B. and each record
in a table B can have only one matching record in table A.
One-to-many:
A one-to-many relationship is the most common type of relationship. A record in table
A can have many matching in table B, but record in table B has only one matching record in
table A.
Many-to-Many:
In a many-to-many relationship, a record in a table A can have many matching records
in table B, and a record in table B can have many matching records in table A.
Questions
1. What is Relationship in a database? What are the types of relationship? Name them.
(Short)
2. Describe the types of relationship in database. (Long)
3. Why do we use Table, Form, and Report in MS-Access?