C Programming Basics

 


C is a general purpose structural language.It is discovered by Dennis Ritchie in year of 1972 at AT 7 T's Bell Laboratories of USA.It is high level language.Instructions of this language is in simple English language such as if else , do while , break , continue , for , int etc. C language is developed by combining two old language such as BCPL(Basic Computer Programming Language).

C Tokens
  • The C character Set
  • Following character is used in C language:
    • Small letters (a....z)
    • Capital latter (A,B.....Z)
    • Digit (0....9)
    • Special Characters (+,-,*,/,%,!,#,&,<,> etc)
    • White space like new line,blank space etc.

  • Identifiers
  • Identifiers are name which is used to identify the different part of program such as Array,Variables and Functions etc.It may be user define or pre-define.Alphabets (a...z),Digits (0...9) and one special symbol (_) are used in identifiers.
  • Keyword
  • Keywords are reserve words.Keywords are those words which is pre-defined by compiler.These keywords are not used as identifiers. There are 32 keywords in C language such as:
    dogotosignedwhile
    autodoubleifstatic
    charexternneartypedef
    breakelseintstruct
    caseenumlongswitch
    constfloatregisterunion
    continuefarreturnunsigned
    defaultforshortvoid
    Structure of program in C
    C program is a combination of one or more functions. C program contain one main function called main().Compiler start execution of program from main() function. Each C program contain following section such as:
    1. Preprocessor
      Programmer specify name of header file in this section. It provide instruction to C compiler.
      Header files such as: #include <conio.h> , #include<stdio.h> , #include<math.h> etc.
    2. Function Heading
      This section contain function name with arguments (if any) such as main().
    3. Variable Declaration
      This section contain variable declaration , definition , initialisation with their data type. Such as int x=1; , cahr a; , float p=3.14; etc. This section end with semi colon.
    4. Compound Statement
      This section contain group of statement. Statements contain printf , scanf , if..else , switch case , getch() , calculation (a=b+c) etc statements in this section. We can say that this section contain Input , Output , calculation part , condition statements.

    Loop Structure in C
    Looping is a repetitive structure.In looping a group of structures are executed until some condition has been satisfied.

    In C following loop structure are available:
    1. for loop
    2. while loop
    3. do-while loop
    1. for loop


    Syntax:

    for ( initialisation expressions;
    test expression;
    update expression;)
    body-of-the-loop;

    Example:

    int i; //declare variable
    for(i=1;i<=10;i++)
    printf("\n%d",i);
    getch();
    Note:
    case 1.:

    for(initialise exp1 initialise exp2;
    test expression 1 test expression 2;
    update expression 1 update expression 2)

    case 2.:

    for( ;test expression;update expression)

    case 3.:

    for(initialise exp; ;update expression)--infinite
    for( ; ;)--infinite

    case 4.:

    for(initialise exp;test expression;update expression); -empty no body

    case 5.:

    for(int i=1;i<10;i++) --deceleration of i

    Function in C

    Function play an important role in programming.It is basically a collection of statements which perform specific task.Each function have some specific name which is used for calling the purpose in any program.We can say that it is a type of sub-program.

    Function is a self contain program that can perform some specific and well define task. A large program is broken down into number of smaller program in the form of function which can perform specific task. we can achieve re-usability by creating functions.Any function can define once and call many times in a program.

    In C programming their are two type of functions such as:

     Predefined functions

    ✔ User define functions.

    BUILT-IN FUNCTION

    PREDEFINED FUNCTION

    Predefined functions are built-in functions or library functions which is used by user.User can not define those functions.These functions are created at the time of creation of C language.We cannot change their definition.These functions are stored in library files and these library files are known as header files. We can identify these file by ( .h ) extension.There are so many header files are present in C.If we want to use any built-in function in our program than we must include these header files in C program by using INCLUDE keyword. 

    like: # include <conio.h> , # include <stdio.h> , # include <math.h> , # include <string.h> etc.

    Now we can simply say that all built-in function are available in header files. So without including these header file we can not use any built-in functions in any program.

    Some built-in functions are printf() , scanf() , sum() , pow() etc

    USER DEFINE FUNCTION:

    User define function are those function which is define by user for their specific task.Let we understand Why user define function needed ?

    We know that we write our program inside main() function. If our program is small than no need to create any functions but if our program goes long in size line of code than we must break our program in small modules called function.

No comments:

Post a Comment