Wednesday, August 17, 2011

C PROGRAMS




C LANGUAGE BOOK

XML SITEMAP For C Language Blog




Pankaj(Pank34aj)





   

Book Name-C Self Practical materials to learning



Author :-Pankaj(Pank34aj)





















☂★☢♔♊☀☂
Copyright © 2012 Pankaj(Pank34aj)~ - All Rights Reserved.
☂★☢♔♊☀☂






C is a programming language developed at AT & T’s  Bell
Laboratories of USA in  1972. It was designed and written by  a
man named Dennis Ritchie. An opinion that is often heard today
is – “C has been already superceded by languages like C++, C# and
Java, so why bother to learn C today” .C++, C# or Java make use
of a principle called Object Oriented Programming (OOP) to organize the
program. This organizing principle has lots of advantages to offer. But even
while using this organizing principle you would still need a good hold over the
language elements of C and the basic programming skills.

Major parts of popular operating systems like Windows,
UNIX, Linux is still written in C.  This is because even today
when it comes to performance (speed of execution) nothing
beats C.


HI THERE SOME C PROGRAM.HOPE YOU LIKE IT.I AM COOL.


C LANGUAGE BOOK

QUES1 BASIC AND SIMPLE ARITHMETIC


C LANGUAGE BOOK


The C Character Set
A character denotes any alphabet, digit or special symbol used to
represent information.  Figure shows the valid alphabets,
numbers and special symbols allowed in C.
 




C-Language keywords


Types of C Constants
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants 




Rules for Constructing Integer Constants

(a) An integer constant must have at least one digit. 
(b)It must not have a decimal point.
(c) It can be either positive or negative.
(d) If no sign precedes an integer constant it is assumed to be
positive.
(e) No commas or blanks are allowed within an integer constant.
(f) The allowable range for integer constants is -32768 to 32767. 



Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.

Following rules must be observed while constructing real
constants expressed in fractional form:

(a)A real constant must have at least one digit.
(b)It must have a decimal point.
(c)It could be either positive or negative.
(d)Default sign is positive.
(e)No commas or blanks are allowed within a real constant. 



In exponential form of representation, the real constant is
represented in two parts. The part appearing before ‘e’ is called
mantissa, whereas the part following ‘e’ is called exponent.

Following rules must be observed while constructing real
constants expressed in exponential form:

(a)The mantissa part and the exponential part should be
separated by a letter e.
(b)The mantissa part may have a positive or negative sign.
(c)Default sign of mantissa part is positive.
(d)The exponent must have at least one digit, which must be a
positive or negative integer. Default sign is positive.
(e)Range of real constants expressed in exponential form is 
-3.4e38 to 3.4e38. 


Rules for Constructing Character Constants

(a)A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas.
(b)Both the inverted commas should point to the left.
For example, ’A’ is a valid character constant whereas ‘A’ is
not.
The maximum length of a character constant can be 1
character. 

DATA TYPES



















Hierarchy of Operations










Arithmetic Instruction

A C arithmetic instruction consists of a variable name on the left
hand side of = and variable names & constants on the right hand
side of =. The variables and constants appearing on the right hand
side of = are connected by arithmetic operators like +, -, *, and /.

Ex.:    int   ad ;
    float   kot, deta, alpha, beta, gamma ;
    ad = 3200 ;
    kot = 0.0056 ;
    deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

Here, 

*, /, -, + are the arithmetic operators. 
= is the assignment operator. 
2, 5 and 3200 are integer constants. 
3.2 and 0.0056 are real constants. 
ad is an integer variable. 
kot, deta, alpha, beta, gamma are real variables.



Use of Logical Operators
C allows usage of three logical operators, namely, &&, || and !.
These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. 

The first two operators, && and  ||, allow two or more conditions
to be combined in an if statement.



 if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "z" ) ;
else
printf("zz");

 z gets printed if both the conditions evaluate to true. If one of the conditions
evaluate to false then the whole thing is treated as false. and zz is printed.

Similarly
 if ( ( per >= 40 ) || ( per < 50 ) )
printf ( "z" ) ;
else
printf("zz");


 z gets printed if one of the conditions or both off the condition evaluate to true. If both conditions
evaluate to false then the whole thing is treated as false. and zz is printed.

The ! Operator
This ! operator reverses the result of the expression it operates on.















Hierarchy of Operators Revisited Including logical Operator













Comment

Single Line Comment can be used by using  // 
MultiLine Comment about the program should be enclosed within /*  */.

A comment can be split over more than one line, as in,
/* This is 
    a jazzy 
    comment */

Such a comment is often called a multi-line comment.


Assuming that you are using a Turbo C or Turbo C++ compiler
here are the steps that you need to follow to compile and execute
your first C program…

(a) Start the compiler at C> prompt. The compiler (TC.EXE is
usually present in C:\TC\BIN directory). 
(b) Select New from the File menu. 
(c) Type the program.
(d) Save the program using  F2 under a proper name (say
Program1.c).
(e) Use Ctrl + F9 to compile and execute the program.
(f) Use Alt + F5 to view the output. 

WAP TO PROGRAM TO PERFORM ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION.


/* here stdio.h is the header file.it is used for standard input and output..it is enclosed in angle bracket or Double Quotes.Printf() function is used to display value on screen.scanf() function is used for inputting value from the user.to use header file we use #include it */


#include<stdio.h>
int main()
{

int i,j;
int d,e,k,h;

printf("\nENTER TWO NUMBERS : \n");
scanf("%d%d", &i, &j);
printf("\nTHE TWO NUMBERS ARE : \n%d%s%d", i," ", j);
d = i + j;
printf("\nTHE ADDITION OF TWO NUMBERS ARE: %d", d);
e = i - j;
printf("\n\nTHE SUBTRACTION OF TWO NUMBERS ARE : %d", e);
k = i * j;
printf("\n\nTHE MULTIPLICATION OF TWO NUMBERS ARE : %d", k);
h = i / j;
printf("\n\nTHE DIVISION OF TWO NUMBERS ARE : %d", h);

getch();
}



C LANGUAGE BOOK

QUES2 BASIC CONTINUE AND SUM AND AVG


C LANGUAGE BOOK


 Conditional Operators

The conditional operators  ? and  : are sometimes called ternary
operators since they take three arguments. In fact, they form a kind
of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is, if its
value is non-zero), then the value returned will be  expression 2,
otherwise the value returned will be  expression 3”.

Control Instructions in C

As the name suggests the ‘Control Instructions’ enable us to
specify the order in which the various instructions in a program are
to be executed by the computer. In other words the control
instructions determine the ‘flow of control’ in a program. There
are four types of control instructions in C. They are:

(a)  Sequence Control Instruction
(b)  Selection or Decision Control Instruction
(c)  Repetition or Loop Control Instruction
(d)  Case Control Instruction

The Sequence control instruction ensures that the instructions are
executed in the same order in which they appear in the program.
Decision and Case control instructions allow the computer to take
a decision as to which instruction is to be executed next. The Loop
control instruction helps computer to execute a group of statements
repeatedly.

 Decision
a  decision control instruction
can be implemented in C using:

(a) The if statement 
(b) The if-else statement
(c) The conditional operators


The if Statement
 The general form of if statement looks
like this:

if ( this condition is true )
 execute this statement ;












The if-else Statement  

the general form of if-else statement is like this

if ( this condition is true )
 execute this statement1 ;
else
 execute this statement2 ;

 statement1 is executed if condition evaluates to true.
 if condition evaluates to false then  statement2 is executed.



Nested if-elses 

 It is perfectly all right if we write an entire if-else construct within
either the body of the if statement or the body of an else statement.
This is called ‘nesting’of  ifs.


Forms of if
The if statement can take any of the following forms:

(a)
if ( condition )
 do this ;

(b)
if ( condition )
{
 do this ;
 and this ;
}

(c)
if ( condition )
 do this ;
else
 do this ;

(d)
if ( condition )
{
 do this ;
  and this ;
}
else
{
 do this ;
 and this ;
}

(e)  if ( condition )
 do this ;
else
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}

 (f)
if ( condition )
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}
else
 do this ;
 


The else if Clause 
the general form of else if clause is like this

if ( this condition1 is true )
 execute this statement1 ;
else if ( this condition2 is true )
 execute this statement2 ;
else if ( this condition3 is true )
 execute this statement3 ;
else
execute this statement3 ;

In this Case every else is associated with its previous if.
The last else goes to work only if all the conditions evaluates to false. Even in
else if ladder the last else is optional.



WAP TO PRINT SUM AND AVERAGE OF 5 NUMBER THAT IS ENTERED BY USER.

#include<stdio.h>
int main(void)
{
int n, h, j, r, m;
float e,t;

scanf("%i%i%i%i%i", &h, &j, &n, &r, &m);
printf("\n%i%s%i%s%i%s%i%s%i", h," ", j, " ", n," ", r, " ",m);
e = h + j + n + r + m;

printf("\nthe sum is%f", e);
t = e / 5;

printf("\nthe average is%f", t);

getch();
}


C LANGUAGE BOOK