Mod1 CS
Mod1 CS
SOLVING
MVJ21CS13
MODULE
Prepared by:
POSHITHA.M
ASP, Dept. of CSE,
MVJCE, Bangalore
UNIX SYSTEM
1) Creating the program
2) Compiling and linking
3) Executing the program
• Executing the program
It’s a simple task
a.out would load the executable object code into the
computer memory and execute the instructions
During the program user inputs are required and
output is produced
Sometime if no output then programlogic is wrong
check the errors in the source code. After that again
compiling,linking and executing will be repeated.
Creating your own executable file
1) Linker will assign the same name a.out when we compile
the other program
2) That object code will be overwritten by the other executable
object code.
3) If we want to prevent from happening
4) Rename the file immediately by using the command
mv a.out file1
suppose you have written the prg in hello.c file
Compiling command is cc hello.c
Executable form object code is created and stored in a.out
Now again if u create the calci.c file and compile it with cc
calci.c then a.out file will be overwritten.
Creating your own executable file
??( [
??) ]
??< {
??> }
Keywords Constants
(int,float) (-15.5,100)
Strings(“h Identifiers
ello”) (main amount)
Special
KEYWORDS AND INDENTIFIERS
Character constants
Numeric constant
Example:-
1) 215. 0.21500 .215
2) .95
3) -.71
4) +.5
MANTISSA e EXPONENT
• A real number can also be expressed in
exponential notation
• Example:- 215.65 = 2.1565e²
• e² means multiply by 10²
• The general form mantissa e exponent
• This mantissa is either a real number expressed
in decimal or an integer.
• The exponent for integer number will have an
optional plus or minus sign
MANTISSA e EXPONENT
• The letter e separating the mantissa and the exponent
can be written in either lowercase or uppercase
• Since the exponent causes the decimal point to “float”,
this notation is said to represent a real number in
floating point form
• Example:-
1) 0.65e4
2) 12e-2
3) 3.18E3
4) -1.2E-1
MANTISSA e EXPONENT
• Embedded whitespace is not allowed
• This is used for representing number that
either very large or very small in magnitude
• Example:-
1) 75000000000=7.5E9
2) -0.000000368=-3.68E-7
MANTISSA e EXPONENT
• 698354L
• 25,000
• -5.0E3
• 3.5e-5
• 7.1e 4
• -4.5e-2
• 1.5E+2.5
• $255
• ox7B
SINGLE CHARACTER CONSTANTS
• It contains the character enclosed within a
pair of single quote marks
• Example:-
• ‘5’ ‘p‘ ‘6’
• ’x’
• ’:’
• ’;’
• ’‘
• Character constants have integer value known as ASCII
values
• Printf(“%d”, ‘a’);
• Output 79
• Printf(“%c”, ‘97’);
• Output a
• Printf(“%d”, ‘z’);
• Output 122
• Printf(“%c”,’122’);
• Output z
STRING CONSTANTS
• It is the sequence of characters enclosed in
double quotes
• The characters can be numbers,special
characters and blankspace
• Example:- “hello”, “1998”, “?...1”, “X”
• Note “X” is not same as ‘x’
BACKSLASH CHARACTER CONSTANTS
• It is used in output functions
• They consist of two characters
\a Audible alert
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Backslash
\o null
VARIABLES
• A variable is a data name that may be used to
store a data value
• The variables remains unchanged during the
execution time
• A variable name can be chosen the
programmer in a meaningful way so as to
reflect its function in the program
• Ex:- sum,average,height,mod.
CONDITIONS
• They must begin with a letter _ is also
permitted in some system
• The length of a variable should not be more
than 8 characters avg_num; AVG_NUM;
• Uppercase and lowercase are significant
• It should not be a keyword
• Whitespace is not allowed
• John
• Delhi
• Sum1
• T_raise
• 123
• %
• (area)
• 25th
• float
• If only the first eight characters are recognised
by a compiler
• For example:-average_height
average_weight
• This both mean same thing to the computer so
rewritten as
• Avg_height and avg_weight
DATA TYPES
• C language is rich in its datatypes
• Storage representations and machine
instructions to handle constants differ from
machine to machine
• The variety of data types available allow the
programmer to select the the appropriate to the
needs of the application as well as machine
DATA TYPES
• ANSI C supoorts three classes of datatypes
1) Primary (or fundamental datatypes)
2) Derived datatypes
3) User-defined datatypes
DATA TYPES
• All C compliers support five fundamentals
datatypes namely
1) integer(int)
2) Character(char)
3) Floating point (float)
4) Double precision floating point (double)
5) Void (no return type)
Many of them also offer extended data types
such as long int and long double
Size and range of the data types
16 bit machines
Size and range of the data types
32 bit machines
INTEGER TYPES
• Integer are whole numbers with a range of
values supported by a particular machine
• Generally integers occupy one word of storage
and since the word sizes of machines vary
(16 to 32 bits)
• In order to provide some control over the
range of numbers and storage space
INTEGER TYPES
• C has three classes of integers storage namely
• Short int
• Int
• Long int
These integers are available in both signed and
unsigned forms
INTEGER TYPES
• For example:- short int represnts fairly small
integer values and requires half the amount of
storage as a regular int.
• Unlike signed integers use all the bits fir the
magnitude of the number and are always
positive
• Therefore the range of a unsigned integer
number in 16bit machine is from 0 to 65,535.
INTEGER TYPES
• We declare long and unsigned integers to
increase the range of values
• The use of qualifer signed on integers is
optional because the defaults declaration
assumes a signed number
FLOATING POINT TYPES
• Floating point (or real) numbers are stored in
32 bits (on all 16 bit & 32 bit machines) with 6
digits precisions
• Floating point numbers are defined in C by the
keyword float.
• When the accuracy provided by a float number
is not sufficient, the tyoe double can be used to
define the number.
FLOATING POINT TYPES
• A double data types number uses 64 bits
giving a precision of 14 digits
• These are known as double precision
numebers
• To extend the precision futher we may use
long double which uses 80bits
VOID TYPES
• The void type has no values
• This is usually used to specify the type of
functions
• The type of a function is said to be void when
it does not return any value to the calling
function.
• It can represent any of the other standard
types.
CHARACTER TYPES
• A single characters can be defined as a
character(char) type data
• Characters are usually stored in 8bits of
internal storage.
• The qualifer signed or unsigned may be
explicitly appiled to char
• While unsigned char have vslues 0 and 255
• Signedchars have values from -128 to 127
Declaration of variables
• After designing suitable variable names, we
must declare them to the complier
• It tells the complier what the variable name is
• It specifies what type of data the variable will
hold
• The declaration of variables must be done
before they are used in the program
Primary Type declaration
• A variable can be used to storea value of any
datatypes.
• Syntax:- datatype v1,v2…..vn;
• V1,v2…vn are the variables separted by commas
and it should end with a semicolon
• Valid declarations are
int count;
int number; total;
double ratio;
Primary Type declaration
• When an adjective short,long or unsigned is
used without basic datatypes specifier.
• C compiler treat the datatypesas an int.
• If we want to declare a character variable as
unsigned ,then we must do so using both the
terms like unsigned char.
Default values of constants
• Integers constants, by default represent int type
data. We can override this default by specifying
unsigned or long after the number by appending U
or L
• Floating point constants, by default represent
double type data.
• If we want the resulting data type to be float or
long double, we must append the letter f or F to the
number for float and letter l or L for long double
Defaults values of constants
User-defined type declaration
• C supports a feature known as “type
definition” that allows users to define an
identifer that would represent an existing data
type.
• The user defined data typeindentifer can later
be used to declare varaibles.
• It takes the general forms
typedef type identifer;
• Where type refers to an existing data type and
“identifers” refers to the “new” name given to
the data types.
• The existing data type may belong to any class
of type, including the user-defined ones.
• typedef int units;
• typedef float marks;
• Here units symbolizes int and marks
symbolizes float.
• They can be later used to declare variables as
follows:
• Units batch1,batch2;
• Marks name1[50],name2[50];
• Another user defined datatype is enumerated
data type provided by ANSI standard.
• It is defined as
enum identifier {value1,value2,…..valuen};
• The identifer is a use defined enumerated data
type which can be used to declare variables
that can have one of the values enclosed
within the braces.
• Enum identifier v1,v2,…vn;
• The enumerated variables v1,v2….vn can only
have one of the values value1,value2,
….valuen
• The assignments of the following types are
valid.
• V1=value3;
• V5=value1;
• Enum day{Monday,Tuesday,……Sunday};
• Enum day week_st,week_end;
• Week_st=Monday;
• Week_end=Friday;
• If(week_st == Tuesday)
• Week_end= Saturday;
Declaration of storage class
• Variables of c have not only datatype but also
storage class that provide information about
their location and visibility
• The storage class decides the portion of the
program within which the variables are
recognized
Global variables and local variables
int m;
main()
{
int i;
float balance;
----
----
function1();
}
function1()
{
int i;
float sum’
-----
------
}
Scope and lifetime of variables
• The concept of scope and lifetime of varibales
are important only in multifunction and multiple
file programs and therefore the storage classes
are considered
• There are four storage class specifies
1) Auto
2) Register
3) Static
4) extern
• auto int count;
• register char ch;
• static int x;
• extern long total ;
• Static and extern variables are automatically
initialized to zero.
• Auto variables contain undefined values (known
as garbage variables)
• Unless they are initialized explicitly.
Storage classes and there meanings
Assigning values to variables
• Variables are created for use in program
statements such as:
Assignment statement
• Values can be assigned to variables using the
assignment operator =
• Variable_name=constant
• Example:-
initial_value=0;
Balance=75.84;
• C permits multiple assignments in one line
• Initial_value=0; final_value=100;
• Year=year+1;