(2 Hours) (Total Marks: 75: Interpretation, Depending On How It Is Carried Out
(2 Hours) (Total Marks: 75: Interpretation, Depending On How It Is Carried Out
(2 Hours) (Total Marks: 75: Interpretation, Depending On How It Is Carried Out
C supports several different types of data, each of which may be represented differently
within the computer’s
memory. The basic data types are listed below. Typical memory requirements are also
given. (The memory
requirements for each data type will determine the permissible range of values for that
data type. Note that the
memory requirements for each data type may vary from one C compiler to another.)
Built in
i n t integer quantity 2 bytes or one word (varies fromone compiler to another)
char single character 1 byte
f l o a t floating-point number (i.e., a number containing 1 word (4 bytes) a decimal point
andor an exponent)
double double-precision floating-point number (i.e., more 2 words (8 bytes)significant
figures, and an exponent which maybe larger in magnitude)
Derived types
They include
(a) Pointer types,
(b) Array types,
(c) Structure types,
(d) Union types and
(e) Function types.
The array types and structure types are referred collectively as the aggregate types. The
type of a function specifies the type of the function's return value.
A given variable can be assigned different data items at various places within the program.
Thus, the information represented by the variable can change during the execution of the
program. However, the data type associated with the variable cannot change.
A declaration associates a group of variables with a specific data type. All variables must be
declared before they can appear in executable statements. A declaration consists of a data
type, followed by one or more variable names, ending with a semicolon.
in t a, b, c ;
f loat rootl , root2;
char flag , text [80] ;
f. Determine if the following identifiers are valid in C
i) record1 --- valid
ii) $tax ---- invalid
iii) 123-45-6789 ---- invalid
iv) address and name ---- invalid
v) file_3 --- valid
The increment and decrement operators can each be utilized two different ways,
depending on whether the operator is written before or after the operand. If the operator
precedes the operand (e.g., ++i), then the operand will be altered in value before it is
utilized for its intended purpose within the program. If, however, the operator follows the
operand (e.g., i++), then the value of the operand will be altered after it is utilized.
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = 0.0, result;
scanf(“%lf”,&x);
result = sqrt(sqrt(x));
printf("The square root of %lf is %lfn", x, result);
return 0;
}
where control string refers to a string containing certain required formatting information,
and argl,arg2, . . . argn are arguments that represent the individual input data items.
The control string consists of individual groups of characters, with one character group for
each input data item. Each character group must begin with a percent sign (%). In its
simplest form, a single character group will consist of the percent sign, followed by a
conversion character which indicates the type of the corresponding data item.Within the
control string, multiple character groups can be contiguous, or they can be separated by
whitespace characters (i.e., blank spaces, tabs or newline characters). If whitespace
characters are used to separate multiple character groups in the control string, then all
consecutive whitespace characters in the input data will be read but ignored. The use of
blank spaces as character-group separators is very common
Example
char item(201;
i n t partno;
f l o a t cost;
.....
scanf("%s %d %f",item, Bpartno, &cost);
.....
f. Explain : ? , += and %= in and expression in C.
:? THE CONDITIONALOPERATOR
Simple conditional operations can be carried out with the conditional operator (7 :). An
expression that makes use of the conditional operator is called a conditional expression.
Such an expression can be written in place of the more traditional if -else statement
A conditional expression is written in the form
expression 1 ? expression 2 : expression 3
When evaluating a conditional expression, expression 1 is evaluated first. If expression 1 is
true (i.e., if its value is nonzero), then expression 2 is evaluated and this becomes the value
of the conditional expression. However, if expression 1 is false (i.e., if its value is zero), then
expression 3is evaluated and this becomes the value of the conditional expression. Note
that only one of the embedded expressions (either expression 2 or expression 3) is
evaluated when determining the value of a conditional expression.
EXAMPLE
In the conditional expression shown below, assume that i is an integer variable.
(i < 0) ? 0 : 100
The expression (i < 0) is evaluated first. If it is true (i.e., if the value of i is less than 0), the
entire conditional
expression takes on the value 0. Otherwise (if the value of i is not less than 0),the entire
conditional expression takes on the value 100.
+= The assignment expression
expression 1 += expression 2
is equivalent to
expression 1 = expression 1 + expression 2
expression 1;
while (expression 2) {
statement
expression 3;
}
The looping action will continue as long as the value of expression 2 is not zero, that is, as
long as the logical condition represented by expression 2 is true.
The f o r statement, like the while and the do - while statements, can be used to carry out
looping actions where the number of passes through the loop is not known in advance.
Because of the features that are built into the f o r statement, however, it is particularly
well suited for loops in which the number of passes is known in advance. As a rough rule of
thumb, while loops are generally used when the number of passes is not known in advance,
and f o r loops are generally used when the number of passes is known in advance
b. Write a program in C to generate the Fibonacci series 0, 1, 1, 2, 3, 4, …… , n terms using a
while loop.
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
printf("Enter the number range:");
scanf("%d",&r);
return 0;
}
It is possible to nest (i.e., embed) if - else statements, one within another. There are several
different forms that nested if - else statements can take. The most general form of two-
layer nesting is
if e1 if e2 s1
else s2
else if e3 s3
else s4
where e I, e2 and e3 represent logical expressions and s 1, s2,s3and s4 represent
statements. Now, one complete if - else statement will be executed if e1 is nonzero (true),
and another complete if – else statement will be executed if e 1 is zero (false). It is, of
course, possible that s1, s2,s3and s4 will contain other if - else statements. We would then
have multilayer nesting.
In general terms, a function declaration can be written as Function prototypes are usually
written at the beginning of a program, ahead of any programmer-defined
functions (including main). The general form of a function prototype is
data-type name( type 1 arg 1 , type 2 arg 2, . . ., type n arg n ) ;
where data- type represents the data type of the item that is returned by the function,
name represents the
function name, and type 1, type 2, . . . , type n represent the data types of the arguments
arg 1 , arg 2,. . . , arg n. Notice that a function prototype resembles the first line of a
function definition (though a function prototype ends with a semicolon).
The names of the arguments within the function prototype need not be declared elsewhere
in the program,since these are "dummy" argument names that are recognized only within
the prototype. In fact, the argumentnames can be omitted (though it is not a good idea to
do so);however, the argument data types are essential
A function definition has two principal components: the first line (including the argument
declarations), and the body of the function.
The first line of a function definition contains the type specification of the value returned by
the function, followed by the function name, and (optionally) a set of arguments, separated
by commas and enclosed in parentheses. Each argument is preceded by its associated type
declaration. An empty pair of parentheses must follow the function name if the function
definition does not include any arguments.
In general terms, the first line can be written as
data- type name( type 1 arg 7, type 2 arg 2, . . ., type n arg n)
where data - type represents the data type of the item that is returned by the function,
name represents the function name, and type I,type 2, . . . , type n represent the data types
of the arguments arg I , arg 2,. . . , arg n.
The arguments are called formal arguments, because they represent the names of data
items that are transferred into the function from the calling portion of the program. They
are also known as parameters or formal parameters. The remainder of the function
definition is a compound statement that defines the action to be taken by the function. This
compound statement is sometimes referred to as the boafy of the function. Like any other
compound statement, this statement can contain expression statements, other compound
statements, control statements, and so on. It should include one or more return
statements, in order to return a value to the
calling portion of the program.
.
e. Write a function fact( ) in C to find the factorial of a number and use it to generate factorial
of numbers from 1 to 10.
#include <stdio.h>
#include <conio.h>
int fact(int);
void main()
{
int i,x;
for (int i=0; i<=10 ; i++)
{
x=fact(n);
printf("\nfactorial of %d is %d ",i,x);
getch();
}
int fact(int z)
{int fac=1;
for(;z!=0;z--)
{ fac*=z ;
}
return(fac);
}
If data is passed by value, the data is copied from the variable used in for example
main() to a variable used by the function. So if the data passed (that is stored in the
function variable) is modified inside the function, the value is only changed in the
variable used inside the function.
#include <stdio.h>
void call_by_value(int x) {
printf("Inside call_by_value x = %d before adding 10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n", x);
}
int main() {
int a=10;
The output of this call by value code example will look like this:
In the main() we create a integer that has the value of 10. We print some information
at every stage, beginning by printing our variable a. Then function call_by_value is called
and we input the variable a. This variable (a) is then copied to the function variable x. In
the function we add 10 to x (and also call some print statements). Then when the next
statement is called in main() the value of variable a is printed. We can see that the value
of variable a isn’t changed by the call of the function call_by_value().
Call by Reference
In this method the reference variables of the actual arguments are created as formal
arguments. Reference variables are aliases of the original variables and refer to the same
memory locations as the original variables do. Since the formal arguments are aliases of the
actual arguments, if we do any changes in the formal arguments they will affect the actual
arguments.
#include <stdio.h>
int main() {
int a=10;
Call by address
In this method the addresses of the actual arguments are copied to the formal arguments.
Since the formal arguments point to the actual arguments as they contain addresses of the
actual arguments, if we do any changes in the formal arguments they will affect the actual
arguments. The formal arguments will be pointers because we have to store addresses in
them
#include <stdio.h>
int main() {
int a=10;
printf("a = %d before function call_by_value.\n", a);
call_by_value(&a);
printf("a = %d after function call_by_value.\n", a);
return 0;
}
int values[3][4] = {
{1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
d Write a program in C to find the sum of 20 double values entered by the user.
#include <stdio.h>
int main()
{
int c;
double sum = 0, array[20];
printf("Sum = %d\n",sum);
return 0;
}
The following declaration and initialization create a string consisting of the word "Hello".
To hold the null character at the end of the array, the size of the character array containing
the string is one more than the number of characters in the word "Hello."
Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '\0' at the end of the string when it initializes the array
iii ) strcmp( ) --- The strcmp function accepts two strings as arguments and returns an
integer value, depending upon the relative order of the two strings, as follows:
1. A negative value if the first string precedes the second string alphabetically.
2. A value of zero if the first string and the second string are identical (disregarding case).
3. A positive value if the second string precedes the first string alphabetically.
Therefore, if strcmp( s t r i n g l , string2) returns a positive value, it would indicate that s t r
i n g 2 must be moved, placing it ahead of s t r i n g l in order to alphabetize the two strings
properly.
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000. Assuming 32-bit integers, let us perform the following
arithmetic operation on the pointer −
ptr++
After the above operation, the ptr will point to the location 1004 because each time ptr is
incremented, it will point to the next integer location which is 4 bytes next to the current
location. This operation will move the pointer to the next memory location without
impacting the actual value at the memory location. If ptr points to a character whose
address is 1000, then the above operation will point to the location 1001 because the next
character will be available at 1001.
Similarly ptr --
ptr decrements 4 bytes in case of interges and one byte in case of characters
Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2
point to variables that are related to each other, such as elements of the same array, then
p1 and p2 can be meaningfully compared.
c. Explain the terms “array of pointers” and “pointer to an array” in C.
ARRAYS OF POINTERS
A multidimensional array can be expressed in terms of an array of pointers rather than a
pointer to a group of contiguous arrays. In such situations the newly defined array will have
one less dimension than the original multidimensional array. Each pointer will indicate the
beginning of a separate (n- 1)-dimensional array.In general terms, a two-dimensional array
can be defined as a one-dimensional array of pointers bywriting
data - type *array[ expression 1 ) ;
rather than the conventional array definition,
data- type array[ expression ] [ expression 2];
Similarly, an n-dimensional array can be defined as an (n - 1)-dimensional array of pointers
by writing
data- type *array[ expression1] [ expression 2) . . . [ expression n- 1] ;
rather than
data- type array[ expression I ] [ expression 2] . . . [ expression n] ;
In these declarations data- type refers to the data type of the original n-dimensional array,
array is the array name, and expression I , expression 2, . . ., expression n are positive-
valued integer
expressions that indicate the maximum number of elements associated with each subscript.
int * x [ l O ] ;
Hence, x[ 01 points to the beginning of the first row, x[ 1 ] points to the beginning of the
second row, and so on. Note that the number of elements within each row is not explicitly
specified.
X[0]
X[1]
*(x[1]+2 ) X[1] +2
Pointer to a array :
An array name is a constant pointer to the first element of the array. Therefore, in the
declaration −
double balance[50];
balance is a pointer to &balance[0], which is the address of the first element of the array
balance. Thus, the following program fragment assigns p as the address of the first
element of balance −
double *p;
double balance[10];
p = balance;
It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4)
is a legitimate way of accessing the data at balance[4].
structure union
Keyword struct defines a structure. Keyword union defines a union.
Example structure declaration: Example union declaration:
Within a structure all members gets memory For a union compiler allocates the memory
allocated and members have addresses that for the largest of all members and in a union
increase as the declarators are read left-to- all members have offset zero from the base,
right. That is, the members of a structure all the container is big enough to hold the
begin at different offsets from the base of WIDEST member, and the alignment is
the structure. The offset of a particular appropriate for all of the types in the union.
member corresponds to the order of its
When the storage space allocated to the
declaration; the first member is at offset 0.
union contains a smaller member, the extra
The total size of a structure is the sum of the
space between the end of the smaller
size of all the members or more because of
member and the end of the allocated
appropriate alignment.
memory remains unaltered.
Within a structure all members gets memory While retrieving data from a union the type
allocated; therefore any member can be that is being retrieved must be the type
retrieved at any time. most recently stored. It is the programmer's
responsibility to keep track of which type is
currently stored in a union; the results are
implementation-dependent if something is
stored as one type and extracted as another.
1. Array elements are accessed using the Subscript variable , Similarly Structure
#include<stdio.h>
struct Vehicle
{
int wheels;
char vname[20];
char color[10];
}v1 = {4,"Nano","Red"};
int main()
{
printf("Vehicle No of Wheels : %d",v1.wheels);
printf("Vehicle Name : %s",v1.vname);
printf("Vehicle Color : %s",v1.color);
return(0);
}
More complex expressions involving the repeated use of the period operator may also be
written. For
example, if a structure member is itself a structure, then a member of the embedded
structure can be accessedby writing
variable. member. submember
where member refers to the name of the member within the outer structure, and
submember refers to the name of the member within the embedded structure. Similarly, if
a structure member is an array, then an individual array element can be accessed by writing
variable. member[ expression] where expression is a nonnegative value that indicates the
array element.
_____________________________