Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Programming in C - SAE1A

Uploaded by

arockia patrick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming in C - SAE1A

Uploaded by

arockia patrick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Programming in C

Unit-I

Introduction to C programming

2 Marks

1. Define identifier.

Identifiers are symbols used to uniquely identify a program


element in the code. They are also used to refer to types, constants
and parameters.
2. Define keyword.

Keywords are predefined; reserved words used in programming


that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an
identifier.
Ex: auto, break, case, char, do, default

3. What are data types?

A data type in programming is a classification that specifies which


type of value a variable has and what type of mathematical,
relational or logical operations can be applied to it without causing
an error.
Ex: String, int, float, Boolean

4. List out any ten data types.

Int
Char
Float
Double
Enum, pointer. array, structure, union, void
5. What is meant by constants?

Constants are also like normal variables. But, only difference is


their values cannot be modified by the program once they are
defined.
Constants refer to fixed values.
They are also called as literals.

6. Define Variables.
A variable is a name given to a storage area that our programs can
manipulate.
Each variable in C has a specific type which determines the size
and layout of the variables memory.

7. How to declare Variables?

A variable declaration provides assurance to the compiler that


there exists a variable with the given type and name so that the
compiler can proceed for further compilation without requiring the
complete detail about the variable.
Syntax: Data type Variable name;

8. What is meant by expressions?

An expression is any legal combination of symbols that represent


a value. Each expression consists of at least one operand and can
have one or more operators.

9. Define Statements?

Computer that instructs the computer to take a specific action,


such as display to the screen, or collect input.
A computer program is made up of a series of statements.

10.What is meant by operators?

An operator is a symbol that tells the compiler to perform specific


mathematical or logical functions.
C language is rich in built-in operators.
11.Define Arithmetic operator.

An arithmetic operator performs mathematical operations such as


addition, subtraction and multiplication on numerical values.
Ex: +, -, *, /, %

12.Define logical operator.

An expression containing logical operator returns either 0 or 1


depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C
programming.
Ex: && ||!

13.Define Assignment operator.

An assignment operator is used for assigning a value to a variable.


The most common assignment operator is =
Ex: = =+ -= *= /= %=

14.Define Conditional operator.

A conditional operator is a ternary operator that is it works on 3


operands.
Syntax: ConditionalExpression?expression1: expression2

5 Marks

1. Write down the rules for declaring the variables.

Every variable name should start with alphabets or underscore (_).


No spaces are allowed in variable declaration.
Except underscore (_) no other special symbol are allowed in the
middle of the variable declaration (not allowed -> roll-no, allowed ->
roll_no).
Maximum length of variable is 8 characters depend on compiler and
operation system.
Every variable name always should exist in the left hand side of
assignment operator (invalid -> 10=a; valid -> a=10;).
No keyword should access variable name (int for <- invalid because
for is keyword).
Syntax

Data type variable name;


int a;
2. Discuss about the Importance of C.

It is robust language whose rich setup of built in functions and


operator can be used to write any complex program.
Program written in C are efficient due to several variety of data
types and powerful operators.
The C compiler combines the capabilities of an assembly
language with the feature of high level language.
There are only 32 keywords
C is portable language
C language is well suited for structured programming, this
requires user to think of a problems in terms of function or
modules or block.
C language has its ability to extend itself.

3. Discuss about the Arithmetic operators with example program.

Arithmetic operators "+" and "-" are used to manipulate


pointers by
adding or
subtracting the
numeric value to
or from the
pointers without
generating any
exception during
overflow of the
of the pointer's
domain
4. Explain the concept of Expressions.

In programming, an expression is any legal combination of


symbols that represents a value.
C Programming provides its own rules of Expression, whether
it is legal expression or illegal expression. For example, in the
C language x+5 is a legal expression.
Every expression consists of at least one operand and can have
one or more operators.
Operands are values and Operators are symbols that represent
particular actions.
( refer notes also and add points)

5. Explain the concept of Relational and Logical operators with


example.

An expression containing logical operator returns either 0 or 1


depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C
programming.
10 Marks

1. Describe any ten commonly used library functions in C.

2. Discuss about the Data types in C.

The types in C can be classified as follows −

S.N. Types & Description


1 Basic Types

They are arithmetic types and are further classified into: (a)
integer types and (b) floating-point types.
2 Enumerated types

They are again arithmetic types and they are used to define
variables that can only assign certain discrete integer values
throughout the program.
3 The type void

The type specifier void indicates that no value is available.


4 Derived types

They include (a) Pointer types, (b) Array types, (c) Structure
types, (d) Union types and (e) Function types.

Integer Types

Type Storage Value range


size
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
Floating-Point Types

Type Storage size Value range Precision


float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

3. Write a C program for Arithmetic operators.

SOURCE CODE
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
OUTPUT
4. Discuss about Constants in C.

Constants/Literals : A constant is a value or an identifier whose


value cannot be altered in a program. For example: 1, 2.5, "C
programming is easy", etc.
As mentioned, an identifier also can be defined as a constant.

const double PI = 3.14


Integer constants: An integer constant is a numeric constant
(associated with number) without any fractional or exponential part.
There are three types of integer constants in C programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
Floating-point constants: A floating point constant is a numeric
constant that has either a fractional form or an exponent form.
Character constants: A character constant is a constant which uses
single quotation around characters. For example: 'a', 'l', 'm', 'F'
Escape Sequences: Sometimes, it is necessary to use characters
which cannot be typed or has special meaning in C programming. F
String constants: String constants are the constants which are
enclosed in a pair of double-quote marks.
Enumeration constants: Keyword enum is used to define
enumeration types.
For example: enum color {yellow, green, black, white};
Unit-II

Loops and Statements

2 Marks

1. Define Branching

The C language programs follow a sequential form of execution of


statements.
Many times it is required to alter the flow of sequence of instructions.
C language provides statements that can alter the flow of a sequence
of instructions.
These statements are called as control statements.
2. List out the types of branching.

If Statement
The If else Statement
Compound Relational tests
Nested if Statement
Switch Statement
3. Define Looping.

A loop statement allows us to execute a statement or group of


statements multiple times.

4. List out the types of Looping.

While loop
Do...while loop
For loop
Nested loops

5. Write down the syntax of if-else statement.

If (condition)
Program statement 1;
Else
Program statement 2;
6. Define Switch Statement.

A switch statement allows a variable to be tested for equality


against a list of values.
Each value is called a case, and the variable being switched on
is checked for each switch case.

7. Define goto statement.

goto statement is highly discouraged in any programming


language because it makes difficult to trace the control flow of a
program, making the program hard to understand and hard to modify.

8. Define comma operator.

Comma operator represented by ‘,’ ensures the evaluation from


left to right, one by one, of two or more expressions, separated with
commas, and result of entire expression is value of rightmost
expression.
Example int a = 10, b = 20, c = 30;

9. What is the purpose of continue statement?

The continue statement in C programming works somewhat


like the break statement.
Instead of forcing termination, it forces the next iteration of the
loop to take place, skipping any code in between.

10. Write the syntax of While loop?

while(condition)
{
statement(s);
}
5 Marks

1. Explain the syntax of switch statement in C with an example.

A switch statement allows a variable to be tested for equality against a


list of values.
Each value is called a case, and the variable being switched on is
checked for each switch case.
Syntax

switch(expression)
{
case constant-expression :
statement(s);
break;

case constant-expression :
statement(s);
break;

default :
statement(s);
}
The following rules apply to a switch statement
The expression used in a switch statement must have an integral or
enumerated type.
The constant-expression for a case must be the same data type as
the variable in the switch
When a break statement is reached, the switch terminates
Not every case needs to contain a break
A switch statement can have an optional default case, which must
appear at the end of the switch
2. Explain the loop with its types.

S.N. Loop Type & Description


1 while loop

Repeats a statement or group of statements while a given


condition is true. It tests the condition before executing the
loop body.
2 for loop

Executes a sequence of statements multiple times and


abbreviates the code that manages the loop variable.
3 do...while loop

It is more like a while statement, except that it tests the


condition at the end of the loop body.
4 nested loops

You can use one or more loops inside any other while, for, or
do..while loop.
10 Marks

1. Describe the loop control statements in C with examples.

The C language programs follow a sequential form of execution of


statements.
Many times it is required to alter the flow of sequence of instructions.
C language provides statements that can alter the flow of a sequence
of instructions. These statements are called as control statements.
Branching Statement are of following categories:
1. If Statement
2. The If else Statement
3. Compound Relational tests
4. Nested if Statement
5. Switch Statement
If Statement : If statement is the simplest form of the control
statement. It is very frequently used in allowing the flow of program
execution and decision making.
The If structure has the following syntax
if(condition)
statement;

The If else Statement The if else is actually just an extension of the


general format of if statement. If the result of the condition is true,
then program statement 1 is executed else program statement 2 will be
executed.
The syntax of the If else statement is as follows:
If (condition)
Program statement 1;
Else
Program statement 2;

Compound Relational tests: To perform compound relational tests,


C language provides the necessary mechanisms. A compound
relational test is simple one or more simple relational tests joined
together by either the the logical OR operators or logical AND.
The syntax of the Compound Relational tests is as follows:
a> if (condition1 && condition2 && condition3)
b> if (condition1 // condition2 // condition3)

Nested if Statement: The if statement may itself contain another if


statement is called as nested if statement. The syntax of the Nested if
Statement is as follows
if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
Switch Statement: The switch-case statement is a multi-way
decision making statement.
Unlike the multiple decision statement that can be created using if-
else, the switch statement evaluates the conditional expression and
tests it against the numerous constant values.
Double and Float are not allowed.
The syntax of switch statement is as follows :
switch( expression )
{
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
}
Unit-III

Functions

2 Marks

1. Define Functions.

A function is a group of statements together perform a task.


Every C program has at least one function, which is main ( ), and all
the most trivial programs cab define additional functions.

2. Write the general form of functions.

return _type function_ name( parameter list)


{
body of the function
}

3. List out the parts of function.

Return type
Function name
Parameters
Function Body

4. Define return type.

A function may return a value.


The return_type is the data type of the value the function returns.
Some functions perform the desired operations without returning a
value.
In this case, the return_type is the keyword void.
5. Define Function Prototype.

computer programming, a function prototype or function


interface is a declaration of a function that specifies the
function's name and type signature (arity, parameter types, and
return type), but omits the function body

6. Define Storage Classes.

A storage class defines the scope (visibility) and life-time of


variables and/or functions within a C Program. They precede
the type that they modify. We have four different storage
classes in a C program −
auto
register
static
extern

7. Define Static Variables.

Static variables have a property of preserving their value even


after they are out of their scope.
static variables preserve their previous value in their previous
scope and are not initialized again in the new scope

8. What is meant by Local variables?

Variables that are declared inside a function or block are called


local variables.
They can be used only by statements that are inside that
function or block of code.
Local variables are not known to functions outside their own
local variable declaration
int main(){
int a, b;
int c;
}
9. What is meant by global variables?

Global variables are defined outside a function, usually on top of the


program. Global variables hold their values throughout the lifetime of
your program and they can be accessed inside any of the functions
defined for the program.
A global variable can be accessed by any function
global variable declaration
int a, b;
int c;
int main(){

}
10.Define Recursion.

Recursion is the process of repeating items in a self-similar way. In


programming languages, if a program allows you to call a function
inside the same function, then it is called a recursive call of the
function.
Syntax:

void recursion()
{
recursion();
}

int main()
{
recursion();
}
5 Marks

1. Give a brief account on Static variables.

Static variables have a property of preserving their value even


after they are out of their scope.
Static variables preserve their previous value in their previous
scope and are not initialized again in the new scope.
Syntax:

static data_type var_name = var_value;

A static int variable remains in memory while the program is


running.
A normal or auto variable is destroyed when a function call
where the variable was declared is over.
Example program:

#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
OUTPUT
12
2. Describe Multi-file programs.

The procedures and global variables of a C program can be split


across multiple files.

Each of the files can be compiled separately, into a *.o file. Later, all
the *.o files can be linked together (still using gcc) into a running program,
an executable object program.

Each file can have access to a set of names that are private to that file,
and to other names that are shared across all the files of the complete
program.

This allows the program to be built and modified in pieces, often


called “modules”, which can be tested separately, and later combined into
the final product.

The programmer determines which names are local to each file, and
which are shared, and indicates his/her decisions by constructing
declarations appropriately.

The storage class specifies extern and static play a role in this, as
does the position of each declaration, and whether a declaration is in fact a
“definition”.
10 Marks

1. What is recursion? Write a C program to find the factorial value


of an integer using recursion.

Recursion is the process of repeating items in a self-similar


way. In programming languages, if a program allows you to call a
function inside the same function, then it is called a recursive call of
the function.
Syntax:
void recursion()
{
recursion();
}
int main()
{
recursion();
}
C programming language supports recursion, i.e., a function to call
itself. But while using recursion, programmers need to be careful to
define an exit condition from the function; otherwise it will go into
an infinite loop.
Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number, generating
Fibonacci series, etc
Example Program: Number Factorial
#include <stdio.h>
int factorial(unsigned int i) {
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}
int main() {
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
OUTPUT: Factorial of 15 is 2004310016
2. Describe the Functions in C.

A function is a group of statements that together perform a task.


Every C program has at least one function, which is main(), and all
the most trivial programs can define additional functions.
A function declaration tells the compiler about a function's
name, return type, and parameters. A function definition provides the
actual body of the function.
Defining a Function :The general form of a function definition
in C programming language is as follows −

return_type function_name( parameter list )


{
body of the function
}

Return Type − A function may return a value. The return_type


is the data type of the value the function returns.
Function Name − This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
Parameters − A parameter is like a placeholder. When a function
is invoked, you pass a value to the parameter. This value is
referred to as actual parameter or argument.
Function Body − The function body contains a collection of
statements that define what the function does.
Unit-IV

Arrays and Structures

2 Marks

1. Define Array.

Arrays a kind of data structure that can store a fixed-size sequential


collection of elements of the same type.
An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same
type.

2. List out the types of array.

One Dimensional Arrays


Two Dimensional Arrays
Multi-dimensional Arrays
Dynamic Arrays

3. How to pass arrays to functions

void myFunction(int param[10]) {


.
.
.
}

4. Define multi-dimensional array.

The simplest form of multidimensional array is the two-


dimensional array.
A two-dimensional array is, in essence, a list of one-
dimensional arrays. To declare a two-dimensional integer array of size
[x][y]
5. Define Structure.

Structure is another user defined data type available in C that


allows combining data items of different kinds.
Structures are used to represent a record

6. Define user defined data types.

the basic set of data types defined in the C language such as int,
float etc. may be insufficient for your application.
In circumstances such as these, you can create your own data
types which are based on the standard ones

7. What is meant by union?

A union is a special data type available in C that allows storing


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 multiple-purpose.

8. Define bitwise operator.

To perform bit-level operations in C programming, bitwise


operators are used.

9. Write the syntax for declaring array.

type arrayName [ arraySize ];


EX: double balance[10];
5 Marks

1. Write a note on bitwise operators.

To perform bit-level operations in C programming, bitwise operators


are used.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
<< Shift left
>> Shift right

Bitwise AND operator &: The output of bitwise AND is 1 if the


corresponding bits of two operands is 1.
If either bit of an operand is 0, the result of corresponding bit is
evaluated to 0.
Bitwise OR operator |: The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1. In C Programming, bitwise OR
operator is denoted by |.

2. Give a short note on structures within structures.

Nested structure in C is nothing but structure within structure. One


structure can be declared inside other structure as we declare
structure members inside a structure.
The structure variables can be a normal structure variable or a
pointer variable to access the data. You can learn below concepts
in this section.
Structure within structure in C using normal variable
Structure within structure in C using pointer variable

3. Give short notes on union.

A union is a special data type available in C that allows storing


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 multiple-purpose.
Defining a Union: To define a union, you must use the union
statement in the same way as you did while defining a structure. The
union statement defines a new data type with more than one member
for your program. The format of the union statement is as follows −

union [union tag] {


member definition;
member definition;
...
member definition;
} [one or more union variables];
Example program

#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};

int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
OUTPUT: Memory size occupied by data : 20
10 Marks

1. Give a Brief note on Arrays.

arrays consist of contiguous memory locations. The lowest


address corresponds to the first element and the highest address to the
last element.
S.N. Concept & Description
Multi-dimensional arrays
1
C supports multidimensional arrays. The simplest form of the multidimensional
array is the two-dimensional array.
Passing arrays to functions
2
You can pass to the function a pointer to an array by specifying the array's name
without an index.
Return array from a function
3
C allows a function to return an array.
Pointer to an array
4
You can generate a pointer to the first element of an array by simply specifying the
array name, without any index.

2. Describe the concept Structures.


structure is another user defined data type available in C that allows
to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep
track of your books in a library. You might want to track the
following attributes about each book −
Title
Author
Subject
Book ID
Defining a Structure: To define a structure, you must use the struct
statement.
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Unit-V

Pointers and Files

2 Marks

1. Define pointers.

Pointers in C are easy and fun to learn. Some C programming


tasks are performed more easily with pointers, and other tasks,
such as dynamic memory allocation, cannot be performed
without using pointers

2. Write the syntax of pointers.


data_type * pt_name;
* - pointer variable
pt_name - name of the variable which needs a memory location

3. Define File.

A file represents a sequence of bytes, regardless of it being a


text file or a binary file. C programming language provides
access on high level functions as well as low level (OS level)
calls to handle file on your storage devices.

4. How to use pointers.

define a pointer variable


assign the address of a variable to pointer
access the value at the address available in the pointer variable

5. Define Null pointer


A pointer that is assigned NULL is called a null pointer
EX:
int main( )
{
int *ptr =NULL;
Printf("The value of pointer is:", ptr);
return 0;
}
6. Define pointer to pointer

A pointer to pointer is a form of multiple indirections, or a chain


of pointers.

7. List out the modes of opening file


r, w, a, r+, w+, a+

8. Write the syntax for fclose( ) and fputc ( ) function.

int fclose( File *fp )


int fputc ( int c, FILE *fp)

9. Write the operations of file.

Create
OPen
Process
Delete
Close

5 Marks

1. Explain the different file types that can be specified by the fopen( )
function.

The C library function FILE *fopen(const char *filename, const


char *mode) opens the filename pointed to, by filename using the
given mode.
Declaration: Following is the declaration for fopen() function.
FILE *fopen(const char *filename, const char
*mode)
Parameters: filename − This is the C string containing the name of
the file to be opened.
mode − This is the C string containing a file access mode.
mode Description
"r" Opens a file for reading. The file must exist.
Creates an empty file for writing. If a file with the same name already
"w" exists, its content is erased and the file is considered as a new empty
file.
Appends to a file. Writing operations, append data at the end of the
"a"
file. The file is created if it does not exist.
"r+" Opens a file to update both reading and writing. The file must exist.
"w+" Creates an empty file for both reading and writing.
"a+" Opens a file for reading and appending.

• Return Value: This function returns a FILE pointer. Otherwise,


NULL is returned and the global variable errno is set to indicate the
error.

2. Explain how to pass pointers to functions.

#include <stdio.h>
#include <time.h>
void getSeconds(unsigned long *par);
int main () {
unsigned long sec;
getSeconds( &sec );
printf("Number of seconds: %ld\n", sec );
return 0;
}
void getSeconds(unsigned long *par) {
*par = time( NULL );
return;
}
OUTPUT: Number of seconds :1294450468

3. Describe the concept creating and closing the files.


Opening Files
You can use the fopen( ) function to create a new file or to open
an existing file. This call will initialize an object of the type FILE,
which contains all the information necessary to control the stream
FILE *fopen( const char * filename, const char * mode );

Mode Description
r Opens an existing text file for reading purpose.
Opens a text file for writing. If it does not exist, then a new file is created. Here
w
your program will start writing content from the beginning of the file.
Opens a text file for writing in appending mode. If it does not exist, then a new file
a is created. Here your program will start appending content in the existing file
content.
r+ Opens a text file for both reading and writing.
Opens a text file for both reading and writing. It first truncates the file to zero
w+
length if it exists, otherwise creates a file if it does not exist.
Opens a text file for both reading and writing. It creates the file if it does not exist.
a+
The reading will start from the beginning but writing can only be appended.

Closing a File: To close a file, use the fclose( ) function. The


prototype of this function is

int fclose( FILE *fp );

10 Marks

1. Briefly explain pointers with its types.

Pointers in C are easy and fun to learn. Some C programming tasks


are performed more easily with pointers, and other tasks, such as dynamic
memory allocation, cannot be performed without using pointers.
So it becomes necessary to learn pointers to become a perfect C
programmer. Let's start learning them in simple and easy steps.
every variable is a memory location and every memory location has
its address defined which can be accessed using ampersand (&) operator,
which denotes an address in memory.

Example Program

#include <stdio.h>
int main () {
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
return 0;
}
OUTPUT
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6

2. Explain File handling functions in C.

File Handling in C: File Handling in c language is used to open,


read, write, search or close file. It is used for permanent storage.

Advantage of File

It will contain the data even after program exit. Normally we use
variable or array to store data, but data is lost after program exit. Variables
and arrays are non-permanent storage medium whereas file is permanent
storage medium.

Functions for file handling

No. Function Description


1 fopen() opens new or existing file
2 fprintf() write data into file
3 fscanf() reads data from file
4 fputc() writes a character into file
5 fgetc() reads a character from file
6 fclose() closes the file
7 fseek() sets the file pointer to given position
8 fputw() writes an integer to file
9 fgetw() reads an integer from file
10 ftell() returns current position
11 rewind() sets the file pointer to the beginning of the file
3. Explain Structures and Pointers.

Structures can be created and accessed using pointers. A pointer


variable of a structure can be created as below:

struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr;
}

Accessing structure's member through pointer

A structure's member can be accesssed through pointer in two ways:

1. Referencing pointer to another address to access memory


2. Using dynamic memory allocation

Referencing pointer to another address to access the memory


Consider an example to access structure's member through pointer.

#include <stdio.h>
typedef struct person
{
int age;
float weight;
};

int main()
{
struct person *personPtr, person1;
personPtr = &person1; // Referencing pointer to memory address of
person1

printf("Enter integer: ");


scanf("%d",&(*personPtr).age);

printf("Enter number: ");


scanf("%f",&(*personPtr).weight);

printf("Displaying: ");
printf("%d%f",(*personPtr).age,(*personPtr).weight);

return 0;
}

You might also like