Beruflich Dokumente
Kultur Dokumente
: 200503256
Name : Ravinder Singh Yadav
Batch : 2005
Course : Post Graduate Diploma in Information Technology
--------------------------------------------------------------------------------
-----------
b) C datatypes
Ans:
In Standard C there are four basic data types. They are int, char, float
, and double.
The int type
The int type stores integers in the form of "whole numbers". An
integer is typically the size of one machine word, wh
ich on most modern home PCs is 32 bits (4 octets). Examples of literals are whol
e numbers (integers) such as 1,2,3, 10, 100... Whe
n int is 32 bits (4 octets), it can store any whole
number (integer) between -2147483648 and 2147483647. A 32 bit word (number) has
the possibility of representing 4294967296 numbers
(2 to the power of 32).
If you want to declare a new int variable, use the int keyword.
For example:
int numberOfStudents, i, j=5;
In this declaration we declare 3 variables, numberOfStudents, i
& j, j here is assigned the literal 5.
The char type
The char type is similar to the int type, yet it is only big eno
ugh to hold one ASCII character. It stores the same
kind of data as an int (i.e. integers), but always has a size of one byte. It is
most often used to store character data, hence its na
me.
Examples of character literals are 'a', 'b', '1', etc., as well
as some special characters such as '\0' (the null c
haracter) and '\n' (endline, recall "Hello, World"). Note that the char value mu
st be enclosed within single quotations.
The reason why one byte is seen as the ideal size for character
data is that one byte is large enough to provide one s
lot for each member of the ASCII character set, which is a set of characters whi
ch maps one-to-one with a set of integers. At compi
le time, all character literals are converted into
their corresponding integer. For example, 'A' will be converted to 65 (0x41).(Kn
owing about the ASCII character set is often useful.)
When we initialize a character variable, we can do it two ways.
One is preferred, the other way is bad programming
practice.
The first way is to write
char letter1='a';
This is good programming practice in that it allows a person rea
ding your code to understand that letter is being
initialized with the letter 'a' to start off with.
The second way, which should not be used when you are coding let
ter characters, is to write
char letter2=97; /* in ASCII, 97 = 'a' */
This is considered by some to be extremely bad practice, if we a
re using it to store a character, not a small number,
in that if someone reads your code, most readers are forced to look up what char
acter corresponds with the number 97 in the encoding s
cheme. In the end, letter1 and letter2 store both the
same thing -- the letter "a", but the first method is clearer, easier to debug,
and much more straightforward.
One important thing to mention is that characters for numerals a
re represented differently from their corresponding nu
mber, i.e. '1' is not equal to 1.
There is one more kind of literal that needs to be explained in
connection with chars: the string literal. A strin
g is a series of characters, usually intended to be output to the string. They a
re surrounded by double quotes (" ", not ' '). An e
xample of a string literal is the "Hello, world!\n"
in the "Hello, World" example.
The float type
float is short for Floating Point. It stores real numbers also,
but is only one machine word in size. Therefore, it is
used when less precision than a double provides is required. float literals mus
t be suffixed with F or f, otherwise they will be int
erpreted as doubles. Examples are: 3.1415926f, 4.0f,
6.022e+23f. float variables can be declared using the float keyword.
The double type
The double and float types are very similar. The float type allo
ws you to store single-precision floating point n
umbers,while the double keyword allows you to store double-precision floating po
int numbers - real numbers, in other words, both int
eger and non-integer values. Its size is typically
two machine words, or 8 bytes on most machines. Examples of double literals are
3.1415926535897932, 4.0,6.022e+23 (scientific notati
on). If you use 4 instead of 4.0, the 4 will be interpreted
as an int.
The distinction between floats and doubles was made because of t
he differing sizes of the two types. When C was first
used, space was at a minimum and so the judicious use of a float instead of a d
ouble saved some memory. Nowadays, with memory more fr
eely available, you do not really need to conserve
memory like this - it may be better to use doubles consistently. Indeed, some C
implementations use doubles instead of floats when y
ou declare a float variable.
If you want to use a double variable, use the double keyword.
Q. 4 Accept principal amount, rate of interest, and duration from the user. D
isplay Interest Amount and Total Amount (Principal + Interest).
Ans: /*Calculate interest amount and total amount*/
#include<stdio.h> /*includes statdard head
er files*/
#include<conio.h>
void main()
{
long int p,r,t; /*declare variables to b
e used*/
float interest,totamt;
clrscr();
printf("\nEnter principle amount: "); /*accept the desired val
ues from user*/
scanf("%ld",&p);
printf("\nEnter rate of interest: ");
scanf("%ld",&r);
printf("\nEnter time: ");
scanf("%ld",&t);
interest = (p*r*t)/100; /*calculate the interest
*/
totamt = p + interest; /*calculate total amount
*/
printf("\nInterest is: %f",interest); /*show the result to the
user*/
printf("\nTotal amount is: %f",totamt);
getch();
}
Q. 5 Accept the salary of an employee from the user. Calculate the gross sal
ary on the following basis:
Basic HRA DA
1 - 4000 10% 50%
4001 - 8000 20% 60%
8001 - 12000 25% 70%
12000 and above 30% 80%
Q. 6 Accept any number from the user. Display whether the number is divisibl
e by 100 or not.
Q. 7 Accept a month in digit from the user. Display the month in words. If n
umber is not between 1 and 12 display message Invalid Month . (Use switch )
Q. 12 Accept any two strings from the user. Display whether both the strings a
re equal or not. (do not use standard functions.)
Ans: /*Display whether two strings are equal or not*/
#include<stdio.h> /*include standard header files*/
#include<conio.h>
void main()
{
int i; /*declare required variables*/
int sflag=0;
char s1[50],s2[50];
clrscr();
printf("\nEnter first string: "); /*get the data from the use
r*/
gets(s1);
printf("Enter second string: ");
gets(s2);
sflag = stringcmp(s1,s2); /*call the defind function
for checking whether strings are equal or not*/
if(sflag!=0)
{
printf("\nTwo strings are not equal"); /* print the res
ult accordingly*/
}
else
{
printf("\nTwo strings are equal");
}
getch();
}
int stringcmp(char str1[50], char str2[50]) /*function stringcmp defi
nded here*/
{
int i,scntr=0,cntr1=0,cntr2=0; /*local variables for fun
ction*/
for(i=0;str1[i]!='\0';i++) /*find first string leng
th*/
{
cntr1++;
}
printf("\nFirst string length: %d",cntr1);
for(i=0;str2[i]!='\0';i++) /*find second string len
gth*/
{
cntr2++;
}
printf("\nSecond string length: %d\n",cntr2);
if(cntr1==cntr2) /* check whether both stri
ngs are of same length or not*/
{
for(i=0;i<cntr1;i++)
{
if(str1[i]!=str2[i]) /* check whether each cha
racter of both strings matches or not*/
scntr++;
}
}
else
{
scntr++;
}
return scntr; /* return the result*/
}
Q. 13 Accept any string from the user. Convert case of the string to lower / u
pper using pointers. (if entered string is in lower case convert it to upperca
se and vice versa.)
Ans: /*Convert case of the string using pointers*/
#include<stdio.h> /* include standard header files*/
#include<conio.h>
#include<ctype.h>
void main()
{
char str[50],*p; /*declare variables to be used*/
clrscr();
printf("\nEnter a string: "); /*get the data from the user
*/
gets(str);
p = str;
if(islower(str[0])) /*check the case of entered
string*/
{
printf("\nOriginal string is: %s",str);
while(*p)
{
*p = toupper(*p); /*change the cas
e of string*/
p++;
}
printf("\nChanged case is: %s",str); /*print changed
string*/
}
else
{
printf("\nOriginal string is: %s",str);
while(*p)
{
*p = tolower(*p); /*change the cas
e of string*/
p++;
}
printf("\nChanged case is: %s",str); /*print changed
string*/
}
getch();
}
Q. 14 Accept any two numbers from the user. Using pointers swap the values two
numbers without using third variable.