Notes C Language

download Notes C Language

of 22

Transcript of Notes C Language

  • 7/31/2019 Notes C Language

    1/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 1

    C. Programming Language:

    Programming: A Computer can do anything but; about itself it can do nothing. It must be giveninstruction about the work which is to be done by the computer. The process of giving instructions to the

    computer to get our work done is known as Programming. Such a list of Instructions given to thecomputer is known as Program.

    Language:A set of Rules and Symbols used for communication between two things are known as Language. We can

    classify the languages in following four categories:

    1. Human Language: This is a Language which is understandable and used by humans for

    communication(Conversation). e.g. English, Hindi. Punjabi.

    2. Machine Language:This language is used by Machine (Computer) for its Internal Processing.All the parts of a Machine use this language to transfer information from one place to another. E.g. Binary

    Language.3. Programming Language: This language is understandable by the human but the instructionwritten in Programming language can be converted into Machine Language also. This is the main use of

    Programming Language as it is used to create an interface for instruction between computer and a Human.

    This language is very similar to the human language due to which it is very easy to understand. This

    language is used to design application softwares. Compiler and interpreters are used to convert the

    instructions from programming language to Machine language. E.g. C,C++, Java, VB etc

    4. Assembly Language: This language do the same as programming language does but the onlydifference is that language is more closer to the machine language. This language use some symbols for

    instructions representation. Due to which this language is very difficult to be learnt for programmer. This

    language is used to design System soft wares. Assembler is used to convert the instructions from assemblylanguage to Machine language. E.g. Assembly Language.

    Character SetCharacter Set means that the characters and symbols that a Langauge can understand and accept. C

    Language is having expressions,words,c-statement and other elements as a tokens. Character set is the

    combination of alphabets or character,digits ,special symbols and white spaces.

    Tokens of C Language:Variable:Computer require memory location to store any data or value. Each location used to store datamust be declared first before its used. A memory location reserved to store some data are known as

    variable. The value stored within a variable can be changed for any no of times:

    Constant:These are some symbols used to represent a value. A Variable can store the value in the formof a constant. We can say, everything which is stored in the variable is constant. These are also known as

    Literals. The value of the constants remain unchanged forever.

    We can classify constants as the hierarchy given below:

    Constant

    Numeric Character

    Integer Real or Float Single Character String Backslash Character

    Signed Unsigned Signed Unsigned

  • 7/31/2019 Notes C Language

    2/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 2

    Identifiers:Each Reserved location must be identified with a unique name. This identification symbolis known as Identifier. We can say An identifier is a name of Variable given on the time of its

    declaration.

    Rollno

    Variable

    Keywords:Each language is having some reserved words which are having their pre defined meaning which are not

    changeable. These kind of reserved words are known are Keywords. C language is having 32 keywords.

    Such as int, char, do, while, if, switch, for, long. etc..

    Data Types:These are keywords which are used to let the computer know about what type of data is to be stored in a

    variable. We can say All those keywords are Data Types which are used to represent the Type of Data

    within a computer Language. Such as int, char, float

    The Hierarchy of Data Types is as under:

    Data Type

    Scalar Derived User Defined Void

    Structure Union Enumerators

    Array String

    Integer Float Double Character

    101Constant

    Identifier

  • 7/31/2019 Notes C Language

    3/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 3

    Scalar Data Type

    Sr.

    No

    Description Keyword Size(In

    byte)

    Range Conversion

    String1 All integers

    Nos without decimal

    points

    int 2 -32768 to

    32767

    %d

    2 All Floats

    Nos with decimal

    points too

    float 4 -32768 to

    32767

    %f

    3 Alphanumeric char 1 256 %c

    4. Extended range of

    Integers

    long int 4 231 %ld

    5. Extended range of

    Floats

    double 8 2308 %lf

    Operators:An Operator is a special symbols that specifies what operation is perfomed mathematical exppressions In

    C Language operator can be either unary ,binary or ternary. These are symbols which are used to represent

    operations on one, two or three operands. We can classify the operands in following three categories:

    Unary operators:All those operators which are applied on one operand (variable) are known as Unary Operators

    Such as

    a. Increment Operator (++)

    b. Decrement Operator(--)

    Increment operator increase the value of the operand by one. Such as

    a++ means that a=a+1e.g.

    a=1;

    a++;

    now a will be 2

    likewise decrement decrease the value of the operand by one. Such as

    a- - means that a=a-1;

    Binary Operators:All those operators which are applied on two operand are known as Binary Operators

    like as a+b

    a. Arithmetic OperatorsThe Arithmetic operators are used for mathematical calculations. The arithmetic operators are

    binary operators that work on the built-in data type eg. int char & float. Here are the most common

    arithmetic operators

    + Additional Operator

    Subtraction Operator

    / Division

    * Multiplication

    % (remainder After Division)

    b. Relational Operators

  • 7/31/2019 Notes C Language

    4/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 4

    Relation operators are used to check relation between two Variables. In Other words, the

    operators used to make comparisons between two variables are called Relational operators. The

    result of a relational operation is a Boolean value that can only be true or false, according to its

    Boolean result. We may want to compare two expressions, for example, to know if they are equal

    or if one is greater than the other is. Here is a list of the relational that can be used in C Language

    a. < Less Thanb. > Greater Thanc. = Greater than equal to

    e. = = Equal to

    f. != Not equal to

    c. Logical OperatorsThe logical operators && and || are used Combine one or more expressions to obtain a single

    relational result. The operator && corresponds with Boolean logical operation AND. This

    operation results true if all Conditions are true, and otherwise false. The following table shows the

    result of operator && evaluating the expression a && b:

    a. && (And)

    b. || (Or)

    c. ! (Not)

    And: It will be true only when all conditions are true. If one of the condition is false, it will be

    false

    Or: It will be false only when all conditions are false. If one of the condition is true, it will be

    true.

    Not: It will generate the compliment of the condition. i.e. it will be true if the condition is false

    or Vice-Versa.

    d. Assignment Operatorsa. += Submission Assignment.

    b. -= Subtraction Assignment.

    c. /= Division Assignment.

    d. *= Multiplication Assignment.

    e. %= Modulation Assignment.

    Such as:

    A+=5; means that

    A=A+5; and so on for other operators.....

    c. Ternary Operators(Conditional operator)All those operators which are applied on three operand are known as Ternary Operators

    Such as

    ?

    :Syntax()? :;

    Such as

    A=4;

    (A

  • 7/31/2019 Notes C Language

    5/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 5

    Variable:Computer require memory location to store any data or value. Each location used to store data must be

    declared first before its used. A memory location reserved to store some data are known as variable. The

    value stored within a variable can be changed for any no of times:

    Syntax for Declaring a Variable:

    data type variable Name;

    Such as

    int Rollno ;

    it means Rollno is a variable which is of integer data type;

    Naming conventions (Rules) for Variables:1. A variable name should be short, meaningful and must be without blank space.

    2. No Two variables can have same name.

    3. No keyword can be used as a variable name.

    4. No special Symbol other than Underscore ( _ ) can be used within a variable name.

    5. Variable names are case sensitive.

    6. Variable name must begin with an alphabet or ( _ ).There are two type of variables :

    Local Variable

    Global Variable

    Local Variables

    A local variable is a variable that is given local scope. Such a variable is accessible only from thefunction

    orblockin which it is declared. Local variables are declared within the body of a function

    Global Variable

    A global variable declaration looks normal, but is located outside any of the program's functions. This is

    usually done at the beginning of the program file, but after preprocessor directives. The variable is not

    declared again in the body of the functions which access it. Global and external variables can be of any

    legal type. They can be initialized, but the initialization takes place when the program starts up, before

    entry to the main function.

    Components of Program.

    1. Comment Entry: This is a Short summary of Program. We can write anything in simpleenglish at any place within a program and every comment will be ignored by the compiler duringcompilation.

    Syntax:// Single line comment.

    /* Multiple

    Lines

    As

    A Comment */

    2. Sentence : Each line of a program is a sentence:

    3. Sentence Terminator : This symbol is used to mark the end of each sentence. The symbol

    used for this purpose is Semi column ( ; ).4. Delimiters : These Symbols are used to mark a block within a program. Each program start and end

    with a Delimiter.

    Symbols used are:

    http://en.wikipedia.org/wiki/Variablehttp://en.wikipedia.org/wiki/Scope_(programming)http://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Statement_blockhttp://en.wikipedia.org/wiki/Scope_(programming)http://en.wikipedia.org/wiki/Subroutinehttp://en.wikipedia.org/wiki/Statement_blockhttp://en.wikipedia.org/wiki/Variable
  • 7/31/2019 Notes C Language

    6/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 6

    { Start of Block

    } End of Block.

    5. Program name : This is an identification symbol for each of our file. Each file must have a unique

    name and must have .c as an extension . Such as

    Myfile.c

    programe.cInput Function:These functions are used to read data from the input device like keyboard. There is a wide range of input

    functions in C language. Some of them are listed bellow.

    1. getch();

    This input function read a character from the keyboard. This function neither display the input character

    nor echo the screen. i.e. no enter key is required to be pressed when the char is entered. As:

    char a;

    a=getch();

    2. getche();

    This input function read a character from the keyboard. This function not display the input character butecho the screen. i.e. Enter key is required to be pressed when the char is entered. As:

    char a;

    a=getche();

    3. getchar();This input function read a character from the keyboard. This function display the input character and echo

    the screen. i.e. Enter key is required to be pressed when the char is entered. As:

    char a;

    a=getchar();

    4. scanf();This input function is widely used for input of data as it can read any type of data from keyboard. User

    required to specify the data type of the variable in which we have to store data. The syntax for using this

    function is as under:

    scanf(Conversion String,&name of Variable);

    e.g.

    int x;

    scanf(%d,&x);

    Output Function:These functions are used to print any value to the output device as like Monitor. This function can

    print the value of any type with the help of conversion string. We can display a simple message to the

    output screen also using printf() function. The syntax for using this input function is as under.

    1. printf();printf([] [],);

    e.g.

    int x;

    x=10;

    printf(x= %d,x);

    Output:

    x=10

    Layout of A C Programe:

  • 7/31/2019 Notes C Language

    7/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 7

    main()

    {

    - - - - -

    Declaration block:

    - - - - -

    - - - - -

    - Operation Block

    - - - - -

    - - - -

    }

    e.g;

    Write a program to find the sum of two nos:#include

    Void main()

    {

    int a,b,c;

    printf(Enter any No :);

    scanf(%d,&a);printf(Enter another No :);

    scanf(%d,&b);

    c=a+b;

    printf(The Sum is : %d,c);

    }

    Output:

    Enter any No : 10

    Enter another No : 20 The Sum is : 30

    Control Statements

    A program consists of a number of statements which are usually executed in sequence. Programs can be

    much more powerful if we can control the order in which statements are run. The following are the Control

    Statements supported by the C language.

    If Statement

    If-else Statement

    Switch Statement

  • 7/31/2019 Notes C Language

    8/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 8

    Goto statement

    If statement :The simplest form of the control statement is the If statement. It is very frequently

    used in decision making and allowing the flow of program execution.

    The if statement is the most simple and powerful decision control statement which is used to control the

    sequence of execution of statements. It is used to execute a block of statements according to the condition,If the condition is true then the block of statements will be execute otherwise not execute.

    The syntax of if statement is shown as following.

    If(Condition)

    Statement 1;

    Statement 2;

    N

    }

    Exp.

    If(marks>34)

    {

    printf( pass );

    }

    The if else Statement

    In the case of if statement ,the block of statements are executed only when the condition is true. Otherwise

    the control is transferred to the next statement following the if block.

    In ifelse statement, If the condition is true then the if block will be execute otherwise else block

    will be execute. The syntax of if-else statement is shown as following.

    Syntax:

    If(Condition)

    {

    Statement 1;

    Statement 2;

    }

    else

    {

    Statement 1;

    Statement 2;

    }

    Exp.

    if (result >= 45)

    {printf("Pass");

    }else{

    printf("Fail");

  • 7/31/2019 Notes C Language

    9/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 9

    }

    If we wish to have more than one statement following the if or the else, they should be grouped together

    between curly brackets. Such a grouping is called a compound statement or a block.

    if (result >= 45){ printf("Passed\n");

    printf("Congratulations\n")}

    else{ printf("Failed\n");

    printf("Good luck in the resits\n");}

    Nested if-else statement:

    When we use if statement within the another if block or else block. This concept is known as nested if-else

    statement. The different ways for representing nested if-else statement are:

    If(Condition1)

    {

    If(Condition2)

    {

    Statement 1;

    Statement 2;

    }

    }

    else

    {If(Condition3)

    {

    Statement 1;

    Statement 2;

    }

    }

    The 2nd syntax of nested if-else statement is also known as ladder if else statement.

    The if statement may be nested as deeply as you need to nest it. One block of code will only be executed if

    two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is

    nested in the first. The second if condition is tested only when the first condition is true else the program

    flow will skip to the corresponding else statement

    Switch StatementThe switch statement is a multi decision making statement. Which Select one of alternate from

    several alternatives based on the value of condition or expression, If we have a large decision tree and all

    the decisions are depend on the condition variable. The switch statement tests the value of given variable

    against a list of case values. When a match a found, a block of statements are executed according to case.

    The break statement is used at the end of each case block. when it executed then the control

    transfer from switch statement. The default is an optional case. It will be executed if the value of

    condition(expression) does not match with any case values.

    The syntax of switch statement is shown as following

    Syntax:

    If(Condition1)

    {

    Statement1;

    Statement2;

    }

    else

    {

    if(Condition2)

    {

    Statement1;Statement2;

    }

    }

  • 7/31/2019 Notes C Language

    10/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 10

    Each case is normally terminated with a break statement. When a break statement is execute the

    program comes out the switch statement.

    If the switch statement is false then default statement will be execute.

    switch(condition)

    {

    case 0 :

    statemet1;

    statement2;

    break;

    case 1 :

    statemet1;

    statement2;

    break;

    case 2 :

    statemet1;

    statement2;

    break;

    default :

    statement;

    break;

    }

    Branching

    The C Programming language programs presented until now follows a sequential form of execution of

    statements. Many times it is required to move(alter) the flow of the sequence of instructions. C language

    provides statements that can alter(move) the flow of a sequence of instructions. These statements are called

    Branching control statements. These Branching statements help to jump from one part of the program to

    another. The control transfer may be conditional or unconditional. Exp: goto statement

    goto Statement:

    The goto statement is an unconditional control statement that jump the control to a different

    location in the program without checking any condition.

    Syntax:

    goto Label;

    Statement 1;

    Statement 2;

    Label :Statement 1;

    Statement 2;

    Continue statement:

    The break takes out of the loop when it is executed. But continue statement is execute the statement

    continually if the condition become false. It go back to the top of the loop. and the continue statement tells

    the compiler to skip the following statement The syntax of continue statement is shown as following:

    Syntax:Continue;

    LOOP

  • 7/31/2019 Notes C Language

    11/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 11

    A loop is a series of commands that will continue to repeat over and over again untill a condition is met.

    For example, loop is the process of execution of block of statement in program according to the condition

    In C programming language three types of loops are used:

    While Loop

    Do-while Loop

    For loopThese above loops follow the necessary three steps:

    Initialization;

    Condition

    Increment/Decrement;

    While loop:While loop is the most common usable loop it is used forrepeating a statement until the condition becomea false. The body of while loop statements are execute according to the condition .if the condition is true

    then the statements are execute otherwise not execute. So that the compiler first check the condition . The

    syntax of while statement is shown as following :Syntax:

    Initialization ;

    While(condition)

    {

    Statement1 ;

    Statement2 ;

    |

    |

    N ;

    Increment/Decrement;}

    The do while Loop

    In a do-while loop, the test expression is evaluated at the beginning of the loop. This is very similar to the

    while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed

    at least once time whether the condition is true or false. The test then verifies the condition, and loops back

    to read again if it was unacceptable

    Syntax.

    Initialization ;

    do

    {

    Statement1 ;

    Statement2 ;

    |

    |

    N ;

    Increment/Decreament;

    }

    While(condition);

    For loop

  • 7/31/2019 Notes C Language

    12/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 12

    The for loop is a easy loop because This loops use the three necessary steps in single line separated by

    semicolons.

    The first is run before the loop is entered. This is usually the initialization of the loop variable.

    The second is a test condition , the loop is exited when condition become false.

    The third is a statement to be run every time the loop body is completed. This is usually anincrement of the loop counter.

    Syntax of for loop is shown as following :

    for(initialization; condition; increament/decreament)

    {

    Statement1;

    Statement 2;

    Statement 3;

    }

    Nested Loops

    When we use a loop within another loop this concept is know as nested loop. The first loop in nested loop

    know as outer loop and another loop is know as inner loop. The controller firstly check the outer loop if

    the outer loop is true than the inner loop will be execute otherwise not execute. It means inner loop isalways depend on the outer loop . The syntax of nested loop (using while loop) is

    Initialization;

    While(condition)

    {

    Initialization;

    While(condition)

    {

    Statement1;

    Statement2;

    ||

    N

    Increment/Decrement;

    }

    Increment/Decrement;

    }

    Array:

    An array is a series of elements ( variable) of the same data type which placed in contiguous memorylocations that can be individually referenced by adding an index to a unique number. for example, we want

    to store 5 values of type int in an array without having to declare 5 different variables, each one with a

    different identifier. Instead of that, using an array we can store 5 different values of in an array

    Outer loop

    Inner loop

  • 7/31/2019 Notes C Language

    13/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 13

    Suppose we want to store 10 students roll_no for this, we have two options :

    we can declare 10 different different variables and each variable can store 10 students roll_no

    we can declare only one variable which can hold 10 students roll_no. this variable is know as

    array variable

    For example, an array to contain 5 integer values of datatype int called billy could be represented like this:

    where each blank box represents an element(variable) of the array, that in this case are integer values of

    type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently

    of its length . Like a regular variable, an array must be declared before it is used. A typical declaration for

    an array in C language is:

    data type ArrayName [Size of array];

    where type is a valid datatype (like int, float...), name is a valid identifier and the size(elements) field

    (which is always enclosed in square brackets []), specifies how many of these elements the array has to

    contain.

    Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:

    Int billy[5];

    There are three types of array:

    1. One dimension array(1D array)

    2. Two dimension array(2D array)

    3. Three dimension array(multiple array)

    Initializing of An one dimension Arrays.when we declare an array, we have the possibility to assign initial values to each one of its elements byenclosing the values in braces { }. For example:

    int billy[5] = { 16, 2, 77, 40, 12071 };

    This declaration would have created an array like this:

    The amount of values between braces { } must not be larger than the number of size of array that we

    declare for the array between square brackets [ ]. When an initialization of values is provided for an

    array, C allows the possibility of leaving the square brackets empty []

    Accessing the values of an array.

    In any point of a program in which an array is visible, we can access the value of any of its elements

    individually as if it was a normal variable, thus being able to both read and modify its value. The format is

    as simple as:

    name[index]=value;

  • 7/31/2019 Notes C Language

    14/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 14

    Following the previous examples in which billy had 5 elements and each of those elements was of type int,

    the name which we can use to refer to each element is the following:

    For example, to store the value 75 in the third element of billy, we could write the following statement:

    billy[2]=75;

    and, for example, to pass the value of the third element of billy to a variable called a, we could write:

    a=billy[2];

    Two dimension Array{Two dimension array is represent the element (variable) in the form of rows and columns. If we want to

    store the values in matrix form than we can use 2D array. The syntax of two dimension array is show as

    below:

    Syntax:

    Datatype arrayname[rows][cols];

    Exp.

    Int a[2][3];

    In above example array a represent 2 rows and 3 cols. It means total 12 element can be store in array a.

    Initialization of 2D array:

    According to the simple variable an array variable is also initialization by used assignment operator and

    scanf() function

    Using assignment operator:

    a[0][0]=45;

    a[0][1]=43;

    a[0][2]=76;

    a[1][0]=45;

    a[1][1]=43;

    a[1][2]=76;

    Using scanf() Function operator:

    for(int i=0;i

  • 7/31/2019 Notes C Language

    15/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 15

    }

    Program for addition of two matrix:#include

    void main()

    {int a[3][3]={5,6,7,8,6,5,4,3,4};

    int b[3][3]={6,8,7,5,3,2,1,2,3};

    int c[3][3],p,q;

    for(p=0;p

  • 7/31/2019 Notes C Language

    16/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 16

    for(p=0;p

  • 7/31/2019 Notes C Language

    17/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 17

    Initialization of null-terminated character sequences

    Because arrays of characters are ordinary arrays they follow all their same rules. For example, if we want

    to initialize an array of characters with some predetermined sequence of characters we can do it just like

    any other array:

    char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };

    In this case we would have declared an array of 6 elements of type char initialized with the characters that

    form the word "Hello" plus a null character '\0' at the end.

    But arrays of char elements have an additional method to initialize their values: using string literals.

    Double quoted strings (") are literal constants whose type is in fact a null-terminated array of characters.

    So string literals enclosed between double quotes always have a null character ('\0') automatically

    appended at the end.

    we can initialize the array of char elements called myword with a null-terminated sequence of

    characters by either one of these two methods:

    1. char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' };

    2. char myword [] = "Hello";In both cases the array of characters myword is declared with a size of 6 elements of type char: the 5

    characters that compose the word "Hello" plus a final null character ('\0') which specifies the end of the

    sequence and that, in the second case, when using double quotes (") it is appended automatically.

    String input/output function :

    Character Input function

    getchar();

    exp:

    char a;

    a=getchar();

    getch();

    exp:

    char a;

    a=getch();

    getche();

    exp:

    char a;

    a=getche();

  • 7/31/2019 Notes C Language

    18/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 18

    PointerWe have already seen how variables are seen as memory cells that can be accessed using their identifiers.

    This way we did not have to care about the physical location of our data within memory, we simply used

    its identifier whenever we wanted to refer to our variable.

    The memory of your computer can be imagined as a block of memory cells, each one of the minimal sizethat computers manage (one byte). These single-byte memory cells are numbered in a consecutive way.

    This way, each cell can be easily located in the memory because it has a unique address and all the

    memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it is

    going to be right between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one

    thousand cells before cell 2776.

    Reference operator (&)

    we declare a variable, the amount of memory needed is assigned for it at a specific location in memory We

    generally do not actively decide the exact location of the variable within the panel of cells that we have

    imagined the memory to be - Fortunately, that task automatically performed by the operating systemduring runtime. However, in some cases we may be interested in knowing the address where our variable

    is being stored during runtime in order to operate with relative positions to it.

    The address that locates a variable within memory is what we call a reference to that variable. Thisreference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign

    (&), known as reference operator

    Consider the following code

    Pqr= 25;

    abc = pqr;

    xyz = &pqr

    The values contained in each variable after the execution of this, are shown in the following diagram:

    pqr

    1775 1776 1777

    abc xyz

    First, we have assigned the value 25 to pqr (a variable whose address in memory we have assumed to be

    1776).

    The second statement copied to abc the content of variable pqr (which is 25). This is a standard

    assignment operation, as we have done so many times before.

    Finally, the third statement copies to xyz not the value contained in pqr but a reference to it (i.e., its

    address, which we have assumed to be 1776). The reason is that in this third assignment operation we have

    preceded the identifierpqr with the reference operator (&),

    The variable that stores the reference to another variable is call apointerVariable. Pointers are a verypowerful feature of the C language., we will see how this type of variable is used and declared.

    Declaring variables of pointer types

    Like the simple variable pointer variable is declared before use it. The syntax of pointer variabledeceleration is shown as following :

    25

    25 1776

  • 7/31/2019 Notes C Language

    19/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 19

    The declaration of pointers follows this format:

    Datatype * Pointer-variable name;

    Exp:

    int *p;

    where int is the data type of the pointer variable P .This type is not the type of the pointer itself! but the

    type of the data the pointer points to. For example:

    int * number;

    char * character;

    float * greatnumber

    These are three declarations of pointers variables. Each one is intended to point to a different data type, but

    in fact all of them are pointers and all of them will occupy the same amount of space in memory of apointer they are said to have different types: int*, char* and float* respectively, depending on the type they

    point to.

    .

    Function:

    Function Contain a block of statements that performs a specific task. in c program It has a name and it is reusable i.e. it

    can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling

    program

    So function in a C program has some properties discussed as below.

    Every function has a unique name. This name is used to call function from main() function. A function can be

    called from within another function.

    A function is independent and it can perform its task without interfering with other parts of the program.

    A function performs a specific task. A task is a different job that Our program must perform as a part of its

    overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube

    root etc.

  • 7/31/2019 Notes C Language

    20/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 20

    A function returns a value to the calling program. This is optional and depends upon the task our function is

    going to accomplish. Suppose we want to just show few lines through function then it is not necessary to return a

    value. But if we are calculating area of rectangle and wanted to use result somewhere in program then we have to

    send back (return) value to the calling function.

    C language is collection of various inbuilt functions. If we have written a program in C then it is evident that you have

    used Cs inbuilt functions.Printf, scanf, clrscr getchar() etc. all are Cs inbuilt functions.

    Function Declaration:

    Function declaration is specified three things

    Function return type

    Function name

    Function list of arguments

    Return type function-name(argument list);

    In the above example function return type is refer to the type of the value that is returned by the function.

    and function-name specifies the name of functions which should be a valid identifier and the list of

    arguments is enclosed within a pair of parenthesis. Each argument should be separated by comma.

    Function Definition:

    Syntax of a Function

    A general Syntax of a C function looks like this:

    FunctionName (Argument1, Argument2, Argument3)

    {

    Statement1;

    Statement2;

    Statement3;

    }

    An example of function.

    int sum (int x, int y)

    {

    int t;

    t = x + y;

    return (t); or return t;

    }

    Advantages of using functions:

    http://hubpages.com/_eknow/hub/Data-Types-in-C-Languagehttp://hubpages.com/_eknow/hub/Data-Types-in-C-Languagehttp://hubpages.com/_eknow/hub/Data-Types-in-C-Language
  • 7/31/2019 Notes C Language

    21/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 21

    There are many advantages in using functions in a program they are:

    1. It makes possible top down modular programming. In this style of programming, the high level logic of the

    overall problem is solved first while the details of each lower level functions is addressed later.

    2. The length of the source program can be reduced by using functions at appropriate places.

    3. A function may be used later by many other programs this means that a c programmer can use function written by

    others, instead of starting over from build a New function.

    4. A function can be used to keep away from rewriting the same block of codes which we are going use two or more

    locations in a program. This is especially useful if the code involved is long or complicated.

    Types of functions:

    A function may belong to any one of the following categories:

    1. Functions with no return values and no arguments.2. Functions with return values and no arguments

    3. Functions with no return values and arguments

    4. Functions with return values and arguments

    Example of a simple function to add two integers.

    #include

    void add(int x, int y) /*function Declaration and definition

    {int t;

    t = x+y;

    printf("Sum of %d and %d is : %d.\n ",x,y,t);

    }

    void main()

    {

    add(10,15); /*function calling

    add(55,64);

    add(168,325);

    }

    Program Output

    Sum of 10 and 15 is : 25

    Sum of 55 and 64 is : 119

    Sum of 168 and 325 is :493

    Recursion:

    Recursion is technique of defining a set or a process in terms of itself. When a function is called

    itself that concept is know as recursion.. A recursion function calls itself. Recursion call of function

    can either condition ot unconditional. The conditional recursion function termination when certaincondition will be true and An unconditional recursive function causes the infinite sequence of calls.

    /* Find the factorial of any number using recursion */

  • 7/31/2019 Notes C Language

    22/22

    Guru Nanak Dev Coaching Centre9 9 1 4 1 2 4 6 2 3 , 9 8 1 4 5 5 2 0 3 7 22

    #include

    int fact(int);

    void main( )

    {

    Int a;

    printf(Enter Number for find factorial : );scanf(%d,&a);

    printf(Output = %d,fact(a));

    }

    int fact(int b)

    {

    If(b==0)

    {

    return(1);

    }

    else{

    return(b*fact(b-1));

    }

    }

    Structure: