CS3251 Programming in C 2 Marks
CS3251 Programming in C 2 Marks
PART A
1
4. What are keywords? Give an example
Keywords are reserved words, they have standard and predefined meaning.
Keywords cannot be used as normal identifiers.
Example : auto, break, char, continue, else, if, switch, struct, union.
5. What do you mean by variables in ‘C’ ?
A variable is an identifier that is used to represent some specified type of information.
Syntax : data_type variable_name;
Example : int marks;
2
10. Differentiate between an expression and a statement in C.
OUTPUT :
Value of (a + b) * c / d is : 90
3
14. Discover the meaning of C pre-processor
1. The preprocessor contains any operations in the processing language, it will be
transformed first.
2. The preprocessing language consists of
Inclusion of header file
Macro expansion
Conditional compilation
Line control
Diagnostics
4
UNIT II ARRAYS AND STRINGS
Introduction to Arrays: Declaration, Initialization – One dimensional array – Example Program:
Computing Mean, Median and Mode - Two dimensional arrays – Example Program: Matrix
Operations (Addition, Scaling, Determinant and Transpose) - String operations: length, compare,
concatenate, copy – Selection sort, linear and binary search.
PART A
5
5. How to create a two dimensional array?
Two dimensional arrays are stored in a row-column matrix, where the left index
indicates the row and right matrix indicates the column.
: int mat[3][3];
Syntax : strrev(string_to_be_reversed);
9. Define string.
String is a sequence / array of characters enclosed with double quotes.
Null character (‘\0’) is used to mark the end of the string
C O M P U T E R \0
10. Name any two library functions used for string handling.
strlen() – finds the length of a string. It returns an integer value. It counts the number
of characters except null character and returns the count Syntax : strlen(str)
6
strcpy() – copies the source string into destination string. So, the source string
should be enough to store the destination string.
Syntax : strcpy(source,destination)
Declaration:
int m1[10][10];
static int m2[2][2] = { {0,1}, {2,3} };
Representation:
101 102 103 104 105 106 107 108 109 110
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
Length calculation:
Length of an array=upper_bound - lower_bound + 1 Here,
upper_bound = 9 and lower_bound = 0
Thus, length of an array = 9-0+1 = 10
7
15. What is the difference between an array and pointer?
iv. Size of(array name) gives the Sizeof(pointer name) returns the
number of bytes occupied by the number of bytes used to store the
array. pointer variable.
8
UNIT III FUNCTIONS AND POINTERS
Introduction to functions: Function prototype, function definition, function call, Built‐in functions
(string functions, math functions) Recursion – Example Program: Computation of Sine series,
Scientific calculator using built‐in functions, Binary Search using recursive functions – Pointers
– Pointer operators – Pointer arithmetic – Arrays and pointers – Array of pointers – Example
Program: Sorting of names – Parameter passing: Pass by value, Pass by reference –
ExampleProgram: Swapping of two numbers and changing the value of a variable using pass by
reference.
PART A
1. What is a function?
Function is a set of instructions
Self contained block
Performs a specific task
Used to avoid redundancy of code
2. What is the need for functions?
To reduce the complexity of large programs
To increase the readability
To achieve reusability
To avoid redundancy of code
To save Memory
3. What are the uses of pointer?
Saves Memory Space
Used for dynamic memory allocation
Faster execution
Used to pass array of values to a function as a single argument
4. Define typedef .
The typedef keyword enables the programmer to create a new data type name by
using an existing data type.
By using typedef, no new data is created, rather an alternate name is given to a
known data type.
9
Indirection operator : *- Dereferencing operator is used to access the value at
the pointer variable
Ex: int a=5; int
*p=&a;
printf(“%d”,*(p));
10
9. What is a function prototype?
Function prototype is a function declaration statement.
Syntax : return_type function_name( parameters_list) Example:
int factorial(int);
13. State the advantages of user defined functions over pre-defined function.
A user defined function allows the programmer to define the exact function of the
module as per requirement. This may not be the case with predefined function. It
may or may not serve the desired purpose completely.
A user defined function gives flexibility to the programmer to use optimal
programming instructions, which is not possible in predefined function.
11
14. Write the syntax for pointers to structure.
Struct S
{
char datatype1;
int datatype2;
float datatype3;
};
Struct S *sptr //sptr ia pointer to structure S
Example:
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
12
UNIT IV STRUCTURES
PART A
Arrays Structures
An array is a collection of data items of A structure is a collection of data items of different
same data type. Arrays can only be data types. Structures can be declared and defined.
declared.
An array cannot have bit fields. A structure may contain bit fields.
Structure Union
Every member has its own memory. All members use the same memory.
13
3. Define Structure in C
C Structure is a collection of different data types which are grouped together and each
element in a C structure is called member.
If you want to access structure members in C, structure variable should be declared.
Many structure variables can be declared for same structure and memory will be
allocated for each separately.
It is a best practice to initialize a structure to null while declaring, if we don’t assign any
values to structure members.
14
6. What is meant by Union in C?
A union is a special data type available in C that enables you to store different data
types in the same memory location. You can define a union with many members, but
only one member can contain a value at any given time.
Unions provide an efficient way of using the same memory location for multi-
purpose.
15
11. What is static storage class?
Static is the default storage class for global variables. The two variables below
(count
and road) both have a static storage class. static
int Count; int Road;
{
printf("%d\n", Road);
}
16
UNIT V FILE PROCESSING
Files – Types of file processing: Sequential access, Random access – Sequential access file
‐ Example Program: Finding average of numbers stored in sequential access file ‐
Random access file ‐ Example Program: Transaction processing using random access files
– Command line arguments
PART A
17
Working with files
When working with files, you need to declare a pointer of type file. This declaration is
needed for communication between the file and program. FILE *fptr;
4. How to open a file?
Opening a file is performed using the library function in the "stdio.h"
header file: fopen().
The syntax for opening a file in standard I/O is: ptr =
fopen("fileopen","mode")
18
Often this data is gathered as it occurs into a transaction file. Periodically (often
overnight) the data in the transaction file is used to update the main file of data.
The picture shows the main file and the transaction file as input to a program. The
output is a new, updated main file. The previous version of the file can be kept as
backup. Management information science is the field that studies how to organize
and manage the information of a corporation using computers and files. Usually the
business school of a university has a management information science department.
19
14. Write the functions for random access file processing.
1. fseek()
2. ftell()
3. rewind()
which are skipped backward (if negative) or forward( if positive) from the current
position.This is attached with L because this is a long integer.
Pointer position:
This sets the pointer position in the file.
0 Beginning 0
1 Current position
2 End of file
20
PART B IMPORTANT QUESTIONS
UNIT I
BASICS OF C PROGRAMMING
UNIT II
ARRAYS AND STRINGS
1. What is an array? Discuss how to initialize a one dimensional and two
dimensional arrays with suitable example?
2. Write a C program to search an element in a given array using linear and binary
search.
3. Write a C program for sorting an array of numbers using selection sort.
4. Write a C program to addition, subtract and multiply two matrices.
5. Write a C program to scaling transformations of a matrix.
6. Write a C program to determinant a matrix.
7. Write a C program to transpose a matrix.
8. Write a C program to find mean, median and mode.
9. Explain in detail about string and list the various string operations with example.
10. Write a C program to find out the length and reverse of the string without using
builtin function
21
UNIT III
1. What is a structure? Create a structure with data members of various types and declare
two structure variables. Write a program to read data into these and print the same.
Justify the need for structured data type.
2. Write a C program to store the employee information using structure and search a
particular employee using Employee number.
3. Define and declare a nested structure to store date, which including day, month and
year.
4. Define a structure called student that would contain name, regno and marks of five
subjects and percentage. Write a C program to read the details of name, regno and marks
of five subjects for 30 students and calculate the percentage and display the name, regno,
marks of 30 subjects and percentage of each student.
5. Explain about array of structures and pointers in structures with example program
6. Write a C program to create mark sheet for students using self referential structure.
7. Discuss about dynamic memory allocation with suitable example C program.
8. Explain about singly linked list with suitable example C program.
22
UNIT V
FILE PROCESSING
23