C Programming Book
C Programming Book
com
C
Programming
Notes
By: Prof. Prakash Sonar
prakashsonar7@yahoo.co.in
4/9/2015
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar)
Email: prakashsonar7@yahoo.co.in
Mobile:+91-88-578-69283/ +91-9890476237
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 2
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
1. Explain various Data Types in C
Language?
A C language programmer has to tell the
system the type of numbers or characters he
is using in his program. These are data
types. There are many data types in C
language. Data types are used to store
various types of data.
C has a concept of 'Data types' which are
used to define a variable before its use. C
supports various data types such
as character, integer and floating-point types.
All the data types are made up of units of
memory called bytes.
In order of size, starting with the smallest,
the integer types are char, short, int,
longetc. The smaller types take less memory,
the larger types takes more space in
memory,.
C has different data types for different types
of data and they are classified as:
I) Primary Data Type
Data Types
II) Secondary
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 3
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
of storage as an 'int' and the 'int'
should be less or the same bytes than
a 'long int'. shortint must be at least
16 bits long.
Declaration of short int
:shortintyellow;
_______________________________
c) Unsigned Integer:unsignedint has a
range of 0 - 65535. "int" can be
negative, but "unsigned int" cannot
be negative. %u Prints an unsigned
integer. The size can range from 2 to
8, or (again) whatever the
implementation provides.
Declaration of unsigned integer
:unsignedintx;ORunsignedchargrey;
___________________________
d) Signed Integer:%d Prints a signed
integer. Range of signed int is -32768
to 32767.
Declaration of signed integer
:signed inty; signed charwhite;
________________________________
2. Floating Point Types:
The float data type is used to store fractional
numbers (real numbers).
Floating point numbers are denoted by the
keywordfloat. The double is same as float
but it takes double space (8 bytes) than
float.
Syntax For Floating Point Types: float
<variable name>;
For Example:
float num1;
double num2;
long double num3;
Memory Occupied by Float:float (4
bytes),double (8 bytes),long double (10bytes)
____________________________________
3. CharacterTypes:
Character type variable can hold a single
character. As there are singed and
unsigned int (either short or long), in the
One DimensionalArray
Two Dimensional Array
Multi Dimensional Array
Declaration OfArrays :
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 4
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Syntax : type var_name[size];
____________________________________
2.Structures:We used variable in our C
program to store value but one variable can
store only single piece information (an integer
can hold only one integer value) and to store
similar type of values we had to declare many
variables.
We used array which can hold numbers of
similar data type. But array too have some
limitations, like in our real world application
we deal with set of dissimilar data types and
single array cannot store dissimilar data.
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 5
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
2) What will be the initial value of the
variable, if the initial value is not
specifically assigned (i.e, the default
initial value).
3) What is the scope of the variable, i.e.,
in which functions the value of the
variable would be available.
4) What is the life of the variables; i.e.,
how long would the variable exist.
There are four storage classes in C.
a) Automatic storage class
b) Register storage class
c) Static storage class
d) External storage class
4.life of the
variables
5.Keyword
Automati
c
memory
garbage
It is local
to the
block in
which it
has been
declared
life is till
the
control
remains
within the
block in
which the
variable is
defined
'auto'.
Register
Static
External
CPU
registers.
garbage
memory
memory
Zero
Zero
is it is
local to
the block
in which
the
variable
is
defined.
Its life is
till the
control
remains
in the
block in
which it
is
defined.
'register
'.
It is
local to
the
block in
which it
has
been
defined
value of
variable
persists
between
different
function
calls
scope of
the variable
is global
static
extern
It is
present as
long as the
program
execution
comes to
an end
3. Explain Operators in C?
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 6
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Different types of operators
Present in C are
1. Arithmetic operator
2. Relational Operators
3.Logical Operators
4.Assignment Operators
5.Increments and Decrement
Operators
6.Conditional Operators
7.Bitwise Operators
8. Special Operators
Arithmetic Operators:
Arithmetic operators means do the all
arithmetic operations like as addition,
subtraction, multiplication , division,
modules etc.
All the operators have almost the
same meaning as in other languages.
Both unary and binary operations are
available in C language. Unary
operations operate on a singe
operand, therefore the number 5 when
operated by unary will have the
value 5.
Arithmetic Operators:
Operator
Meaning
+
Addition or Unary Plus
sum = num1+num2;
storing in sum.
//addition and
//division storing in
Example:
#include<stdio.h>//include header file
stdio.h
void main() // the start of the program
{
int numb1, num2, sum, sub, mul, div,
mod;
//declaration of variables
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 7
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Then the integer operation leads to the
following results.
x+y=27
xy=15
2. Relational Operators
x*y=100
x%y=0
x/y=4
In integer division the fractional part is
truncated
<
arithmetic.
The floating point results can be truncated
according to the properties requirement.
The remainder operator is not applicable for
floating point arithmeticoperands.
Let x=14.0 and y=4.0
then
x+y=18.0
xy=10.0
x*y=56.0
x/y=3.50
Operator
Meaning
is less than
<=
isless than or equal to
>
is greater than
>=
is greater than or equal to
==
is equal to
!=
is not equal to
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 8
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Relational expressions are used in decision
making statements of C language such as if,
while and for statements to decide
the course of action of a running program.
3. Logical Operators
C has the following logical operators, they
compare or evaluate logical and relational
expressions.
Operator
&&
||
!
Meanings
Logical AND
Logical OR
Logical NOT
4. Assignment Operators
The Assignment Operator evaluates an
expression on the right of the expression and
substitutes it to the value or variable on the
left of the expression.
Example : x = a + b
Here the value of a + b is evaluated and
substituted to the variable x.
In addition, C has a set of shorthand
assignment operators of the form.
Varoper = exp;
Here var is a variable, exp is an expression
and oper is a C binary arithmetic operator.
The operator oper = is known as shorthand
assignment operator
Example :
x + = 1 is same as x = x + 1
The commonly used shorthand assignment
operators are as follows
Shorthand assignment operators
Statement with simple Statement with
assignment operator shorthand operator
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 9
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
a
a
a
a
a
=
=
=
=
=
a
a
a
a
a
+1
1
* (n+1)
/ (n+1)
%b
a += 1
a -= 1
a *= (n+1)
a /= (n+1)
a %= b
#define N 100
//creates a
variable N with constant value 100
#define A 2
//creates a
variable A with constant value 2
main()
program
//start of the
_______________________________
4. Explain Various File Related
Function
Or
Any Two Input Output Functions
For files
Different File Related Functions as Follows
fputc( ) , fgetc() , getc() , putc() , fputs(
),fgets()
fputc(): this function writes a character to a
specified file at the current file position and
then increment the file position pointer .The
putc() macro acts essentially identically to
fputc(), but is a macro that expands in-line.
It may evaluate stream more than once, so
arguments given to putc() should not be
expressions with potential side effects.
{
int a;
declaration
a = A;
to a
while (a < N)
of a is less than N
{
following
printf(%d \n,a);
current value of a
a *= a;
of a = a * a
//variable a
//assigns value 2
//while value
//evaluate or do the
//print the
//shorthand form
}
program
//end of the
Output :
2
4
16
Syntax:
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 10
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
fgetc() Function : fgetc() is a character
oriented function.this function reads the
single character from a given file and
increment the file pointer position. On success
it return the character after converting it. it to
an int without sign extension. on error it
return EOF .
Syntax: intfgetc(FILE *stream );
Return: If successful, it return the next
requested object from the stream. Character
values are returned as
putc(c,fp1);
intgetc (FILE *file) .
#include <stdio.h>
#include<stdio.h>
void main()
{
file *fp1;
char c;
fp1=fopen("myfile.txt","w")
while((c=getchar())!=EOF)
{
putc(c,fp1);
}
fclose(fp1);
fp1=fopen("myfile.txt","r")
while((c=getc(fp1))!=EOF)
{
printf("%c",c)
}
fclose(fp1);
}
void main()
{
int c; /* Character read from the file.
*/
FILE *ptr;/* Pointer to the file. FILE is a
structure defined in <stdio.h>*/
ptr = fopen("Ritu.txt","r");
/* Read one character at a time, checking
for the End of File. EOF is defined in
<stdio.h> as -1 */
while ((c = fgetc(ptr)) != EOF)
{
printf("%c",c);
/* O/P the character to the
screen */
}
fclose(ptr); /* Close the file.
}
getc( ) function and putc( ) function :
The getc( ) and putc( ) are exactly similar to
that of fgetc( ) and fputc( ).The putc function
writes the character contained in character
fputs( ) function :
*/
Declaration:
char * fputs(const char
*str , FILE *stream);
The fputs( ) function writes the content of the
string pointed to by str to the specified
stream. The null terminator is not written.
The fputs( ) function returns the last
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 11
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
character written on success and EOF on
failure.
/* program to understand the use of fputs( )
*/
#include<stdio.h>
void main()
{
FILE *fp1;
charstr[100];
fp1=fopen("rajesh.txt","w");
printf("enter the text\n")
if((gets(str)!=NULL))
fputs(str,fp1);
fclose(fp1);}
Declarations of fgets() Function: char
*fgets(char *str, int n, FILE *fptr);
Here str is the base address where the string
is to be stored, n is the number of characters
to be stored and fptr is a file pointer of type
FILE structure which specifies the name of the
file to which the input has to be read .The
fgets() function reads characters from the
current file pointer position upto and including
the first new line character \n, upto the end
of the stream, or until the number of
characters read is equal to (n-1), whichever
comes first. Now look at the second argument
of fgets() function more carefully. This second
argument is used to prevent fgets() from
reading a too long string and overflowing the
array.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *FP1;
char name[100];
fp1=fopen("rohit.txt","r");
printf("enter the data")
while(fgets(name,80,fp1)!=NULL)
puts(name);
fclose(fp1);
}
OUTPUT:master ofcomputer application
____________________________________
5. Dynamic Memory Allocation
Or
Explain malloc(),calloc(),reallc(),
And free() function in C
In dynamic memory management
memory will be allocate at runtime. This is
also known as heap memory. Some language
at the run time have ability to calculate and
assign the memory space at run time for an
array but c can't have this feature. But in C
there is some function which provide the
facility to allocate and deallocate the memory.
In ,Local Memory, when these function will
call memory will allocate automatically and
when function will exit memory will
deallocated automatically . In dynamic
memory allocation ,allocation of memory is
explicitly requested for a particular size of
block .For deallocation of memory explicitly
requested.
the function through which the dynamic
memory allocation and deallocation will
performed are :
1.
2.
3.
4.
malloc()
calloc()
free()
realloc()
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 12
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
memory block needed explicitly requested.
The malloc() function is same as a function is
request for RAM in the system memory. If
the request is grant then a void pointerreturn
and the pointer point start of that block. If
the request fail then a NULL pointer return.
Example:
void *calloc(size_tnumber,size_t size);
malloc( number of element * size of each
element);
int * ptr;
ptr = malloc(10*sizeof(int));
Where size represents the memory required
in bytes .The memory which is provided is
contiguous memory. But malloc function
return void pointer so it needed type casting
of that pointer.
Examlpe:
(type cast)malloc( number of element *
size of each element);
int * ptr;
ptr =(int*) malloc(10*sizeof(int));
similarly for allocation of memory to a
structure variable :
Examlpe:
(struct name)malloc( sizeof(struct
name));
struct employee
{
intemp_id;
charemp_name[20];
floatemp_contact_no;
};
struct employee *ptr
ptr=(struct employee*)malloc(sizeof(struct
employee));
2.calloc():In malloc requested memory is
provided a block of contiguous memory
.calloc() function is similar to the malloc
rather then calloc() function
allocated the memory block for an array of
elements. Memory for a group of objects used
free(ptr);
int main () {
intnumber,i;
printf("Enter the number ");
scanf("%d",&number);
printf("Number which is here
are",number);
for (i=0; i<number;i++) {
ptr[i] = i +i;
}
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 13
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
for (i=0; i<number;i++) {
printf("Result is
strcat(msg, buffer);
}
%d %d\n",i,ptr[i]);
free(ptr);}
4.realloc()
realloc() function is used for resize the size
of memory block which is allocated by the
malloc() and calloc () function.
Two situation where use realloc() function.
Void main {
do{
printf(hello world);
}
while (4<1); }
for example using do while loop
statement
#include <stdio.h>
void main()
{ int x = 5;
int i = 0;
// using do while loop statement
do{ i++;
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 14
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
printf("%d\n",i);
}while(i < x);
}
The program output
1
2
3
4
3. For loop:Thefor loop is used to implement
any type of loopcondition. It is based on
numeric variables.There are three parts which
is separated by semi-colons in control block of
the forloop.initialization_expression is
executed before execution of the loop starts.
The execution of the loop continues until the
loop_condition is false
Theincrement_expression, is usually used to
increment the loop counter. This is executed
at the end of each loop iteration.
for example of using for loop statement
to print an integer five times
intp,n,count;
float r, si;
for (count =3;count <=3;count=count ++ )
{
printf (enter the value of p,n,r);
scanf(%d%d%f,&p,&n,&r);
si =(p*n*r)/100;
printf(simple interest =%f,si);
}
getch();
}
nesting for loop: void main()
{
int r ,c,sum;
for(r =1;r<=3;r++)
{
for (c=1;c=2;c++)
{
sum =r+c;
printf(r=%d,c=%d,sum=%d\n,r,c,sum);
}
}
}
#include <stdio.h>
void main()
{
// using for loop statement
int max = 5;
int i = 0;
for(i = 0; i <max;i++){
printf("%d\n",i);
}
}
And the output is
1
2
3
4
5
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 15
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
immediately.
Void main()
{
int a;
for (a=1;a<=10;a++)
{
if (j==5)
Break;
else
printf(%d\n,a);
}
Printf(hello) // control pas here
getch();
}
operator
asm
equivalent
description
&
AND
Bitwise AND
OR
Bitwise Inclusive OR
XOR
Bitwise Exclusive OR
NOT
Unary complement
(bit inversion)
<<
SHL
Shift Left
>>
SHR
Shift Right
sizeof()
This operator will accept one parameter,
which can be a variable itself and returns the
size of it in bytes of that type or object.
9.ExpalinRECURSION
A function is called recursive if a statement
within the body of a function calls the same
function. Sometimes called circular
definition, recursion is thus the process of
defining something in terms of itself.
example of recursion-/*factorial of a number*/
main( )
{
int a, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &a ) ;
fact = rec ( a ) ;
printf ( "Factorial value = %d", fact ) ;
}
rec ( int x )
{
int f ;
if ( x == 1 )
return ( 1 ) ;
else
f = x * rec ( x - 1 ) ;
return ( f ) ;
}
And here is the output for four runs of the
program
Enter any number 1
Factorial value = 1
Enter any number 2
Factorial value = 2
Enter any number 3
Factorial value = 6
Enter any number 5
Factorial value = 120
a = sizeof (char);
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 16
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
is copied into x. Since x turns out to be 1 the
condition if ( x == 1 ) is satisfied and hence
1 (which indeed is the value of 1 factorial) is
returned through the return statement.
When the number entered through scanf( ) is
2, the ( x == 1 ) test
fails, so we reach the statement,
f = x * rec ( x - 1 ) ;
And here is where we meet recursion. How do
we handle the expression x * rec ( x - 1 )?
We multiply x by rec ( x - 1 ). Since the
current value of x is 2, it is same as saying
that we must calculate the value (2 * rec ( 1
)). We know that the value returned by rec (
1 ) is 1, so the expression reduces to (2 * 1),
or simply 2. Thus the statement,
x * rec ( x - 1 ) ;
evaluates to 2, which is stored in the variable
f, and is returned to main( ), where it is duly
printed as
Factorial value = 2
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 17
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
The macros that we have used so far are
called simple macros. Macros can have
arguments, just as functions can. Here is an
example that illustrates this fact.
main( )
{
float r1 = 6.25, r2 = 2.5, a ;
a = 3.14 * r1 *r1 ;
printf ( "Area of circle = %f\n", a ) ;
a = 3.14 *r2 * r2 ;
printf ( "Area of circle = %f", a ) ;
}
Here is another example of macros with
arguments:
#define ISDIGIT(y) ( y>= 48 && y <= 57 )
main( )
{
charch ;
printf ( "Enter any digit " ) ;
scanf ( "%c", &ch ) ;
if ( ISDIGIT ( ch ) )
printf ( "\nYou entered a digit" ) ;
else
printf ( "\nIllegal input" ) ;
}
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 18
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Passing the array To pass an array
argument in a function specifies the name of
array to pass without any bracket.
intmyarray[24];
myfunction(my array,24).
array usually pass to the function
array using call by refrence
function knows where the array stored
Passing array element:
using call by value
pass subscripted name (ex: myarray[5]) to
function.
Function prototype: void modify array
(intb[],int array size);
Parameter name may be optional.
int b[] may be int[]
int array size may be int
Multidimensional Array: An array with
more than one index value is called a
multidimensional array. All the array above is
called single-dimensional array. To declare a
multidimensional array you can do follow
syntax
data_typearray_name[][][];
The number of square brackets specifies the
dimension of the array. For example to
declare two dimensions integer array we can
do as follows:
1. intmatrix[3][3];
Initializing Multidimensional Arrays
you can initialize an array as a singledimension array. Here is an example of
initialize an two dimensions integer array:
1. int matrix[3][3] = { {11,12,13},
{21,22,23},
{32,31,33},
};
Two dimensional arrays
Multidimensional array have two or more than
indexs values which specify the element in
the array. i.e., multi[i][j].
Where [i] specify row and [j] specify column
Declaration and calculation:
int a[10][10];
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 19
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
Random access means we can move to any
part of a file and read or write data from it
without having to read through the entirefile.
we can access the data stored in the file in
two ways.
1. sequentially 2.Randomly
if we want to access the forty fourth record
then first forty three record read sequentially
to reach forty four record .In random access
data can be accessed and processed directly
.There is no need to read each record
sequentially .if we want to access a
particular record random access takes less
time than the sequential access.
C supports these function for random access
file.
1.fseek( ) Function
2.ftell ( ) Function
How to use fseek() function in C:
This function is used for setting the file
position pointer at the specified bytes .fseek
is a function belonging to the ANCI C
standard Library and included in the file
stdio.h. Its purpose is to change the file
position indicator for the specified stream.
Syntax :intfseek(FILE*stream_pointer,
longoffset, intorigin);
Argument meaning:
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 20
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
char name[30];
int age;
floatsal;
}emp;
void main()
{
FILE *fp;
fp=fopen("employee.dat","rb");
if (fp==NULL)
{
printf("/n error in opening file");
exit(1);
}
printf("position pointer in the beginning ->
%1d ",ftell(fp));
while("fread(position pointer ->
1%d\n",ftell(fp));
printf("position pointer -> %1d\n",ftell(fp));
printf("%s\t",emp.name);
printf("%d\t",emp.age);
printf("%f",emp.sal);
}
printf("size of the file in bytes is
%1d\n",ftell(fp));
fclose(fp);
getch();
}
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 21
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
The program then checks to see if the file is valid
by trying to open it. This is a standard operation,
and if it results in the file being opened, then the
return value of fopen will be a valid FILE*;
otherwise, it will be 0, the NULL pointer. After that,
we just execute a loop to print out one character at
a time from the file. The code is self-explanatory,
but is littered with comments; you should have no
trouble understanding its operation this far into the
tutorial.
struct student
{
introllno;
char name[50];
float salary;
};
union student
{
introllno;
char name[50];
float salary;
};
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 22
Pune University C Programming Theory Question Answer 2007-08 to 2011-14 Date : 10 October 2011
http://www.cprogram99.blogspot.com
arrays in c, two dimensional array in c, example of
array in c, arrays in c with examples, array
examples in c,what is array in c with example,
array in c with example, two dimensional array in c
example, examples of arrays in c, examples of
array in c, array in c examples, array of structures
in c example, one dimensional array in c example,
2d array in c example, explain two dimensional
array with example, two dimensional array
example in c, multidimensional array in c with
example, example of two dimensional array in c,2
dimensional array in c example, about arrays in c,
multidimensional arrays in c, what is 2d array in c,
pointers to array in c,c program for file operations,
c programs on file handling, c programs for file
handling, c program on file handling, file handling
in c programs, file handling using c, c program for
creating a file, what is header file in c
programming, bitwise programs in c, bitwise
operator program in c, bitwise programming in c,
dynamic memory allocation in c programming,
Dynamic memory allocation in c example
programs, c programming dynamic memory
allocation, c program for dynamic memory
allocation, c program using dynamic memory
allocation, example program for dynamic memory
allocation in c, Dynamic memory allocation in c
example, memory management in c programming,
dynamic allocation of memory in c, memory
allocation program in c, c programming memory
management, relational operator in c, conditional
operator in c example, Ternary operator in c
example, conditional operator in c programming,
Conditional operator in c with example, unary
operators in c language, increment and decrement
operators in c , programming, conditional operator
example in c, c program for string operations,
relational operators in c with examples, bitwise
operator program in c, conditional statements in c
programming, conditional statement in c with
example, c programs on structures, structure
programming in c, structure of c program with
example, structure of c programming, data
structure programs in c, data structure using c
programs, structures in c programming with
examples, data structures in c programs, c
programming using structures, data structures
programs in c, c programming using structure,
simple c program using structures, structures of c
programming, programs of structure in c, data
http://www.cprogram99.blogspot.com
Prof. Prakash Soanr (IMSCDR, Ahmednagar) Email: prakashsonar7@yahoo.co.in
Mob: +91-88-578-69283/ +91-9890476237
Page 23