Internal 1 QB
Internal 1 QB
PART A
2. Define interpreter.
• An interpreter is a software/program that reads one statement from the source code, translates it to the
machine code or virtual machine code, and then executes it right away.
• Interpreter is a program that translates source code written in high-level language to machine code.
1 if statement is used to test a specific if…else statement is also used to test a specific
condition. condition and else statement follows an if statement
2 if condition is true, the block of codes if condition is false, then if block is skipped and control
within the if statement is executed. moves to the else and the code of blocks within the else
statement is executed
3 Syntax: Syntax:
if (condition) if (condition)
{ {
//Code of blocks // Code to execute if condition is true
} }
else
{
// Code to execute if condition is false
}
6. Define Compiler.
• Compiler is a program that converts human readable code into machine readable code. This process
is called compilation.
• A Compiler is a program or set of programs that converts source code written in a high-level language
to low-level language (assembly language or machine language).
• For example GCC C, Turbo C, Quick C etc. are different compilers for C programming language.
7. What is Expression?
• An expression in programming refers to a combination of variables, constants, operators, and function
calls that evaluates to a value.
• Expressions are fundamental building blocks in programming languages, that allows to perform
calculations, manipulate data, and control the flow of programs.
Example: C=A+B here A, B, C is operand and =, + is operator
Internal test 1 – U23CS101 C Programming
2. The condition is checked before entering the The condition is checked before entering the
iterations. loop.
3 Syntax: Syntax:
array size is 25
Internal test 1 – U23CS101 C Programming
PART B
1. Documentation Section: The documentation section consists of a set of comment lines giving the name of
the program, the author and other details.
2. Link Section: The link section provides instructions to the compiler to link functions from the system
library such as using the #include directive.
3. Definition Section: The definition section defines all symbolic constants such as #define directive.
4. Global Declaration Section: There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global declaration section. This section also
declares all the user-defined functions.
5. main () function Section: Every C program must have one main function section. This section contains two
Internal test 1 – U23CS101 C Programming
parts; declaration part and executable part
i) Declaration part: The declaration part declares all the variables used in the executable part.
ii) Executable part: There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces. The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a semicolon.
6. Sub-program Section:
The subprogram section contains all the user defined functions that are used to perform a specific task.
1) KEYWORDS
• Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
• Thus, the keywords cannot be used as variable names.
• C language has reserved 32 words as keywords
• All the Keywords are written in lower-case letters. Since C is case-sensitive
int distance ;
int roll_mo ;
float area ;
Internal test 1 – U23CS101 C Programming
2) IDENTIFIERS
• Identifiers are the names given to or names of variables, union, function name, structure names
• Identifier must follow some rules. Here are the rules:
1. All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an
underscore.
2. They must not begin with a digit
3. After the first character, an identifier can have any combination of characters.
4. C keywords cannot be used as an identifier.
5. Identifiers in C are case sensitive, foo and Foo are two different identifiers.
Example:
3) CONSTANTS OR LITERALS
• C constants refer to the variables that do not change their value during the program execution.
• They are also called as literals. Constants may belongs to any of the data type.
Syntax:
const data_type variable_name;
Example: int a=10;
Here a is variable, 10 is Constant
Internal test 1 – U23CS101 C Programming
4) STRING
❖ In C, strings are nothing but an array(group) of characters ended with a null character (‘\0’).
❖ This null character indicates the end of the string. Strings are always enclosed in double quotes.
❖ Whereas, a character is enclosed in single quotes in C and C++.
❖ Declarations for String:
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = "atnyla";
char string [] = "atnyla";
5. SPECIAL SYMBOLS
The following Special symbols are used in C having some special meaning and it cannot be used for some
other purpose
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
6.OPERATORS:
❖ An operator is a symbol that operates on a value or a variable.
❖ For example: + is an operator to perform addition. C has a wide range of operators to perform
various operations.
❖ An operator is a symbol that takes one or more arguments and operates on them to produce a result.
Internal test 1 – U23CS101 C Programming
13. Explain the working of the while and do-while loops with examples. Compare their
flow of execution.
S.No while loop do-while loop
1 While the loop is an entry control loop The do-while loop is an exit control loop because in
because firstly, the condition is checked, this, first of all, the body of the loop is executed then
then the loop's body is executed. the condition is checked true or false.
2 If condition not true, statement of while loop Statement of the do-while loop must be executed at
may not be executed at all. least once.
3 while loop terminates when the condition As long as the condition is true, the compiler keeps
becomes false. executing the loop in the do-while loop.
4 In a while loop, the test condition variable In a do-while loop, the variable of test condition
must be initialized first to check the test Initialized in the loop also.
condition in the loop.
5 In a while loop, at the end of the condition,
In this, at the end of the condition, there is a
there is no semicolon. semicolon.
Syntax: Syntax:
while (condition) while (condition);
6 While loop is not used for creating menu- It is mostly used for creating menu-driven programs
driven programs. because at least one time; the loop is executed
whether the condition is true or false.
7 In a while loop, the number of executions In a do-while loop, irrespective of the condition
depends on the condition defined in the mentioned, a minimum of 1 execution occurs.
while block.
8 Syntax of while loop: Syntax of do-while loop:
while (condition) do
{ {
Block of statements; statements;
} }while(condition);
Statement X; Statement X;
Internal test 1 – U23CS101 C Programming
Program of while loop: Program of while loop:
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i=1; int i=1;
while(i<=3) do
{ {
printf("hello"); printf("hello");
i = i + 1; i = i + 1;
} }while(i<=3);
} }
OUTPUT: OUTPUT:
hellohellohello hellohellohello
14. Write a C program Program to take 5 values from the user and store
them in an array and Program to find the average of n numbers using
arrays
#include<stdio.h>
void main()
{
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array OUTPUT:
for(int i = 0; i < 5; ++i) Enter 5 integers: 11
{ 22
scanf("%d", &values[i]); 33
} 44
printf("Displaying integers: "); 55
// printing elements of an array Displaying integers: 11
for(int i = 0; i < 5; ++i) 22
{ 33
printf("%d\n", values[i]); 44
} 55
}
#include <stdio.h>
void main()
{
int marks[10], i, n, sum = 0;
int average;
printf("Enter number of elements: ");
scanf("%d", &n);
Internal test 1 – U23CS101 C Programming
for(i=0; i < n; ++i)
{
printf("Enter number %d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i]; /*sum=sum+mark[i]*/
}
average = sum / n;
printf("Average = %d", average);
}
OUTPUT:
Enter number of elements: 3
Enter number 1: 66
Enter number 2: 88
Enter number 3: 99
Average = 84
16.Draw a flowchart for a program that finds the largest of three numbers. Explain the
steps involved.
• Flow chart is defined as graphical representation of the logic for problem solving.
• The purpose of flowchart is making the logic of the program clear in a visual representation.
Internal test 1 – U23CS101 C Programming
1. Declaring an Array
To declare an array, specify the type of its elements, followed by the array name and the size in square
brackets.
Syntax:
type arrayName[size];
Example:
int numbers[5]; // Declares an array of 5 integers
2. Initializing an Array
An array can be initialized at the time of declaration. If not initialized, the values will be undefined (unless
it's static or global, which will be initialized to zero).
Syntax:
type arrayName[size] = {value1, value2, ..., valueN};
Example:
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes the array with values
/*Partial initialization is possible; remaining elements will be set to zero:*/
int numbers[5] = {1, 2}; // numbers[2], numbers[3], and numbers[4] will be 0
Syntax:
arrayName[index]
Example:
#include <stdio.h>
void main()
{
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing elements
printf("First element: %d\n", numbers[0]); // Output: 1
printf("Second element: %d\n", numbers[1]); // Output: 2
}
Output:
First element: 1
Second element: 2
Internal test 1 – U23CS101 C Programming
18. Write a C program to demonstrate the use of the break and continue statements in
loops. Explain how control is transferred in each case.
Break Statement
Usage: The break statement is used to exit a loop immediately. When the break statement is encountered,
control is transferred out of the loop entirely.
Example:
#include <stdio.h>
void main()
{
printf("Using break statement:\n");
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("\nloop break", i);
break; // Exit the loop when i is 5
}
printf("%d ", i); // This will print numbers from 1 to 4
}
OUTPUT:
Using break statement:
1234
loop break
In the Example:
• The loop iterates from 1 to 10. When i equals 5, the break statement is executed.
• This causes the loop to terminate, and control transfers to the first statement after the loop.
• As a result, only numbers 1 through 4 are printed, and the message "Loop break" is displayed.
Continue:
Usage: The continue statement skips the rest of the code inside the current iteration of the loop and proceeds
to the next iteration.
Example:
#include <stdio.h>
void main()
{
printf("\n\nUsing continue statement:\n");
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
printf("\nskipped loop no: %d\n", i);
continue; // Skip the current iteration for even numbers
}
printf("%d ", i); // This will print odd numbers from 1 to 10
}
Internal test 1 – U23CS101 C Programming
}
OUTPUT:
Using continue statement:
1234
skipped loop no: 5
6 7 8 9 10
In the Example:
• The loop iterates from 1 to 10. When i is five (i.e., i == 5), the continue statement is executed.
• This causes the loop to skip the printf statement that follows the continue, moving directly to the next
iteration of the loop.
• As a result, only numbers (1, 2, 3, 4) are printed, when i=5 loop is skipped and 6, 7, 8, 9, 10 numbers are
displayed
Advantages of Algorithms:
It is easy to understand.
Algorithm is a step-wise representation of a solution to a given problem.
Internal test 1 – U23CS101 C Programming
In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to
convert it into an actual program.
Disadvantages of Algorithms:
Example
Syntax:
#include <header.h> // For standard library headers
#include "header.h" // For user-defined headers
Example:
#include <stdio.h> // Includes the standard input-output library
int main()
{
printf("Hello, World!\n");
return 0;
}
Internal test 1 – U23CS101 C Programming
In this example, #include <stdio.h> allows the program to use the printf function, which is defined in the
standard I/O library.
2. #define
The #define directive is used to create macros, which are essentially constants or expressions that can be
reused throughout the code. This helps improve code maintainability.
Syntax:
#define NAME value
Example:
#include <stdio.h>
#include<stdio.h>
#define PI 3.14 // Define a constant for PI
#define SQUARE(x) (x * x) // Define a macro for squaring a number
void main()
{
printf("Value of PI: %f \n", PI); // Uses the defined constant
printf("Square of 5: %d \n", SQUARE(5)); // Uses the defined macro
}
OUTPUT:
Value of PI: 3.140000
Square of 5: 25
In this example:
The c compilation process converts the source code taken as input into the object code or machine code.
The compilation process can be divided into four steps.
1.Pre-processing
2.Compiling
3.Assembling
Internal test 1 – U23CS101 C Programming
4.Linking
Preprocessor
• The preprocessor takes the source code as an input, and it removes all the comments from the source
code. The preprocessor takes the preprocessor directive and interprets it.
• For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets
the directive and replace this directive with the content of the 'stdio.h' file.
• The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor expands
this code. After expanding the code, the expanded code is passed to the compiler.
Compiler
• The code is expanded by the preprocessor is passed to the compiler.
• The compiler converts this code into assembly code or the C compiler converts the pre-processed code
into assembly code.
Assembler
• The assembly code is converted into object code by using an assembler. The name of the object file
generated by the assembler is the same as the source file.
• The name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.
Linker
• All the programs written in C use library functions. These library functions are pre-compiled, and the
object code of these library files is stored with '.lib' (or '.a') extension.
• The main working of the linker is to combine the object code of library files with the object code of
our program. It links the object code of these files to our program. So the job of the linker is to link
the object code of our program with the object code of the library files and other files.
• For example, if we are using printf() function in a program, then the linker adds its associated code in
an output file
Internal test 1 – U23CS101 C Programming
1 Mark
1 Which are the fundamental data types in C? CO1 L1
a) Char b) Int
c) Float d) All of the above
2 What is the correct variable to declare a variable in C? CO1 L1
a) int a; b) int 1a;
c) int $a; d) int @a b;
3 How do you insert COMMENTS in C code? CO1 L1
a) _ _This is comment b) # This is comment
c) //This is comment d) /This is comment
4 Which of the following is operator? CO1 L1
a) a b) c=a+b
7 Which stage of the compilation process involves replacing macros and CO3 L1
including header files?
a) Linking b) Assembly
c) Preprocessing d) Compilation
a) Assembly b) Linking
c) Preprocessing d) Interpretation
9 Which of the following is the correct syntax to declare an integer array of 10 CO4 L1
elements in C?
a) int arr;
b) int arr[10];
Internal test 1 – U23CS101 C Programming
c) int arr(10);
d) int[10] arr;
10 What is the index of the first element in a C array? CO4 L1
a) 0
b) 1
c) -1
a) 32 b) 33
c) 64 d) 18
a) scanf() b) printf()
c) getch() d) print()
a) Stops the program b) Skips the remaining part of the loop body and
continues with the next iteration