ch1 c(basics)

Upload: jayvijay1988

Post on 04-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Ch1 c(Basics)

    1/22

    C - Introduction

    Ramiz Ahmed

    (Trainer)

  • 7/31/2019 Ch1 c(Basics)

    2/22

    What Is C

    Currently, the most commonly-used language forembedded systems.

    High-level assembly.

    Very portable: compilers exist for virtually everyprocessor.

    Easy-to-understand.

    Produces efficient code

  • 7/31/2019 Ch1 c(Basics)

    3/22

    History

    Developed between 1969 and 1973 along with Unix.

    Due mostly to Dennis Ritchie.

    Designed for systems programming -

    Operating systems. Mobile devices like cellular phones and

    palmtops. 3D computer games.

    Hardware devices. Common consumes devices like microwave

    ovens, washing machines and digitalcameras.

  • 7/31/2019 Ch1 c(Basics)

    4/22

    C character set

    Alphabets A,B,.... Y,Za,b,.... y,z

    Digits 0,1,2,3,4,5,6,7,8,9

    Special Symbols ~ ! @ # $ % & * ( ) _ = +[ ] \ { } : ; ' < > , . ? /

  • 7/31/2019 Ch1 c(Basics)

    5/22

    Constants and Variables

    Constant is an entity that doesn't change. Variable is an entity that may change.

    Location whose name is x can hold different values at adifferent times x is known as a variable.As again this 3 or

    5 do not change, hence are known as constants.

  • 7/31/2019 Ch1 c(Basics)

    6/22

    Types of constants

  • 7/31/2019 Ch1 c(Basics)

    7/22

    Rules for constructing integerconstants

    1. An integer constant must have at least one digit.

    2. It must not have a decimal point.

    3. It can be either positive or negative.

    4. If no sign precedes an integer constant it is assumed to be

    positive.

    5. No commas or blanks are allowed within an integer constant.

    6. The allowable range for integer constants is

    2147483648 to +21474836477. Size = 4 bytes.

    For Example 426 , -8000

  • 7/31/2019 Ch1 c(Basics)

    8/22

    Rules for constructing realconstants

    A real constant must have at least one digit.

    It must have a decimal point.

    It could be either positive or negative.

    Default sign is positive. No commas or blanks are allowed within a real constant.

    Size = 4 bytes.

    Ex.: +325.34

    426.0

    -32.76

    -48.5792

  • 7/31/2019 Ch1 c(Basics)

    9/22

    Continue...

    The mantissa part and the exponential part should be separated by aletter e.

    The mantissa part may have a positive or negative sign.

    Default sign of mantissa part is positive.

    The exponent must have at least one digit, which must be a positive ornegative integer.

    Default sign is positive.

    Range of real constants expressed in exponential form is -3.4e38 to3.4e38.

    Ex.: +3.2e-54.1e8

    -0.2e+3

    -3.2e-5

  • 7/31/2019 Ch1 c(Basics)

    10/22

    Rules for constructing characterconstants

    A character constant is a single alphabet, asingle digit or a single special symbol enclosedwithin single inverted commas. Both the invertedcommas should point to the left. For example, Ais a valid character constant whereas A is not.

    The maximum length of a character constant canbe 1 character.

    Range -128 to +127

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

  • 7/31/2019 Ch1 c(Basics)

    11/22

  • 7/31/2019 Ch1 c(Basics)

    12/22

    Continue...

    Rules --A variable name is any combination of 1 to 31

    alphabets, digits or underscores.The first character in the variable name must be an

    alphabet or underscore.No commas or blanks are allowed within a variablename.No special symbol other than an underscore (as in

    gross_sal) can be used in a variable name.

    Ex.: si_intm_hrapop_e_89

    These rules remain same for all the types of primary and

    secondary variables.

  • 7/31/2019 Ch1 c(Basics)

    13/22

    C keywords

    There are only 32 keywords available in C.The keywords cannot be used as variable namesbecause if we do so we are trying to assign a newmeaning to the keyword, which is not allowed by

    the computer.It would be safer not to mix up the variable namesand the keywords.

  • 7/31/2019 Ch1 c(Basics)

    14/22

    Hello World in C

    #includevoid main(){

    printf(Hello, world!\n);}

    http://www.worldofshopping.com/assets/product_images/aar-MMWG1.jpg
  • 7/31/2019 Ch1 c(Basics)

    15/22

    Simple interest Example in C

    /* Calculation of simple interest */main( ){int p, n ;

    float r, si ;p = 1000 ;n=3;r = 8.5 ;

    /* formula for simple interest */si = p * n * r / 100 ;printf ( "%f" , si ) ;}

  • 7/31/2019 Ch1 c(Basics)

    16/22

    Getting values from user

    #includeInt main(){int p,n;Float r,si;printf(Enter values of Principle,NOI,ROI);scanf(%d%d%f,&p&n&r);

    si=p*n*r/100;printf(simple interest=%f);Return 0;}

  • 7/31/2019 Ch1 c(Basics)

    17/22

    C Instructions

    Type declaration instruction To declare the typeof variables used in a C program.Arithmetic instruction To perform arithmeticoperations between constants and variables.Control instruction To control the sequence ofexecution of various statements in a C program.

    C ti

  • 7/31/2019 Ch1 c(Basics)

    18/22

    Continue...

    Type declaration instruction

    Int a;Float b;Char name;

    Arithmatic instruction* , / , - , +

    Arithmatic operations can be performed on int's,

    float's and char'sChar x,y;Int z;X='a'; y='b';

    z=x+y; /* a and b are 97 and 98*/

  • 7/31/2019 Ch1 c(Basics)

    19/22

    Conversion integer to float

  • 7/31/2019 Ch1 c(Basics)

    20/22

    Type conversion in assignments

    K is an integer and a is real variable.

  • 7/31/2019 Ch1 c(Basics)

    21/22

    Hierarchy of operations

    The priority or precedence in which the operations in an arithmeticstatement are performed is called the hierarchy of operations.

    I=2*3/4+4/4+8-2+5/8

  • 7/31/2019 Ch1 c(Basics)

    22/22

    THANK YOU