C: Basics
C's Character Set
C does not use, nor requires the use of, every character
found on a modern computer keyboard. The only characters required by the C
Programming Language are as follows:
- A - Z
- a -z
- 0 - 9
- space . , : ; ' $ "
- # % & ! _ {} [] ()
< > | \
- + - / * = ?
The form of a C
Program
All C programs will consist of at
least one function, but we can write a C programs that have several functions
body . The only function that has to be present is the function main.
For more advanced programs the main
function will act as a controling function calling other functions in their
turn to do the dirty work! The main
function is the first function that is called when your program executes. So
main is the starting of execution of program.
The layout of C Programs
The general form of a C program is
as follows :
preprocessor directives
global declarations of user functions
global variables declarations
main()
{
local variables to function main ;
statements associated with function main ;
}
definitions of functions like
function1()
{
local variables to function 1 ;
statements associated with function 1 ;
}
function2()
{
local variables to function f2 ;
statements associated with function 2 ;
}
.
.
.
etc
Note the use of the bracket set () and {}:
() are used in conjunction with function names whereas {} are used with the C statements that are associated with that
function and used for statements block. A semicolon (;) is used to terminate C
statements. C is a free format language and long statements can be continued,
without truncation, onto the next line. The semicolon informs the C compiler
that the end of the statement has been reached. Free format also means that you
can add as many spaces as you like to improve the look of your programs.
A very common mistake made by everyone, who is new
to the C programming language, is to miss off the semicolon. The C compiler
will concatenate the various lines of the program together and then tries to
understand them - which it will not be able to do. The error message produced
by the compiler will relate to a line of program which could be some distance
from the initial mistake.
Data types Variables and Constants
C language supports following data
types;
1. Primary(or fundamental) data types
2. User-defined data types
3. Derived data types
4. empty data sets.
Primary Data types and associated
variables :
There are five basic data types in
C associated with variables:
- int
- integer: a whole number.
- float
- floating point value: i.e. a number with a fractional part.
- double
- a double-precision floating point value.
- char
- a single character.
Variables
A variable is a named data storage
location in the computer's memory. By
using
a variable's name in our program, we are, in effect, referring
to the data
stored there.
Numeric Variable
Types
C provides several different types
of numeric variables. We need different
types of variables because different numeric values have
varying memory storage
requirements and differ in the ease with which certain
mathematical operations
can be performed on them. Small integers (for example, 1,
199, and -8) require
less memory to store, and the computer can perform
mathematical operations
(addition, multiplication, and so on) with such numbers very
quickly. In
contrast, large integers and floating-point values
(123,000,000 or
0.000000871256, for example) require more storage space and
more time for
mathematical operations. By using the appropriate variable
types, we ensure
that our program runs as efficiently as possible.
Following table shows the data types and the byte
required and Renges of values for different variable.
Variable Type Keyword Bytes
Required Range
Character char 1 -128 to 127
Integer int 2 -32768 to 32767
Short integer short 2 -32768 to 32767
Long integer long 4 -2,147,483,648 to 2,147,438,647
Unsigned
character unsigned char 1 0 to 255
Unsigned integer
unsigned int 2 0 to 65535
Unsigned short
integer unsigned short 2 0
to 65535
Unsigned long
integer unsigned long 4 0
to 4,294,967,295
Single-precision
float 4 3.4E-38 to 3.4E+38
floating-point
Double-precision double 8 1.7
E-308 to 1.7E+308
floating-point
Integer Variables
The first type of variable we need
to know about is of class type int
- short for integer. An int
variable can store a value in the range -32768 to +32767. We can think of it as
a largest positive or negative whole number: no fractional part is allowed. To
declare an int you use
the instruction:
int
variable name;
For example:
int
a;
declares that we want to create an int variable called a.
To assign a value
to our integer variable we would use the following C statement:
a=10;
The C
programming language uses the "=" character for assignment. A
statement of the form a=10;
should be interpreted as take the numerical value 10 and store it in a
memory location associated with the integer variable a. The "="
character should not be seen as an equality otherwise writing statements of the
form:
a=a+10;
will get mathematicians blowing
fuses! This statement should be interpreted as take the current value
stored in a memory location associated with the integer variable a; add the
numerical value 10 to it and then replace this value in the memory location
associated with a.
Floating point Number Variables
As described above, an integer
variable has no fractional part. Integer variables tend to be used for
counting, whereas real numbers are used in arithmetic. C uses one
of two keywords to declare a variable that is to be associated with a decimal
number: float and double. They are each offer a
different level of precision as outlined below.
float
A float, or floating point, number
has about seven digits of precision and a range of about 1.2E-38 to 3.4E+38. A
float takes four bytes to store.
double
A double, or double precision,
number has about 13 digits of precision and a range of about 2.2E-308 to
1.8E+308. A double takes eight bytes to store.
For example: float total;
double sum;
To assign a numerical value to our floating point and double precision variables we would use the following C statement:
total=0.0;
sum=12.50;
Character Variables
C only has a concept of
numbers and characters. It very often comes as a surprise to some programmers
who learnt a beginner's language such as BASIC that C has no
understanding of strings but a string is only an array of
characters and C does have a concept of arrays which we shall be meeting
later in this course.
To declare a variable of type character we use the keyword char. - A single character stored in
one byte. For example:
char c;
To assign, or store, a character value in a char data type is easy - a character
variable is just a symbol enclosed by single quotes. For example, if c is a char variable you can store the letter A in it
using the following C statement:
c='A'
Notice that you can only store a single character
in a char variable. Remember
that the value for a char
variable is 'A' and not "A".
A program that displays the size of variable
types.
/* C--Program to tell the size of the C variable */
/* type in bytes */
#include <stdio.h>
main()
{
printf( "\nA char is %d bytes", sizeof( char ));
printf( "\nAn int is %d bytes", sizeof( int ));
printf( "\nA short is %d bytes", sizeof( short ));
printf( "\nA long is %d bytes", sizeof( long ));
printf( "\nAn unsigned char is %d bytes", sizeof( unsigned char ));
printf( "\nAn unsigned int is %d bytes", sizeof( unsigned int ));
printf( "\nAn unsigned short is %d
bytes", sizeof( unsigned short ));
printf( "\nAn unsigned long is %d bytes", sizeof( unsigned long ));
printf( "\nA float is %d bytes", sizeof( float ));
printf( "\nA double is %d bytes\n", sizeof( double ));
return 0;
}
output:
A char is 1 bytes
An int is 2 bytes
A short is 2 bytes
A long is 4 bytes
An unsigned char is 1 bytes
An unsigned int is 2 bytes
An unsigned short is 2 bytes
An unsigned long is 4 bytes
A float is 4 bytes
A double is 8 bytes
User
defined data types:
The data types that are defined by programmer themselves are
called user defined data types. C supports a feature known as “type definition”
that allows user to define an identifier that would represent an existing type.
The user defined type is later used to declare the variables.We can use keyword
typedef
The general form is :
typedef type
identifier;
type refers to the
existing type and identifier refers to the new name given to the type.
Eg. typedef int integer;
typedef
char character;
typedef
int boolean;
The user defined type is then used to declare the variable
as
integer
sum,product;
boolean flag;
character c;
Another user-defined type is
enumerated data type provided by ANSI standard C .it is defined as follows:
enum identifier
{value1,value2…….valuen};
The identifier is a user-defined
enumerated type which can be used to declare variable that can have one of the
values enclosed within braces(known as enumeration constants). After this
definition, we can declare variables to be of new type as
enum identifier var1,var2….varn;
The enumerated variables
var1,var2,….varn can only have one of the values from the enumeration constants
listed within {…}
The assignment can be done as:
var1=value2;
var2=
valuen;
The
Compiler automatically assigns integer digits beginning with 0 to all the
enumeration constants. i.e. value for value1 above is 0 ,value2 is 1 and so on.
But automatic assignments can be changed by assigning values to the enumeration
constants as
enum day { Monday=1,
Tuesday,…….Sunday};
0 comments:
Post a Comment