Wednesday, July 31, 2013

Overview of C Programming Language

Basic Information About C Programming Language

Computer Component

Hardware
    • Hardware is the equipment, or the devices, associated with a computer.

Software
    • The instructions that tell the computer what to do are called software or programs.
    • Program written by programmers

Hardware and software accomplish four major operations:

    • Input
    • Processing
    • Output
    • Storage.

Types of Language

High – level Language
    • Language in human readable form are known as high – level language.
    • E.g. C, C++, Java etc.
Low – level Language
    • Language in the form of 0s and 1s is known as low level language.
    • Also known as machine language

Types of Error

Syntax Error
    • Refers to an error in the syntax of a sequence of characters or tokens for particular programming language.
    • Syntax error will be display at compile time.
Logical Error
    • Error occurs because of logic of code is known as logical error.
    • It will be display at run time.

Compiler and Interpreter

    • It is one kind of program to convert high level language into low level language.
    • Interpreter check code line by line if error than display error.
    • Compiler check whole code and than display list of errors.

Reserve Keywords

    • Some words are reserved for special purpose which are known as reserve keywords.
    • Reserve keywords are not used as an name of variable, functions, class etc.
    • The keywords must be written in lower case only.

Identifiers

    • Identifiers refer to the names of variables, functions and arrays.
Rules for identifiers
    • Must consists of only letters, digits, or underscore.
    • First character must be an alphabet (or underscore).
    • Only first 31 characters are significant.
    • Cannot use a reserve keyword.
    • Must not contain white space.

Constants

    • Constants refer to fixed values that do not change during the execution of program.

Backslash Character Constant

C Programming Backslash Character

Variables

    • Variable is a named location of memory that is used to hold a value.
    • Its value can be modified during the program execution.
    • We can also say that variable is an identifier that is used to represent a single value.

Data Types

    • It is a type of data that can be represent in the value of data element.
    • It defines the type of constant which is going to store into the variable.
    • Compiler will allocate memory for variable based on data type.

Comments

    • Sometime we want to write comment in the program which is not an executable part of our source code.

There are two types of comment

Single line comment ( // )
    • E.g. // This is a single line comment
Multi line comment ( /* ------------ */)
    • E.g. /* Program name : sum.cpp
      Definition : To calculate the sum */

Declaring Variable

    • Before using the variable, you have to define variable to give information to compiler.
    • Syntax
      Datatype VariableName;
    • E.g.
      int StudentId;
      char Name;
      float Average;
      double SumOfSalary;
    • Statements used to declare variable is known as declarative statement.
    • When we declare a variable
    • Space for a variable is set in memory to hold a value of the specified data type.
    • That space is associated with the variable name.
    • That space is associated with a unique address.
    • E.g. int StudentId;

Variable Initialization

    • At the time of variable declaration we can assign default value which is known as variable initialization.
    • E.g. int StudentId = 25;

Assigning value to variable

    • We can also assign the value to the variable.
    • The statement which assign value to the variable is known as assignment statement.

C Program Structure

Preprocessor Directive
Global Declaration
User Defined Functions
int main()
{ Local Declaration
Statements }

Output Function : printf()

    • printf( ) is a function defined in stdio.h header file.
    • It reads data from the computer’s memory and display it on standard output device.
    • printf stands for print formatted.
    • On success, the total number of characters written is returned otherwise return negative number.
      • Syntax :
        printf(“control string”, VariableList);
      • Ex.
        Input Code :
        printf(“Welcome to home”);
        Output is : Welcome to home
      • Ex.
        Input Code : int StudentId = 10;
        printf(“%d”, StudentId);
        Output is : 10
    • You can also combine message and value of variable.
      • Ex.
        Input Code : int StudentId = 10;
        printf(“Id of student is %d”, StudentId);
        Output is : Id of student is 10
    • You can also specify the width of field.

      • Syntax :
        printf(“%field-width FS”, VariableName);
      • E.g.
        Input Code : int No = 2563;
        printf(“%3d”, No);
        Output : 256

Input Function : scanf()

    • It is input formatting function.
    • It will read data from standard input and stored in given variable.
    • scanf stands for scan formatted.
    • Remember that leading white spaces are ignored.
    • On success, the function returns the number of items successfully read.
    • In the case of an input failure before any data could be successfully read, EOF is returned
      • Syntax :
        scanf(“Control String”, &VariableList);
      • E.g.
        int No;
        scanf(“%d”, &No);
        char Str;
        scanf(“%c”, &Str);
    • You can't use message in scanf function.
    • If you want to display message for user understanding than use printf function.
      • E.g.
        int No;
        printf("Enter one number:¨);
        scanf("%d", &No);
    • Width is used to specified that maximum number of characters to be read.
    • But if the whitespace character found before width, that only many characters are going to be stored.
      • E.g.
        int No;
        scanf(“%2d”, &No);

Operator

    • Unary Operator
      • Operator which required one operand is known as unary operator.
    • Binary Operator
      • Operator which required two operand is known as binary operator.
    • Ternary Operator
      • Operator which required three operand is known as ternary operator.


Goto Statement

    • We can change the normal sequence of execution by transferring control to other part of the program using goto statement.
    • Syntax: goto label_name;
    • Here the label name should be valid identifier which may appear in anywhere in the program.

Break Statement

    • The break statement uses to come out from a loop.
    • It can be used to force to end the loop before its natural end.
    • Remember that break statement can only used with loop statements and switch statement.

Continue Statement

    • The continue statement skip the rest part of the loop and transfer control at the beginning of the loop for next iteration.

Switch Statement

    • We can choose statements to execute based on some value.
    • This can be achieve using nested if….else statement. But it is very complex.
    • C language provide switch statement to avoid this type of complexity.

0 comments:

Post a Comment