Constant and variables in C class 9




    SEBA 
    CLASS- 9
    CHAPTER-15
     VARIABLES AND CONSTANTS IN C


    NoteThe star Ã¬ mark contained question specify that they are most likely to come in examination and very important. But I suggest you to read all the questions that I post on my site.


    OBJECTIVE TYPE QUESTIONS

    1. Fill in the blanks:

    a) The variable is portion of memory used to store a value.

    b) The compiler specific keywords words cannot be used as a name of an identifier.

    c) The fundamental data types in c are integer, floating numbers, characters and string.

    d) The real constant can be written in Decimal and E character.

    e) Each variable declared as float requires four bytes of storage.

    f) unsigned types can be represented only positive values including zero.

    g) The named portion of memory stores a value at the time of declaration of the variable.

    h) Constant refers to a fixed value that does not change during the execution of a program.

    i) If there is no sign before an integer constant, it assumed to have a plus sign.

    j) An octal number is preceded by Zero characters.

    ¬ To display size of any data types, we take help of sizeof special operator.

    ¬ Void indicate nothing.

    ¬ For integers type of data 4 or 2 bytes of memory is allocated.

    ¬ For float type of data 4 bytes of memory is allocated.

    ¬ For double type of data 8 bytes of memory is allocated.

    ¬ For char type of data 1 bytes of memory is allocated.

    ¬ For void type of data 1 or 0 bytes of memory is allocated.

    ¬Constants are also known as Literal.

    ¬ Non- primitives’ data types are also called as derive, complex, or compose data types.


    Table of Content

    2. Write true or false:

    a) The identifier name can start with numeric value. FALSE

    b) Signed data type can represent only positive values. FALSE

    c) Signed and unsigned specifiers are always written before the data type. FALSE

    d) You can change the value of a constant during execution of a program. FALSE
    e) Integer constant can only be written in decimal numbers. FALSE

    f) The hexadecimal number is preceded b 0. TRUE

    g) A string constant consist of more than one character enclosed in single quotes. FALSE

    h) Escape sequence code is a special non graphical character preceded by a backslash. TRUE

    i) Type casting allows conversion of none data type into another.TRUE

    j) scanf() is used for reading the data input through the keyboard. TRUE

    3. Choose the correct options:

    a) A C keyord. All of these

    b) The portion of memory used to store a value. Variable

    c) Which one is a correct identifier in C? total_1

    d) Which one in not a built-in data type? CHAR

    e) Which one is not correct way of defining data type? Inta;b;

    f) Data type of “ADD” is int

    g) Data type of 37.56 is float

    h) Data type of -34 is int

    i) Which one is not a character constant. “8”

    j) Which one is not a correct integer constant in C? 1,776


    Table of Content

    DESCRIPTIVE TYPE QUESTIONS
    A. Short answer questions:

    1) What are C keywords?

    C keywords are the reserved words that have predefine meaning in a programming language for a specific task. C has total 32 reserved words. They are given below:

    auto

    double

    int

    struct

    break

    else

    long

    switch

    case

    enum

    register

    typeof

    const

    extern

    return

    union

    continue

    float

    short

    unsigned

    default

    for

    signed

    volatile

    do

    goto

    sizeof

    void

     

    if

    static

    while

     

    2) Mention the name of any two built-in data types.

    The name of two built in data type are integers and characters

    3) Which of the following are valid constant? If valid, specify whether integers or real.

    i) 0.5 →Invalid

    ii) 9.2e – 0.5→Valid, real constant

    iii) 1234567 →Valid integer constants

    iv) 012CDF →Invalid    

    v) 0X87e3h →Valid, real constant         

    vi) 12345678L→ Valid, Integer constants.

     

    4) What do you mean by constant in C language?

    Constant refers to a fixed value that does not change during the execution of the program. It also known as literals.

    5) What are the two ways of defining constant in a C program?

    The two ways of defining constant in a C program are:

    i) Using #define

    ii) Using const Keyword.

    6) Which of the following are a valid character constant.

    ‘a’ and $ are the valid characters constant.

    g) While writing integer constants, how they are decimal, octal and hexadecimal constants distinguish from one another?

    While writing integer constants:

    The decimal constants are distinguished by valid integer which consists of 0 to 9.

    An octal number is preceded by a 0. And 

    A hexadecimal number is preceded by the characters 0x or 0X.

    7) What is an escape sequence? Name any two escape sequence codes in C.

    Escape sequence are special non-graphical characters preceded by a backslash. Any two escape sequence are \n and \r

    8) Name the two types of C datatype conversion.

    The two data type conversion in C are:

    i. Implicit

    ii. Explicit 

    9) What is the use of type casting?

    Type casting is used to convert the data type of one operand to another data.


    Table of Content


    B. Long answer questions: 

    1. List the rules for naming an identifier.

    The rules or guidelines for writing identifier name are given below:

    i) Identifier should be meaningful and descriptive.

    ii) Keywords should never be used as an identifier.

    iii) The first character must be an alphabets or underscore.

    iv) The first character should not be a number.

    v) All succeeding characters can be alphabets, a digit, or underscore.

    vi) No special characters are allowed excepts underscore. Not even space.

    vii) More then one successive underscore should not be used.

    viii) More than one identifier names should not be same in the same scope.

    ix) Identifier are case sensitive.

     

    2. Which of the following are valid identifiers? If not give reasons.

    i) emp-name → Invalid, because cannot use symbols -.

    ii) pinCode→ Valid

    iii) emp name → Invalid, because cannot use space.

    iv) Emp_name → Valid

    v) $pay→ Invalid, because cannot used special characters.

    vi) 2file→ Invalid, because cannot start with numeric

    vii) Name→ Valid

    viii) Record1: Valid

    ix) 123name→ Invalid, because cannot start with numeric

    x) 1 Address→ Invalid, because cannot start with numeric, also cannot use space.

     

     

    3. What are Constants in C? Name the various C constant with examples.

    Constant refers to the fixed values that do not change during the execution of a program. A constant can be a numbers, character, or character, or characters string that can be used as a value in program. Below are the different types of constants we can use in C.

    i) Integer constant: It consists of digits 0 to 9 (whole number). Integer constant are of three types:

    * Decimal constant (base 10) → 0 to 9 Example: 23,72, 333, +790, -234

    * Octal Constant (base 8) → 0 to 8 Example: 012, 0113, 011

    * Hexadecimal (base 16) → (0 to 9, A to F) Example: 01A, 0FB, )x4B

    ii) Real Constant: Number with a decimal point are called real constant. It is also known as Float Value.

    * It consists of digits 0 to 9. Example: 3.2, 3.1415, 6.02e23, 345.171

    iii) Character constant → A character constant represent single alphabet, digit, or a single symbol enclosed within a pair of quotes (‘’).

    * Example: ‘A’, ‘1’, ‘#’

    iv) Strings character constants: A string constant is a sequence of characters enclosed in pair of double quotes (“”).

    * Example: “Hello”, “How are You”, “p”

     

    4. List the rules that apply to an integer constant?

    The rules for using the integer constant are:

    i. It consists of digits 0 to 9.

    ii. It must not have decimal point.

    iii. It does not include commas and blank space.

    iv. We can add plus (+) or minus (-) sign if needed. if there is no sign, it assumed to have positive value a plus sign (+).

    v. The allow range for constant is -32768 to 32767.

     

    5. Lists the rule that apply to a real constant?

    The rules for using the real constant are:

    i. It consists of digits 0 to 9.

    ii. It must have decimal point.

    iii. It does not include commas and blank space.

    iv. We can add plus (+) or minus (-) sign if needed. if there is no sign, it assumed to have positive value a plus sign (+).

    v. The allow range for constant is -32768 to 32767.

     

    6. How do string constant differ from character constant?

    The different between string constant and character constant is that:

    Character constant can only be represented as a single character. Whereas, string constant is an array of characters with a null character at the end of the string.

     

    7. Write appropriate declaration for each of the following groups of variables in C.

    Integer variable: x,y

    Real variable:a,b,c

    Character variables: p,q,r

     

    Declaration for each of the following groups of variables in C are as follows:

    Int x,y;

    float a,b,c;

    char ‘p’, ‘q’, ‘r’;

     

     

    8. Write appropriate declaration and assign the given initial values for the following variables

    Floating point variables: a= -4.6, b= 0.05

    Integer variables: x=2, y= 121, z= -5

    Character variables: l1 = ‘c’, h2= ‘o’

    Declaration and assign of the given initial values for the following variables are:

    float a = -4.6, b= 0.05;

    int x = 2, y = 121, z = -5;

    char l1= ‘c’, h2 = ‘0’

     

     

    9. A C program has the following declaration:

    int I,j; long int ix; short s; float x; double dx; char c;

    Determine the type of each of the following expressions:

    i. i+c   ii. int(dx) + ix    iii. x+i    iv. dx + x    v. x + c   vi. s + c


    i) i+ c → Integer

    ii) int(dx) + ix → long double

    iii) x+i   float

    iv) dx + x → double

    v) x + c  float

    vi) s + c → int

     

     

    10. Match the following:

    i. Function                                                   Xmain()                      

    ii. Character Constant                               A              

    iii. Format Specifier                                  %d%f%c

    iv. Input Function                                     scanf()

    v. Output                                                   functionprintf()

    vi. Address of operator                           &

    vii. Statement terminator                       ;

    viii. Escape sequence                              \n

    ix. Integer Constant                               2365

    x. Exponent form                                    4E-2

     

    11. Check the correct syntax of the following C program:

    #include <stdio.h>

    Int main()

    {

    Int a,b;

    printf(“ Enter two numbers”);

    scanf(“%d%d”,a,b);

    printf(“You entered %d%d”,ab);

    return0;

    }

    #include <stdio.h>

    Int main()

    {

    Int a,b;

    printf(“ Enter two numbers”);

    scanf(“%d%d”,&a,&b); /*→ Error is here required address in variables in scanf()*/

    printf(“You entered %d%d”,ab);

    return0;

    }


    Table of Content

    Additional Questions:

    ¬What is tokens?

    A smallest unit of any programing language is called as tokens (word).

    OR

    Tokens are building block of C programming.

    OR

    A meaningful combination of C characters is known a C tokens.

    OR

    In passage of text, individual words and punctuation marks are called as tokens.

     

    ¬What are the six types of tokens?

    The six types of tokens are:

    i) Comment

    ii) Keywords and Data type

    iii) Identifiers

    iv) Constants and Variables

    v) String, Symbol and Characters

    vi) Operators

     

    ¬What are the primary or built-in or fundamental data types?

    The built-in or fundamental data types are

    i. integer

    ii. float type

    iii. void type.

    ¬What is integral widening conversation?

    Whenever char or short int appears in an expression, it is converted to int. this is called integral widening conversation.

     

    ¬ What is initialization?

    Initialization is the method of assigning the value and defining the data type at the same time.

     

    ¬What are Variables?

    Variable are those who assign the value in itself.

    OR

    It is a data name that refers to a memory location where data value may be store.

     

    ¬What do you mean be declaration and definition of a variable?

    Declaration means announcing the properties of a variable to the compiler like

    i. Size of a variable

    ii. Name of Variable.

    Definition: means allocating memory to a variable.

     

    ¬ What is string constant and character constant?

    String constant is a sequence of characters enclosed within the pairs of double quotes. For Example: “Hello”. It is a string. Whereas,

    Character constant is a single character including all the alphanumeric characters (A to Z, a to z, 0to 9) and other character such as *,#,@,!, & etc.

     

    ¬Name the two types of data types which are used to store and process different types of data type in C.

    To process and store different types of data C provide two different types of data type. They are

    i. Primitive’s data types

    ii. Non primitive data type (it is also called as derive, complex, or compose data types).

     

    ¬How many types of data type are available in C language? Name them.

    There are six types of data types available in C language. they are:

    i. int indicate integer or whole numbers.

    ii. float and double indicate real number or fractional numbers with decimal point

    iii. char indicate character type of data, single character, digits or symbol enclosed with single quotations.

    iv. Void indicate noting

     

    ¬ What is the use of Data types modifier? How does it help?

    Data type modifier is used to modify the properties of primitives or basic data types excepts float and void data types according to the application requirements. With the help of data types modifiers we can:

    i. Modify the size of (i.e the amount of memory to be allocated).

    ii. Modify the size (i.e. decide only +ve or both +ve and -ve can be stored).

     

    ¬How many types of data types modifier are there? Name them with theirs uses?

    There are 4 types of data types modifiers. They are:

    i. Short and Long: short and long modifier are used to modifies the size of primitive’s data type.

    Short and long are used to increase and decrease the size of primitive’s data type.

    ii. Sign and Unsigned: Signed and unsigned modifier are used to modify the sign of primitive’s data types.

    Sign and Unsigned are used to restrict the size whether only +ve value can be able to store or both +ve and – ve value can be stored in a specific type of memory location.

     

    ¬ What is Implicit type conversion and explicit type conversion? Explain with example.

    Implicit type conversion is done automatically by the compiler itself without any user intention. The data type of all operand are automatically converted into datatype operand with the largest data type.

    Example:

    #include <stdio.h>

    Int main()

    {

    int a=5;

    float b= 2.5, c;

    c=a+b; /* here, float is the largest type, so a is implicitly converted to a float */

    printf(“c =%f”,c);

    return0;

    }

    Explicit type conversion is done explicitly by the user by prefixing the operand or expression with the data type to be converted. It is also called type casting.

    Syntax: (data type) expression or operand.

    Example:

    #include <stdio.h>

    Int main()

    {

    int a=5, b= 8, c= 9;

    float avg;

    avg =(float) (a+b+c)/ 3; /* here, the value of expression, (a+b+c)/3 is explicitly converted to float t match the avg data type */

    printf(“average=%f”,avg);

    return0;

    }

    ¬ What is an identifier?

    Identifiers are a sequence of characters which help us to identify specific parts of a program. It is a name given to a program elements like variable, constant, function and user-define data by the programmers


    Table of Content
    PLIZ SHARE YOUR EXPRESSION