include

INTRODDUCTION TO C LANGUAGE
2016-2017
#include
This is a pre-processor directive. It is not a part of the
program it is an instruction to the compiler to make some
thing. It tells the C compiler to include the comments of a file
,in this case the system file stdio.h.The compiler knows it is a
system file, and therefore must be locked for a special
place,by the fact that the name is enclosed in<>characters.
<Stdio.h>
This is the name of standard library definition file for all
standard input.Your program will almost certainly want to
send stuff to the screen and read things from the key board.
stdio.h is the name of the file in which the functions that we
wantto use are defined. A function is simply a chunk of
program that we want to use a lot, so we stuck it in a parcel
and gave it a name. the function we want to use is called
printf. To use printf correctly C needs to know what it looks
like ie what things it can work on and what value it returns.
The actual code which performs the printf will be tied in later
by the lincker. Note that without the definition of what printf
looks like the compiler makes a guess when it common
cause of program crashing. The<> characters around the
name tell c to look in the system area for the file stdio.h. If I
had given the name "saad.h" insteadS it would tell the
compiler to look in the current directory. This means that I
can setup libraries of my own routines and use them in my
program, a very useful feature
1
void
Means literally this means nothing ,in this case it referring to
the function whose name follows. It tells C That this function,
which could return some thing interesting, in fact returns
nothing of interest whatsoever,
Character Escape Sequences
Enclosing character literals in single quotes works for most printing
characters, but a few characters, such as the carriage return, pose a
special problem when a text editor is used. In addition, certain other
characters, such as the single and double quotes,have special meaning in
C, so you cannot use them directly. For these reasons, C provides the
character escape sequences shown in Table no. 1
Table no (1) character escape sequence
code
\b
\f
\n
\r
\t
\”
\’
\\
\v
\a
\?
A\N
\xN
meaning
Back space
Form feed
New line
Carriahge return
Horizontal tab
Double quote
Single quote character
Back slash
Vertical tab
alert
?
octal constant(where N is an octal constant)
hexa decimal constant(where N is hexadecimal constant)
2
The following sample program illustrates the use of backslash codes.
When this program is run, it outputs a newline, a backslash, and a
backspace.
#include <stdio.h>
#include<conio.h>
int main()
{
Printf(“\n\\\b”);
getch();
return 0;
}
Variable Initializations
You can assign a value to a variable at the same time that it is declared
by placing an equal sign and the value after the variable name. The
general form of initialization is:
type variable name = value;
Some examples are:
char ch = 'a';
int first = 0;
3
float balance = 123.23;
Although variables are frequently initialized by constants, you can
initialize a variable by using any expression valid at the time of the
initialization. As you will see, initialization plays an important role when
you are working with objects. Global variables are initialized only at the
start of the program. Local variables are initialized each time the
function in which they are declared is entered. All global variables are
initialized to zero if no other initializer is specified. Local variables that
are not initialized will have unknown values before the first assignment
is made to them. Here is a simple example of variable initialization. This
program uses the total( )function to compute the summation of the value
that it is passed. In other words, total( ) sums the digits from 1 to the
value. For example, the summation of 3 is 1 + 2 + 3, or 6. In the process,
total( ) displays a running total. Notice the use of the sum variable in
total( ).
Data Types and Sizes
There are only a few basic data types in C:
char a single byte, capable of holding one character in the local character set
int an integer, typically reflecting the natural size of integers on the host
machine
float single-precision floating point
double double-precision floating point
In addition, there are a number of qualifiers that can be applied to these basic
types. short and long.
short int sh;
long int counter;
The word int can be omitted in such declarations, and typically it is.
The intent is that short and long should provide different lengths of
integers where practical; int will normally be the natural size for a
particular machine. short is often 16 bits long, and int either 16 or 32
bits. Each compiler is free to choose appropriate sizes for its own hardware,
subject only to the restriction that shorts and ints are at least 16 bits, longs
are at least 32 bits, and short is no longer than int, which is no longer than
long. The qualifier signed or unsigned may be applied to char or any
integer. unsigned numbers are
4
always positive or zero, and obey the laws of arithmetic modulo 2n, where n is
the number of bits in the type. So, for instance, if chars are 8 bits, unsigned
char variables have values between 0 and 255,while signed chars have
values between -128 and 127 (in a two's complement machine.) Whether plain
chars are signed or unsigned is machine-dependent, but printable characters
are always positive. The type long double specifies extended-precision
floating point. As with integers, the sizes of floating-point objects are
implementation-defined; float, double and long double could
represent one, two or three distinct sizes.
Table(2)data sizes
Type
Bit Width
Common Range
Char
8
-128 to 127
unsigned char
8
0 to 255
Signed char
8
-128 to 127
Int
32
-2,147,483,648to 2,147,483,647
unsigned int
32
0 to 4,294,967,295
signed int
32
-2,147,483to2,147,483,647
short int
16
-32,768to 32,767
unsigned short int
16
0 to 65,535
signed short int
16
-32.768 to 32,767
long int
32
Same as int
unsigned long int
32
Same as unsigned int
Signed long int
32
Same as signed int
Float
32
3.4E-38to 3.4E+38
Double
64
1.7E-308 to 1.7E+308
long double
80
3.4E-4932 to 1.1E+4932
Bool
True or false
Wchar-t
16
0 to 65,535
Declaration of Variables
The general form of a variable declaration statement is shown
here:type variable list;
Here, type must be a valid C data type, and variable_list may consist
of one or more identifier names separated by commas. Some
declarations are shown here, for example:
int i,j,k;
char ch, chr;
float f,balance;
5
double d;
Signed int index=4198;
Signed int temperature=-32;
Unsigned int count=0;
int balance = -67
int normal=1000;
Short int smallvalue = 100;long int bigvalue =10000;
float Celsius =37.623;
double Fahrenheit =98.415;
In C, the name of a variable has nothing to do with its
type.Standard C states that at least the first 1,024 characters of any
identifier name (including variable names) will be significant. This
means that if two variable names differ in at least one character
within the first 1,024 characters, then the compiler will consider
them to be different names.
There are three places where variables will be declared: inside
functions, in the definition of function parameters, and outside of all
functions. These variables are called local variables, formal
parameters, and global variables, respectively. Although we will
examine the importance of these three different types of variables in
greater detail later in this book, let’s take a brief look at them
now.All variables must be declared before use, although certain declarations
can be made implicitly by content. A declaration specifies a type, and contains
a list of one or more variables of that type, as in
int lower, upper, step;
char c,line[1000];
Variables can be distributed among declarations in any fashion; the lists
above could well be written as
int lower;
int upper;
int step;
char c;
char line[1000];
A variable may also be initialized in its declaration. If the name is followed by
an equals sign and an expression, the expression serves as an initializer, as in
6
char esc = '\\';
int i =0;
int limit= MAXLINE+1;
float eps = 1.0e-5;
Operators
C is rich in built-in operators. An operator is a symbol that tells the
compiler to perform specific mathematical or logical manipulations.
C has three general classes of operators: arithmetic, relational and
logical, and bitwise. In addition, C has some special operators for
particular tasks. This chapter will examine the arithmetic,
relational, and logical operators, reserving the more advanced
bitwise operators for later. The reason the last line prints a 0 and 1
is because 1/2 in integer division is 0, with a remainder of 1. Thus,
1%2 yields the remainder 1.The unary minus, in effect, multiplies its
single operand by –1. That is, any number preceded by a minus sign
switches its sign.
Arithmetic Operators
Table(3) arithmetic operator
Operator
Action
subtraction, also unary
minus
+
addition
*
multiplication
/
division
%
modulus
-decrement
++
increment
The operators +, –, *, and / all work the same way in C as they do in
any other computer language (or algebra, for that matter). These
can be applied to any built-in data type allowed by C. When /is
applied to an integer or a character, any remainder will be
truncated; for example,10/3 will equal 3 in integer division. The
7
modulus operator % also works in C in the same way that it does in
other languages. Remember that the modulus operation yields the
remainder of an integer division. This means that the%cannot be
used on type float or double.
The following program illustrates its use:
#include <stdio.h>
#include <conio.h>
main()
{
int x, y;
x = 10;
y = 3;
printf("(x/y)=%d",(x/y));// will display 3
printf("\n");
printf("(x%y)=%d",x%y);/* will display 1,the
remainder of the integer division */
printf("\n");
x =1;
y =2;
printf("%d
%d",(x/y),(x%y));//will display 0 1
getch();
return 0;
}
8
Relational and Logical operators
In the terms relational operator and logical operator, relational refers
to the relationships that values can have with one another, and
logical refers to the ways in which true and false values can be
connected together. Since the relational operators produce true or
false results, they often work with the logical operators. For this
reason, these operators will be discussed together here.The
relational and logical operators are shown in Table 3-5. Notice that
in C, not equal is represented by != and equal is represented by the
double equal sign, ==. In Standard C, the outcome of a relational or
logical expression produces a bool result.
That is, the outcome of a relational or logical expression is either
true or false. For older compilers, the outcome of a relational or
logical expression will be an integer value of either 0 or 1. This
difference is mostly academic, though, because C automatically
9
converts true into 1 and false into 0, and vice versa.The operands for
a relational operator can be of nearly any type, as long as they can
be compared. The operands to the logical operators must produce a
true or false result.
Because any non-zero value is true and zero is false, the logical
operators can be used with any expression that evaluates to a zero
or non-zero result.
Relational Operators
Table(4)Relational operators
operator
>
>=
<
<=
==
!=
meaning
Greater than
Greater than or equal to
Less than
Less than or equal to
Equal to
Not equal to
Table(5)Logical operators
operator
&&
||
!
meaning
AND
OR
NOT
Increment and Decrement Operators
C has two operators not found in some other computer languages.
These are the increment and decrement operators, ++ and – –.,
when the for loop was introduced. The ++ operator adds 1 to its
operand, and – – subtracts 1. Therefore,
x = x+1;
10
is the same as ++x;and x=x-1;is the same as --both the
increment and decrement operators can either precede (prefix) or
follow (postfix) the operand. For example: x=x+1;
can be written as ++x;//prefix form or as x++;
//
postfix form In the foregoing example, there is no difference
whether the increment is applied as a prefix or a postfix. However,
when an increment or decrement is used as part of a larger
expression, there is an important difference. When an increment or
decrement operator precedes its operand, C will perform the
corresponding operation prior to obtaining the operand’s value for
use by the rest of the expression. If the operator follows its operand,
then C will obtain the operand’s value before incrementing or
decrementing it. C provides two unusual operators for incrementing and
decrementing variables. The increment operator ++ adds 1 to its operand,
while the decrement operator -- subtracts 1. We have frequently used ++ to
increment variables. The unusual aspect is that ++ and -- may be used
either as prefix operators (before the variable, as in ++n), or postfix operators
(after the variable: n++). In both cases, the effect is to increment n. But the
expression ++n increments n before its value is used, while n++ increments
n after its value has been used. This means that in a context where the value
is being used, not just the effect, ++n and n++ are different. If n is 5, then
x=n++; sets x to 5, but x=++n;
sets x to 6. In both cases, n becomes 6. The increment and decrement
operators can only be applied to variables; an expression like (i+j)++ is
illegal. In a context where no value is wanted, just the incrementing effect.
Bitwise Operators
C provides six operators for bit manipulation; these may only be applied to
integral operands, that is,
char, short, int, and long, whether signed or unsigned.
11
Table(6)Bitwise operators
operator
&
|
^
<<
>>
~
meaning
bitwise AND
bitwise inclusive OR
Bitwise exclusive XOR
Left shift
Right shift
One,s complement(unary)
Table(7)Format Specifiers Table
Specifier
%c
%d
%o
%x
%u
%ld
%f
%lf
%e
%s
type
Character
Decimal integer
Octal integer(leading 0)
Hexa decimal integer(leading 0x)
Unsigned decimal integer
Long integer
Floating point
Double or long double
Exponential floating point
Character string
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y;
x=150;
printf("x= %d \n",x);
y=x>>2;
printf("y= %d \n",y);
getch();
return 0;
}
12
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y;
x=150;
printf("x= %d \n",x);
y=x<<2;
printf("y= %d \n",y);
getch();
return 0;
}
13
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y;
x=26;
y=9;
printf("x&y = %d \n",(x&y);
getch();
return 0;
}
14
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y;
x=10;
y=9;
printf("x or y =%d \n",(x|y));
getch();
return 0;
}
15
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y;
x=10;
y=9;
printf("XOR=%d \n",(x^y));
getch();
return 0;
}
16
The ? Operator
One of C most fascinating operators is the ?. The ? operator can be
used to replace if-else statements of this general form:
if (condition)
var = expression1;
else
var = expression2;
Here, the value assigned to var depends upon the outcome of the
condition controlling the if.
The ? is called a ternary operator because it requires three operands.
It takes the general form
Exp1 ? Exp2 : Exp3;
where Exp1, Exp2, and Exp3 are expressions. Notice the use and
placement of the colon. The value of a ? expression is determined
like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and
becomes the value of the entire ? expression. If Exp1 is false, then
Exp3 is evaluated and its value becomes the value of the expression.
17
while(something)
{
x=count>0?0:1;
// ...
}
Here, x will be assigned the value 0 until count is less than or equal
to 0. The same code written using an if-else statement would look
like this:
while(something) {
if(count>0)x=0;
else x = 1;
// ...
}
Here’s an example of the ? operator in action. This program divides
two numbers, but will not allow a division by zero.
/* This program uses the ? operator to prevent a
division by zero. */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,result;
printf("Enter the dividend\n ");
scanf("%d",&i);
printf("Enter the divisor\n ");
scanf("%d",&j);
// This statement prevents a divide by zero error.
j!=0?printf("%d",(i/j)):printf(" can not divide by zero \n");
getch();
return 0;
}
18
Here, if j is non-zero, then i is divided by j, and the outcome is the
integer division of (i/j). Otherwise, the program, will print the
sentence cannot divide by zero.
/Use ? to write C program to find the value of
the integer value Z if you know that
A
if A<B
B
for other values
Z=
#include<stdio.h>
#include<conio.h>
main()
{
int A,B,Z;
printf("Give the value of A\n");
scanf("%d",&A);
19
printf("Give the value of B\n");
scanf("%d",&B);
Z=(A>B)?A:B;
printf("The value of Z= %d",Z);
getch();
return 0;
}
Use ? operator to find the value of z
8A-23
A>B
Z=
4*B -20
other values
#include<stdio.h>
#include<conio.h>
main)(
{
int A,B,Z;
printf("Give the value of A\n");
scanf("%d",&A);
20
printf("Give the value of B\n");
scanf("%d",&B);
Z=(A>B)?8*A-23:4*B-20;
printf(" The value of Z= %d",Z);
getch)(;
return 0;
}
Use ? operator to find the maximum number
between 2 numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,z;
printf("Enter first number \n ");
scanf("%d",&a);
printf("Enter the second number \n ");
scanf("%d",&b);
z=(a>b)?a:b;//max(a,b)
21
printf("The max number=%d \n",z);
getch();
return 0;
}
Compound Assignment
C has a special shorthand called compound assignment that
combines assignment with another operation. For example,
x=x+10;
can be written using a compound assignment as
x+=10;
The operator pair += tells the compiler to assign to x the value of x
plus 10. As this example illustrates, compound assignment simplifies
the coding of a certain type of assignment statement. Depending
upon the compiler, it may also produce more efficient code.
Compound assignment operators exist for all of the binary
operators in C (that is, those that require two operands). Thus, for
the binary operators, any assignment that has this general form
var = var op expression;
can be rewritten as
22
var op = expression;
Here is another example:
x=x- 100;
is the same as
x-=100;
You will see compound assignment used widely in professionally
written C programs, so you should become familiar with it.
Assignment Operators and Expressions
An expression such as
i=i+2
in which the variable on the left side is repeated immediately on the
right, can be written in the compressed form
i+= 2
The operator += is called an assignment operator.
Most binary operators (operators like + that have a left and right
operand) have a corresponding assignment operator op=, where op
is one of
+ - * / % << >> & ^ |
If expr1 and expr2 are expressions, then
expr1 op= expr2
is equivalent to
expr1 = (expr1) op (expr2)
except that expr1 is computed only once. Notice the parentheses
around expr2:
x*=y+1
means
x=x*(y+1)
Assignment operatorsِ
x+=2 ;
x=x+2;
y*=(5+x);
y=y*(5+x);
b/=(3+y);
b=b/(3+y);
23
The Comma Operator
Another interesting C operator is the comma. You have seen some
examples of the comma operator in the for loop, where it has been
used to allow multiple initialization or incrimination statements.
However, the comma can be used as a part of any expression. Its
purpose is to string together several expressions. The value of a
comma-separated list of expressions is the value of the right-most
expression. The values of the other expressions will be discarded.
This means that the expression on the right side will become the
value of the entire comma-separated expression. For example,
var=(count=19,incr=10,count+1);
first assigns count the value 19, assigns incr the value 10, then adds
1 to count, and finally, assigns var the value of the rightmost
expression, count+1, which is 20. The parentheses are necessary
because the comma operator has a lower precedence than the
assignment operator. To see the effects of the comma operator, try
running the following two programs:
#include<stdio.h>
#include<conio.h>
main()
{
int var,count,incr;
var=(count=19,incr=10,count+1);
printf("%d",var);
getch();
return 0;
}
24
#include <stdio.h>
#include<conio.h>
int main()
{
int i,j;
j =10;
i=(j++,j+100,999+j);
printf(“%d”,i);
getch();
return 0;
}
This program prints 1010 on the screen. Here is why: j starts with
the value 10. j is then incremented to 11. Next, j is added to 100.
Finally, j (still containing 11) is added to 999, which yields the result
1010.
25
Essentially, the comma’s effect is to cause a sequence of operations
to be performed. When it is used on the right side of an assignment
statement, the value assigned is the value of the last expression in the
comma-separated list. You can, in some ways, think of the comma
operator as having the same meaning that the word "and" has in
English when used in the phrase "do this and this and this."
Multiple Assignments
C allows a very convenient method of assigning many variables the
same value: using multiple assignments in a single statement. For
example, this fragment assigns count, incr, and index the value 10:
count=incr=index=10;
In professionally written programs, you will often see variables
assigned a common value using this format.
#include<stdio.h>
#include<conio.h>
main()
26
{
int count,incr,index;
count=incr=index=10;
printf(" count= %d \n incr= %d \n
index=%d",count,incr,index);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y,z;
x=y=z=0;
x=++y + ++z;
printf(" The value of x= %d",x);
27
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y,z;
x=y=z=0;
x=y++ + ++z;
printf(" The value of x= %d",x);
getch();
return 0;
}
28
Using sizeof
Sometimes it is helpful to know the size, in bytes, of a type of data.
Since the sizes of C built-in types can differ between computing
environments, knowing the size of a variable in all situations can be
difficult. To solve this problem, C includes the size of compile-time
operator, which has these general forms:
sizeof (type) sizeof value
The first version returns the size of the specified data type, and the
second returns the size of the specified value. As you can see, if you
want to know the size of a data type,such as int, you must enclose
the type name in parentheses. If you want to know the size of a
value, no parentheses are needed, although you can use them if you
want. To see how sizeof works, try the following short program. For
many 32-bit environments, it displays the values 1, 4, 4, and 8.
// Demonstrate sizeof().
#include<stdio.h>
#include<conio.h>
int main()
29
{
printf("\n
printf("\n
printf("\n
printf("\n
sizeof
sizeof
sizeof
sizeof
character =%d",sizeof(char)); // size of char
integer =%d",sizeof(int)); // size of integer
float =%d",sizeof(float)); // size of float
double =%d",sizeof(double)); // size of char
getch();
return 0;
}
As mentioned earlier, sizeof is a compile-time operator. All
information necessary for computing the size of a variable or data
type is known during compilation. You may apply sizeof to any data
type. For xample, when it is applied to an array, it returns the
number of bytes used by the array. Consider this fragment:
#include<stdio.h>
#include<conio.h>
main()
{
int nums[4];
printf(" %d",sizeof(nums));// displays 8
30
getch();
return 0;
}
Assuming 4-byte integers, this fragment displays the value 8 (i.e., 2
bytes times 4 elements).sizeof primarily helps you write code that
depends upon the size of the C data types. Remember, since the sizes
of types in C are defined by the implementation, it is bad style to
make assumptions about their sizes in code that you write.
AND, OR, XOR, and NOT
The bitwise AND, OR, and one’s complement (NOT) are governed by the
same truth table as their logical equivalents, except that they work on a bitby-bit level. The exclusive OR (XOR) operates according to the following
truth table:
31
Truth Table(8)& OR NOT
p
0
0
1
1
q
0
1
1
0
p AND q
0
0
1
0
p OR q
0
1
1
1
NOT p
1
1
0
0
As the table indicates, the outcome of an XOR is true only if exactly one of the
operands is true; it is false otherwise. In terms of its most common usage, you
can think of the bitwise AND as a way to turn bits off. That is, any bit that is 0
in either operand will cause the corresponding bit in the outcome to be set to
0. The logical operators are used to support the basic logical
operations AND, OR, and NOT, according to the following truth
table. The table uses 1 for true and 0 for false. Although C does not
contain a built-in exclusive-OR (XOR) logical operator, it is easy to
construct one. The XOR operation uses this truth table: In words,
the XOR operation produces a true result when one, and only one,
operand is true. The following function uses the && and || operators
to construct an XOR operation. The result is returned by the
function.
Table(9)XOR
p
0
0
1
1
q
0
1
0
1
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int A(4),B(16),Z(0),H;
32
xor
0
1
1
0
Z=A+B;
H=A*B;
printf(" The value of Z= %d\n",Z);
printf(" The value of H= %d",H);
getch();
return 0;
}
Conditional Expressions
The statements
if(a > b)
z=a;
else
z=b
compute in z the maximum of a and b. The conditional expression,
written with the ternary operator ``?:'', provides an alternate way
to write this and similar constructions. In the expression
expr1 ? expr2 : expr3
the expression expr1 is evaluated first. If it is non-zero (true), then
the expression expr2 is evaluated, and that is the value of the
conditional expression. Otherwise expr3 is evaluated, and that is the
33
value. Only one of expr2 and expr3 is evaluated. Thus to set z to the
maximum of a and b,
z=(a>b)?a:b;/* z = max(a, b) */
It should be noted that the conditional expression is indeed an
expression, and it can be used wherever any other expression can be.
If expr2 and expr3 are of different types, the type of the result is
determined by the conversion rulesr. For example, if f is a float
and n an int,then the expression
(n>0)?f:n
is of type float regardless of whether n is positive. Parentheses
are not necessary around the first expression of a conditional
expression, since the precedence of ?:is very low, just above
assignment. They are advisable anyway, however, since they make
the condition part of the expression easier to see. Conditional
operator(?)evaluates an expressions returning a value if that
expression true and a different one if the expression is evaluated is
false.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
float f,z;
int N;
printf("Give the value of f \n");
scanf("%f",&f);
printf("Give the value of N \n");
scanf("%d",&N);
z=(N>0)?f:N;
printf("z= %f \n",z);
getch();
return 0;
}
34
Casting
We can force C to regard a value as being of certain type by the use
of casting A cast takes the form of additional instruction to the
compiler to force it regard a value in a particular way. You cast a
value by putting the type you want to see there in brackets before it .
For example
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int i=3,j=2;
float fraction;
fraction=float(i)/float(j);
printf("fraction = %f\n",fraction);
getch();
35
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
float i=60.53,j=20.8;
float fraction;
fraction=int(i)/int(j);
printf("fraction = %f\n",fraction);
getch();
return 0;
}
36
The (float)part of the above program tells the compiler to regard the
values in the integer variables as a floating point ones, so that we get
1.5 printed out rather than 1.When you cast, which you need to
occasionally, remember that casting does not affect the actual value,
just how C regards the number. AS we saw above, each type of
variable has a particular range of possible values, and the range of
floating point is much greater than that for integers. This means
that if you do things like:
int i;
i=(int)12345678.999;
The cast is doomed to fail. The value which gets placed in I will be
invalid Nowhere in C does the language check for mistakes like this.
It is up to you when write your program to make sure that you
never exceed the range of data you are using-the program will not
notice but the user certainly will!
#include<stdio.h>
#include<conio.h>
main()
37
{
clrscr();
float A=654.876;
printf(" integer of A= %d",int(A));
getch();
return 0;
}
example to print numeric value
#include<stdio.h>
#include<conio.h>
main( )
{
clrscr( );
printf("%d",100);
getch( );
return 0;
}
38
#include<stdio.h<
#include<conio.h<
main()
{
clrscr) (;
int a=100;
float pie=3.14 ;
printf(" a= % d \n pie= % f ",a,pie(;
getch();
return 0;
}
39
Input strings by using gets function & printing
a strings by using puts function
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char A[20];
printf("Give an string \n");
gets(A);
printf("The entered string is \n");
puts(A);
getch();
return 0;
}
40
write a program to find the area of triangle with base d and height h
//a program to find the area of triangle
#include<stdio.h>
#include<conio.h>
main) (
{
clrscr)(;
float b,h,area ;
printf("enter the value of the base b\n ");
scanf("%f",&b);
printf("enter the value of the height h\n ");
scanf("%f",&h);
area=0.5*b*h ;
printf("\n the area of triangle=%f",area);
getch) (;
41
return 0;
}
// division
#include<stdio.h>
#include<conio.h>
main( )
{
clrscr( );
float a=25.0;
float b=2.0;
float z;
z=a/b;
printf(" The value of z=%f\n",z);
getch( );
return 0;
}
42
#include<stdio.h<
#include<conio.h<
main) (
{
clrscr)(;
float x;
printf("give a floating point number \n");
scanf("%f",&x);
printf("the value of x is equal to %f",x);
getch)(;
return 0;
}
43
#include<stdio.h>
#include<conio.h>
main()
{
int x1;
int x2;
printf("Give 2 integers \n");
scanf("%d%d",&x1,&x2);//input 2 integers
printf(" The value of x1= %d \n",x1);
printf(" The value of x2= %d \n",x2);
printf(" The value of (x1+x2) is equal to
getch();
return 0;
}
44
\n",(x1+x2));
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,z;
x=10;
y=30;
printf(" The value of x=
printf(" The value of y=
z=2*x+7*y;
printf(" The value of z=
getch();
return 0;
}
45
%d\n",x);
%d\n",y);
%d\n",z);
//program summation of 2 integers
#include<stdio.h>
#include<conio.h>
main()
{
int x1,x2,sum;
printf("Enter the first integer\n");
scanf("%d",&x1);
printf("Enter the second integer\n");
scanf("%d",&x2);
sum=x1+x2;
printf(" The summation of the 2 integers= %d",sum);
getch();
return 0;
}
46
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,z;
x=10;
y=30;
printf(" The value of x= %d\n",x);
printf(" The value of y= %d\n",y);
z=2*x+7*y;
printf(" The value of z= %d\n",z);
getch();
return 0;
}
47
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf(" Give single character \n");
scanf("%c",&ch);
printf("The entered letter is %c ",ch);
getch();
return 0;
}
48
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y,z;
x=y=z=0;
x=++y + ++z;
printf(" The value of x=
printf(" The value of y=
getch();
return 0;
}
49
%d\n",x);
%d\n",y);
Write a program to compute the slope & the
length of the line which joins the points
(x1,y1),(x2,y2)on plane. If you know that the
slope of the line
=
y 2  y1
x 2  x1
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float x1,x2,y1,y2;
float slope,length;
printf("give the value of x1,x2,y1,y2 \n");
scanf("%f%f%f%f",&x1,&x2,&y1,&y2);
slope=(y2-y1)/(x2-x1);
50
length=sqrt(pow((x2-x1),2)+pow((y2-y1),2));
printf("The slope of the line%f \n",slope);
printf("The length of the line= %f",length);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
main)(
{
clrscr));
int c;
c=5;
printf("%d\n",c);
printf("%d\n",c++)
51
printf("%d\n",c);
printf("\n");
c=10;
printf("%d\n",c);
printf("%d\n",++c);
printf("%d\n",c);
getch();
return 0;
}
52
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x,y,z;
x=y=z=0;
x=y++ + z++;
printf(" The value of x=
printf(" The value of y=
getch();
return 0;
}
53
%d\n",x);
%d\n",y);
54