EXERCISE: 4 Basic C Language

C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the UNIX operating system.
Although C was designed for implementing system software; it is also widely used for developing portable application software. C is one of the most popular programming languages. It is widely used on many different software platforms, and there are few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which originally began as an extension to C.
As a programming language, C is rather like Pascal or FORTRAN. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.
Of the three languages, C allows the most precise control of input and output. C is also rather easier than FORTRAN or Pascal. This can result in short efficient programs, where the programmer has made wise use of C's range of powerful operators. It also allows the programmer to produce programs which are impossible to understand.
The American National Standards Institute defined a standard for C, eliminating much uncertainty about the exact syntax of the language. This newcomer, called ANSI C, proclaims itself the standard version of the language. As such it will inevitably overtake, and eventually replace common C.
ANSI C does incorporate a few improvements over the old common C. The main difference is in the grammar of the language. The form of function declarations has been changed making them rather more like Pascal procedures.
Let’s do some simple problems on C language.
1. Which of the following are invalid variable names and why?

(a) BASICSALARY
(b) _basic
(c) basic-hra
(d) #MEAN
(e) group.
(f) 422
(g) population in 2006
(h) mindovermatter
(i) FLOAT
(j) hELLO
(k) team’svictory
(l) Plot # 3
(m) 2015_DDay

Ans: Option c, d, e, g, k, l are invalid variable because -, #, period, space, ‘, are not allow in variables, in case of option f & m variable can’t start with number and in case of option I, it is key word.

2. Point out the errors, if any, in the following C statements:

(a) int = 314.562 * 150 ;
Ans: 314.562 is a float not integer.
(b) name = ‘Ajay’ ;
(c) varchar = ‘3’ ;
(d) 3.14 * r * r * h = vol_of_cyl ;
Ans: Variable should be left side: vol_of_cyl = 3.14 * r * r * h;
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
Ans: k = ( a * b ) ( 2.5*a + b )+c *( d + e );
(f) m_inst = rate of interest * amount in rs ;
Ans: m_inst = rate_of_ interest * amount_in_rs ;
(g) si = principal * rateofinterest * numberofyears / 100 ;
(h) area = 3.14 * r ** 2 ;
Ans: 3.14 * r *r;
(i) a = b = 3 = 4 ;
Ans: Not possible


3. What would be the output of the following programs:

(a) main( )
{
int i = 2, j = 3, k, l ;
float a, b ;
k = i / j * j ;
l = j / i * i ;
a = i / j * j ;
b = j / i * i ;
printf( "%d %d %f %f", k, l, a, b ) ;
}

Output: 0 2 0.000000 2.000000

(b) main( )
{
float a = 5, b = 2 ;
int c ;
c = a % b ;
printf ( "%d", c ) ;
}
Rectified: The program has an error. Correct is given bellow:
main( )
{
int a = 5, b = 2 ;
int c;
c = a % b ;
printf ( "%d", c ) ;
}
Output: 1

(c) main( )
{
int a, b ;
printf ( "Enter values of a and b" ) ;
scanf ( " %d %d, ", &a, &b ) ;
printf ( "a = %d b = %d", a, b ) ;
}
Output: Enter values of a and b 5
6
a = 5 b = 6

(d) main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
}
Output:
nn

nn
nn /n/n nn/n

4. Pick up the correct alternative for each of the following questions:

(a) C language has been developed by
(1) Ken Thompson
(2) Dennis Ritchie
(3) Peter Norton
(4) Martin Richards
Ans: (2)

(b) C can be used on
(1) Only MS-DOS operating system
(2) Only Linux operating system
(3) Only Windows operating system
(4) All the above
Ans: (4)

(c) C programs are converted into machine language with the help of
(1) An Editor
(2) A compiler
(3) An operating system
(4) None of the above
Ans: (4)

(e) The statement char ch = ‘Z’ would store in
(5) The character Z
(6) ASCII value of Z
(7) Z along with the single inverted commas
(8) Both (1) and (2)
Ans: (6)

(d) The maximum value that an integer constant can have is
(1) -32767
(2) 32767
(3) 1.7014e+38
(4) –1.7014e+38
Ans: (2)

(e) A C variable cannot start with
(1) An alphabet
(2) A number
(3) A special symbol other than underscore
(4) Both (2) & (3) above
Ans: (4)

(f) In b = 6.6 / a + 2 * n ; which operation will be performed first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
Ans: (1)

(f) If a is an integer variable, a = 5 / 2 ; will return a value
(1) 2.5
(2) 3
(3) 2
(4) 0
Ans: (3)

(g) The expression, a = 30 * 1000 + 2768 ; evaluates to
(1) 32768
(2) -32768
(3) 113040
(4) 0
Ans: (1)

(h) The expression x = 4 + 2 % - 8 evaluates to
(1) -6
(2) 6
(3) 4
(4) None of the above
Ans: (2)

(i) In C a variable cannot contain
(1) Blank spaces
(2) Hyphen
(3) Decimal point
(4) All the above
Ans: (4)

(j) Which of the following is FALSE in C
(1) Keywords can be used as variable names
(2) Variable names can contain a digit
(3) Variable names do not contain a blank space
(4) Capital letters can be used in variable names
Ans: (1)

5. Write C programs for the following:

(a) The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.

Ans:
#include
#include
void main( )
{
clrscr();
float k, m, f, i, c;
printf("Enter distance between two cities (in km)\n");
scanf("%f",&k);
printf("distance in km %f km\n",k) ;
m=k*1000;
c=m*100;
f=c*0.0328084;
i=c*0.3937008;
printf("distance in meters %f m\n",m) ;
printf("distance in feet %f feet\n",f) ;
printf("distance in centimeters %f cm\n",c) ;
printf("distance in inch %f''\n",i) ;
getch();
}
Input: 12.4 km.
Output:
distance in km 12.400000 km
distance in meters 12400.000000 m
distance in feet 40682.414062 feet
distance in centimeters 1240000.000000 cm
distance in inch 488189.000000''

(b) Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

Ans:
#include
#include
void main( )
{
clrscr();
float f, c;
printf("Enter temperature in Fahrenheit \n");
scanf("%f",&k);
c=5*(f-32)/9;
printf("temperature in Centigrade %f km\n",k) ;
getch();
}

Input:
Enter temperature in Fahrenheit
-40
Output:
temperature in Centigrade -40.000000

(c) Two numbers are input through the keyboard into two locations a and b. Write a program to interchange the contents of a and b.

Ans:
#include
#include
void main( )
{
int a, b, c, d;
clrscr();
printf ( "Enter values of a and b\n\n" ) ;
scanf ( " %d %d,", &a, &b ) ;
printf ( "\na= %d b= %d\n",a,b) ;
c=a;
d=b;
a=d;
b=c;
printf ( "a= %d b= %d ",a,b) ;
getch();
}
Input: 2 and 8
Output:
a= 2 b= 8
a= 8 b= 2

Followers