Cs Imp25
Cs Imp25
Q1) Define Program And What are the steps of problem solving? Define each of them.
Ans:
Program:A program is a set of instructions that computer must follow in order to process data
into information. The instructions consists of statements used in programming language, such as
BASIC, Pascal ,C, C++,Java etc.
The five main steps in the programming process are as follow:
1. Defining the problem.
2. Planning the solution.
3. Coding the program.
4. Testing the program.
5. Documenting the program.
INTERPRETER:
Interpreter translate the program one statement at a time as a program is actually being run on the
computer. Each statement is translated and executed before interpreter processed to the next
statement. The process continuous until the end of the program is reached
COMPILER:
Compiler translate the entire source program into machine language in one process creating an object
file. Another referred to Linking or linkage editor prepare the object program.
ASSEMBLER:
The assembler translate the program written in assembly language into machine language instruction
for execution
Types of Errors
There are three types of errors that occur in a computer program: syntax errors, logic errors and
runtime errors. +
Syntax errors: A syntax error is a violation of the rules of whatever the language the programs
being written. These are often caused by simple typing mistakes. for example, the C statement
C = (A+B/2
Logic errors: the logical error is an error in planning the program’s logic. In this case the
language processor successfully translates the source code into machine code.
When a logical error occurs, all you know is that the computer is not printing the correct output.
The computer does not tell you what is wrong.
Let,
A=5
B=6
C=A+B
But while typing the programmer has’ however typed the following expression:
C=A – B
Runtime errors: Runtime errors occur when the program is run on the computer and the results
are not achieved due to some misinterpretation of a particular instruction. This could be
something like dividing a number by zero which results in a very large value of quotient. It may
also be any other instruction which a computer is not able to understand.
CHAPTER # 3 & 4
C is a powerful programming language developed by Dennis Ritchie at Bell (now AT & T’s Bell)
Laboratories of USA in 1972.
C evolved from two earlier languages: BCPL (Basic Combined Programming Language) and B.BCPL
was developed in 1967 by Martin Richards at Cambridge University as a language for writing
operating systems software and compilers. In 1970, Ken Thompson at Bell Laboratories developed a
language based on BCPL and called it B, the first letter of BCPL. Dennis Ritchie developed B further
into a new language and gave it the name C, the next letter of BCPL.
Q5) What is escape sequence? Enlist the escape sequence and write purpose of each escape
sequences.
Ans:
OR
You can control the way the cursor moves on your monitor and some other computer functions, by
using special codes, called escape sequences. An escape sequence consists of the escape character
(the backslash ’\’) and a second character.
Escape sequence Characters Meaning
Escape Sequence
\n New line Result at Execution Time
\t Tab Move the cursor to the next
tab stop
\a Alert Produces a beep
\r Carriage return Moves the active position to
the initial position of the next
paragraph
\b Backspace Moves the active position to
the previous position of the
current line
\f Form feed Moves the active position to
the initial position of the next
logical page
\0 End of string Null
\\ Backslash Present with the Backslash \
\' Single quote Present with the single qoute
\" Double quote Present with the double
quote
\? Question mark Present with the question
mark
The special character \r, or carriage return, instructs the cursor to move from the end of one line
back to the start of the same line, essentially overwriting any previous content entered.
On the other hand, \n, or line feed, instigates a new line at whichever point it is entered; the
existing content is not deleted when using \n.
Q) Define reserved words and also make a list of reserved words used in C language.
Reserved words: There are certain reserved words, called keyword, that have standard, predefined
meanings in C. These keywords can be used only for their intended purpose; they cannot be used as
variable names. The standard keyword for C are listed below:
Q6) What do you know about identifier?And write the rules for construct identifier
Ans:
Identifiers and Keywords
Identifiers are names given to various program elements, such as variables, functions and arrays.
Following rules must be observed while constructing identifiers:
1. An Identifier must start with a letter or (rarely) an underscore (_).
2. An Identifier can contain letters, digits, or underscores (_).
3. An identifier cannot contain blanks, commas, or any other special characters.
4. An identifier cannot consist of more than 31 characters.
5. An identifier must confirm to case sensitivity (C differentiates between uppercase and lower case
alphabetical characters). For examples, the identifier MAX is not the same as the identifier max.
C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given name
and data type. When the variable is declared compiler automatically allocates the memory for it.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some value to it. A defined
variable will contain some random garbage value till it is not initialized.
Example
int var; char var2;
3. C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to the
variable.
Int var = 5;
Float avg = 9.8;
Q8) Explain Data types. Write names of different data types used in C language.
Ans:
int data type
Q9) Explain Arrays and write its syntax using in C language.
Ans:
Arrays:
An array is an identifier that refers to a collection of data items that all have the same name. The data
items must all be of the same type (e.g., all integers, all characters, etc.). C does not contain a string
type and it does not have any built-in string functions. Fortunately, C does allow us to work with
string-type data through the use of arrays. The characters of the string are stored together, in
consecutive memory storage locations.
You cannot assign more characters to the variable than you declare. That’s because C sets aside just
enough space to store the maximum number of characters. So think carefully before declaring the
variable.
For example, suppose you need a variable to store a student’s name. You declare it this way:
Char student [10]; you could declare the variable with some large number, such as
Char student [80];
a) int (Integer)
int number;
b) char (Character)
char letter;
float temperature;
double distance;
CHAPTER # 5 & 6
Q11) Define operators. Explain the classifications of operators with its examples.
a) Arithmetic b) assignment c) Relational d) Logical e) equality
f) Increment g) Decrement
Ans:
Operators are needed to perform such type of calculations that transform data into information. An
operator defines what is done with data. Therefore we can say that ‘operators’ are use to perform
some ‘operation’ on the ‘operands’. For example:
a=b+c
Here, b and c are operands, and the operation is being performed is ‘addition’ by using ’+’
operator.
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 by
arithmetic operators. Following list provide the detail about arithmetic operators.
+ ADDITON A+B
- SUBTRACTION A-B
* MULTIPLY A*B
/ DIVIDE A/B
% MODULUS/REMAINDER A%B
The basic assignment operator is = . This is used to assign the value of an expression to an identifier.
It has the general form
identifier = expression
where identifier generally represents a variable, and expression represents a constant, a variable, or a
more complex expression.
Some examples of expression that make use of the = operator are:
a = 5, x = y
OPERATOR OPERATIONS EXAMPLE EXPLAINATION
Relational Operator:
Relational operators are used to compare two numeric operands. The operands can be variables,
constants or expressions that ultimately get evaluated to a numerical value. Since characters can be
represented as integers using ASCII codes, therefore operands to be compared can be characters as
well. All C relational operators are binary operators and hence required two operands. The
relational operators are summarized as
Logical/Boolean Operators
A logical operator also called Boolean operator to honor the Irish mathematician Boole (1815-1864)
combines the result of one or more expressions and the resultant expression is called the logical
expression. After testing the conditions, they return either true (non-zero value) or false (0) as net
result.
The logical operators are unary or binary operators. The operands may be constants, variables or
expressions. The basic logical operators are:
Equality Operator:
Operators that are used to compare values for equality or inequality fall under the category of equality
operators. These operators evaluate whether the values of operands are equal or not and return a
boolean value (true or false).
Q12) Write formatted and unformatted I/ O functions with their data type..
ANS:
UNFORMATTED I/O FUNCTIONS
GETS():-
The function gets() stands for “get string”. It reads a string from the keyboard, adds the null
characters’\0’ to it, and assigns it to the required variable, which comes as an argument of the
function. The newline charater’\n’(created by pressing the enter key)signals the end of the string
GETCH():
The getch() function is an unformatted input function in C used to read a single character from
the keyboard. It does not display (echo) the character on the screen and does not require
pressing Enter.
GETCHE():
The getche() function is similar to getch(), but it displays (echoes) the entered character on the
screen as soon as a key is pressed.
GETCHAR():-
The function getchar() stands for “get character” and inputs a single character from a keyboard.
When called, the function waits for a key to be pressed. You can input a character either a char or int
type because getchar() function treats characters as integers and accept as ASCII value if a character
as variable of type int.
Therefore, to input a character, use either of these formats:
Int name;
name = getchar();
Or
char name;
name = getchar();
PUTS():
Definition:
The puts() function is an unformatted output function in C, used to display a string on the screen
followed automatically by a newline character.
Q15) What do you know about format specifiers? Write format specifiers with all data
types using C language.
ANS:
FORMAT SPECIFIERS
1: Those characters that are used to call the variable .
2: They start with (%)
3: There should be each variable for each format specifier used in printf functio.
4: They can be use in printf and scanf as well
5: %d,%f,%lf are some common examples .
CHAPTER # 7 & 8
Q21) What is meant by iteration? How many types of iterations used in C language?
ANS
Iteration is often referred to as a loop, because the program will keep repeating the activity
until the condition becomes false. Loops simplify programming because they enable the
programming to specify certain instruction only once to have the computer execute them many
times. C and C++ provides three types of iteration structures:
1. for loop
2. while loop
3. do-while loop
Q22) Why we used FOR loop? Write FOR loop syntax in C language.
ANS
THE FOR LOOP:-
The for loop is used to repeat a statement or a block of statements for a specified number of times. It
is also called pretest loop or definite loop .The for loop statement includes three parts 1 st part is the
initialization of the counter, 2nd part is the condition test , and the 3rd part is
increment or decrement in counter.
Example:
#include<stdio.h>
#include<conio.h>
int main ( )
{
int count;
for (count = 1; count <= 10; count++) //increment operator
printf (“%3d”, count);
getch ( );
}
.
Q26) What is the purpose of continue statement?
ANS
CONTINUE:
The continue statement does not terminate the loop, but it evaluate the loop. The continue
statement transfer the control at the beginning of the loop. In loop the continue statement transfer
the control to the next repetition. It is only used with if else statement.
CHAPTER # 9 & 10
Q27) Describe functions Also write main types of functions? Define each of them.
ANS:
Functions A function is a set of statements that performs a specific task. They make the
code reusable and flexible. Every C program has at least one function. There are two types of
functions. Advantages of using functions: They make the code easier to read and understand.
They allow us to break a program into smaller manageable parts. They can be used multiple
times, reducing redundancy. They make debugging easier.
1. Declaration/Prototype :
The function declaration tells the compiler about the function’s name, return
data type and parameters. The function is declared before the main() function.
Syntax : return_type function_name(data_type parameter1, data_type
parameter2, ...); Note: declaration is not needed if definition is before main()
2. Definition:
The function definition contains the actual body of the function. It includes the
statements to be executed when the function is called. Function can be
defined before or after main(). The definition has two parts; header and body.
Syntax: return_type function_name(parameters) { Statements; return value; }
3. Calling:
It is the process of execution of a function. When a function is called, the
program jumps to the function definition and executes the body. Finally it
returns to where the function was called. Syntax: function(arguments); If the
function returns a value, we can assign it to a variable. Syntax:
variable=function(arguments);
Q28) Write different built-in function and their purpose used in C language.
Hint: pow ( ) etc
ANS:
Function Purpose
pow(x, y) Returns x raised to the power y (x^y)
sqrt(x) Returns the square root of x
ceil(x) Rounds x up to the nearest integer
floor(x) Rounds x down to the nearest integer
fabs(x) Returns the absolute value of a floating-point number
fmod(x, y) Returns the remainder of x divided by y
sin(x) Returns the sine of angle x (in radians)
cos(x) Returns the cosine of angle x
tan(x) Returns the tangent of angle x
Local variables are variables that are declared within a function, block, or compound statement. They
are only accessible within that scope.
✅ Key Points:
Global variables are variables that are declared outside of all functions, usually at the top of a program.
These variables are accessible from any function in the same source file.
✅ Key Points:
A two-dimensional array is essentially an array of arrays, where each element itself is an array. It can be
visualized as a matrix or table, with rows and columns. A 2D array is often used to store data in a grid-
like structure, such as a chessboard or a table of values.