V Unit PDF
V Unit PDF
1. Array
Introduction of array
Declaring Arrays
Example
double balance[10];
1
Initializing Arrays
The number of values between braces { } cannot be larger than the number
of elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the
initialization is created.
2. Declaring of one and two dimensional array
Array which have elements with single subscript are known as one
dimensional array.
It is simply a grouping of like type data.
It can be used to represent a list of number or a list of names.
Syntax:
datatype array_name[size];
regno [10]
All the variables are declared before they are used in the program. Similarly,
an array must be declared before it is used.
During declaration, the size of the array has to be specified. The size used
during declaration of the array informs the compiler to allocate and reserve
the specified memory locations.
2
Syntax:-
data_type array_name[n];
datatype array_name[ROW][COL];
data_type array_name[row_size][column_size];
Example:-
int arr[3][3];
Where first index value shows the number of the rows and second index
value shows the number of the columns in the array.
3
3. Initialization of one and two dimensional array
Syntax
Example:
Example:
int a[2][3]={0,0,0,1,1,1};
initializes the elements of the first row to zero and the second row to one. The
initialization is done row by row.
int a[2][3]=
{0,0,0},
{1,1,1}
};
4
4. Declaring and initialling string variables
String
Char variable_name[size];
Example
char name[10];
char city[20];
They are three types of using reading strings scanf, gets, and fgets
Scanf
Gets
fgets
1. scanf
5
scanf does skip over any leading whitespace characters in order to find the
first non-whitespace character.
The width field can be used to limit the maximum number of characters
read.
The argument passed to scanf needs to be of type pointer-to-character, i.e.
char *, and is normally the name of a char array. ( In this case the size of the
array -1 should be given as the width field, to make sure scanf can append
the null byte without overflowing the array.
char * gets ( char * str );
The gets function will read a string of characters from standard input ( the
keyboard ) until it encounters either a newline character or the end of file.
Unlike scanf, gets does not care about spaces in the input, treating them the
same as any other character.
Note that gets has no provision for limiting the number of characters read,
leading to possible overflow problems.
When gets encounters a new line character, it stops reading, but does NOT
store it.
A null byte is always appended to the end of the string of stored characters.
char * fgets ( char * str, int num, FILE * stream );
fgets is equivalent to gets, except for two additional arguments and one key
difference:
num provides for a limit on the maximum number of characters read
The FILE * argument allows fgets to read from any file. stdin may be
entered to read from the keyboard.
fgets stops reading when it encounters a new line character, and it DOES
store the newline character.
A null byte is always appended to the end of the string of stored characters.
They are two types of using writing string printf and puts
printf
6
int puts ( const char * str );
Writes the string out to standard output ( the screen ) and automatically
appends a newline character at the end.
This is a lower-level routine that printf, without formatting, and possibly
faster
Syntax
Example:
printf("%d",x);
/* Display Result = 25