10marks
1.What are the various data types in C?Explain.
A data-type in C programming is a set of values and is determined to act
on those values. C provides various types of data-types which allow the
programmer to select the appropriate type for the variable to set its
value.
The data-type in a programming language is the collection of data with
values having fixed meaning as well as characteristics. Some of them
are an integer, floating point, character, etc. Usually, programming
languages specify the range values for given data-type.
C Data Types are used to:
Identify the type of a variable when it declared.
Identify the type of the return value of a function.
Identify the type of a parameter expected by a function.
ANSI C provides three types of data types:
1. Primary(Built-in) Data Types:
void, int, char, double and float.
2. Derived Data Types:
Array, References, and Pointers.
3. User Defined Data Types:
Structure, Union, and Enumeration.
Primary data types
4. Every C compiler supports five primary data types:
As the name suggests, it holds no value and is generalthe type of function or what it returns. If the
void
not return any value.
int Used to denote an integer type.
char Used to denote a character type.
float, double Used to denote a floating point type.
int *, float *, char Used to denote a pointer type.
*
Declaration of primary data types of names of value
After taking suitable variable names, they need to be assigned with a
data type. This is how the data types are used along with variables:
int age;
char letter;
float height, width;
Derived Data Types
C supports three derived data types:
Data Types
Arrays Arrays are sequences of data items having homogeneous values. They have adjacent memory
References Function pointers allow referencing functions with a particular signature.
Pointers These are powerful C features which are used to access the memory and deal with their addres
User defined data types
C allows the feature called type definition which allows programmers to define
their identifier that would represent an existing data type. There are three such
types:
Data Types Description
Structure It is a package of variables of different types under a single name. This i
efficiently. "struct" keyword is used to define a structure.
Union These allow storing various data types in the same memory location. Pro
union with different members, but only a single member can contain a va
used for
Enum Enumeration is a special data type that consists of integral constants, an
with a specific name. "enum" keyword is used to define the enumerated
2.what is a file ? explain the various file operations in c with examples
WHAT IS FILE?
File is a collection of bytes that is stored on secondary storage devices like disk. There are two kinds of
files in a system. They are,
1. Text files (ASCII)
2. Binary files
Text files contain ASCII codes of digits, alphabetic and symbols.
Binary file contains collection of bytes (0’s and 1’s). Binary files are compiled version of text
files.
BASIC FILE OPERATIONS IN C PROGRAMMING:
There are 4 basic operations that can be performed on any files in C programming language. They are,
1. Opening/Creating a file
2. Closing a file
3. Reading a file
4. Writing in a file
Let us see the syntax for each of the above operations in a table:
File operation Declaration & Description
fopen() – To Declaration: FILE *fopen (const char *filename, const char *mode)
open a file
fopen() function is used to open a file to perform operations such as reading,
writing etc. In a C program, we declare a file pointer and use fopen() as below.
fopen() function creates a new file if the mentioned file name does not exist.
FILE *fp;
fp=fopen (“filename”, ”‘mode”);
Where,
fp – file pointer to the data type “FILE”.
filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r,
w, a, r+, w+ and a+. Please refer below the description for these mode of
operations.
Declaration: int fclose(FILE *fp);
fclose() – To fclose() function closes the file that is being pointed by file pointer fp. In a C
close a file program, we close a file as below.
fclose (fp);
Declaration: char *fgets(char *string, int n, FILE *fp)
fgets function is used to read a file line by line. In a C program, we use fgets
function as below.
fgets (buffer, size, fp);
where,
buffer – buffer to put the data in.
fgets() – To size – size of the buffer
fp – file pointer
read a file
Declaration:
int fprintf(FILE *fp, const char *format, …);fprintf() function writes
string into a file pointed by fp. In a C program, we write string into a file as
below.
fprintf() – To fprintf (fp, “some data”); or
write into a file fprintf (fp, “text %d”, variable_name);
MODE OF OPERATIONS PERFORMED ON A FILE IN C LANGUAGE:
There are many modes in opening a file. Based on the mode of file, it can be opened for reading or writing
or appending the texts. They are listed below.
r – Opens a file in read mode and sets pointer to the first character in the file. It returns null if file
does not exist.
w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are
overwritten.
a – Opens a file in append mode. It returns null if file couldn’t be opened.
r+ – Opens a file for read and write mode and sets pointer to the first character in the file.
w+ – opens a file for read and write mode and sets pointer to the first character in the file.
a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it
can’t modify existing contents.