Вы находитесь на странице: 1из 12

Assignment No.

: 02

1. What are the rules used in naming variable? Give


examples.

Answer:

Within limits, you can give your variables and functions any names you
want. These names (the formal term is “identifiers”) consist of letters,
numbers, and underscores. For our purposes, names must begin with
a letter. Theoretically, names can be as long as you want, but
extremely long ones get tedious to type after a while, and the compiler
is not required to keep track of extremely long ones perfectly. (What
this means is that you were to name a variable, say,
supercalafrgalisticespialidocious, the compiler might get lazy and
pretend that you’d named it supercalafragalisticespialidocio, such that
if you later misspelled it supercalafragalisticespialidociouz, the
compiler wouldn’t catch your mistake. Nor would the compiler
necessarily be able to tell the difference if for some perverse reason
you deliberately declared a second variable named
supercalafragalisticespialidociouz.)

The capitalization of names in C is significant: the variable names


variable, Variable, and VARIABLE (as well as combinations like
variAble) are all distinct.

A final restriction on names is that you may not use keywords (the
words such as int and for which are part of the syntax of the
language) as the names of variables or functions (or as identifiers of
any kind).

1
Assignment No.: 02

2. What are the precedence of operators? How expressions


are evaluated using precedences?

Answer:

The precedence of C operators dictates the order of evaluation within


an expression. The highest precedence operators are given first.

Operators Associativity
()->. left to right
!~+-++--&* right to left
*/% left to right
+- left to right
<<=>>= left to right
==!= left to right
& left to right
| left to right
&& left to right
|| right to left
=*=/=%=+=-= right to left

Where same operator appears twice (for example *) the first one is
the unary version.

2
Assignment No.: 02

3. What are the commonly used input / output functions in


C? How they are accessed.

Answer:

Commonly used input functions:

fgets Get a string from a file.


fread Read blocks from a file.
getc Get a character from a file.
getchar Get a character from stdin.
gets Get a string from stdin.
fscanf Read formatted input from a file.
scanf Read formatted input from stdin.
sprintf Write formatted output to a string.

Now, here are the output functions:

fprintf Write formatted output to a file.


fputc Write a character to a file.
fputs Write a string to a file.
fwrite Write blocks to a file.
printf Write formatted output to stdout.
putc Write a character to a file.
putchar Write a character to stdout.
puts Write a string to stdout.
sscanf Read formatted input from a string.

Finally, here are the file manipulation and file status query functions:

fclose Close a file.


feof End-of-file check.
feror Error check.
fflush Flush output buffer.
fopen Open a file.

3
Assignment No.: 02

4. Explain the different types of if statements with


examples.

Answer:
If-Else
The if-else statement is used to express decisions. Formally the syntax
is
if (expression)
statement1
else
statement2
where the else part is optional. The expression is evaluated; if it is true
(that is, if expression has a nonzero
value), statement1 is executed. If it is false (expression is zero) and if
there is an else part,
statement2 is executed instead.
Since an if tests the numeric value of an expression, certain coding
shortcuts are possible. The most
obvious is writing
if (expression)
instead of
if (expression != 0)
Sometimes this is natural and clear; at other times it can be cryptic.
Because the else part of an if-else is optional,there is an ambiguity
when an else if omitted from a
nested if sequence. This is resolved by associating the else with the
closest previous else-less if.
For example, in
if (n > 0)
if (a > b)
z = a;
else
z = b;
the else goes to the inner if, as we have shown by indentation. If that
isn't what you want, braces must
be used to force the proper association:
if (n > 0) {
if (a > b)
z = a;
}
else

4
z = b;
The ambiguity is especially pernicious in situations like this:
if (n > 0)
for (i = 0; i < n; i++)
if (s[i] > 0) {
printf("...");
return i;
}
else /* WRONG */
printf("error -- n is negative\n");
The indentation shows unequivocally what you want, but the compiler
doesn't get the message, and
associates the else with the inner if. This kind of bug can be hard to
find; it's a good idea to use braces
when there are nested ifs.
By the way, notice that there is a semicolon after z = a in
if (a > b)
z = a;
else
z = b;
This is because grammatically, a statement follows the if, and an
expression statement like ``z = a;''
is always terminated by a semicolon.
Else-If
The construction
if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else if (expression)
statement
else
statement
occurs so often that it is worth a brief separate discussion. This
sequence of if statements is the most
general way of writing a multi-way decision. The expressions are
evaluated in order; if an expression is
true, the statement associated with it is executed, and this terminates
the whole chain. As always, the
code for each statement is either a single statement, or a group of
them in braces.

5
The last else part handles the ``none of the above'' or default case
where none of the other conditions is
satisfied. Sometimes there is no explicit action for the default; in that
case the trailing
else
statement
can be omitted, or it may be used for error checking to catch an
``impossible'' condition.
To illustrate a three-way decision, here is a binary search function that
decides if a particular value x
occurs in the sorted array v. The elements of v must be in increasing
order. The function returns the
position (a number between 0 and n-1) if x occurs in v, and -1 if not.
Binary search first compares the input value x to the middle element
of the array v. If x is less than the
middle value, searching focuses on the lower half of the table,
otherwise on the upper half. In either case,
the next step is to compare x to the middle element of the selected
half. This process of dividing the
range in two continues until the value is found or the range is empty.
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low+high)/2;
if (x < v[mid])
high = mid + 1;
else if (x > v[mid])
low = mid + 1;
else /* found match */
return mid;
}
return -1; /* no match */
}
The fundamental decision is whether x is less than, greater than, or
equal to the middle element v[mid]
at each step; this is a natural for else-if.

6
Assignment No.: 02

5. Write a program to check whether a given number is


palindrome.

Answer:

/*To check whether a string is palindrome/not without using string


header file*/
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,f=0;
char a[10];
clrscr ();
gets(a);
for (i=0;a[i]!='\0';i++)
{
}
i--;
for (j=0;a[j]!='\0';j++,i--)
{
if (a[i]!=a[j])
f=1;
}
if (f==0)
printf("string is palindrome");
else
printf("string is not palindrome");
getch ();
}

7
Assignment No.: 02

6. Write recursive function to find sum of even numbers


from 2 to 50.

Answer:

8
Assignment No.: 02

7. Write a program to add two 3x3 matrices.

Answer:

9
Assignment No.: 02

8. Write a program to count the number of vowels and


consonants in a given string.

Answer:

/*To count the number of vowels and consonants in a given string*/


#include <iostream.h>
#include <conio.h>
void main()
{
char string[50];
int vowel=0,consonant=0;
cout<<"Enter the string";
cin.getline(string,50);
for(int i=0;string[i]!='\0';i++)
{
switch (string[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':vowel++;
continue;
}
if (string[i]!=' ')
consonant++;
}
cout<<"No of vowels="<<vowel<<"\nNo of
consonants="<<consonant;
getch();
}

10
Assignment No.: 02

9. What is a pointer? What are the advantages of pointer?

Answer:

A pointer is a variable that points at, or refers to another variable.

That is if we have a pointer variable of type ``pointer to int,`` it

might point to the int variable i, or to any one of the locations at the

int array a.

Advantages of Pointer:

a. Function cannot return more than one value. But when the same

function can modify many pointer variables and function as if it

is returning more than one variable.

b. In the case of arrays, we can decide the size of the array at

runtime by allocating the necessary space.

11
Assignment No.: 02

10. Explain how unions are different from structure.

Answer:

Unions look similar to structures. They have identical declaration


syntax and member access, but they serve a very different purpose.

union Utype
{
int ival;
float fval;
char *sval;
};
union type x,y,z

Accessing members of a union is via “.” member operator or, for


pointers to unions, the -> operator.

A union holds the value of one – variable at a time. The compiler


allocates storage for the biggest member of the union. The type
retrieved from the union must be the type most recently stored.
Otherwise, the result is implementation dependent.

union Utype x;
x.fval = 56.4 /*x holds type float.*/
printf(“%f\n”, x.fval); /*OK.*/
printf(“%d\n”, x.fval); /*Implementation dependent.*/

Unions are used to store one of a set of different types. These are
commonly used to implement a “variant” array. (This is a form of
generic programming). There are other uses also, but they are quiet
advanced (e.g., concern the alignment properties of unions).

12

Вам также может понравиться