Sei sulla pagina 1di 34

Overview

 Developed by Dennis Ritchie at AT & T Bell


Laboratories in 1992
 Reliable, simple and easy to use
 Top-down programming

3/23/2018 ESG-NIELIT Calicut 2


Why ‘C’
“C has been already super ceded by languages like
C++, C# and Java, so why bother to learn C
today?”

3/23/2018 ESG-NIELIT Calicut 3


Why ‘C’
 Reasons
 No body can learn C++ or Java directly, the

basic programming elements are learned from C


 C++ and Java tools frameworks are still built on

C only
 Major part of popular Operating Systems like

Unix, Linux and Windows are written in C

3/23/2018 ESG-NIELIT Calicut 4


Why ‘C’
 Reasons
 When it comes to performance in terms of

execution speed nothing beats ‘C’


 Device drivers are commonly written in C

 In case embedded systems like mobiles and

palmtops the programs not only have to run


fast but also have to work in limited memory.
 C is the best language in choice with these

constraints on time and space.

3/23/2018 ESG-NIELIT Calicut 5


Syllabus - ‘C’ programming
 Storage Classes, Data Types, Controlling
program flow, arrays, functions, Memory
Management
 Pointers, Arrays and pointers, Pointer to
functions and advanced topics on pointers,
Structures and unions, Data structures
 Linked List, Stacks, Queues, Conditional
Compilation, Preprocessor directives, File
operations, Variable arguments in Functions,
Command line arguments, Bitwise operations,
Typecasting

3/23/2018 ESG-NIELIT Calicut 6


Structure of a ‘C’ Program
 Preprocessor Commands
 Type definitions
 Function prototypes - declare function types and
variables passed to function.
 Variables
 Functions (main() function-Must)

3/23/2018 ESG-NIELIT Calicut 7


Character Set – Alphabets, Digits and Special symbols

3/23/2018 ESG-NIELIT Calicut 8


Constants, Variables and Keywords
Alphabets Constants
Numbers Variables
Special Symbols
Keywords

Constant - Entity that doesn’t change

Variable – Entity that may change

3/23/2018 ESG-NIELIT Calicut 9


Types of Constants

3/23/2018 ESG-NIELIT Calicut 10


Primary Constant -Integer
 Must have atleast one digit
 Must not have a decimal point
 Can be either positive or negative
 By default, positive
 No commas or blanks allowed within the integer
constant
 Range is -32768 to 32767

Ex.: 426,+400,-324,-3287

3/23/2018 ESG-NIELIT Calicut 11


Primary Constant-Real
 Must have at least one digit
 Must have a decimal point
 Could be positive or negative
 By default, positive
 No commas or blanks are allowed within a real
constant

Ex. : +325.34,467.0,-67.54,-12.675

3/23/2018 ESG-NIELIT Calicut 12


Primary Constant-Real
 Exponential form of representation
mantissa e exponent
 Mantissa and Exponential should be separated by a
letter e
 Mantissa may have a positive or negative sign
 By default, mantissa sign is positive
 Exponent must have at least one digit, can be
positive or negative and by default it is positive
 Range is -3.4e38 to +3.4e38
Ex.: +3.2e-5,12.1e18, -0.7e+5, -3.2e-5
3/23/2018 ESG-NIELIT Calicut 13
Primary Constant-Character
 Can be a single alphabet, single digit or a single
special symbol enclosed within a single inverted
commas. Both the inverted commas should
point to the left.
 ’A’ is a valid character whereas ‘A’ is not.
 Maximum length can be one character.

Ex. : 'A' , 'I' , '5'

3/23/2018 ESG-NIELIT Calicut 14


Primary Data types
 Integer
 Long
 Short
 Signed
 Unsigned

 Char
 Signed
 Unsigned

 Floats and Doubles

3/23/2018 ESG-NIELIT Calicut 15


Note: use sizeof() function to check the bytes occupied by the data types
3/23/2018 ESG-NIELIT Calicut 16
Types of C Variables
 Particular type of variable can hold only the
same type of constant.
 Integer variable holds only integer constant
 Rules for constructing different types of
constant varies whereas for constructing
variable names of all types the same set of rules
apply.

3/23/2018 ESG-NIELIT Calicut 17


Rules for constructing variable names
 Variable name is combination of 1 to 31 alphabets,
digits or underscores.
 Maximum length is 31 characters
 First character must be an alphabet or underscore
 No commas or blanks allowed
 No special symbol other than an underscore can be
used.

Ex. : si_int , m_hra , si_intr_2008

3/23/2018 ESG-NIELIT Calicut 18


Rules for constructing variable names
 It is compulsory to declare the type of variable before
use in the program. This type declaration is done at
the beginning.
 Example of type declaration.

int si, m_hra ;


float bassal ;
char code ;

3/23/2018 ESG-NIELIT Calicut 19


C Keywords
 Words whose meaning has already been explained to
the compiler
 Cannot be as variable names

3/23/2018 ESG-NIELIT Calicut 20


First ‘C’ Program
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
si = p * n * r / 100 ; /* formula for simple interest */
printf ( "%f" , si ) ;
}

3/23/2018 ESG-NIELIT Calicut 21


Rules for writing a program
 Every C statement must end with a ; , which acts
as a terminator
 All statements are entered in small letters
 Comment about the program should be enclosed
with /* */.
 Printf function is used to display value in the
screen.

3/23/2018 ESG-NIELIT Calicut 22


Printf() function
 It is defined in stdio library.
 General form of Printf() function is,
Printf("<format string>" ,<list of variables>);
 Table represents the list of format strings used in
C.
 Escape sequences are also used in Printf to
cause an escape from normal interpretation of
string.

3/23/2018 ESG-NIELIT Calicut 23


3/23/2018 ESG-NIELIT Calicut 24
Printf() function
 some examples of usage of printf( ) function:
 printf ( "%f", si ) ;

 printf ( "%d %d %f %f", p, n, r, si ) ;

 printf ( "Simple interest = Rs. %f", si ) ;

 printf ( "Print = %d \nRate = %f", p, r ) ;

3/23/2018 ESG-NIELIT Calicut 25


Escape Sequences

3/23/2018 ESG-NIELIT Calicut 26


Scanf() function
 To enter the data from the keyboard during run-time.
scanf ( "format string", list of addresses of
variables ) ;
Example:
scanf ( "%d %f %c", &c, &a, &ch ) ;

3/23/2018 ESG-NIELIT Calicut 27


Scanf() function
 We are sending addresses of variable to scanf()
function.
 Values received from the keyboard must be
dropped into variables corresponding to these
addresses.

3/23/2018 ESG-NIELIT Calicut 28


C Instructions
 Three type of instructions in C.
 Type Declaration Instruction-To declare the

type of variable used in C program


 Arithmetic Instruction-To perform arithmetic

operations between constants and variables


 Control Instruction-To control the sequence

of execution of various statements in C


program

3/23/2018 ESG-NIELIT Calicut 29


Type Declaration Instruction
 Used to declare the type of variable used in the
program and written at the beginning of main()
function.
 While declaring variable, we can also Initialize.
int i=10,j=25;
Order of definition matters in some cases.
int i = 10, j = 25 ; is same as int j = 25, j = 10 ;
float a = 1.5, b = a + 3.1 ; is alright, but
float b = a + 3.1, a = 1.5 ; is not.
int a, b, c, d ; a = b = c = 10 ; will work,
int a = b = c = d = 10 ; will not.
3/23/2018 ESG-NIELIT Calicut 30
Arithmetic Instruction
 Consists of variable name on the left hand side
of =,variable name and constants on the right
hand side of the equal.
 Variables and constants appearing on the right
hand side of the equal are connected by
arithmetic operators like +,-,* and /.
 Arithmetic statement could be of three types.

3/23/2018 ESG-NIELIT Calicut 31


Arithmetic Statement
 Integer mode-operands are integer variables and
integer constants.
 Real mode-operands are real variables and real
constants
 Mixed mode-some of the operands are real and
some of them are integers.
Ex. : float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

3/23/2018 ESG-NIELIT Calicut 32


Compilers
 GCC from Linux
 Turbo C
 Ansi C
 Visual C

3/23/2018 ESG-NIELIT Calicut 33


3/23/2018 ESG-NIELIT Calicut 34

Potrebbero piacerti anche