Fundamentals of Computing & Computer Programming Unit V - 2 Marks
Fundamentals of Computing & Computer Programming Unit V - 2 Marks
UNIT V – 2 Marks
If a function calls itself again and again, then that function is called
Recursive function.
2. What is an array?
An array is a group of similar data types stored under a common name. int
a[10];
Pointer Declaration:
datatype *variable-name;
Example:
x=&a;
· Pointers are used to return more than one value to the function
{{
Output:
1. 2
2.
Preprocessor is the program, that process our source program before the
compilation.
8. How can you return more than one value from a function?
A Function returns only one value. By using pointer we can return more than
one value.
· Array name
· Type and
· Size
12. What are the steps involved in program development life cycle?
1. Program Design
2. Program Coding
1. Syntax errors
2. Runtime errors
3. Logical errors
4. Latent errors
Testing is the process of executing the program with sample or tested data.
· Human testing
enum mar_status
· Missing semicolon
· Missing braces
· Missing quotes
· Undeclared variables
· Macro Inclusion
· Conditional Inclusion
· File Inclusion
· Declaring a variable means describing its type to the compiler but not
allocating any space for it.
· Defining a variable means declaring it and also allocating space to hold the
variable.
· “Some where in the program there will be a variable with this name, and this
is the kind of data
· Type it is.” On the other hand, a definition says, “Right here is this variable
with this name and
· This data type”. Note that a variable can be declared any number of times,
but it must be defied
· Exactly once. For this reason, definitions do not belong in header files,
where they might get #included into more than one place in a program.
(1) Variables that are defined outside any function (whether of global or file
static scope), and variables that are defined inside a function as static
variables, exist for the lifetime of the program‟s execution. These variables are
stored in the data segment. The data segment is a fixed-size area in memory
set aside for these variables.
(2) Variables that are the arguments functions exist only during the execution
of that function. These variables are stored on the stack. The stack is an area
of memory that starts out as small and
(3) The third area is the one that does not actually store variables but can be
used to store data pointed to by variables. Pointer variables that are assigned
to the result of a call to the function malloc() contain the address of a
dynamically allocated area of memory. This memory is in an area called the
heap.
it can have its own segment, it all depends on the compiler options and
operating system. The heap, like the stack, has a limit on how much it can
grow, and the same rules apply as to how that limit is determined.
Array Pointer
1.Array allocates space 1.Pointer is explicitly assigned to point to
The function main () invokes other functions within it. It is the first function
to be called when the program starts execution.
5. Program exection ends when the closing brace of the function main() is
reached.
7. Any user-defined name can also be used as parameters for main() instead
of argc and argv
After this memory is freed with the free() function, the pointer itself will still
contain the address of the released block. This is referred to as a dangling
pointer. Using the pointer in this state is a serious programming error. Pointer
should be assigned NULL after freeing memory to avoid this bug.
Arrays Structures
An array is a collection of data A structure is a collection of data items of
items of same data type. different data types.
An array cannot have bit fields. A structure may contain bit fields.
Macros are more efficient (and faster) than function, because their
corresponding code is inserted directly at the point where the macro is called.
There is no overhead involved in using a macro like there is in placing a call to
a function.
However, macros are generally small and cannot handle large, complex
coding constructs. In cases where large, complex constructs are to handled,
functions are more suited, additionally; macros are expanded inline, which
means that the code is replicated for each occurrence of a macro.
All elements of an array share the same name, and they are distinguished
form one
2. Two-Dimensional Array
3. Multi-Dimensional Array
The sscanf():
This function allows to read character from a character Array and writes to
another array. Similar to scanf(), but instead of reading from standard input, it
reads from an array.
The sprintf():
This function writes the values of any data type to an array of characters.
Strings:
The group of characters, digit and symnbols enclosed within quotes is called
as Stirng
(or) character Arrays. Strings are always terminated with „\0‟ (NULL)
character. The compiler automatically adds „\0‟ at the end of the strings.
Prepered by Lenin Raja B.E(Dual) M.E.,(Research scholar Karpagam University)
Page 11
Example:
char name[]={„C‟,‟O‟,‟L‟,‟L‟,‟E‟,‟G‟,‟E‟,‟E‟,‟\0‟};
C O L L E G E \0
It is used to create a new data using the existing type. Syntax: typedef data
type name;
Example:
typedef int hours: hours hrs;/* Now, hours can be used as new datatype */
The user-defined functions can be called inside any functions like main(),
user-defined function, etc.
c) Function Definition:
The function definition block is used to define the user-defined functions with
statements.
Syntax:
{ {
====== ======
function calling; (or) function calling;
====== ======
} }
function definition;
Depending on the arguments and return values, functions are classified into
four types.
UNIT V – 16 Marks
Types:
· Library Functions
returntype function-name(Parameters);
Example:
function-name(actual parameters);
Example:
int square(a,b);
Example:
printf(“%d”,(a*b));
2. Define arrays. Explain the array types with an example program for
each type.
Arrays are data structures which hold multiple variables of the same data
type. Consider the case where a programmer needs to keep track of a
number of people within an organization. So far, our initial attempt will be to
create a specific variable for each user.
We created an array called names, which has space for four integer variables.
You may also see that we stored 0 in the last space of the array. This is a
common technique used by C programmers to signify the end of an array.
Arrays have the following syntax, using square brackets to access each
indexed value (called an element).
x[i]
/* Introducing array's, 2 */
#include <stdio.h>
main()
char word[20]; word[0] = 'H'; word[1] = 'e'; word[2] = 'l'; word[3] = 'l'; word[4] =
'o'; word[5] = 0;
DECLARING ARRAYS
Arrays may consist of any of the valid data types. Arrays are declared along
with all other variables in the declaration section of the program.
/* Introducing array's */
#include <stdio.h>
main()
--numbers[2];
The above program declares two arrays, assigns 10 to the value of the 3rd
element of array numbers, decrements this value ( --numbers[2] ), and finally
prints the value. The number of elements that each array is to have is
included inside the square brackets
The declaration is preceded by the word static. The initial values are enclosed
in braces,
Example:
#include <stdio.h>
main()
Prepered by Lenin Raja B.E(Dual) M.E.,(Research scholar Karpagam University)
Page 18
{
int x;
Multi-dimensioned arrays have two or more index values which specify the
element in the array.
multi[i][j];
In the above example, the first index value i specifies a row index, whilst j
specifies a column index.
DECLARATION
int m1[10][10];
NOTE the strange way that the initial values have been assigned to the two-
dimensional array m2. Inside the braces are,
{ 0, 1 },
{ 2, 3 }
Example:
#include <stdio.h>
main()
sum = 0;
#include <stdio.h>
main()
The difference between the two arrays is that name2 has a null placed at the
end of the string, ie, in name2[5], whilst name1 has not. To insert a null at the
end of the name1 array, the initialization can be changed to,
Consider the following program, which initialises the contents of the character
based array word during the program, using the function strcpy, which
necessitates using the include file string.h
Example:
#include <stdio.h>
#include <string.h>
main()
char word[20];
printf("%s\n", word );
Strings:
Example:
· strlen( )
· strcpy( )
· strncpy( )
· stricmp( )
· strcmp( )
· strncmp( )
· strcat( )
· strrev( ) etc.,
Example program:
4. What are pointers? When and why they are used? Explain in detail
with sample programs. (JAN 2009/MAY 2009)
datatype *variable-name;
Exmaple:
int *a;
Example program:
THE PREPROCESSOR
The define statement is used to make programs more readable, and allow the
#define FALSE 0
#define NULL 0
#define OR |
#define EQUALS ==
game_over = TRUE;
................
MACROS
Macros are inline code which are substituted at compile time. The definition of
a macro, which accepts an argument when referenced,
y = SQUARE(v);
while(*string) {
++string;
CONDITIONAL COMPILATIONS
These are used to direct the compiler to compile/or not compile the lines that
follow
#ifdef NULL
#define NL 10
#define SP 32
#endif
In the preceding case, the definition of NL and SP will only occur if NULL has
been defined prior to the compiler encountering the #ifdef NULL statement.
The scope of a definition may be limited by
#undef NULL
This renders the identification of NULL invalid from that point onwards in the
source file.
Typedef
typedef struct {
} DATE;
Enumerated data type variables can only assume values which have been
previously
declared.
enum month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
this_month = feb;
Example:
#include <stdio.h>
main()
direction = east;
values.
Here variables may exist in separately compiled modules, and to declare that
the
variable is external,
This means that the data storage for the variable move_number resides in
another source
module, which will be linked with this module to form an executable program.
In using a variable across a number of independently compiled modules,
space should be allocated in only one module, whilst all other modules use
the extern directive to access the variable.
NULL STATEMENTS
These are statements which do not have any body associated with them.
#include <stdio.h>
if( argc == 2 )
else
*argv[1] is
a pointer to the first argument supplied, and *argv[n] is the last argument. If no
arguments
are supplied, argc will be one. Thus for n arguments, argc will be equal to n +
1. The program is called by the command line, myprog argument1.
Call by value:
In call by value the value of the actual arguments are passed to the formal
arguments and the operation is done on formal arguments.
Example program:
Call by reference:
Example program:
Syntax:
struct struct-name
} structure_variables;
Example:
struct student
char name[25];
int rollno;
int m1,m2,m3,total;
float avg;
}s1,s2;
· Pointers to structures
Example program:
· To define a structure and read the member variable values from user.
Example:
union student
char name[20];
int rollno,m1,m2,m3,tot;
float avg;
}s1;
Union of structure
Example program: