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

PC Ia-Ii 2 Marks

The document covers fundamental concepts in C programming, including pointers, function parameter handling, structures, unions, dynamic memory allocation, file operations, and graphics programming. It explains key definitions, syntax, and differences between various programming constructs, such as pass by value and pass by reference, as well as the use of functions like malloc(), calloc(), and file handling functions. Additionally, it provides code snippets for practical implementation of file operations and graphics functions.

Uploaded by

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

PC Ia-Ii 2 Marks

The document covers fundamental concepts in C programming, including pointers, function parameter handling, structures, unions, dynamic memory allocation, file operations, and graphics programming. It explains key definitions, syntax, and differences between various programming constructs, such as pass by value and pass by reference, as well as the use of functions like malloc(), calloc(), and file handling functions. Additionally, it provides code snippets for practical implementation of file operations and graphics functions.

Uploaded by

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

Programming for problem solving using

C
2 Marks
UNIT-3

1.What is a pointer in C?
Pointers are nothing but memory addresses. A pointer is a variable that
stores the memory address of another variable.
2.Define pass by value.
The pass by value method is a technique used to pass arguments to a
function. When an argument is passed by value, a copy of the original value
is created and passed to the function. The function receives a copy of the
original value, and any changes made to the value within the function do not
affect the original value.
3.How do pass by value and pass by reference differ in function
parameter handling?
In pass by value, a copy of the original value is created and passed to the
function. The function receives a copy of the original value, and any changes
made to the value within the function do not affect the original value.
In pass by reference, a reference to the original value is passed to the
function. The function receives a reference to the original value, and any
changes made to the value within the function affect the original value.
4.What is a null pointer?
Null pointer is a special pointer value that is known not to point
anywhere.This means null pointer does not point to any valid memory
addresses.
5.What are storage classes in C?
Storage classes defines the scope and lifetime of variables and/or functions
declared within a c program.

UNIT-4

1.Define Structures with its syntax.


A structure is a collection of variables under a single name.The variables
within a structure are of different data types and each has a name that is
used to select it from the structure.
2.How will you declare and initialize Structure variables?
A structure is declared using the keyword struct followed by the structure
name.
syntax:
struct struct_name
{
data_type var-name1;
data_type var-name2;
......
}
A structure can be initialized in the same way as other data types are
initialized.
syntax:
struct struct_name
{
data_type name1;
data_type name2;
....
}struct_var={constant1,constant2,..};
3.Define Union with its syntax.
A union is a collection of variables of different data types in which memory is
shared among these variables.
syntax:
union union-name
{
data_type var-name1;
data_type var-name2;
....
};
4.What is meant by Enumerated data type?
An enumerated data type is a user-defined data type in C that consists of a
set of named integer constants.It is used to assign names to integral
constants, which makes a program easier to read and maintain.
5.Define Dynamic Memory Allocation.
The process of allocating memory to the variables during the execution of
the program or at run time is known as dynamic memory allocation.
6.What are the common functions used for dynamic memory
allocation?
a) malloc()-memory allocation
b) calloc()-contiguous memory allocation
c) realloc()-redefined memory allocation
d) free()-release the memory allocation.
7.What are the different ways to access members of a structure in
C?
Dot Operator (.): The dot operator is used to access members of a structure
variable.
Arrow Operator (->): The arrow operator is used to access members of a
structure through a pointer to the structure.
8.What are the advantages of using structures compared to using
simple variables?
Data Management: Structures help manage complex data more efficiently.
Code Readability: Structures improve code readability by providing a clear
and logical way to represent complex data.
Modularity: Structures promote modularity by encapsulating related data and
functions.
9.Differentiate between Structure and Union.
Structure:
a) Each member of a structure has its own memory location.
b)You can access all members of a structure independently.
Union:
a)All members of a union share the same memory location.
b)You can access only one member of a union at a time.
10.Differentiate malloc( ) and calloc ( ) in terms of memory
initialization.
malloc():
a) malloc() allocates a block of memory of the specified size (in bytes) but
does not initialize the memory.
b) example,
int *ptr;
ptr = (int*) malloc(5 * sizeof(int));
calloc():
a) calloc() allocates memory for an array of num elements, each of the
specified size (in bytes), and initializes all bytes to zero.
b) example,
int *ptr;
ptr = (int*) calloc(5, sizeof(int));

UNIT-5

1.What is a file? List some operations on it.


A file is a collection of data stored on a secondary storage device like hard
disk.
Opening a File: The fopen() function is used to open a file in read, write, or
append mode.
Closing a File: The fclose() function is used to close a file that is currently
open.
Reading from a File: The fread() function is used to read data from a file.
Writing to a File: The fwrite() function is used to write data to a file.
2.What are the various file accessing methods?
Sequential Access:
In sequential access, data is read or written in a linear order, from the
beginning to the end of the file
Random access:
In random access, you can move the file pointer to any position within the
file and read or write data from that position.
3.How do we open a file? Write a sample code snippet to open the
file.
The fopen() function is used to open a file.
sample code:
#include <stdio.h>
void main()
{
FILE *file;
file = fopen("example.txt", "r");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
fclose(file);
}
4.Describe the role of the fprintf ( ) function with an example.
The fprintf() is used to write formatted output to stream.
The syntax of the fprintf() is:
int fprintf(FILE*stream,const char*format,...);
example:
#include <stdio.h>
void main()
{
FILE *file;
file = fopen("example.txt", "w");
if (file == NULL)
{
printf("File not found\n");
exit(1);
}
int age = 30;
char name[ ] = "abc";
fprintf(file, "Name: %s\n", name);
fprintf(file, "Age: %d\n", age);
fclose(file);
printf("Data written to file successfully.\n");
}
5.How does the fseek ( ) works?
The fseek() function in C is used to move the file pointer to a specific location
within a file. This allows you to read from or write to a particular position in
the file,enabling random access to the file's contents. The fseek() function is
defined in the stdio.h header file.
Syntax:
int fseek(FILE *stream, long int offset, int whence);
6.What is the difference between text and binary files?
Text file:
a) Stores data in plain text format
b) Each character in the file is represented by a single byte (8 bits) using the
ASCII character set.
c) Text files typically have a .txt extension.
Binary file:
a) Store data in binary format (0s and 1s)
b) Can store any type of data, including images, audio, video, and executable
programs
c) Binary files typically have a specific extension, such as .jpg, .mp3, .exe,
etc.
7.List the functions used to initialize and close the graphics mode in
C.
initgraph(): This function initializes the graphics system by loading the
graphics driver and setting the graphics mode.
closegraph(): This function closes the graphics mode and deallocates all
memory allocated by the graphics system.
8.What is graphics programming in C?
Graphics programming in C involves creating visual content, such as images,
shapes, and animations, using the C programming language.
9.How can one draw basic shapes in a C program?
To draw basic shapes in a C program, you can use the Borland Graphics
Interface (BGI) library, which provides functions for drawing various shapes.
10.Write a c program to draw a circle using graphics program in c.
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(100, 100, 50);
getch();
closegraph();
}

You might also like