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

Bca 06

Uploaded by

knowledgesystem7
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 views7 pages

Bca 06

Uploaded by

knowledgesystem7
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/ 7

BCA (First Year) Assignment July 2022 - January 2023

Internal Assignment- July 2022 - January 2023


Paper Code – BCA-06 (Programming in C)
Section-A for more pdf 7976271752
(Very Short Answer Type Questions)
Q.A.1. Define syntax error.
a syntax error is an error in the syntax of a sequence of characters or tokens that is intended
to be written in a particular programming language. For compiled languages, syntax errors
are detected at compile-time. A program will not compile until all syntax errors are corrected.
Q.A.2. Define implicit type casting with example.
An example of implicit type casting in Java is when you assign an integer value to a variable
of a larger data type, like assigning an int to a double. This kind of type casting or type
conversion also includes conversion from int to float or long.
Q.A.3. Why storage classes are used in C?
Storage class in the C language defines the location, lifetime and other specific attributes of a
variable. Knowledge of the storage class is important in accessing a block of code. A basic
understanding of storage classes in the C language can help you answer questions
confidently during interviews .
Q.A.4 How a program is compiled and executed?
A compiler is an executable program that takes program source code (text) as input and
translates it into an executable program (binary machine code) that it writes into a file as
output. That executable program can then be run to process input data and generate output
according to whatever we wrote our program to do.
Q.A.5. What is the use of library functions?
Library functions simplify programmers' tasks by providing ready and tested routines that
can be called and used in our programs.
Q.A.6. What are Escape Sequence characters in C? Explain with example.
n C, all escape sequences consist of two or more characters, the first of which is the
backslash, \ (called the "Escape character"); the remaining characters determine the
interpretation of the escape sequence. For example, \n is an escape sequence that denotes a
newline character.
Section-B
(Short Answer Questions)
Note: Answer any four questions. Each answer should not exceed 200 words.
Each question carries three marks. 4 x 3 = 12
Q.B.1. Differentiate between array and structure.

Comparing Structures and Arrays in C


Parameter Structure in C Array in C
Definition A Structure is a data structure that can An Array is a data structure that can
contain variables of different data only contain variables of the same data
types. type.
Memory Structures do not require the data to be Arrays store data in contiguous memory
Allocation stored in consecutive memory locations, meaning that memory blocks
locations. are assigned consecutively.
Accessibility To access elements in a Structure, the In Arrays, elements can be accessed by
name of the specific element is their index.
required.
Pointer Structures do not use internal Pointers. Arrays use internal Pointers that point to
the first element in the array.
Instantiation An object can be created from a Arrays do not allow object creation after
Structure even after its declaration in their declaration.
the program.
Data Type Structures can include input variables Arrays can only include input variables
Variables of multiple data types. of the same data type.
Performance Structures can be slower to search and Arrays can be faster to search and
access due to the presence of multiple access due to the absence of multiple
data types. data types.
Syntax struct structure_name{ data_type array_name[size]

element type 1;

element type 2;

} variable1, variable2, . .;
Operators The operator to access elements in a The operator to declare and access
Structure is the dot operator “ . “ elements in an Array is the square
bracket [ ]
Size Elements in a Structure can be of Elements in an Array are always of the
different sizes. same size.
Keyword The keyword “struct” is used to define Arrays do not require a keyword for
a Structure. declaration.
User-defined A Structure is a user-defined data type. An Array is not user-defined and can be
declared directly.

Q.B.2 Explain about the basic data type in C language with example.

Primary Data Types in C


Here are the five primitive or primary data types that one can find in C programming
language:
1. Integer – We use these for storing various whole numbers, such as 5, 8, 67, 2390, etc.
2. Character – It refers to all ASCII character sets as well as the single alphabets, such as
‘x’, ‘Y’, etc.
3. Double – These include all large types of numeric values that do not come under either
floating-point data type or integer data type. Visit Double Data Type in C to know more.
4. Floating-point – These refer to all the real number values or decimal points, such as 40.1,
820.673, 5.9, etc.
5. Void – This term refers to no values at all. We mostly use this data type when defining the
functions in a program.
Q.B.3. What is the use of pointer? Define preprocessor directive.
Preprocessor directives such as "#define" allow the programmer to create constants and
macros for goals. #define statements are matched with their corresponding value, allowing
the compiler to replace any successive references to the #define statement with its value.
#define is used to declare a constant value or expression with a CNAME that can be used
throughout the program. #include is used to include the content of a header file in our C
program.
Q.B.4. Explain the following:
a)Enum
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names
to integral constants, the names make a program easy to read and maintain.
enum State {Working = 1, Failed = 0};
The keyword ‘enum’ is used to declare new enumeration types in C and C++. Following is
an example of enum declaration.
// The name of enumeration is "flag" and the constant
// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and
// so on.
enum flag{constant1, constant2, constant3, ....... };

b)Nested Structure
A nested structure in C is a structure within structure. One structure can be declared
inside another structure in the same way structure members are declared inside a structure.
Syntax:
struct name_1
{
member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
The member of a nested structure can be accessed using the following syntax:
Variable name of Outer_Structure.Variable name of Nested_Structure.data member to
access

Q.B.5. What is an algorithm? Write an algorithm to find largest number among three
numbers. Also draw flow chart for the same.
Section-C
(Long Answer Questions)
Note: Answer any two questions. You have to delimit your answer maximum up
to 500 words. Each question carries six marks. 2 x 6 = 12
Q.C.1. Differentiate between.
(a) Scanf( ) and gets( )?
The difference can be shown in tabular form as follows:
scanf() gets()

when scanf() is used to read string when gets() is used to read input it stops reading input
input it stops reading when it when it encounters newline or End Of File. It does not
encounters whitespace, newline or stop reading the input on encountering whitespace as it
End Of File considers whitespace as a string.

It is used to read input of any


It is used only for string input.
datatype

Its syntax is -: Its syntax is -:


scanf(const char *format, …) char *gets(char *str)

It takes one parameter that is the pointer to an array of


It is fast to take input.
chars

Format specifiers is used inside


Its return value is str on success else NULL.
scanf to take input.
(b)Formatted and unformatted I/O function?
Formatted I/O in C
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a
certain format. Printf() and scanf() are two examples of C routines that handle formatted I/O.
The type and format of the data to be read or written are specified by format strings, which
are used by these operations. The program's execution replaces the placeholders for the data
found in the format strings with the actual data.

Syntax:
Formatted output syntax for the printf() function:

printf(format string, argument list);


Here, the argument list comprises the variables or values to be printed, and the format string
determines the output format.

Backward Skip 10s

Play Video

Forward Skip 10s

Formatted input syntax for the scanf() function:

Here, the argument list comprises the variables that will receive the input, and the format
string describes the format of the input.
Unformatted I/O in C
Unformatted I/O refers to a category of I/O operations that reads or writes data as a stream of
bytes without regard to any format. Unformatted I/O in C is carried out with the aid of
functions like fread() and fwrite(). Without formatting, these operations are used to read and
write data directly to and from files.

Syntax:
Syntax for using the fwrite() function to print unformatted data:

fwrite(data, size, count, file pointer);


Here, count is the number of elements to be written, size is the size of each element to be
written, and the file pointer is a pointer to the file where the data will be written.
Syntax for using the fread() method with unformatted input:
fread(data, size, count, file pointer);
In this syntax, a pointer to the buffer where the data will be read, the size of each element to
be read, the number of elements to be read, and a pointer to the file from which the data will
be read.
(c) Printf( ) and puts( )?
Q.C.2. Define macro called “birthday” which describes the day of the month upon which
your birthday was. Also explain it.
Q.C.3. Write C program to find out the sum of first five prime numbers. Draw a flowchart.

Q.C.4. Explain the following terms with suitable examples:


a)Malloc
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically allocate a
single large block of memory with the specified size. It returns a pointer of type void which
can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so that
it has initialized each block with the default garbage value initially.
Syntax of malloc() in C
ptr = (cast-type*) malloc(byte-size)
For Example:

ptr = (int*) malloc(100 * sizeof(int));


Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the
pointer ptr holds the address of the first byte in the allocated memory.
b)Calloc
C calloc() method
1. “calloc” or “contiguous allocation” method in C is used to dynamically allocate
the specified number of blocks of memory of the specified type. it is very much
similar to malloc() but has two different points and these are:
2. It initializes each block with a default value ‘0’.
3. It has two parameters or arguments as compare to malloc().
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each element.

For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of
the float.

c)Relloc
C realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change the memory
allocation of a previously allocated memory. In other words, if the memory previously
allocated with the help of malloc or calloc is insufficient, realloc can be used
to dynamically re-allocate memory. re-allocation of memory maintains the already
present value and new blocks will be initialized with the default garbage value.
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
d)Free
C free() method
“free” method in C is used to dynamically de-allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-allocated on their own. Hence the free()
method is used, whenever the dynamic memory allocation takes place. It helps to reduce
wastage of memory by freeing it.
Syntax of free() in C
free(ptr);
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);

return 0;
}

You might also like