ANSI C (by Devendra Sir)

download ANSI C (by Devendra Sir)

of 78

Transcript of ANSI C (by Devendra Sir)

  • 8/2/2019 ANSI C (by Devendra Sir)

    1/78

  • 8/2/2019 ANSI C (by Devendra Sir)

    2/78

    Language:The way of communication between computer and user known as language.

    Program:The set of instruction arrange in a sequence to solve a problem known as program.

    Software:The group of programs is known as software.

    Programming Language:All the languages were we can design our program is known as programming language.

    History of C languageThe C language developed in 1972 by Dennis Ritchie in AT & T Bell labs, USA. It is developed

    by B language to reduce its problem.

    Features of C language1. Middle Level Language:

    The C language is a combination of high level and low level programming.

    The languages where program design using 0 and 1 symbols known as low level

    languages.

    The languages where we have general English words in our program is known as high

    level languages.

    2.

    Modular ProgrammingThe C language provides permission to design a complete program by dividing them in

    multiple parts. These parts known as module.

    Example:

    The program of calculator design using four parts or modules i.e. Addition, Subtraction,

    Multiply and Divide.

    3. Procedural Programming

  • 8/2/2019 ANSI C (by Devendra Sir)

    3/78

    In the C language a complete program design using the multiple steps and we have to

    provide complete logic from top to bottom of program.

    4. Case SensitiveIn the C language all the instructions design in small letters that means C language is a

    case sensitive.

    5. C provide feature to design system software.Example: UNIX operating system.

    6. User FriendlyIt is a user friendly language because it is easy to use, understand and learn.

    Elements of ProgramWhen we design a program is C language we have to use some elements the elements are

    follows:-

    1. #includeIt is known as Pre- Possessive Directive it forms as a connector to connect pre- define

    instruction with our program.

    2. The collection of instructions available in a file known as header file. It is available in the

    library of C identifies by (.h).

    3. Main()In our program we have lots of instructions and we have to execute them in a sequence

    this task can be performing by main function.

    The function started by {opening bracket and closed by closing bracket}.

    4. In the C language every instruction or line enclosed by (;) semi colon. It represent endof statement.

    Working steps of programWhen we design a program in C we have to follow some steps from starting to the result.

    Design a program in C language identified by (.C). (.C) program translate into machine code by a translator known as compiler. Using ALT+

    F9 key.

    Now the machine code must be execute using the CTRL+F9 key and then we have aresult.

  • 8/2/2019 ANSI C (by Devendra Sir)

    4/78

    Diagram of working steps of a C program

    Output Function (Printf)The printf is a output function it is utilize to print our data on the output screen. The

    printf function is available in the stdio.h header file.

    Syntax: printf("Enter your message here);

    \n (New line character)To transfer the cursor at the new line C provides a special character known as \n. It

    perform with printf and transfer cursor at new line.

    Example:

    C TokensWhen we design a program we have to use some pre- define instruction that can help to

    develop a complete program. The smallest unit of program that are utilize to design

    our programs known as tokens.

    program.c compile(ALT+F9) program.obj Run(CTRL+F9) program.exe

    #include

    main()

    {

    printf("Hello \n World");

    }

    #include

    main()

    {

    printf("Hello\n");

    printf("World");

    }

    Hello

    World

  • 8/2/2019 ANSI C (by Devendra Sir)

    5/78

    1. KeywordsAll the pre define words in the library of C that are directly utilize in our program we need not

    to define their explanation known as keywords.

    Example:

    Main, include, int, float, if, switch, for, while, do etc.

    Note: There are 32 keywords in C library.

    2. DatatypesIf we have a program then we have to use various types of data in it. Basically we have 3 basic

    representation of data these are Integer numbers (2,5,32,-45 etc), Floating numbers (1.2, 56.32

    etc) and Characters (a,c,v etc).

    So we have to define some basic explanation for the every data before utilization the

    programming elements or symbol which are utilize to explain the nature of data known as

    datatype.

    A data type provide three basic explanation for every data.

    i. What is the nature of data?ii. What is the minimum and maximum capacity of data?

    iii.

    How many size require by data?

    Name Keywords Size Identifier Memory Limit

    Integer Int 2 Byte %d -32768 to 32767

    Float Float 4 Byte %f

    Character Char 1 Byte %c 0 to 128

    3. ConstantThe programming elements whos value decided at design time is known as constants. They

    have values which is previously decided.

    Syntax: datatype constant_name= value;Example: int a=10;

    Float b=10.25;

    Rules:

    i. They cannot declared similar name constant in our program.ii. The dissimilar members represents in seprate line but similar member represent in a

    single line seprate by (,).

    Example: int a=5,b=20;

    Tokens

    Keywords

    (main, #include)

    Datatypes

    (int, float)

    Operators

    (+,-,/,*)Variables Constant Expression

  • 8/2/2019 ANSI C (by Devendra Sir)

    6/78

    Q- Write a program to find out the square of a constant?

    #include

    Main()

    {

    Int a=2, b=0;

    B=a*a;

    Printf(Square=%d,b);

    }

    Q- Write a program to find out the multiply of 3 numbers?

    #include

    Main()

    {

    Int a=1, b=2, c=3, d=0;

    D=a*b*c;

    Printf(Answer=%d,d);

    }

    4.

    Variable

    The programming element whos value decided at the rum time of program by user known as

    variable.

    Input Function (Scanf)To accept the value at rum time by user we have scanf function. The function required name of

    variable with its address the address identify by & sign and scanf also required name of

    variable where we want to insert value.

    Syntax: scanf(identifier,&variable_name);

    Example:scanf(%d,&a);

    Q- Input two number by user and find out sum?

    #include

    Main()

    {

    Int a,b,sum=0;

  • 8/2/2019 ANSI C (by Devendra Sir)

    7/78

    Printf(Enter the numbers);

    Scanf(%d%d,&a,&b);

    Sum=a+b;

    Printf(Sum=%d,sum);

    }

    Q- Take input from user quantity and price and then print amount?

    #include

    Main()

    {

    Int quantity,prize,amount=0;

    Printf(Enter the quantity);

    Scanf(%d,&quantity);

    Printf(Enter the prize);

    Scanf(%d,&prize);

    Amount=quantity*prize;

    Printf(Amount=%d,amount);

    }

    Q- Design a program to input marks in hindi, English and maths by user and find out sum and average?

    #include

    #include

    Main()

    {

    Int h,e,m,sum,avg;

    Clrscr();

    Printf(Enter the marks);

    Scanf(%d%d%d,&h,&e,&m);

    Sum=h+e+m;

  • 8/2/2019 ANSI C (by Devendra Sir)

    8/78

    Avg=sum/3;

    Printf(Sum=%d\nAvg=%d,sum,avg);

    Getch();

    }

    Q- Input the salary by user find out the 10% bonus and total salary?

    #include

    #include

    Main()

    {

    Int sal,bon,tot;

    Clrscr();

    Printf(Enter the salary);

    Scanf(%d,&sal);

    Bon=sal*(10/100);

    Tot=sal+bon;

    Printf(Total=%d,tot);

    Getch();

    }

    Q- Input the prize of book by user find out 10% discount and then final amount?

    #include

    #include

    Main()

    {

    Int pri,dis,tot;

    Clrscr();

    Printf(Enter the prize);

  • 8/2/2019 ANSI C (by Devendra Sir)

    9/78

    Scanf(%d,&pri);

    Dis=pri*(10/100);

    Tot=pri-dis;

    Printf(Final Amount=%d,tot);

    Getch();

    }

    OperatorThe programming element or symbols that are utilize to connect the elements with each other is

    known as operator. The operators are symbols that define what is our operation on the data.

    The members where operator execute known as operants.

    Categories of operators

    According to executionAccording to the execution or run of a operator we can define three basic types:-

    a) Uniary operatorb) Binary operatorc) Ternary operator

    1. Uniary operatorIf operator execute using a single value then it is called uniary operator.

    Example:

    ++,-- etc.

    2. Binary operatorIf the operator executes using two values then it is called as binary operator.

    Example:

    +, -, *, / etc

    3. Ternary operatorIf the operator executes using three values then it is called ternary operator.

    Example:

    ?: etc

    According to natureWhich type of value accepted by operator known its nature according to this concept

    the operator are:-

    a) Air thematic operatorb) Bitwise operatorc) Relational operatord) Conditional operatore) Logical operator

  • 8/2/2019 ANSI C (by Devendra Sir)

    10/78

    1. Air thematic operatorThe operators that executes using the numbers or airthematic values known as Air thematic

    operator.

    Example:

    +, -, *, /, ++, -- etc

    Difference between + and ++

    ++ +

    Increment Additon

    Increase value by one Add two numbers

    Uniary operator Binary operator

    Increment operatorThe increment operator used to increase the value of an element by 1. It is a uniary

    operator and perform into two formats.a) Prefix

    If the operator declared before the elements these it is prefix. It have highest

    priority that means increase the value by 1 then perform other task.

    b) PostfixWhen operator assign after the variables then it is postfix. That means it have

    last priority we have to perform other tasks then increase the value by one.

    Decrement operatorIt is a uniary operator that can reduce the value of an element by one. It also available in

    two categories as a prefix and postfix.

    2. Bitwise operatorThe operator that can executes using the binary data known as bitwise operator.

    Shift LeftThe shift left operator accept a number by user and convert it in binary and shift one by

    one bit at the left side after that we have a new answer in the binary format.

    Shift RightLike the shift left. It can also accept a number by user and design its binary and then

    shift a bit from right side.

    3. Relational operatorAll the operators that are utilize two design a condition for the comparison known as relational

    operator.

    Symbols Meaning

    > Greater then

    < Less then

    == Exactly equal to

    >= Greater then equal to

  • 8/2/2019 ANSI C (by Devendra Sir)

    11/78

    b)?a:b;

    F=(c>d)?c:d;

    G=(e>f)?e:f;

    Printf(Max is=%d,g);

    Getch();

    }

    5. Logical operatorAll the operators that return the answer in the true and false known as logical operators.

    The logical operators are utilize when we want to connect more than

    one conditions with each other. We have three representation of logical operators.

    AND (&&)The AND operator always return true if all the connected conditions are true.

    Condition one Condition two &&

  • 8/2/2019 ANSI C (by Devendra Sir)

    12/78

    True True True

    True False False

    False True False

    False False False

    OR (||)The OR operator return true if all the conditions are true or any one condition is true.

    Condition one Condition two ||

    True True True

    True False True

    False True True

    False False False

    NOT (!)It does not connect more than one conditions. It is utilize to reverse the conditions.

    Condition !

    True False

    False True

    Type castingThe type casting is a concept or an a operator which is utilize to convert the nature of element in

    our program. The type casting provided facilities to find our the results of higher data type using

    the elements of lower data type. It is benefit because it provide memory advantages.

    In C language we have two representation for the type casting.

    1. Implicit type castingThe 1st representation of type casting where we need not to define about the nature of dataknown as implicit type casting.

    Example:

    #include

    Main()

    {

    Int a=4,b;

    Float b;

    B=a/2;

    Printf(%d,b);

    }

    Output: 2.0

    2. Explicit type castingThe representation of type casting that provide permission to decided what is the new data type

    of result known as user define type cast.

    Example:

    #include

  • 8/2/2019 ANSI C (by Devendra Sir)

    13/78

    Main()

    {

    Int a=5;

    Float b;

    B=(float)(a/2);

    Printf(%f,b);

    }

    In the following example we have a variable with integer datatype. Now convert

    the integer variable into the float at run time.

    Control statement Switch case Goto label If else

    1. Switch caseThe switch case is a first control statement. It provide facility to divide our program into multiple

    partitions. By using the switch case we can execute the particular module or part from our

    program which is divided by user. To use the switch we have to follow some keywords:-

    SwitchIt is a controller that can execute the particular case which is required by user. The

    switch keyword accept name of case to execute. If the case is not available then transfer

    the cursor to the default.

    CaseTo design the partitions in our programs we have case keyword.

    BreakTo seprate the multiple case at run time we have break keyword. It exit the user from

    control statement.

    DefaultIf we does not have any case to execute then default partition perform.

    Syntax:

    Switch(case_variable){

    Case value:

    Statement;

    Break;

    Case value:

    Statement;

  • 8/2/2019 ANSI C (by Devendra Sir)

    14/78

    Break;

    Case value:

    Statement;

    Break;

    Default:

    Statement;

    }

    Q- Write a program to input a number by user and print the message according to the condition?

    #include

    #include

    Main()

    {

    Int ch;

    Clrscr();

    Printf(Enter the choice);

    Scanf(%d,ch);

    Switch(ch)

    {

    Case 1:

    Printf(One);

    Break;

    Case 2:

    Printf(Two);

    Break;

    Case 3:

    Printf(Three);

  • 8/2/2019 ANSI C (by Devendra Sir)

    15/78

    Break;

    Default:

    Printf(Invalid);

    }

    Getch();

    }

    Disadvantages of switch case It does not provide permission to use the relational and logical operator. The name of case does not decided by user. We have a single default for the multiple cases.

    2. Goto labelIt is a first concept where we can design a program with the multiple partitions to design the

    gote label we have some steps:-

    Every partition have an identification known as label. To select the name of label for execution we have goto keyword.

    Syntax:

    Label name:

    Statement;

    Lable name:

    Statement;

    Goto label_name;

    Disadvantage of Goto label It does not support relational and logical operators. The name of label with goto decided by programmer and not by the user. If user enter the name of label which is not available then we have an error in the

    program.

    3. IfelseIt is a 3

    rdcontrol statement that provide facility to use every type of operators in our logic. The if

    else depends on keywords.

  • 8/2/2019 ANSI C (by Devendra Sir)

    16/78

    IfThe if keyword accept a condition and every condition have two answers either true or

    false after the if condition we always have true partition.

    ElseThe else always represent the partition which is execute when the condition is false.

    Simple if Ifelse Nested if -else

    If(condition)

    True;

    If(condition)

    True;

    Else

    False;

    If(condition)

    True;

    Else

    If(condition)

    True;

    Else

    False;

    Q- write a program to input a number by user and findout the number is even or odd?

    #include

    #include

    Main()

    {

    Int no;

    Clrscr();

    Printf(Enter the Number to check);

    Scanf(%d,&no)

    If(no%2==0)

    Printf(Number is EVEN);

    Else

    Printf(Number is ODD);

    Getch();

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    17/78

    Q- write a program to input marks of hindi, English and maths and findout the sum, average and print

    the grade?

    #include

    #include

    Main()

    {

    Int h,e,m,sum=0,avg=0;

    Clrscr();

    Printf(Enter the marks);

    Scanf(%d%d%d,&h,&e,&m);

    Sum=h+e+m;

    Avg=sum/3;

    If(avg>70)

    Printf(A Grade);

    Else

    Printf(B Grade);

    Getch();

    }

    Q- Write a program to input salary by user find out the total salary if salary is greater than 20,000 then

    bonus is 20% otherwise 10% ?

    #include

    #include

    Main()

    {

    Int sal,tot,bon;

    Printf(Enter the salary);

    Scanf(%d,&sal);

  • 8/2/2019 ANSI C (by Devendra Sir)

    18/78

    If(sal>20000)

    Bon=sal*(20/100);

    Else

    Bon= sal*(10/100);

    Tot=sal+bon;

    Printf(Total Salary=%d,tot);

    Getch();

    }

    Q- Write a program to input salary by user if salary greater than 20,000 then bonus is 20% and pf is

    10% otherwise bonus is 10% and pf is 5% ?

    #include

    #include

    Main()

    {

    Int sal, tot, bon, pf;

    Clrscr();

    Printf(Enter the salary);

    Scanf(%d,&sal);

    If(sal>20000)

    {

    bon=sal*(20/100);

    Pf=bon*(10/100);

    }

    Else

    {

    Bon=sal*(10/100);

    Pf=sal*(5/100);

  • 8/2/2019 ANSI C (by Devendra Sir)

    19/78

    }

    Tot=(sal+bon)-pf;

    Printf(Total Salary=%d,tot);

    Getch();

    }

    Q- Write a program to find out the maximum from 2 numbers?

    #include

    #include

    Main()

    {

    Int a,b;

    Clrscr();

    Printf(Enter the numbers);

    Scanf(%d%d,&a,&b);

    If(a>b)

    Printf(A is max);

    Else

    Printf(B is max);

    Getch();

    }

    If with logical operators

    Q- Write the program to find out the maximum from 3 numbers?

    #include

    #include

    Main()

  • 8/2/2019 ANSI C (by Devendra Sir)

    20/78

    {

    Int a,b,c;

    Clrscr();

    Printf(Enter the numbers);

    Scanf(%d%d%d,&a,&b,&c);

    If((a>b)&&(a>c))

    Printf(A is max);

    Else

    If(b>c)

    Printf(B is max);

    Else

    Printf(C is max);

    Getch();

    }

    Q- Write a program to input time by user if the time is between 0 to 12 then print good morning

    otherwise good afternoon?

    #include

    #include

    Main()

    {

    Int t;

    Clrscr();

    Printf(Enter the time);

    Scanf(%d,&t);

    If((t>0)&&(t

  • 8/2/2019 ANSI C (by Devendra Sir)

    21/78

    Printf(Good Afternoon);

    Getch();

    }

    Q- Write a program to input marks of hindi, English and maths. Find out the average and print the

    grade according to the condition?

    95-80=A Grade

    80-65= B Grade

    0-65= C Grade

    #include

    #include

    Main()

    {

    Int h,e,m,avg;

    Clrscr();

    Printf(Enter the marks);

    Scanf(%d%d%d,&h,&e,&m);

    Avg=(h+e+m)/3;

    If((avg>80)&&(avg65)&&(avg

  • 8/2/2019 ANSI C (by Devendra Sir)

    22/78

    Q- Write a program to input a number and check the number is even or odd and positive or negative?

    #include

    #include

    Main()

    {

    Int no;

    Clrscr();

    Printf(Enter the number);

    Scanf(%d,&no);

    If((no%2==0)&&(no>0))

    Printf(Even, Positive);

    Else

    If((no%2==0)&&(no0))

    Printf(Odd, Positive);

    Else

    printf(Odd, Negative);

    getch();

    }

    Decision making and loopingIf we have a program and we wants to repeat a single line multiple time than C languageprovide a concept known as looping.

    The looping provide permission to ignore the size of program. Every

    loop depends on three basic components:-

    Counter variableIt is the first variable for the loop which is utilize to decide how many times we have to repeat

    the statement and it is utilize to count the repetation.

  • 8/2/2019 ANSI C (by Devendra Sir)

    23/78

    ConditionIt is responsible to decide that we have to stop or run the loop. If the condition is true than we

    have to perform repetation otherwise stop the repetation.

    Increment or Decrement operatorTo modify the value of counter variable we have to use increment and decrement operators.

    They provide permission to update the value of the counter variable from starting to the ending.

    Categories of looping (Types of looping)

    Top tested loopIt is the first caregory of loop where we have to check the condition at first priority and

    then perform other task.

    Bottom tested (Post tested loop)It is another type of looping where the condition have last priority that means we can

    execute all the instructions than check condition.

    1. FOR loopIt is the first type of looping where we have to define three elements inside the for. The

    elements are a counter variable, a condition and increment or decrement.

    Syntax:

    For(counter_variable=value; condition; increment/Decrement)

    {

    Statement one;

    Statement two;

    }

    2. WHILE loopIt is second type of looping and it is top tested looping. It provide permission to represent all the

    element of a loop. In three basic steps. The while always represent a condition which we wants

    to execute.

    Syntax:

    Counter_variable= counter_value;

    While(condition)

    {

    Statement one;

    Statement two;

    Increment\\Decrement;

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    24/78

    3. DO WHILE loopThe do while is a bottom tested loop that is utilize two important keywords.

    DoIt always represent that we have some repetitive statements.

    WhileIt represent the condition to run the loop.

    Some examples of looping

    Q- Write a program to print Hello message 10 times?

    FOR WHILE DO WHILE

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    25/78

    Q- Write the program to print the series 10 to 1?

    FOR WHILE DO WHILE

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();

    For(i=10;i>=1;i--)

    {

    Printf(%d,i);

    }

    Getch();

    }

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();

    I=10;

    While(i>=1)

    {

    Printf(%d,i);

    I--;

    }

    Getch();

    }

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();

    I=10;

    do

    {

    Printf(%d,i);

    I--;

    }while(i>=1);

    Getch();

    }

    Q- Write a program to the series 1, 4, 9, 16, 25, 100 square of each?

    FOR WHILE DO WHILE

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    26/78

    Int i;

    Clrscr();

    For(i=10;i>=1;i--)

    {

    Printf(%d,ii);

    }

    Getch();

    }

    Int i;

    Clrscr();

    I=10;

    While(i>=1)

    {

    Printf(%d,ii);

    I--;

    }

    Getch();

    }

    Int i;

    Clrscr();

    I=10;

    do

    {

    Printf(%d,ii);

    I--;

    }while(i>=1);

    Getch();

    }

    Q- Write a program to print only even numbers from 1 to 10?

    FOR WHILE DO WHILE

    #include

    #include

    Main()

    {

    Int i;

    Clrscr();

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    27/78

    Printf(%d,-i);

    }

    }

    Printf(%d,i);

    }

    }

    Q- Write a program to print the series from 1 to N? (Where N is input by user)

    #include

    #include

    Main()

    {

    Int i,n;

    Clrscr();

    Printf(Enter the Number);

    Scanf(%d,&n);

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    28/78

    Scanf(%d,&n);

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    29/78

    #include

    #include

    Main()

    {

    Int no,r,sum=0;

    Clrscr();

    Printf(Enter the Number);

    Scanf(%d,&no);

    While(no!=0)

    {

    R=no%10;

    No=no/10;

    Sum=sum+r;

    }

    Printf(Sum=%d,sum);

    Getch();

    }

    ArmstongIf we have an number with n digits and the sum of each digit with power n is always equals to

    number than it is called armstong.

    Q- Write a program to input a number by user and find out is it armstong or not?

    #include

    #include

    Main()

  • 8/2/2019 ANSI C (by Devendra Sir)

    30/78

    {

    Int no,r,a,sum=0;

    Clrscr();

    Printf(Enter the Number);

    Scanf(%d,&no);

    A=no;

    While(no!=0)

    {

    R=no%10;

    No=no/10;

    Sum=sum+(r*r*r);

    }

    If(a==s)

    Printf(Number is Armstong);

    Else

    Printf(Number is not Armstong);

    Getch();

    }

    Fibonacci seriesSequence of numbers in which 1 appears twice as the first two numbers, and every subsequent

    number is the sum of two preceding numbers: 1, 1, 2, 3, 5, 8, 13 ... and so on.

    Q- Write a program to design a febonacci series?

    #include

    #include

    Main()

    {

    Int a=0,b=1,c,I;

  • 8/2/2019 ANSI C (by Devendra Sir)

    31/78

    Printf(%d%d,a,b);

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    32/78

    All the types of errors that are not identify by compiler or user known as unknown errors or

    runtime errors.

    In these types of error the program terminate from runtime without any

    description.

    Example:

    The divide by 0 Data in the variable out of the limitation of data types.

    Nested looping (Branching)The loop inside another loop known as nested looping.

    Syntax:

    For(counter_variable=counter_value; condition; Inc//dec)

    {

    For(counter_variable=counter_value; condition; Inc//dec)

    {

    For(counter_variable=counter_value; condition; Inc//dec)

    {

    }

    }

    }

    Q-Write a program to print numbers as follows?

    1

    12

    123

    1234

    #include

    Main()

    {

    Int I,j;

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    33/78

    {

    Printf(%d,j);

    }

    Printf(\n);

    }

    }

    Program DocumentationIf we have a software or program for the development then we have to perform some squencial

    steps to create the complete program or software. It will be possible if we have some steps

    which are known as documentation life cycle.

    1. Requirement analysisIt is the first step where we accept the requirement from user if the requirement is valid then

    check the environment of program and decide the software desiging is possible or not. If it is

    possible then discuss the next step.

    2. Feasibility (Possibility)In this step we have to analysis the possibility of software for that we have three points:-

    We have to know about the financial position of software that is financially possible ornot.

    If the financially possible then confirm that we have all the technical components andlogic or not.

    If we have technical resources then we have to decide about the operational staff. Ifstaff is not available then it is not possible.

    3. Input/Output designFinally when we have to design the software we have to decide the designing of input/output

    screen.

    4. CodingAfter the complete designing we have to write program or logic for every input and output.

    5. TestingAfter complete set of coding we have to confirm that our code is correct or not. It is possible

    with the help of testing.

    6. OutputAfter complete successful testing we can show our program for the customer.

  • 8/2/2019 ANSI C (by Devendra Sir)

    34/78

    ArrayA variable can store single value at a time to store multiple values we have to create multiple

    variables. Every variable have its separate memory address so when we search the variables we

    have to waste out time this problem can be solved using the concept of array.

    The continue memory allocation of same type data identified by a

    common name known as array. They provide permission to store multiple data in the continue

    memory and we need not to waste out time during working on it. All the datas identify by a

    common name but separate according to the index.

    Types of array Single array (one dimensional array) Two dimensional array Multi- dimensional array

    1. Single array (one dimensional array)The continue memory allocation of the same type data in the format of row or column known assingle array.

    Syntax: Datatype array_name[size]

    Rules to create a single array To design a single array in the program we have to define data type with the name and

    size of array.

    Every data have common name but seprated using indexs. from 0 to n-1 where n= size. To perform any task on the array we have to use name of array with its index.

    Constant arrayQ- Write a program to create a constant array and then print its data?

    Constant array without looping Constant array with looping Array in memory

    #include

    Main()

    {

    Int a[3]= {10,25,50};

    Printf(%d,a*0+);

    Printf(%d,a*1+);

    Printf(%d,a*2+);

    }

    #include

    Main()

    {

    Int a[3]= {10,25,50},I;

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    35/78

    #include

    Main()

    {

    Int a[10],I;

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    36/78

    {

    Printf(%d,a*i+);

    }

    }

    Disadvantages of arrayWhen we use an array we have a disadvantage that if we does not use the complete size of data

    or memory which we declare then it is memory wastage.

    Q- Write a program to input N elements in the array and print in reverse order?

    #include

    Main()

    {

    Int a[10],I,totno;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i=0;i--)

    {

    Printf(%d,a*i+);

    }

    }

    Q- Write a program to input N element in the array and find out its sum?

    #include

  • 8/2/2019 ANSI C (by Devendra Sir)

    37/78

    Main()

    {

    Int a[10],I,totno,sum=0;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i=0;i--)

    {

    Sum=sum+a[i];

    }

    Printf(Sum=%d,sum);

    }

    Q- Write a program to input N element in the array and find out maximum number from it?

    #include

    Main()

    {

    Int a[10],I,totno,m=-32768;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    38/78

    }

    For(i=0;im)

    M=a[i];

    }

    Printf(Max=%d,m);

    }

    Q- Write a program to input N numbers by user then find out the maximum and its position in array?

    #include

    Main()

    {

    Int a[10],I,totno,m=-32768,p=0;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    39/78

    }

    Printf(Max=%d,m);

    Printf(Position=%d,p);

    }

    Q- Write a program to input N number in array then find out the maximum and its repetation?

    #include

    Main()

    {

    Int a[10],I,totno,m=-32768,count=0;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    40/78

    }

    Searching in arrayQ- Write a program to input N element in the array and search a number on it?

    #include

    Main()

    {

    Int a[10],I,totno,sno;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    41/78

    Palindrome arrayQ- Write the program to input data by user and check the array is palindrome or not?

    #include

    Main()

    {

    Int a[10];

    Int totno,I,j;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    42/78

    2. 2D Array (two dimension array)It is another type of array where we can maintain our data in the row and column. It is also

    known as matrix representation of array. To declare the 2D array we have to define its datatpe

    name and size of row and columns.

    Syntax: datatype array_name[row][column];

    In the 2D array the row and columns started from 0 to N-1. If we want to insert or retrieve data. We have to define name of array with index of row

    and column.

    Constant 2D arrayInt a[2][2]= {{10,20},{20,30}}

  • 8/2/2019 ANSI C (by Devendra Sir)

    43/78

    Q- Design a 2D array of 2 row and 2 column and perform input output?

    #include

    Main()

    {

    Int a[2][2];

    Int I,j;

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    44/78

    Q- Write a program to input a 2D array of m*n size the find out its addition?

    #include

    Main()

    {

    Int a[2][2];

    Int I,j,m,n,sum=0;

    Printf(Enter the row and column mn);

    Scanf(%d%d,&m,&n);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    45/78

    Q- Write a program to input a 2D array of M*N size and find out the sum of straight line?

    #include

    Main()

    {

    Int a[2][2];

    Int I,j,m,n,sum=0;

    Printf(Enter the row and column mn);

    Scanf(%d%d,&m,&n);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    46/78

    Q- Write a program to input a 2D array of M*N size and find out the sum of upper triangle?

    #include

    Main()

    {

    Int a[2][2];

    Int I,j,m,n,sum=0;

    Printf(Enter the row and column mn);

    Scanf(%d%d,&m,&n);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    47/78

    Q- Write a program to input a 2D array of M*N size and find out the sum of Lower triangle?

    #include

    Main()

    {

    Int a[2][2];

    Int I,j,m,n,sum=0;

    Printf(Enter the row and column mn);

    Scanf(%d%d,&m,&n);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    48/78

    Q- Write a program to input two 2D array by user and find out their sum in 3 rd array?

    #include

    Main()

    {

    Int a[5][5], b[5][5], c[5][5];

    Int I,j,m,n,sum=0;

    Printf(Enter the row and column mn);

    Scanf(%d%d,&m,&n);

    //INPUT START

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    49/78

    For(j=0; j

  • 8/2/2019 ANSI C (by Devendra Sir)

    50/78

    }

    3. Toupper();If we wants to convert character of small letter into the capital letter then we have to use

    toupper(); function. The function accept character type variable.

    Syntax: toupper(variable_name);

    4. Tolower();It is also a pre- define function that can convert a capital letters or data into small letters it is

    also accept character type variable.

    Syntax: tolower(variable_name);

    5. Isupper();If we wants to find out that data is capital or not then we have isupper(); function. It return the

    answer in true or false.Syntax: isupper(variable_name);

    6. Islower();To find out that our data is small letters or not we have islower function.

    Syntax: islower(variable_name);

    Character type array (Strings)A string is a sequence of characters that is treated as a single data item.

    %SIt is an identifier which is utilize when we perform on a character type array. The %s has

    some rules:-

    It is utilize just with string. It does not provide permission to call the Ampercent (&) with scanf. With the %s we just use the name of array we does not provide index. After inserting string in the array we have a special character known as null

    character \0 that represent end of string.

    Q- Write a program to input a string by user print data in one by one character?

    #include

    Main()

  • 8/2/2019 ANSI C (by Devendra Sir)

    51/78

    {

    Int I;

    Char a[5];

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!=\0; i)

    {

    Printf(%c,a*i+);

    }

    }

    Q- Write a program to input a string by user and find out its length?

    #include

    Main()

    {

    Int I,len=0;

    Char a[5];

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!=\0; i)

    {

    Len=len+1;

    }

    Printf(Length=%d,len);

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    52/78

    Q- Write a program to input a string by user and print in reverse order?

    #include

    Main()

    {

    Int I,len=0;

    Char a[5];

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!=\0; i)

    {

    Len=len+1;

    }

    For(i==0; i--)

    {

    Printf(%c,a*i+);

    }

    }

    Q- Write a program to input a string by user and and copy it in another string?

    #include

    Main()

    {

    Int I,len=0;

    Char a[5],b[5];

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!=\0; i)

  • 8/2/2019 ANSI C (by Devendra Sir)

    53/78

    {

    B[i]= a[i];

    }

    Printf(%s,b);

    }

    String concateIf we have two strings and we wants to connect the first string at the last of second string then it

    is known as string concate.

    Q- Write a program to input two strings by user and transfer the first string at the last of second

    string?

    #include

    Main()

    {

    Char a[20], b[20];

    Int I,j,len=0;

    Printf(Enter the strings);

    Scanf(%s%s,a,b);

    For(i=0; b*i+!=\0; i)

    {

    Len=len+1;

    }

    For(i=0, j=len; a*i+!=\0; i, j)

    {

    B[j]= a[i];

    }

    B*j+= \0;

  • 8/2/2019 ANSI C (by Devendra Sir)

    54/78

    Printf(%s,b);

    }

    String.hIf we are working on character type array or string then we have a pre- define header file known

    as string.h.

    String.h header file have some pre- define function which are follows:-

    1. Strlen();It is a pre- define function in the string.h that accept name of array and return length as integer.

    Syntax: strlen(array_name);

    Example:

    Q- Write a program to input a string by user and find out its length using pre- define function strlen();

    ?

    #include

    #include

    Main()

    {

    Char a[20];

    Int len=0;

    Printf(Enter the string);

    Scanf(%s,a);

    Len=strlen(a);

    Printf(Length=%d,len);

    }

    2. Strcpy();It is string copy function that is utilize to copy the data of a string into another string. The

    function accept two arguments name of source array and name of destination array.

    Syntax: Strcpy(destination_array, source_array);

    Example:

  • 8/2/2019 ANSI C (by Devendra Sir)

    55/78

    Q- Write a program to input a string by user and copy it in another string using pre- define function?

    #include

    #include

    Main()

    {

    Char a[20], b[20];

    Printf(Enter the string);

    Scanf(%s,a);

    Strcpy(b,a);

    Printf(%d,b);

    }

    3. Strcat();It is concate function which is utilize to copy one string at the end of another string.

    Syntax: strcat(destination_array, source_array);

    Example:

    Q- Write a program to input a string by user and copy it in end of another string using pre- definefunction?

    #include

    #include

    Main()

    {

    Char a[20], b[20];

    Int len=0;

    Printf(Enterthe string);

    Scanf(%s,a);

    Strcat(b,a);

  • 8/2/2019 ANSI C (by Devendra Sir)

    56/78

    Printf(%d,b);

    }

    Q- Write a program to input a string by user and print it capital letters?

    #include

    #include

    Main()

    {

    Char a[20];

    Int i;

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!= \0; i)

    {

    Printf(%c,toupper(a*i+));

    }

    }

    Q- Write a program to input a string by user and print it small letters?

    #include

    #include

    Main()

    {

    Char a[20];

    Int i;

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; a*i+!= \0; i)

    {

  • 8/2/2019 ANSI C (by Devendra Sir)

    57/78

    Printf(%c,tolower(a*i+));

    }

    }

    2D character type array (2D string)

    Q- Write a program to input a string in the 2D array and print one by one character?

    #include

    Main()

    {

    Int I,j;

    Char a[3] [3];

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    58/78

    Printf(Enter the string);

    Scanf(%s,a);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    59/78

    Printf(%d,sum);

    }

    Pointer with if- elseQ- Write a program to input 3 numbers by user and find out maximum?

    #include

    Main()

    {

    Int a,b,c;

    Int *p, *q, *r;

    Printf(Enter the numbers);

    Scanf(%d%d%d,&a,&b,&c);

    P= &a;

    Q= &b;

    R= &c;

    If((*p > *q )&&( *p > *r ))

    Printf(A is max);

    Else

    If(*q > *r)

    Printf(B is max);

    Else

    Printf(C is max);

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    60/78

    Pointer with looping

    Q- Write a program to design a febonacci series using pointer?

    #include

    Main()

    {

    Int a=0,b=1,c,I;

    Int *p, *q, *r;

    P= &a;

    Q= &b;

    R= &c;

    Printf(%d%d,p,q);

    For(i=1;i

  • 8/2/2019 ANSI C (by Devendra Sir)

    61/78

    Q- Write a program to find out the maximum number from array using pointer?

    #include

    Main()

    {

    Int a[10], totno, I, m= -32768, *p;

    Printf(Enter the size of array);

    Scanf(%d,&totno);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    62/78

    2. User define datatypesThe datatypes that are design by the user to solve some particular problem known as user

    define datatype or derive datatype.

    To create the user define data type we have two representation the structure

    and union.

    StructureTo create a derive data type we have the first representation known as structure. It is design to

    solve the problems of array that we can mention similar or dissimilar type of data in continue

    memory which is not possible in the array.

    It is utilze to create the group of data which we wants to ude in our program

    multiple times.

    Rules:

    The structure always create before the main function because after creating adata type we can use it. To create the structure we have to use struct keyword.Syntax:

    Struct structure_name{

    Data type member1;

    Data type member2;

    };

    To change the name of structure we have typedef keyword that accept theold name of structure and new name of structure.

    Syntax:

    Typedef struct_old_name new_name;

    To use the structure in the program. Create the variable of structure type. To use the member of structure by the structure type variable we have (.)

    operator.

    Syntax:

    Structure_variable.member_name;

    The size of structure datatype always equals to total size of structure member.

  • 8/2/2019 ANSI C (by Devendra Sir)

    63/78

    Q- Write a program to design a structure to maintain a details of student the details are:-

    3 subjects Roll number Name

    #include

    Struct sub{

    Int h,e,m;

    Int rno;

    Char na[10];

    }; typedef struct sub sub;

    Main()

    {

    Sub b;

    Printf(Enter the Roll number);

    Scanf(%d,&b.rno);

    Printf(Enter the Name);

    Scanf(%s,b.na);

    Printf(Enter the Marks of 3 subjects);

    Scanf(%d%d%d,&h,&e,&m);

    Printf(Roll no=%d\nName=%s,b.rno,b.na);

    Printf(\nHindi=%d\nEnglish=%d\nMaths=%d,b.h, b.e, b.m);

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    64/78

    Nested structureIf a structure is utilize in side another structure then it is known as nested structure.

    Q- Write a program to maintain the details of student where we have its roll no, name and date of

    birth in day, month and year format?

    #include

    Struct date{

    Int d,m,y;

    }; typedef struct date date;

    Struct stu{

    Int rno;

    Char na[10];

    Date t;

    }; typedef struct stu stu;

    Main()

    {

    Stu s;

    Printf(Enter the name and roll number);

    Scanf(%s%d, s.na, &s.rno);

    Printf(Enter the date of birth);

    Scanf(%d%d%d, &s.t.d, &s.t.m, &s.t.y);

    printf(%s%d, s.na, s.rno);

    printf(%d%d%d, s.t.d, s.t.m, s.t.y);

    }

    UnionTo create a user define data type or derive data type it is also follow similar syntaxs and rule

    that are provided by structure but we have two basic difference between the structure and

    union that are follows:-

    1. To create the union we have to use union keyword but in the structure we have structkeyword.

  • 8/2/2019 ANSI C (by Devendra Sir)

    65/78

    2. The size of union always represent by the maximum size of member so it is an advantage toprotect our memory. But in the structure the size always represent by total size of members.

    FunctionThe group of instructions arrange in a sequence and identify by a name known as function. The

    function is based on write ones use many features.

    If we have to use a logic in our program multiple times then we have to write

    the complete description but by using the fuctions whenever we require the logic just call the

    fuction.

    Types of functions1. Pre- define function

    All the set of function that are previously available in the library of C language is knownas pre- define functions.

    Example:

    Scanf, printf, getchar, putchar etc.

    2. User define functionThe functions that are design by user to solve some particular problems known as user

    define functions. In the user define function the name, logic and working decided by user these function

    can be design in three basic steps that are follows:-

    a) Function declaration (Function prototype)To create a new function in our program we have to declare it in the main function for that we

    have the written type of function (Which type of value return by function or not).

    Syntax:

    Void function_name(argument_datatype1, argument_datatype2 )

    b) Function bodyIt is second part of function where we have to define the complete logic of function. It also have

    written type function name and argument with data type. They are design out side the main

    function.

    c) Function callingTo call our user define function we have to use function calling that can transfer the cursor from

    main function to the user define function in the calling we have to use name of function with

    data.

    Return keyword

  • 8/2/2019 ANSI C (by Devendra Sir)

    66/78

    To transfer the data from the user define function to the function calling we have return();

    keyword.

    Q- What do you mean by function define the differences and similarities berween return or

    non- return type function using common example?

    Return type Non Return type

    #include

    Main()

    {

    Int sum(int,int);

    Int a,b,c;

    Printf(Enter the number);

    Scanf(%d%d,&a,&b);

    C=sum(a,b);

    Printf(%d,c);

    }

    Int sum(int a,int b){

    Int s;

    S=a+b;

    Return(s);

    }

    #include

    Main()

    {

    void sum(int,int);

    Int a,b;

    Printf(Enter the number);

    Scanf(%d%d,&a,&b);

    sum(a,b);

    }

    void sum(int a,int b)

    {Int s;

    S=a+b;

    Printf(%d,s);

    }

    Q- Write the program to input a number by used and print its square using return and non return type

    function?

    Return type Non Return type

    #include

    Main()

    {

    Int sqr(int);

    Int no,c;

    Printf(Print the number);

    Scanf(%d,&no);

    C=sq(no);

    Printf(%d,c);

    }

    Int sqr(int no)

    {

    Return(no*no);

    }

    #include

    Main()

    {

    void sqr(int);

    Int no;

    Printf(Print the number);

    Scanf(%d,&no);

    sq(no);

    }

    Int sqr(int no)

    {

    Int s;

    S=no*no;

    Printf(%d,s);

    }

  • 8/2/2019 ANSI C (by Devendra Sir)

    67/78

    Q- Write a program to input two no by user and design a function the find out the maximum from two

    numbers?

    #include

    main()

    {

    Int max(int, int);

    Int a,b,c;

    Printf(Enter the number);

    Scanf(%d%d,&a,&b);

    C=max(a,b);

    Printf(%d,c);

    }

    Int max(int a, int b)

    {

    If(a>b)

    Return(a);

    Else

    Return(b);

    }

    Q- Design a function to find out even or odd from a given number ?

    #include

    Main()

    {Void even(int);

    Int no;

    Printf(Enter the number);

    Scanf(%d,&no);

    Even(no);

    }

    Void even(int no)

    #include

    Main()

    {int even(int);

    Int no;

    Printf(Enter the number);

    Scanf(%d,&no);

    C= Even(no);

    If(c==1)

    Printf(Number is Even);

  • 8/2/2019 ANSI C (by Devendra Sir)

    68/78

    {

    If(no%2==0)

    Printf(Number is Even);

    Else

    Printf(Number is Odd);

    }

    Else

    Printf(Odd);

    }

    int even(int no)

    {

    If(no%2==0)

    Return(1);

    Else

    Return(0);

    }

    Function with loopingQ- Design a function to find out series from 1 to n?

    #include

    Main()

    {

    Void series(int);

    Int totno;

    Printf(Print the Total number);

    Scanf(%d,&totno);

    Series(totno);

    }

    Void series(int totno)

    {

    For(int i=1; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    69/78

    Non Return type Return type

    #include

    Main()

    {

    Void fact(int);

    Int no;

    Printf(Enter the number);

    Scanf(%d,&no);

    Fact(no);

    }

    Void fact(int no)

    {

    Int I, f=1;

    For(i=1; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    70/78

    Printf(Enter the number);

    Scanf(%d,&no);

    C=fact(no);

    Printf(%d,c);

    }

    Int fact(int no)

    {

    Int f=1;

    If(no==1)

    Return(f);

    Else

    F=no* fact(no-1);

    }

    Function with ArrayWhen we use the array in the function then we have some changes in the declaration of fuction

    that we have to define data type with symbol of array and when we call the fuction we just use

    the name of array without index or size.

    #include

    Main()

    {

    Void print(int[ ], int)

    Int a[10],I,totno;

    Printf(Enter the total number);

    Scanf(%d,&no);

    For(i=0; i

  • 8/2/2019 ANSI C (by Devendra Sir)

    71/78

    #include

    Main()

    {

    Void copy(char[],char[]);

    Char a[10], b[10];

    Printf(Enter the copy string);

    Scanf(%s,a);

    Copy(a,b);

    }

    Void copy(char a[10], char b[10])

    {

    Int I;

    For(i=0; a*i+!\0; i)

    {

    B[i]=a[i];

    }

    B*i+=\0;

    Printf(%s,b);

    }

    According to callingAccording to calling of function we have two representation call by value and call by reference.

    1. Call by valueIf we call the function by the name of variable then it is call by value fuction.Example:

    #include

    Main()

    {

    Void sum(int, int);

    Int a,b;

  • 8/2/2019 ANSI C (by Devendra Sir)

    72/78

    Printf(Enter the number);

    Scanf(%d%d,&a,&b);

    Sum(a,b);

    }

    Void sum(int a, int b)

    {

    Int c=a+b;

    Printf(%d,c);

    }

    2. Call by referenceThe call by reference is the representation where function can be call by the address of variable

    to solve this problem we have to use pointer with the function.

    In this types of functions the calling always perform with (&) sign.

    Example:

    #include

    Main()

    {

    Void sum(int *, int*);

    Int a,b;

    Printf(Enter the numbers);

    Scanf(%d%d,&a,&b);

    Sum(&a, &b);

    }

    Void sum(int *p, int *q)

    {

    Int c= (*p)+(*q);

    Printf(%d,c);

    }

    Q- Write the program to input 3 number by user and design a function to find out the maximum using

    call by reference?

    #include

    Main()

    {

    Void max(int *, int*, int *)

    Int a,b,c;

    Printf(Enter the number);

  • 8/2/2019 ANSI C (by Devendra Sir)

    73/78

    Scanf(%d%d%d,&a,&b,&c);

    Max(&a,&b,&c)

    }

    Void max(int *p, int *q, int *r)

    {

    If((*p>*q)&&(*p>*r))

    Printf(%d,q);

    Else

    Printf(%d,r);

    }

    File handlingWhen we design a program all our data and information does not save permanently in the

    memory that means all our output are useless. Because we cannot view them in next programs.

    To solve this problem we have a concept known as file handling.

    The procedure to transfer the data of RAM into the memory and retrieve data

    from memory to again RAM known as file handling.

    The file handling performs into different partitions:-

    Input the data by user in the RAM and transfer this data into the secondary memory. When we wants to retive data than transfer the data of secondary memory into the

    RAM and RAM data into output.

    Types of fieAccording to the concept there are two types of files random files and squencial files.

    The representation of file where input perform in a sequence but output can be

    random known as random file organization.

    The representation of file where input and output both perform in a sequence

    known as sequencial organization.

    Rules for file handling1. To represent the address of file we have to use a file pointer.2. To open the file at run time we have to use fopen method the method required to arguments

    name of file and mode.

    The modes are small characters that provide different type of permission on the

    file.

  • 8/2/2019 ANSI C (by Devendra Sir)

    74/78

    a) W modeW stands for write if we provide a new file name than create it otherwise overwrite the

    previous data of that file.

    b) r modeIt accept the name of available file. If file found than open for reading otherwise error.

    c) a modeIt accept the name of file and open the file by transfer the cursor at the bottom of file for the

    new data otherwise an error.

    3. To perform data input operation in the file from the RAM we have purc function. The functiontransfer one by one character in the file.

    4. To retrieve one by one character from the file we have getc method. The method requiredaddress of file.

    5. To control the data in the file we have end of file (eof) character. It is helpful to execute orterminate our program when input data and retrieve data.

    6. After complete working on the file we have to close it using the fclose method. The methodaccept name of file pointer.

    Q- Write a program to create a text file by the name decided by user?

    #include

    Main()

    {

    FILE *fp;

    Char na[10], ch;

    Printf(Enter the file name);

    Scanf(%s,na);

    Fp= Fopen(na,w);

    Printf(Press F6 to exit);

    Do{

    Ch= getchar();

    Putc(ch,fp);

  • 8/2/2019 ANSI C (by Devendra Sir)

    75/78

    }while(ch!=EOF );

    Fclose(fp);

    }

    Reading a file#include

    Main()

    {

    FILE *fp;

    Char ch, fna[10];

    Printf(Enter the file name);

    Scanf(%s,fna);

    Fp= fopen(fna,r);

    Do{

    Ch=getc(fp);

    Printf(%c,ch);

    }while(ch!==EOF);

    Fclose(fp);

    }

    Q- Write a program to open a file and find out how many vowels, space, character and coma(,)?

    #include

    Main()

    {

    FILE *fp;

    Int v,co,sp,c;

    Char ch, fna[10];

  • 8/2/2019 ANSI C (by Devendra Sir)

    76/78

    Printf(Enter the file name);

    Scanf(%s,fna);

    Fp= fopen(fna,r);

    Do{

    Ch=getc(fp);

    If((ch==a)||(ch==e)||(ch==i)||(ch==o)||(ch==u))

    V=v+1;

    Else

    If(ch==,)

    Co=co+1;

    Else

    If(ch== )

    Sp=sp+1;

    Else

    C=c+1;

    }while(ch!==EOF);

    Printf(%d%d%d%d,v,co,sp,c);

    Fclose(fp);

    }

    Working on numerical fileIf we wants to create a file with numeric data then we have some different kind of input output

    function.

    Putw();The function is utilize to transfer numeric data from ram to the file.

    Syntax: putw(Number, file pointer)

    The function accept two arguments a number and file pointer.

    Getw();

  • 8/2/2019 ANSI C (by Devendra Sir)

    77/78

    The function is utilize to return a number from the file into the RAM function

    accept file pointer.

    Syntax: getw(file pointer);

    Q- Design a program to create a file with numberic data?

    #include

    Main()

    {

    FILE *fp;

    Char fna[10];

    Int no;

    Printf(Enter the file name);

    Scanf(%s,fna);

    Fna= fopen(fna,w);

    Printf(Enter zero to exit);

    Do{

    Scanf(%d,&fna);

    Putw(no, fp);

    }while(no!=0);

    Fclose(fp);

    }

    Symbolic constantAll the constant that are design with the pre- define symbol or representative known as

    symbolic constant.

    They can be design with the character or string we have some pre- define

    symbolic constant.

    Example:

    Null character \0= Represent end of string. End of file EOF = Represent end of file.

    Expression

  • 8/2/2019 ANSI C (by Devendra Sir)

    78/78

    The group of variables, constants and data types to design an equation known as expression.

    The Airthematics expressions are based on Airthematic operator.

    ScopeWhen we declare variable in our program then known as scope that means how a variable can

    be use by others.

    Types of Scope1. Local (internal variable)

    All those variables that are design inside a function known as internal or local variable.

    They cannot use by out sider.

    2. Global (external variable)The variables that are design at the top of program window outside the functions known

    as global variables. They can be call by everyone or every module of our program.

    Register variableWhen we design a variable to perform input output of data we have some set of temporary

    storage area known as register that can hold small bit of data for small time duration every

    variable also known as register variable.

    Bit fieldIt is the first genetation of data storage that provide permission to store users data in the

    predecided length of field. At the starting we have character data type that stores its data in onebyte of memory that means its length of bit field is 8 bit.