Chapter-1-Basic of C Language
Chapter-1-Basic of C Language
History of C
- Where C stands
- C Character set ,Tokens ,Constant ,Variables and Keywords
- C operators(Arithmetic , Logical , Relational, Assignment, Increment
and Decrement ,Conditional , Bitwise ,Special Operator ,Precedence ,
C expression , Data Types ,
- Formatted Input and Output
Introduction
C is a programming language is most popular computer language
today because it is a structured high level machine independent
language. Programmers need not worry about the hardware platform
where they will be implemented
There are 32 key words and its strength lies in its built in
functions. Several standard functions are available which can be used for
development of the programs.
Definitions
Compiler is software program, which converts high-level language
program into m/c language programs. Compilers are compact & they
generate object program that are very small & highly efficient After
compiling the program is linked and the final product is Executable
Object code i.e. Exe file. Hence when C program is written, compiled and
linked the different files generated are .C, .Bak, .Obj and .Exe.
About C language
C is a programming language was originally developed in 1972 by
Dennis. Ritchie at AT&T Bell Labs USA. C is developed from the
successor BCPL (Basic Combined Programming Language) also called as
B language. Developed in the 1960‘s at Cambridge University B was
modified by Dennis Ritchie and was implemented at Bell Laboratories in
1972.
History of C language
By 1960 a board of computer languages had come into existence,
almost each for a specific purpose. For example, COBOL was being used
for Commercial Applications, FORTRAN for Engineering and Scientific
Applications and so on. At this stage people started thinking that instead
of learning and using so many languages, each for a different purpose,
why not use only one language which can program all possible
applications.
Therefore, an international committee was set up to develop such a
language. This committee came out with a language called ALGOL 60.
However, ALGOL 60 never really became popular because it seemed too
abstract, too general. To reduce this abstractness and generality, a new
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 2
language called Combined Programming Language (CPL) was developed
at Cambridge University. CPL was an attempt to bring ALGOL 60 down
to earth. However, CPL turned out to be so big, having so many features,
that it was hard to learn and difficult to implement.
C 's compactness and coherence is mainly due to the fact that it's
a one-man language. Other examples of one-man languages are LISP,
PASCAL and APL. Counter examples include many headed monsters
like PL/l, ALGOL 60 and ADA. Figure shows the various stages in
evolution of C language.
Programmers who are familiar with the use of pointers (or indirect
addressing, to use the correct term) will welcome the ease of use
compared with some other languages. Undisciplined use of pointers can
lead to errors which are very hard to trace. This course only deals with
the simplest applications of pointers.
Common C
Until recently there was one dominant form of the C language. This
was the native UNIX form, which for historical reasons is known as
either Bell Labs C, after the most popular compiler, or K. &R. C, after the
authors of the most popular textbook on the language. It is now often
called "Classic C"
ANSI C
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.
3. Compound statement
which comprises remainder of function. Arguments are symbols that
represents information being passed between function & other part of
program (argue are also referred to as parameters)
Structure of C program
Documentation Section
Link Section
Definition Section
Global declaration Section
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 6
main() function section
{
Local Declaration Section
Executable Section
}
sub-program Section
function1
{
Statements
}
function2
{
Statements
}
function3
{
Statements
}
Documentation Section
The documentation section consists of a set of comment lines
giving the name of the program, the author and other details such as a
short description of the purpose of the program.
Link Section
The link section provides instructions to the compiler to link
functions from the system library.
Definition Section
The definition section defines all the symbolic constants. The
variables can be declared inside the main function or before the main
function.
Global Section
Declaring the variables before the main function makes the
variables accessible to all the functions in a C language program, such
variables are called Global Variables.
Local Section
Declaring the variables within main function makes the usage of
the variables confined to the main function only and it is not accessible
outside the main function.
Main Function
Declaration Part
In the declaration part we have all the variables.
Executable Part
There is at least one statement in the executable part.
The two parts must appear between the opening and closing braces
Executing a C Program
The following basic steps is carried out in executing a C Program.
Type the C language program.
Store the program by giving a suitable name and following it with
an extension .c
Compile the program
Debug the errors if any, that is displayed during compile.
Run the program.
With in main()
Token in C language
The smallest individual units in a program are known as ―Tokens‖.
There are different types of tokens: -
1. Keywords
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 10
2. Identifiers
3. Constants
4. Strings
5. Operators
6. Literals
C Character Set
A character denotes any alphabet, digit or special symbols used to
represent information .The character that can be used to form words
numbers and expressions depending upon the computer which the
program is executed. The character set of C are grouped into following
categories:
1. Letter (Alphabets )
2. Digits
3. Special Symbols
4. White Space
Identifiers
Identifiers are names given to the various program elements such
as variable function arrays, identifiers consist of letters & digits.
First character must be a letter both upper case & lower case letters are
allowed. Underscore character can be included & it is considered as to be
a letter.
Both uppercase and lowercase letters are permitted. The
underscore character is also permitted in identifiers.
C Keywords
Keywords are the words whose meaning is already known to the C
compiler .All the key words has fixed meaning and these meaning cannot
be changed. The keywords cannot be used as variable names. The
keywords are also called as Reserved Words. All the keywords must be
written in lower case. Only 32 keywords are available in C. Following
table shows the list of keywords. Programmer cannot use these keywords
as variable. Every word in C language is a keyword or an identifier
Integer(int)( %d)
These are whole numbers, both positive and negative. Unsigned
integers (positive values only) are supported. In addition, there are short
and long integers.
The keyword used to define integers is,
int
An example of an integer value is 32. An example of declaring an integer
variable called sum is,
int sum;
sum = 20;
Floating point(float)(%f)
The data type float stores a single precision floating point (real)
number.These are numbers which contain fractional parts, both positive
and negative. The keyword used to define float variables is,
float
float money;
money = 0.12;
Double(double)(%Lf)
The data type double can store floating-point value with a greater
exactness than a float. Hence it requires twice as much a storage space
as float does.These are exponential numbers, both positive and negative.
The keyword used to define double variables is,
double
double big;
big = 312E+7;
Character(char)(%c)
The data type char is used to store any character belonging to
char letter;
letter = 'A';
Modifiers
The modifiers define the amount of storage allocated to the
variable. The amount of storage allocated is not cast in stone. To
override the default nature of a data type, C has provided us with data
type modifiers.
Some basic data types can be argumented by using data types,
modifiers, those modifiers are
1. Short
2. Long
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 14
These are further classified as
1. signed
2. unsigned, etc.
tells the C compiler that the integer variable x can only assume positive
values from 0 to 65535 (that is, 216_1), if the int data type is 16 bits long.
In fact, unsigned int is equivalent to unsigned according to the
ANSI standard. In other words,
Also, the ANSI standard allows you to indicate that a constant is of type
unsigned by suffixing u or U to the constant. For instance,
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 15
unsigned int x, y;
x = 12345U;
y = 0xABCDu;
Declaration
short x;
unsigned short y;
By default, a short int data type is a signed number. Therefore, in the
short x; statement, x is a signed variable of short integer.
long int x, y;
x = 123456789l;
y = 0xABCD1234L;
long x;
long int x;
Summary
Example: - integer modifier can be defined as
Short int,
Long int,
Unsigned short int
Unsigned long
1. Short int require less memory (2 bytes) than an ordinary int or
may take same as an int, if short int and int both have same
memory requirement.
2. Then long int will have double the requirement (4 byte) or if int and
long int have same memory requirement (2 bytes) such
specification will vary from one c compiler to another.
3. Unsigned int requires 2 bytes in ordinary int, leftmost bit indicate
sign of number but in unsigned int all bits are used to represent
numeric values. Int varies from -32768 to +32767 i.e (2-byte int) &
unsigned int varies from 0 to 65535. Unsigned modifier can be
applied to other qualified int. We declare long and unsigned integer
to increase the range of value.
Qualifier
A type qualifier is used to refine the declaration of a variable, a
function, and parameters, by specifying whether:
The value of an object can be changed
The value of an object must always be read from memory rather
than from a register
More than one pointer can access a modifiable memory address
You can put more than one qualifier on a declaration: the compiler
ignores duplicate type qualifiers
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 17
Type qualifiers give one of two properties to an identifier. The
const type qualifier declares an object to be nonmodifiable. The volatile
type qualifier declares an item whose value can legitimately be changed
by something beyond the control of the program in which it appears,
such as a concurrently executing thread.
The two type qualifiers, const and volatile, can appear only once
in a declaration. Type qualifiers can appear with any type specifier;
however, they cannot appear after the first comma in a multiple item
declaration. For example, the following declarations are legal:
typedef volatile int VI;
const int ci;
Syntax
type-qualifier:
constvolatile
Declaration
Declaration associates a group of variables with a specific data
type all variables must be declared before they can appear in executable
statement.
Declaration consist of a data type followed by one or more variable
names ending with a semi-colon each array variable, must be followed by
a pair of square bracket containing a +ve which specifies the size of the
array.
Example: int a, b, c, z[10];
Expression
An expression represents a single data item. The expression may
consist of single entity such as constant, a variable, an array element or
reference to a function. It may consist of combination of such entities
inter connected by one or more operators
Example: - c = a + b here value of the expression a + b is assigned to the
variable c.
Statements
Statement causes the computer to carry out some action. There
are 3 different classes of statements in C. They are
a} Expression statement
b} Compound statement
c} Control statement.
Constants
C has basic four types of constants. They are
1} Integer constant,
2} Floating point Constant ,
3} Character constant and
4} String constant.
Integer and Floating point constant represents numbers. They are
referred to as numeric type constant.
Examples
Valid decimal constants are - 0, 1, 99, -124, +454 etc.
Invalid decimal constants are (12,12), 36.0, $45.45, 20,000 and
09,etc.
Example
Valid octal integer are: -0, 01, 0777, etc.
Invalid octal integer constants are: - 743,058,077.77.
Example
Valid hexadecimal integer constants are: - 0xabcd, etc.
Invalid hexadecimal integer constants are: - 0x12.34, 0bcd, etc.
Example
0xffffful - hexadecimal unsigned long.
0x50000u - hexadecimal unsigned
0123456l - octal long
50000u - decimal unsigned.
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 21
Rules for constructing integer constant
Character constant
It is a single character enclosed in apostrophes (single quotation
marks) i.e pair of single quotes (' ')
Constant value
'A' 65
'a' 97
'3' 57
'0' 48
Escape Sequence
Escape sequence certain non printing characters as well as (" "),
Apostrophes, ?, !, can be expressed in terms of escape sequence always
begin when backward slash and is followed by one or more special
characters.
Example:- Line fed or new line can be represented as \n. Some escape
sequences are:-
Char Escape Sequence
Bell \a
Backspace \b
Hor. Tab \t
Ver. Tab \v
New line \n
Quotation mark \"
Apostrophe \'
Question mark \?
Back slash \\
Carriage Return \r
String constant
Symbolic constant
A symbolic constant is a name that substitutes for a sequence of
characters. The characters may represent a numeric constant, character
constant or string constant. A symbolic constant allows a name to
appear in place of a numeric constant, character constant or string.
When a program is compiled each occurrence of a symbolic constant are
replaced by its corresponding character sequence.
Symbolic constant are defined at the beginning of the program .The
symbolic constant may appear in the program in place of numeric
constant. A symbolic constant is defined by writing #define pi 3.14 then
the statement to find the area of circle will appear as
Format Specifier
Classification of operator
Unary Operators
Unary operators are operators that only deal with one argument (which
is generally a single variable). In C, there's only a few of these as shown
in Table 1:
Operator Use
! negation
++ increment by 1
-- decrement by 1
Example
c = i++,
y = --I ,
z = ctr-- ,
p= ++ctr
Binary Operators
Binary operators are operators that deal with two arguments, both
generally being either variables or constants. Table 2 shows some
examples of binary operators found in C
Operator Use
+ addition
- subtraction
* multiplication
/ division
% remainder division
= assignment
boolean equality
==
comparision
> boolean greater than
< boolean less than
&& boolean AND
|| boolean OR
Ternary Operators(?:)
Tertiary operators are operators that deal with three arguments which
can be anything from a constant to a complete boolean expression. The
ternary operator in C which is used to reduce if else statements to a
single line.
Example
Ch = ( ch > ‘A’ && ch <= ‘Z”) ? ch+32 : ch-32 ;
If ch is between ‗A‘ and ‗Z‘ ie capital letter ch will be stored with ‗a‘
and ‗z‘ respectively. Hence this single line will convert upper case
character to smaller case character.
Operators in C language
Operators are the symbols, which represent a particular operation
that can be performed on some particular data. The data itself can be
called as Operand. The operator operates on the operand.
C supports Rich set of operators. C has 45 different operators
depending on the function performed operators are classified as
1. Arithmetic operator
2. Increment and Decrement operator
3. Modulo Division Operator
4. Relational operator
5. Logical operator
6. Bitwise operator
7. Conditional operator
8. Assignment operator
9. Comma operator
10. Sizeof operator
11. Cast type operator
12. Address & Data type operator ( We will see this operators in details
in pointer chapter)
13. Arrow operator
14. Member or dot operator
if i=1 then
printf ("i= %d", i);
printf ("i= %d", ++i);
printf ("i=%d",i);
i=1
i=2
i=2
The first statement prints the original value of i. The second
statement first increments the value of i by 1 and then prints it's value.
The final value of i is printed by the last statement.
However the second statement instead ++i we write i++ then o/p
will be:-
i=1,i=1,1=2. This is because second statement uses initial value of
i and then increment i by 1 so third statement prints increased value of i.
i. Let a = 10
Y = a++
After the operation y = 10 and a = 11
ii. Let a = 40
Y = ++a
After the operation y = 41 and a = 41
iii. a++ or ++a are used alone then both has same effect.
Generally this operator are used for FOR, DO and WHILE Loops.
Relational operators are used with if, do, and While loops. The last
two operators are also called as equality operator.
All these 6 operators are used to form logical expressions,
representing conditions that are either true or false. True is represented
by integer value 1 and false is represented by value 0 in integer
expression.
Example
I=1,
J=2,
K=3.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical Not
These operators are used to test more than one condition and
make decision.
For Example
d>c && x == 45, AN expression of such type which combines two
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 30
or more relational expression is termed as logical expression or a
compound relational expression.
The result of logical and operation will be true when both the
operands are true the result of logical or operation will be true. If either
operand is true or if both the operands are true.
Example
if int i=7 and
float f=5.5 then
(i>=6)||(f<0) true 1.
if (i>=6)&&(f<0) false 0.
Conditional Operator (? , : )
The conditional operator are ? and : which also called as Ternary
operators because they take three operands. A conditional expression is
written in the form e
This indicates that AND operator has a precedence lower than that
of any arithmetic or relational but higher than that of logical OR
operator. Logical operators‘ associates from left to right except unary
logical not, that associates from right to left.
Thus the expression a || b && c is interpreted as a || ( b &&
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 32
c).since &&has higher precedence than logical OR.
The expression! a&&b and ! a|| b are interpreted as (!a)&&b and
(!a)||b respectively, due to left to right associativity of logical AND and
logical OR.
The expression a && b && c, a||b||c are interpreted as (a && b )
&& c , (a||b)||c respectively.
The expressions a<b && c%d , a-b||c=d are interpreted (a<b) &&
(c%d),(a-b)||(c=d) since logical AND and logical OR have lower
precedence than relational and arithmatic operators.
!a>=b && c/d, a*b||!c!=d are interpreted as (c!a)>=b) && (c/d) , (
a * b ) || ( c ! c ) ! = d)
Symbol operation
& Bitwise and
| Bitwise or
^ Bitwise ex-or
~ One's complement
<< Left shift
>> Right shift
e.g. if ABC and PQR are integers ABC = 18 and PQR = 32 then XYZ
= ABC & PQR will assign 2 to XYZ
Bitwise OR operator :-in this operation each bit i:e 1 in first value or in
second value will produce 1 in corresponding bit of result.
Example
A1=431 & B2=152 C3=563 in octal.
A1 = 100 011 001 431
B2 = 001 101 010 152
Bit Result(~)
1 0
0 1
Result 1 4 3 2 1
variable = expression;
For example
Number = 7; assigns the value 7 to the variable number Variable
name always appears to the left of the equal to sign. In statement c = a +
b ; the arithmetic expression a + b is evaluated and the result is stored
in the variable c. If two operands in assignment expression are of
different data types then value of right hand side will automatically
converted to the type of identifier on the left.
Example
i is an integer even if the expression is i = 8.1 or I = 8.4 then
value of 8 is assigned to the variable i.
let j = 7 , the expression I = 2*(j/2); will give result 6.
Example
i +=15 is equivalent to i=i+15;
i-=15 is equivalent to i=i-15;
i*=15 is equivalent to i=i*15;
Comma Operator ( , )
The comma operator can be used to link the related expression
together. The comma operator permits two different expressions to
appear in situation where only one expression would ordinarily be used.
The following program illustrate the use of the comma operator
main()
{
int a,b,c ;
c = ( a = 10 , b = 20 ,a+b)
printf(―%d‖,c);
}
Sizeof operator
The size of operator is a compile operator and when used with
operand , it returns the number of bytes occupied by operand. The
operand may be a variable, a constant or a data type qualifier.
Examples
Long double sum;
N = sizeof (float);
M = sizeof (sum);
P = sizeof (long int);
Q = sizeof (‗A‘);
Type Conversion
It is a process of converting the data type into another data type in
the process of programming .'c' permits converting variable of type into
another. Converting integer to floating point and vice versa . 'c' permits
integer values to be stored into variable that have been declared to be
floating point. The number is converted into floating point equivalent it's
value is not changed. 'C' also enables programmer to store floating point
into variables declared to be integer. In this case the value is changed
since number is truncated i:e it's fraction part is lost.
example
1)int a,c;
float b,d;
a = 3; /*assignment statement */
b = 987.654;
c = b; /*b has been stored in c*/
2) x = 5 * 6.7 ;
In this mixed mode arithmetic expression one operand is of type
integer and another of type float. The integer value is automatically
converted into type float this type of conversion is known as implicit
conversion (or arithmetic conversion) 'C' programmer also can specify
explicitly how the values are to be converted in mixed mode expression.
This feature is known as cast operator or type casting. The name of
the data type to which the conversion is to be made is enclosed in
parenthesis and placed directly to be left value to be converted.
Example
int a=7;
float b;
b=(float)a+10;
Output :-
125.125000 assignment to int produce 123
-150 assignment to float produce –150.000000
-150 divided by 100 to produce is –1.00000
-150 divided by 100.0 to produce is –1.50000
main()
{
int sum;
float money;
char letter;
double pi;
C Programming Expressions
Arithmetic Expressions
An expression is a combination of variables constants and
operators written according to the syntax of C language. In C every
expression evaluates to a value i.e., every expression results in some
value of a certain type that can be assigned to a variable. Some examples
of C expressions are shown in the table given below.
Evaluation of Expressions
Expressions are evaluated using an assignment statement of the form
Variable = expression;
x=a*b–c
y=b/c*a
z = a – b / c + d;
main ()
output
x = 10.00
y = 7.00
z = 4.00
High priority * / %
Low priority + -
Explicit Conversion
Many times there may arise a situation where we want to force a type
conversion in a way that is different from automatic conversion.
The operator float converts the female_students to floating point for the
purpose of evaluation of the expression. Then using the rule of automatic
conversion, the division is performed by floating point mode, thus
retaining the fractional part of the result. The process of such a local
conversion is known as explicit conversion or casting a value. The
general form is
(type_name) expression
The operators of same precedence are evaluated from right to left or from
left to right depending on the level. This is known as associativity
property of an operator.
8 Equality == Equal to L → R
!= Not Equal to
Format Codes
Code Meaning
%c Single Character
%d Decimal Integer
%e Floating Point
%f Floating Point
%g Floating Point
%h Short Integer
%i Decimal , Hexadecimal , Octal
%o Octal Integer
%s String
%u Unsigned integer
%x Hexadecimal
Prefix Letter
h for short integer
l for long integer or double
L for long double
Formatted IO Statement
Formatted input refers to an input data that has been arranged in
a particular format. For example, consider the following data:
%Wd
Printf(―%d‖,2902) 2 9 0 2
%w.p.f
%w.p.e
Padding the leading blanks with zeros and printing with left-justification
is also possible by introducing 0 or — before the held width specifier w.
The following examples illustrate the output of the number x = 29.0272
under different format specifications;
Format Output
printf(―%7.4f‖,x) 2 9 . 0 2 7 2
printf(―%7.2f‖,x) 2 9 . 0 2
printf(―%-7.2f‖,x) 2 9 . 0 2
printf(―%f‖,x) 2 9 . 0 2 7 2
printf(―%10.2e‖,x) 2 . 9 0 e + 0 1
Formatting Character
A single character can be displayed in a desired position using the format
%wc
Printing of Strings
The format specification for outputting strings is similar to that of real
numbers. It is of the form
%w.ps
where w specifies the field width for display and p instructs that only the
first p characters of the string are to be displayed. The display is right-
justified.
The following examples show the effect of a variety of specifications in
printing a string ―MSK Shahad 421001‖, containing 16 characters
(including blanks)
Format Output
0 1 2 3 4 5 6 8 9 0 1 2 3 4 5 6 7 8 9 0 1
%s M S K S H A H A D 4 2 1 0 0 1
%21s M S K S H A H A D 4 2 1 0 0 1
%21.10 M S K S H A H A D
%.5s M S K S
%-21.10s N E W S H A H A D
%5s N E W S H A H A D 4 2 1 0 0 1
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a,b,c;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
c = a+b; /* With Using Third Variable */
2.Enter two numbers, and Subtract second number from first. Show
result with or without using third variable.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a,b,c;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
c = a-b; /* With Using Third Variable */
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a,b,c,d;
float avg;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
printf ("Enter Third number :");
scanf ("%d",&c);
printf ("Enter Fourth number :");
scanf ("%d",&d);
avg = (float) (a + b + c + d)/ 4;
/* Type Casting is done here, because this operation will produce a
floating value. */
4.Enter two numbers, and Add them without using '+' operator.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a,b;
printf ("Enter first number :");
scanf ("%d",&a);
printf ("Enter second number :");
scanf ("%d",&b);
a = a -(-b);
printf ("\n\n\n\t\t\tAddition is %d",a);
getch ( );
}
# include <stdio.h>
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 53
# include <conio.h>
void main ( )
{
float a,r;
printf ("Enter the radius of circle:");
scanf ("%f",&r);
a = 3.142857 * r * r; /* Also, a = (float) 22 / 7 * r * r; */
printf ("Area of circle = %f",a);
getch ( );
}
6.Enter Length and Breadth of a Rectangle and find out it's Area.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int l,b,a;
printf ("\n\nEnter the Length of Rectangle :");
scanf ("%d",&l);
printf ("\n\nEnter the Bredth of Rectangle :");
scanf ("%d",&b);
a = l * b;
printf ("\n\n\t\tArea of Rectangle = %d",a);
getch ( );
}
# include <stdio.h>
# include <conio.h>
main ( )
{
float f,c;
clrscr ( );
printf ("\nEnter the Degree Celcius :");
scanf ("%f",&c);
f = (c * 5/ 9) +32;
printf ("\n\n\t\tDegree in Ferhenite is = %.2f",f);
getch ( );
}
# include <stdio.h>
# include <conio.h>
main ( )
{
int a,b;
clrscr ( );
printf ("\nEnter the first Number( A ) :");
scanf ("%d",&a);
printf ("\nEnter the Second Number( B ) :");
scanf ("%d",&b);
a = a + b;
b = a - b;
a = a - b;
printf ("\n\n\n\t\tNow the first Number( A ) is : %d",a);
printf ("\n\n\t\tNow the Second Number( B ) is : %d",b);
getch ( );
}
# include <stdio.h>
# include <conio.h>
main ( )
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 55
{
int a,b,c;
clrscr ( );
printf ("\nEnter the first Number( A ) :");
scanf ("%d",&a);
printf ("\nEnter the Second Number( B ) :");
scanf ("%d",&b);
a = a * b; /* also , a = a - (-b); */
b = a / b; /* also , b = a - b; */
a = a / b; /* also , a = a - b; */
# include <stdio.h>
# include <conio.h>
void main ( )
{
float s,in;
int ta,da;
printf ("\n\nEnter the salary in Rs. :");
scanf ("%f",&s);
printf ("\nEnter T.A. in Rs. :");
scanf ("%d",&ta);
printf ("\nEnter D.A. in Rs. :");
scanf ("%d",&da);
printf ("\nEnter the increment (in %):");
scanf ("%f",&in);
s + = ta + da + (s /100) * in;
printf ("\n\n\t\tSalary after increment is Rs %.2f",s);
getch ( );
}
# include <stdio.h>
# include <conio.h>
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 56
void main ( )
{
int h,e,m,tot;
float per;
printf ("SUBJECT");
printf ("\t M.M.");
printf ("\t M.O.");
printf ("\n\nHindi");
printf ("\t 100 \t");
scanf ("%d",&h);
printf ("\nEnglish");
printf ("\t 100 \t");
scanf ("%d",&e);
printf ("\nMaths");
printf ("\t 100 \t");
scanf ("%d",&m);
printf ("\n\n\nTOTAL");
printf ("\t 300 ");
printf ("\t%d",tot=(h+e+m));
printf ("\n\nPERCENT");
per = (float) tot/3;
printf ("\t %.2f %",per);
getch ( );
}
14.Enter any Number and find out it's Square and Cube.
For example, if no. is n then find out n2 and n3.
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 57
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n;
long s,c;
printf ("Enter any Number : ");
scanf ("%d",&n);
s = (long)n*n;
c = (long)n*n*n;
printf ("\n\n\t\tSquare of %d = %ld",n,s);
printf ("\n\n\t\tCube of %d = %ld",n,c);
getch ( );
}
Exam Programs
1) Basic salary of employee is input through the keyboard. DA is
40% of basic salary & house rent allowance (HRA) is 20% of the
basic salary. Calculate his gross salary?
Ans :
#include<stdio.h>
#include<conio.h>
void main( )
{
float Basic, DA, HRA, GS;
clrscr( );
printf(―Enter the basic salary‖);
scanf(―%f‖, &basic);
DA = (40/100) * basic;
HRA = (20/100)* basic;
GS = DA + HRA + Basic;
printf (―Gross salary = %f ‖,GS);
getch ( );
}
2) Temperature of a city is Fahrenheit degrees is input to keyboard
write a program to convert this temperature into centigrade.
[TC = (TF-32)*5/9]
Ans :
#include<stdio.h>
#include<conio.h>
void main( )
{
float centi;
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 58
int faren;
printf(―Enter Fahrenheit temp‖);
scanf(―%d‖, &faren);
centi = (( float) faren -32) * 5 / 9;
printf(― cent temp = %f‖, cent);
getch( );
}
3) The length and breadth of the rectangle & radius of the circle are
input through key board. Write a program to calculate the area,
perimeter & the circumference of the circle.
Ans :
#include<stdio.h>
#include<conio.h>
void main( )
{
float l, b, r, area, perim, circumf;
clrscr( );
printf(―Enter length & breadth‖, l, b,);
scanf(―%f%f‖, &l,&b);
area = l * b;
perim = 2 * (l + b);
printf(―\n area =%f ‖, area);
printf(―\n perim = %f ‖, perim);
printf(―\n enter radius ‖,r);
scanf(―%f ‖, &r);
area = 3.14 * r * r;
circumf = 2 * 3.14 * r;
printf(―\n area = %f ‖,area);
printf(―\n circumf = %f ‖, circumf);
getch( );
}
4) Two numbers are input through keyboard into two locations C &
D. Write a program to interchange the content of C & D.
Ans :
#include<stdio.h>
#include<conio.h>
void main( )
{
int a, b, c;
clrscr( );
printf(―a = %d, b = %d ‖, a, b);
c = a;
a = b;
b = c;
printf(―\n a = %d, b = %d‖, a, b);
getch( );
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 59
}
Review Questions
Summer 2007 Marks
1. State different data types supported by ‗C‘ language (W-2007). 2
2. Write a ‗C‘ statement for each of the following: 2
i. Relational operator.
ii. Assignment operator.
3. State the use of increment and decrement operator and give its
precedence and Associativity. 4
4. Define the following: 4
i. Expressions
ii. Data types
iii. Bitwise operators
iv. Logical operators
Winter 2007
1. Explain the use of bitwise operator. 2
2. State the use of increment and decrement operators. Also give
difference between i++ and ++i with example. 4
3. What is variable? How to declare it? How variable differs from
constant? 4
Summer 2008
a) What is variable declaration and variable initialization? 2
b) What are the uses of printf( ) and scanf( )? 2
c) List different relational and logical operators. What will be the output
of following program? 4
#include<stdio.h>
void main( )
{
float a = 5, b = 2;
int c;
c = a % b;
printf(―%d‖,c);
}
d) State use of %c %d and %f. 4
Write output of following:
#include<stdio.h>
main( )
{
float y = 123.456;
printf(―%f %.3f %.1f \n\n‖, y, y, y);
}
e) Define the following: 4
1) Keyword
Programming in C (Er.Manoj S. Kavedia /9324258878 /8329988732) Page 60
2) Variable.
3) Data types
4) Constants.
Winter 2008
a) State four relational operators with its meaning. 2
b) State four printf format codes. 2
c) State the arithmetic and logical operators with its meaning. 4
d) Write a program to take marks of five subjects and display the total
and average. 4
e) State four rules of choosing variable‘s name. 4
f) What is the output of the following code? 4
void main( )
{
int i = 1, j = -1, k = 0, w, x, y, z;
w = i || j ||k ;
x = i && j && k ;
y = i || j && k ;
z = i && j && k ;
printf(« w=%d x=%d y=%d z=%d »,w,x,y,z) ;
}
Other:
1. Differentiate between unary, binary and ternary operators. 4
2. What is precedence and associativity of operators? 2
3. Explain the use of manual and automatic type casting with suitable.
Example. 4
4. What is comment? Explain its use. 2
Note : Prepared
Er.Manoj S. Kavedia
9324258878
8329988732
YouTube Channel
KavediaSir
Facebook
KavediaSir
Instagram
KavediaSir