Você está na página 1de 19

The C Language

C language programmer has to tell the


system before-hand, the type of
numbers or characters he is using in
his program. These are data types.
There are many data types in C
language. A C programmer has to use
appropriate data type as per his
requirement.
Copyright © 2001 Stephen A. Edwards All rights reserved
History of C:

Developed between
1969 and 1973 along
with Unix
Due mostly to Dennis
Ritchie
Designed for systems
programming
Copyright © 2001 Stephen A. Edwards All rights reserved
While writing C program some rules are to be kept in mind

It should be in lower case.


0 – 9 (numerical values) should be written
26 Alphabets should be written (A – Z)
Special character is allowed (;, $, ,, %, !, ?)
With in two statements we can leave the space but in argument we can’t
leave the space
First declare the variables then start programming
In each and every question we have to declare the header file in starting only
The main function should be written in “main ()”
The function is starts from “{” and ends from “}”
After finishing each & every statement we have to terminate the program
with “;”
In this “/* - * /” we have to write more information, comments or remarks

Copyright © 2001 Stephen A. Edwards All rights reserved


C language data types can be broadly classified as

Primary data type


Derived data type
User-defined data type

Copyright © 2001 Stephen A. Edwards All rights reserved


1. Integer int

2. Character char

3. Floating Point float

Double
4. precision double
floating point

5. Void void

Copyright © 2001 Stephen A. Edwards All rights reserved


The size and range of each data type is given in the
table below

DATA TYPE RANGE OF VALUES

char -128 to 127

Int -32768 to +32767

float 3.4 e-38 to 3.4 e+38

double 1.7 e-308 to 1.7 e+308

Copyright © 2001 Stephen A. Edwards All rights reserved


Character Type:
A single character can be defined as a defined as a character
type of data. Characters are usually stored in 8 bits of internal
storage. The qualifier signed or unsigned can be explicitly
applied to char. While unsigned characters have values
between 0 and 255, signed characters have values from –128 to
127.

Integer Type:
Integers are whole numbers with a machine dependent
range of values. A good programming language as to
support the programmer by giving a control on a range of
numbers and storage space. C has 3 classes of integer storage
namely short int, int and long int. All of these data types
have signed and unsigned forms. A short int requires half
the space than normal integer values. Unsigned numbers are
always positive and consume all the bits for the magnitude
of the number. The long and unsigned integers are used to
declare a longer range of values.

Copyright © 2001 Stephen A. Edwards All rights reserved


Floating Point Types:

Floating point number represents a real number with 6 digits


precision. Floating point numbers are denoted by the
keyword float. When the accuracy of the floating point number
is insufficient, we can use the double to define the number. The
double is same as float but with longer precision. To extend the
precision further we can use long double which consumes 80
bits of memory space.

Void Type:

Using void data type, we can specify the type of a function.


It is a good practice to avoid functions that does not return
any values to the calling function.

Copyright © 2001 Stephen A. Edwards All rights reserved


Data Types declaration

Int a; /* General declaration */


Int a = 10; /* Valued Declaration */
float b = 12.6;
float b;
char c;
char c = ‘y’;
Copyright © 2001 Stephen A. Edwards All rights reserved
Constants.

Constants are data values that cannot be changed


during the execution of your program. Like
variables, constants also have a type.
There are four kinds of constants in C:
•Integer Constants
•Floating-point Constants
•Character Constants
•String Constants

Copyright © 2001 Stephen A. Edwards All rights reserved


Variable

A Variable names a place in memory where you store a Value of a certain


Type.
You first define a variable by giving it a name and specifying the type,
and optionally an initial value
char x;

char y=‘e’; Initial value of x is undefined

char = Type is single character (char)

y = Name

e = Initial value
Copyright © 2001 Stephen A. Edwards All rights reserved
Keywords:
Data type Keyword Equivalent
Character char
Unsigned Character unsigned char
Signed Character signed char
Signed Integer signed int (or) int
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long
Un Signed Integer unsigned int (or) unsigned
Un Signed Short Integer unsigned short int (or) unsigned short
Un Signed Long Integer unsigned long int (or) unsigned long
Floating Point float
Double Precision Floating Point double
Extended Double Precision Floating Point long double
Operator Meaning Operator Meaning
+ Add > Greater Then
- Sub < Lesser Then
* Multiplication == Equal to
/ Division != Not Equal to
Copyright © 2001 Stephen A. Edwards All rights reserved
Functions:
main ():
The first line of any program of ‘c’ always begins with the
function name main (). main () is the special name indicates the
begging of ‘c’ program.
printf :
printf statement is used to make the compiler display the
words / sentences on the screen as given by the user. The word
or sentences given with in the “” will be displayed exactly.
getchar():
To write one word, character etc.

putchar():

To print one character, word etc.

Copyright © 2001 Stephen A. Edwards All rights reserved


scanf:
scanf function is used to receive a value from the user. scanf function requires
the conversion specifier indicated the type of value which is come from
keyboard and get stored in specific variable.
Following are the conversion specifier.
%d: Integer.
%f: Float.
%c: Single Character.
%s: String without spaced.
%[^/n]: String with space.
%/d: Long integer.
%u: Unsigned Integer (short).
%o: Octal No.
%x: Hexa No.
%e: Exponantional form.
&: Address of operator.

Copyright © 2001 Stephen A. Edwards All rights reserved


Structure:

# include <stdio.h>
#: It will start the compiler.

include: Compiler

.h: It is the header file.

stdio: Standard input & output.

Header File:

It is a set of program necessary to understand the syntax of


the c language or any other language.
Copyright © 2001 Stephen A. Edwards All rights reserved
Syntax:
A command that is given to a particular language.
This statements is indicates that to include standard input out
header files.

# include <conio.h>
This statement is used to include constant input output header
file.

<> Angle Bracket:

Angle brackets indicate by the file is located in the stander


default directory and it is provided ‘c’ compiler. If an user
define file is to included then the file name is specify inside
“” . “” indicated that the file is stored in the user directory.

Copyright © 2001 Stephen A. Edwards All rights reserved


1. To calculate area of rectangle:
#include<stdio.h>
#include<conio.h>
Void main()
{
Float l,b;
Float area;
Clrscr();
Printf(“Enter the value of length”);
Scanf(“%f”,& l);
Printf(“Enter the value of breath”);
Scanf(“%f”,&b);
Area=l*b;
Printf(“/n The area of rectangle is %f”,area);
Getch();
}
Copyright © 2001 Stephen A. Edwards All rights reserved
2. To calculate square root of given number:
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
Int n;
Flaot ans;
Clrscr();
Printf(“Enter the Number”);
Scanf(“%d”, &n);
Ans=sqrt(n);
Printf(“\n The answer is %f”, ans);
Getch();
}

Copyright © 2001 Stephen A. Edwards All rights reserved


3. To calculate simple Interest:
#include<stdio.h>
#include<conio.h>
Main()
{
Float si, p, n, r;
Clrscr();
Printf(“/t/t Program to calculate simple interest”);
Printf(“/n/t Enter the principle amount:”);
Scanf(“%f”, &p);
Printf(“/n/t Enter the no of years”);
Scanf(“%f”, &n);
Printf(“%f”, &r);
Si = p*n*r/100
Printf(“The simple interest is : %f”, si);
Getch();
}

Copyright © 2001 Stephen A. Edwards All rights reserved

Você também pode gostar