0% found this document useful (0 votes)
11 views29 pages

Cs Imp25

The document outlines the fundamentals of programming, specifically in C language, detailing the steps of problem-solving, types of translators, and various programming concepts such as errors, data types, and operators. It explains the structure of C programs, the role of Integrated Development Environments (IDEs), and the significance of identifiers and reserved words. Additionally, it covers the characteristics of C language, its advantages and disadvantages, and provides an overview of escape sequences and variable declarations.

Uploaded by

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

Cs Imp25

The document outlines the fundamentals of programming, specifically in C language, detailing the steps of problem-solving, types of translators, and various programming concepts such as errors, data types, and operators. It explains the structure of C programs, the role of Integrated Development Environments (IDEs), and the significance of identifiers and reserved words. Additionally, it covers the characteristics of C language, its advantages and disadvantages, and provides an overview of escape sequences and variable declarations.

Uploaded by

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

CHAPTER # 1 & 2

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.

Step 1: Defining the problem


It is impossible to develop a solution to a problem if you do not have a clear understanding of the
problem itself. Defining the problem requires that the programmer specify our mini tasks.
Determining Program Objectives:
Determining the Desired output:
Determining the Input Data:
Determining the Processing Requirements:

Step 2: Planning the Solution


Once the programmer has determined what the program must do, the next set is to plan a
solution, preferably using structured programming techniques consist of the following:
Top-Down Program Design:
Algorithm:
Flowchart:

Step 3: Coding the Program


The language used depends on the system resources available, the expertise of the
programmer, and there are two type of programming language.
Machine Language
High Level Language

Step 4: Testing the Program (Debugging)


After the program has been coded comes the stage to check whether the program works or not?
Testing a program to see if it does what it is supposed to do is usually the hardest, longest and
most frustrating stage in the program development process. Programming errors are known as
bugs and the process of detecting and correcting these errors is called debugging.

Step 5: Documenting the Program


Documentation is a written detailed description of the programming cycle and specific facts
about the program. Typical program documentation materials include the origin and nature of the
problem, a brief narrative description of the program, algorithm and flowcharts, data-recorded
description program, listings, and testing result. Comments in the program itself are also
considered an essential part of documentation.
1. User documentation.
2. Operator Documentation.
3. Programmers Documentation

Q) Define TRANSLATOR and its type


ANS
TRANSLATOR:
Translator are used to translate high level language into machine language because machine language
cannot understand high level language so every program written in high level language will be
converted into machine language through translator
There are three types of translator:
INTERPRETER
COMPILER
ASSEMBLER

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

Q2) Define errors and its types.


Ans:
ERRORS:
Computer error refers to a mistake or malfunction that occurs within a computer system, leading to
unexpected or incorrect behavior.

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

Q3) Explain C language and its characteristics.


Ans:

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.

Q2) What do you know about basic structure of C language


The Basic Structure of a C Program
Basically C is a structure programming language. All C programs consist of one or more functions.
Remember, a function is a group of one or more statements designed to accomplish a specific task.
Many functions that you will be using are already written and compiled for you in the function
library supplied with your compiler. Instead of writing the individual instructions yourself, you just
tell the compiler to use one of its standard function. Only when you want to perform a task that is not
in the library do you need to write your own function
We begin by considering the basic structure of a C program which may look like:
Q) Difference between source code and object code

Q) Advantage And Disadvantage of C Language:


ANS:
Advantages
1. C works well with microcomputers.
2. It has a high degree of portability-it can be run without change on a variety of computers.
3. It is fast and efficient.
4. It enables the programmer to manipulate individual bits in main memory.
Disadvantages
1. C is considered difficult to learn.
2. Because of its conscience, the code could be difficult to follow.
3. It is not suited to applications that require a lot of report formatting the data file manipulation.

Q4) Explain IDE and also write basic structure of C language.


Ans:
IDE: -
IDE stands for integrated development environment . IDE is the blank screen of turbo c++ and
dev C++ where user can write c or C++ program.
Turbo c++ IDE consist of menu bar, windows, status line and quick reference key.
Initially the name of program will be “ NONAME00.CPP” which is turbo c++’s default. . CPP is the
extension of C++ language program.

Q5) What is escape sequence? Enlist the escape sequence and write purpose of each escape
sequences.
Ans:

1: Those characters used to skip sequence are known as Escape sequence.


2: All escape sequence start with back slash ( \ )
3: For any escape sequence, there is no need to write variable in printf() function.
4: Escape sequence can only be used in printf() function.
5: \n ,\t,\a etc are some common examples of Escape sequence.

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.

Q: What are pre-processor directives? Explain two pre-processor directives used in C.


Ans:
C – PREPROCESSOR DIRECTIVES:
DEFINITION: the source code of c program is processed by a program called preprocessor that is
usually located before the main( ) function. These pre-processor directives usually begins with “#”
symbol.
1) MACRO PRE-PROCESSOR:
The macro defines constant value and can be any of the basic data types.
Its syntax is #define
e.g. #define PI = 3.14
2) HEADER FILE PRE-PROCESSOR:
This pre-processor directive is used to include specific file in the main program at the specified place.
Its syntax: #include <file-name>
e.g. #include<studio.h>

Q) Define constant and define its types.


ANS:

Q) Define Variables and define rules of its construction.


Variables
A variable is a programmer-defined identifier whose value can change during the execution of the
program. All variables must have a name and a data type. C supports most of the common data types
for variables---integers, float (real), and character. The data type should be chosen carefully because
it determines how the computer internally represents the associated data. If the situation calls for
integers data, such as the number of pages in a book or the number of characters in a word, use the
int data type.
Rules for constructing Variable Names
1. The first character in the variable name must be an alphabet.
2. No commas or blanks are allowed within a variable name.
3. No special symbol other than an underscore (as in tax_rate) can be used in a variable name.

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;

Q7) Why C language called case sensitive language?


Ans:
C is considered a case-sensitive programming language because it treats uppercase and lowercase
letters as different characters in identifiers such as variable names, function names, and keywords.
🔹 Definition:
In a case-sensitive language, the identifiers sum, Sum, and SUM are all treated as distinct and
independent.
🔹 Impact in C:

 Variable value and Value refer to two different memory locations.


 Keywords like int, return, and main must be written in lowercase only. Writing Int or MAIN
will result in a compilation error.
 Function names, macros, and structure names also follow this rule.

🔹 Reason Behind It:


This design makes the language more flexible and expressive, as it allows more combinations of
names. However, it also demands more careful attention from the programmer.

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];

Q10) How can we declare the following data type?


a) int b) char c) float d) double

a) int (Integer)

 Used to store whole numbers (positive or negative).


 Declaration:

int number;

b) char (Character)

 Used to store a single character.


 Declaration:

char letter;

c) float (Floating-point number)

 Used to store decimal (fractional) numbers with single precision.


 Declaration:

float temperature;

d) double (Double-precision floating point)

 Used to store decimal numbers with higher precision than float.


 Declaration:

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.

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 by
arithmetic operators. Following list provide the detail about arithmetic operators.

OPERATORS OPERATIONS EXAMPLE

+ ADDITON A+B
- SUBTRACTION A-B
* MULTIPLY A*B
/ DIVIDE A/B
% MODULUS/REMAINDER A%B

Assignment Operator (=)

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

+= Add-assign A+=B same as a = a + b


-= Subtract-assign A-=B same as a = a – b
*= Multiply-assign A*=B same as a = a * b
/= Divide-assign A/=B same as a = a / b
%= Remainder-assign A%=B same as a = a% b

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:

Increment And Decrement Operator


The increment operator ++ and decrement operator - - are very useful operators and are not found in
other languages. Is a variable a is incremented by 1, the increment operator ++ can be used rather
than the expression a = a + 1 or a += 1. The increment operator causes its operand to be increased by
1, whereas the decrement operator causes its operand to be decreased by 1.
These are ‘unary’ operators since they operate on only one operand. The operand has to be a variable
and not a constant. Thus, the expression a ++ is valid whereas 6++ is invalid.
The increment or decrement operators can be of two types, these are:
1. The increment or decrement the value of the variable and then take this new value for processing
(prefix).
2. First take the value of the variable for processing and then only increment or decrement the
variable (postfix).

OPERTOR OPERATION EXAMPLE


< less than A<B
> greater than A>B
<= less than or equal to A<=B
>= greater than and A>=B
equal to
== equal to A==B
!= not equal to A!=B

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).

OPERTOR OPERATION EXAMPLE EXPLAINATION

== Equal to 5==5 Compares values


!= Not equal to 4!=5 Checks inequality of values

=== Strict equal to 5===5 Compares both value and


type
!== Strict not equal to 5!==5 True if value or type differs
Q: CONDITIONAL OPERATOR: -
Conditional operator is also called a decision-making operator. It has the distinction of being the only
ternary operator in c (works on three operand).
The conditional operator has the form:
Condition: Expression 1: expression 2
The conditional operator consist of both the question mark and a colon (:). Condition is a logical
expression that evaluate to either true or false, while expression 1 and expression 2 are either value or
expressions that evaluate to values.
EXAMPLE: max (num1>num2)? num1: num2

Q12) Write formatted and unformatted I/ O functions with their data type..
ANS:
UNFORMATTED I/O FUNCTIONS

SINGLE CHARACTER INPUT:-


There are some functions in C that handle the characters only. These functions are useful in some
applications when the user is prompted to answer yes/no, or ask you to select from a menu of ]items,
such as the number of the choice or the first letter of the choice name.
Three most common input functions are : the original getchar(), function which was introduced by
Ritchie Kernighan, and the new functions getch() and getche () which were added later to suit the
microcomputer interactive environment. These three functions are used without arguments, but
you must keep the empty parentheses.

FORMATTED I/O FUNCTIONS:-


The scanf() and printf() fall under the category of formatted input and output function respectively.
These functions instruct the compiler how to interpret the various values of input data and obtain the
output in
the specified from. Let us discuss these one by one.
THE SCANF() FUNCTION:-
The scanf() is the most versatile function because it can handle all of the different variables and
control their formatting. Think of scanf() as meaning SCAN formatted characters from the keyboard.
The function scans (monitors) the keyboard for keystrokes to be entered, then interprets the input
based on the format specifiers. This function can be used to enter any combination of numeric,
characters, and string variables all the same time.

THE PRINFT() FUNCTION:-


The prinft () is a more versatile function because it can display all data types and can work with
multiple arguments. This function can be done output any combination of numerical values, single
character and string. In addition, prinft () can format how the data appears. It is similar to the input,
function scanf (), except that its purpose is to display data rather than to enter is into the computer.
Q13) Define the following unformatted functions with their syntax.
a) gets ( ) b) getch ( ) c) getche ( ) d) getchar ( )
e) puts ( ) f) putch ( ) g) putchar ( )

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.

THE PUTCHAR() AND PUTCH() FUNCTIONS:-


The function putchar () stands for “put character” and displays only a single char value on the
monitor. The char can be a character variable or the character itself contained in single quotes. This
function is complement of input function getchar ().
The argument (the information
Q14) Explain formatted functions and also write their syntax using in C language.
ANS:
FORMATTED I/O FUNCTIONS:-
The scanf() and printf() fall under the category of formatted input and output function respectively.
These functions instruct the compiler how to interpret the various values of input data and obtain the
output in
the specified from.

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 .

%d Inputs an integer number


%u Inputs an unsigned integer
%f Inputs a float
%e Inputs number in scientific notation (float)
%c Inputs a character
%s Inputs a string
%o Inputs an octal number (unsigned)
%x Inputs a hexadecimal number (unsigned)

CHAPTER # 7 & 8

Q16) What do you know about selection control statement?


ANS:
CONTROL STRUCTURE:
In a program, control structure controls the flow of execution in a program or function. Structured
programming uses a limited number of control structures to minimize the complexity of programs and
thus cut down on errors. These are three basic control structures in structured programming.
1. Sequence, or sequential flow
2. Selection, or conditional flow
3. Iteration, or repetitive flow
Q17) Explain if selection structure. Write syntax for If selection using in C language.
ANS
THE IF SELECTION STRUCTURE:-
The if statement tests a particular condition. Whenever that condition evaluates as true, an action is
performed but if it is not true, then the action is skipped and control transfers to the structure of the
command is:
If (condition)
{
Statement;
}
The command says “if this condition is true, then perform the following statement”(see fig. 7.2). The
computer performs the statement, then continues normally, performing the statements that follow the
if command.
If the condition is false, however, the computer skips the statement that is part of the if command, and
moves on to the next statement in the program. Writing if command is easy as long as you remember
these basic points.
1. Place the condition in parentheses.
2. There is no semicolon after the condition it appears after the statement.
3. While we paraphrase the command by saying “If condition then do statement,” there is no then
command in C.
4. C is a free-from language, so the condition and statement can do on the same line. Separating and
indenting the condition, however, make a program easier to read.

Q18) Define nested if statement also write its syntax.


ANS
NESTED IF STATEMENT:-
An if condition can be written as deeply as you need within the body of another if statement. This is
called nesting of if’s. For example an action is performed if two conditions are true. The condition 1 is
tested and then condition 2 is tested.
If (condition 1)
If (condition 2)k
Statement;

Q19) Define if-else selection structure


ANS
The if-else statement is used to perform two operations for a single condition.
The if-else statement is an extension to the if statement using which, we can
perform two different operations, i.e., one is for the correctness of that
condition, and the other is for the incorrectness of the condition. Here, we
must notice that if and else block cannot be executed simultaneously. Using if-
else statement is always preferable since it always invokes an otherwise case
with every if condition. The syntax of the if-else statement is given below.
if(expression)
{
statement
}
else{
statement
}

Q20) Why we used switch statement?


ANS
THE SWITCH STATEMENT:-
The if- else statement is good for choosing among few (two to three) alternatives. It quickly becomes
confusing when several alternatives are needed. An alternatives is the switch statement. The switch
statement is used to select one of the several alternatives, such as menu options. The switch statement
is especially useful when the selection expression (called the controlling variable)or of a simple
expression (called the controlling expression).The value of this variable may be of type int or char,
but not of type float. The switch statement also differs from if, in that switch can only test for
equality, where the if conditional expression can be of any type. The general form of the switch
statement is
Syntax :
switch (variable)
{
Case constant 1;
Statement (s);
Break;
Default: Statement(s); }

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 ( );
}

Q23) Define NESTED IF LOOP,NESTED WHILE LOOP, NESTED DO WHILE LOOP


Ans
NESTED FOR LOOP:-
When one for command is performed within another, they are said to be nested. The inside
loop is completely repeated for each repetition of the outside loop. Picture nested for loops as
being two-dimensional and single for loops as one-dimensional.
NESTED WHILE LOOPS:-
The while loop can be nested just like for loop. The following program shows a while loop nested
within another while loop to read a number and print its factorial

NESTED DO-WHILE LOOPS:-


You can nest do-while loops to provide two levels of repetition. You can use an outer loop to repeat
the entire program until the user decides otherwise, and inner loops to take a series of inputs, The
inner loop is used to calculate average of a list of numbers. The outer loop is repeated as long as
this condition in true:

Q23) Define While loop. Write its syntax used in C language.


ANS

Q24) Explain do-while loop with its example.


ANS
THE DO-WHILE LOOP:-
One important characteristic of the while statement is that the test condition is at the top of the loop.
This means that if the condition is false the first time, the body of while loop will never be executed.
But there are certain situations where you need to execute the body at least once. These situations are
not common, but when they do occur, you should use the do-while statement.
The only difference between a do-while and while loop is that the test condition is at the bottom of
the loop. This means that the program always executes the statement(s) at least once , even if the
condition false for the first time itself. Then, depending on the value of test expression, it may loop
back to do or it may continue with the next statement.

Q25) Why we used break statement? Write its syntax..


ANS
BREAK:
The break statement immediately terminates the loop. It transfers the control to the next statement
following the loop. If it is used in inner loop, the control will be transfer to outer loop. The break
statement can be used with if – else and switch case statement as well

.
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.

Q) DIFF B/W FOR, WHILE, DO-WHILE LOOP

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) System Defined/Predefined/Library functions


These are the functions that are provided by the programming language. They are stored in a header
file, which needs to be included in the program by using the #include preprocessor directive. Predefined
functions are not required to be declared and defined. They can be used by simply calling it. Examples :
printf(), sqrt(), pow(), abs() Syntax : function(parameters);

2) User defined functions A user defined function is created by the


programmer to perform specific tasks. The function can be given any name
other than the reserved words of the language. It does not require a header
file.
Example : multiply() , sum(), average() User defined function has three parts.

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

Q31) Define local and global variable.


ANS:
Local Variables – Theory
✅ Definition:

Local variables are variables that are declared within a function, block, or compound statement. They
are only accessible within that scope.

✅ Key Points:

 Scope: Limited to the block or function where declared.


 Lifetime: Exists only during the execution of that block or function.
 Memory Allocation: Typically stored in the stack.
 Initialization: Not automatically initialized; may contain garbage values if not explicitly
initialized.
 Accessibility: Cannot be used or referenced outside the function or block where they are
declared.
 Use Case: Used when data is temporary and only needed within a specific function.

🔹 Global Variables – Theory


✅ Definition:

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:

 Scope: Extends throughout the entire program.


 Lifetime: Exists for the duration of the program execution.
 Memory Allocation: Stored in the data segment of memory (static memory area).
 Initialization: Automatically initialized to zero if not explicitly given a value.
 Accessibility: Can be accessed and modified by any function in the same file, and even other
files using the extern keyword.
 Use Case: Useful when data needs to be shared across multiple functions.

Q32) Define ARRAY And How to declare ARRAY?


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];

Q33) Explain pointer in detail.


ANS:
Pointers: A pointer is a variable that stores the memory location of another
variable. It allows us to access and manipulate addresses of variables in the
program. It is denoted by * symbol. A pointer's data type must match the type of
the variable it points to.

Declaration Syntax: datatype *pointer_name;


Example: int *ptr;

Initialization Syntax: datatype *pointer_name = &variable;


Example: int num=10; int *ptr=&num;

Q34) How to use ARRAY and pointer?


ANS:
In C, arrays are collections of elements stored in contiguous memory, and their
name acts as a pointer to the first element. A pointer is a variable that holds the
memory address of another variable. You can use pointers to access or modify
array elements using pointer arithmetic like *(arr + i). While array names cannot
be reassigned, pointers can point to different memory locations. Pointers are
especially useful when passing arrays to functions or when using dynamic
memory allocation with functions like malloc(). This makes arrays and pointers
powerful tools for efficient data handling in C.

Q35) Explain one dimensional and two dimensional ARRAY.


ANS:

One-Dimensional (1D) Array

A one-dimensional array is a collection of elements of the same type, stored in contiguous


memory locations, and is accessed using a single index. It can be thought of as a list of values
where each element is indexed from 0 to n-1 (where n is the size of the array). For example, an
array of integers can store a list of numbers, and you access each number by its index.

Syntax: type array_name[size];


Two-Dimensional (2D) Array

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.

Syntax: type array_name[rows][columns];

You might also like