MCA1020- PROGRAMMING IN C
Q1.) Explain the history of C Language. What are the advantages of C language?
Ans.) C was developed by Dennis Ritchie & came after B in the year 1972.
C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C. Its instructions consist of terms that resemble algebraic expressions, augmented by certain English keywords such as if, else, for, do & while.
C was the offspring of the ‘Basic Combined Programming Language’ (BPCL) called B, developed in the 1960’s at Cambridge University.
In C, the type of a variable determines what kind of values it may take on. The type of an object determines the set of values it can have & what operations can be performed on it. This is a fairly formal, mathematical definition of what a type is, but it is traditional (& meaningful)
Advantages of C Language:-
· C language is a building block for many other currently known languages.
C language has variety of data types and powerful operators. Due to this, programs written in C language are efficient, fast and easy to understand.
C language has variety of data types and powerful operators. Due to this, programs written in C language are efficient, fast and easy to understand.
· C is highly portable language. This means that C programs written for one computer can easily run on another computer without any change or by doing a little change.
· There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs.
· Another important advantage of C is its ability to extend itself. A C program is basically a collection of functions that are supported by the C library this makes us easier to add our own functions to C library. Due to the availability of large number of functions, the programming task becomes simple.
· C language is a structured programming language. This makes user to think of a problem in terms of function modules or blocks. Collection of these modules makes a complete program. This modular structure makes program debugging, testing and maintenance easier.
--------------------------------------------------------------------------------------------------------------------------
Q2.) Differentiate between formal parameters and actual parameters with example
Ans.) Formal Parameter:-
· Consider y = f(x) // where x is independent & value of y is dependent on value returned by f. Here f(x) is formal parameter or argument to f
· Formal parameters are place holders for values of "Actual parameter"
· Formal parameters are declared / defined during function definition.
Examples:-
int f (int x):- x is formal parameter
Examples:-
int f (int x):- x is formal parameter
int miles (int km, int mileage):- km & mileage are formal parameter
Actual parameter:-
Actual parameter:-
· In C program values that appears inside the parentheses are called "Actual Parameter"
· Formal parameters must match in position, number & type
· When a function call is made a memory location is allocated for each formal parameter & a copy of corresponding actual parameter is kept in that memory location. All calculations involving the formal parameter use this memory location.
· Actual parameters are always "Passed" & variables that are declared & passed as arguments to procedure by caller.
Examples :-
#define MILEAGE 25:- Here MILEAGE is actual parameter
int miles(int gallons, int mileage):- Both gallons & mileage are formal parametersConsider following example:-
Problem:- Accepts a number of gallons as input from end user and calculates & displays the number of miles that can be traveled
#include
#define MILEAGE 25
int miles(int gallons, int mileage);
int main(void)
{
int f ; /*(f is actual parameter & f stores value in Gallons)*/
printf ("Enter number of Gallons of Gas : ");
scanf ("%d", &f);
printf ("\n You will be able to travel %d miles from %d Gallons of Gasoline", miles (f, MILEAGE),f);
/* here f & MILEAGE are actual parameters whose values are stored into gallons & mileage respectively which are formal parameters*/
return 0;
}
int miles(int gallons, int mileage)
{
return ( gallons * mileage); /*Here both are formal parameters i.e. gallons & mileage*/
}
-----------------------------------------------------------------------------------------------------------------
Q3.) Describe about static and external variables.
Ans.) Static Variables: Static variables are defined within individual functions & therefore have the same scope as automatic variables, i.e. they are local to the functions in which they are declared. Unlike automatic variables, however, static variables retain their values throughout the life of the program. As a result, if a function is exited & then reentered later, the static variables defined within that function will retain their previous values. This feature allows function to retain information permanently throughout the execution of a program. Static variables can be utilized within the function in the same manner as other variables. They cannot be accessed outside of their defining function. In order to declare a static variable the keyword static is used as shown below:
Static int count;
External Variables: It is possible to split a function up into several source files, for easier maintenance. When several source files are combined into one program the compiler must have a way of correlating the variables which might be used to communicate between the several source files. Furthermore, if a variable is going to be useful for communication, there must be exactly one of it: you wouldn’t want one function in one source file to store a value in one variable named externvar, & then have another function in another source file read from a different variable named externvar. Therefore, a variable should have exactly one defining instance, in one place in one source file. If the same variable is to be used anywhere else (i.e. in some other source file or files), the variable is declared in those other file(s) with an external declaration, which is not a defining instance. The external declaration says the compiler that the variable will be used in this source file but defined in some other source file. Thus the compiler doesn’t allocate space for that variable with this source file.
To make a variable as an external declaration, which is defined somewhere else; you precede it with the keyword extern:
Extern int j;
-----------------------------------------------------------------------------------------------------------------
Q4.) Distinguish between pass by value and pass by reference with the help of an example.
Ans.) Pass By value: Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.
For example:
#include <stdio.h>
#include <conio.h>
Int add (int p, int q);
Int main ()
{
Int a, b, c;
Clrscr();
Printf (“Enter two numbers\n”);
Scanf (“%d%d” , &a, &b);
C=30;
Printf (“\n Sum of %d & %d is %d”, a, b, c);
Getch();
Return 0;
}
Int add (int p, int q)
{
Int result;
Result = p + q;
Return (result);
}
Pass by reference: When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflect on the calling function. Here you don't need to return anything to calling function. For Example:
#include <stdio.h>
#include <conio.h>
Void add (int *p, int *q, int *r);
Void main()
{
Int a, b, c;
Clrscr ();
Prntf(“Enter two number \n”);
Scanf (“%d%d”,&a,&b);
Add (&a,&b,&c);
Printf (“Sum of %d & %d is %d”, a, b, c);
Getch();
}
Void add (int *p, int *q, int *r)
{
*r = *p + *q;
}
-----------------------------------------------------------------------------------------------------------------
Q5.) Define macro. How can we declare a macro statement? Explain with an example.
Ans.) A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the preprocessor directive.
A preprocessor line of the form:
# define name text
Defines a macro with the given name, having as its value the given replacement text. After that (for the test of the current source file), wherever the preprocessor sees that name, it will replace it with the replacement text. The name follows the same rules as ordinary identifiers (it can contain only letters, digits, & underscores, & may not begin with a digit). Since macro behave quite differently from normal variables (or functions), it is customary to give them names which are all capital letters (or at least which begin with a capital letter). The replacement text can be absolutely anything – it’s not restricted to numbers, or simple strings, or anything.
The most common use for macros is to propagate various constants around & to make them more self-documenting. We’ve been saying things like
Char line [100];
…
Getline (line, 100);
But this is neither readable nor reliable; it’s not necessarily obvious what all those 100’s scattered around the program are, & if we ever decide that 100 is too small for the size of the array to hold lines, we’ll have to remember to change the number in two (or more) places. A much better solution is to use a macro:
# define MAXLINE 100
Char line [MAXLINE];
…
Getline (line, MAXLINE);
Since the replacement text of a preprocessor macro can be anything, it can also be an expression, although you have to realize that, as always the text is substituted (& perhaps evaluated) later. No evaluation is performed when the macro is defined. For example, suppose that you write something like
#define A 2
#define B 3
#define C A + B
-----------------------------------------------------------------------------------------------------------------
Q6.) What is the use of fopen () and fclose () function? List and explain different modes for opening a file.
Ans.) fopen () function: - fopen () function will open file specified in the specified mode. After opening file in the read mode fopen () will return the address of the structure that contains information needed for subsequent I/O on the file being opened.(i.e. File Pointer). In order to store file pointer we have assigned return address to FILE pointer variable “fp“.
Example:
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("INPUT.txt","r"); { here fopen () will open “INPUT.txt” in the “Read” mode}
while(1)
{
ch = fgetc(fp);
if(ch == EOF )
break;
printf("%c", ch);
}
fclose(fp);
}
|
fclose () function :- Whenever we open a file in read or write mode then we perform appropriate operations on file and when file is no longer needed then we close the file using fclose() function.
Example:
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("INPUT.txt","r")
-----------
-----------
fclose(fp); //File is no longer needed
}
|
Different modes for opening a file:- File can be opened in basic 3 modes: Reading Mode, Writing Mode, Appending Mode. If File is not present on the path specified then New File can be created using Write and Append Mode.
Mode
|
Meaning
|
fopen Returns if FILE-
| |
Exists
|
Not Exists
| ||
r
|
Reading
|
–
|
NULL
|
w
|
Writing
|
Over write on Existing
|
Create New File
|
a
|
Append
|
–
| |
r+
|
Reading + Writing
|
New data is written at the beginning overwriting existing data
|
Create New File
|
w+
|
Reading + Writing
|
Over write on Existing
|
Create New File
|
a+
|
Reading + Appending
|
New data is appended at the end of file
|
Create New File
|