STUDY MATERIAL CS&IS BRANCH

U can find the study material here.. U can request for study materials my mailing the topics to pesitreviews@gmail.com... It will be uploaded soon from then and will be available for everybody..
























 C language Tutorials



C – Language History

                                                                                                                                  
  • C language is a structure oriented programming language, was developed at Bell Laboratories in 1972 by Dennis Ritchie
  • C features were derived from earlier language called “B” (BCPL language)
  • C was invented for implementing UNIX operating system
  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition  “The C Programming Language” and commanly known as K&R C
  • In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”, was completed late 1988.

C standards

  • C89/C90 standard – First standardized specification for C language was developed by American National Standards Institute in 1989. C89 and C90 standards refer to the same programming language.
  • C99 standard – Next revision was published in 1999 that introduced new futures like advanced data types and other changes.

C11 and Embedded C

  • C11 standard adds new features to C and library like type generic macros, anonymous structures, improved Unicode support, atomic operations, multi-threading, and bounds-checked functions. It also makes some portions of the existing C99 library optional, and improves compatibility with C++.
  • Embedded C includes features not available in normal C like fixed-point arithmetic, named address spaces, and basic I/O hardware addressing
  • Operating systems, C compiler, all UNIX application programs are written in C
  • It is also called as procedure oriented programming language
  • C language is reliable, simple and easy to use.
  • C has been coded in assembly language

Features of C language:

  • Reliability
  • Portability
  • Flexibility
  • Interactivity
  • Modularity
  • Efficiency and Effectiveness

Uses of C language:

C is used for developing system applications that forms portion of operating system. Below are some examples of C being used.
  • Database systems
  • Graphics packages
  • Word processors
  • Spread sheets
  • Operating system development
  • Compilers and Assemblers
  • Network drivers
  • Interpreters

Which level the C language belonging to?

S.no
High Level
Middle Level
Low Level
1 High level languages provides almost everything that the programmer might need to do as already built into the language Middle level languages don’t provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we want Low level languages provides nothing other than access to the machines basic instruction set
2 Examples:Ada, BASIC, COBOL, FORTRAN, Modula-2 C, C++, JAVA, FORTH, Macro-Assembler Assembler

C is a structured language

S.no
Structure oriented
Object oriented
Non structure
1 In this type of language, large programs are divided into small programs called functions In this type of language, programs are divided into objects There is no specific structure for programming this language
2 Prime focus is on functions and procedures that operate on data Prime focus is on the data that is being operated and not the functions or procedures N/A
3 Data moves freely around the systems from one function to another Data is hidden and cannot be accessed by external functions N/A
4 Program structure follows “Top Down Approach” Program structure follows “Bottom UP Approach” N/A
5
Examples:
BASIC, COBOL, FORTRAN
C, C++, JAVA, Ada, Pascal, Modula-2
Assembler

Key points to remember:

  1. C is structured, middle level programming language developed by Dennis Ritchie
  2. Operating system programs such as Windows, Unix, Linux are wriiten by C language
  3. C89/C90 and C99 are two standardized editions of C
  4. C has been written in assembly language                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

    C – Basic Program

                                                                                                                                   

    C – Basic Program:

    We are going to learn a simple “Hello World” program in this section. Functions, syntax and the basics of a C program are explained in below table.

    Output:

    Hello World!
    Let us see the parts of the program line by line.
    S.no Command Explanation
    1 #include<stdio.h> This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
    2 int main() This is the main function from where execution of any C program begins
    3 /* some comments */ whatever is given inside /*   */ this command won’t be considered by C program for compilation and execution.
    4 printf(“Hello World! “); printf command prints the output onto the screen
    5 return 0;
    This command terminates main function then returns 0

    Compile and execute C program:

    • Compilation is the process of converting a C program which is user readable code into machine readable code which is 0′s and 1′s.
    • This compilation process is done by a compiler which is an inbuilt program in C.
    • As a result of compilation, we get another file called executable file. This is also called as binary file.
    • This binary file is executed to get the output of the program based on the logic written into it.

    Steps to compile & execute a C program:

    Step1 Type the above C basic program in a text editor and save as “sample.c”.
    Step2 To compile this program, open the command prompt and goto the directory where you have saved this program and type “cc sample.c” or “gcc sample.c”.
    Step3 If there is no error in above program, executable file will be generated in the name of “a.out”.
    Step4 To run this executable file, type “./a.out” in command prompt.
    Step5 You will see the output as shown in the above basic program.

    C basic structure:

    Structure of C is defined by set of rules called protocol, to be followed by programmer while writing C program.
    Output:
    This is main function
    Total = 2
    Let us see about each section of a C basic program in detail below.
    S.No Sections Description
    1 Documentation section We can give comments about the program, creation or modified date, author name etc in this section. Comments can be given in two ways inside a C program.       

    1. Single line comment – Syntax :

    // Your Comments Here

    2. Multi line comment – Syntax :

    /*
    comment line1
    comment line2
    comment line3
    */
    The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
    2 Link Section Header files that are required to execute a C program are included in this section
    3 Definition Section In this section, variables are defined and values are set to these variables.
    4 Global declaration section Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
    5 Function prototype declaration section Function prototype gives many information about a function like return type, parameter names used inside the function.
    6 Main function Every C program is started from main function and this function contains two major sections called declaration section and executable section.
    7 User defined function section User can define their own functions in this section which perform particular task as per the user requirement.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

    C – Printf and Scanf

                                                                                                                                

    C printf():

    The printf() function is used to print the character, string, float, integer, octal and hexadecimal values onto the output screen.
    To display the value of an integer variable, we use printf  statement with the %d format specifier.
    Similarly %c for character, %f for float variable,%s for string variable, %lf for double, %x for hexadecimal variable.
    To generate a newline,we use \n in C printf statement.

    Example program for C printf():

     Output:

    Character is A
    String is fresh2refresh.com
    Float value is 10.234000
    Integer value is 150
    Double value is 20.123456
    Octal value is 226
    Hexadecimal value is 96
    You can see the output with the same data which are placed within the double quotes of printf statement in the program except
    %d got replaced by value of an integer variable(no),
    %c got replaced by value of a character variable(ch),
    %f got replaced by value of a float variable(flt),
    %lf got replaced by value of a double variable(dbl),
    %s got replaced by value of a string variable(str),
    %o got replaced by a Octal value corresponding to integer variable(no),
    %x got replaced by a hexadecimal value corresponding to integer variable(no),
    \n got replaced by a newline.

    C scanf :

    scanf() function is used to read a character, string,  numeric data from keyboard.
    Consider the below example where the user enters a character and assign it to the variable ch and enters a string and assign it to the variable str.
    Example program for C printf() and scanf():

     Output:

    Enter any character   a
    Entered character is a
    Enter any string ( upto 100 character ) 

    hai
    Entered string is hai
    The format specifier %d is used in scanf statement so that the value entered is received as an integer and %s for string.
    Ampersand is used before the variable name ch in scanf statement as &ch. It is just like in a pointer which is to point to the variable.For more information about how pointer works,please click here.






    C – Data Types

                                                                                                                                             
    • C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
    • Data types are used to define a variable before to use in a program.
    • Size of variable, constant and array are determined by data types.

    C – data types:

    There are four data types in C language. They are,
    S.no
    Types
    Data Types
    1 Basic data types int, char, float, double
    2 Enumeration data type enum
    3 Derived data type pointer, array, structure, union
    4 Void data type void

    1. Basic data types

    Integer data type:

               This data type allows a variable to store numeric values. int keyword is used to refer integer data type. The storage size of int data type is 2 or 4 or 8 byte. It varies depend upon the processor in the CPU that we use.  If we are using 16 bit processor, 2 byte(16 bit) of memory will be allocated for int data type. Like wise, 4 byte(32 bit) of memory for  32 bit processor and 8 byte(64 bit) of memory for  64 bit processor is allocated for int.
    int(2 byte) can store values from -32,768 to +32,767
    int(4 byte) can store values from -2,147,483,648 to +2,147,483,647.
    If you want to use the integer value that crosses the above limit, you can go for long int and long long int for which the limits are very high.
    Note:
               You can’t store decimal values using int data type. If you use decimal values, those would be truncated and you will get only whole number.
    float data type can be used to store decimal values in a variable.

    Character data type:

               This data type allows a variable to store only one character. storage size of char data type is 1. We can store only one character using char data type. char keyword is used to refer character data type.
    For example, ‘A’ can be stored using char datatype.
    You can’t store more than one character using char data type. Please refer strings topic to know how to store more than one characters in a variable.

    Floating point data type:

    Floating point data type consists of 2 types. They are, float and double.

    float:

    Float data type allows a variable to store decimal values. storage size of float data type is 4. This also varies depend upon the processor in the CPU. We can use upto 6 digits after decimal using float data type.
    For example, 10.456789 can be stored in a variable using float data type.

    double:

    Double data type is also same as float data type which allows upto 10 digits after decimal. and the range is from 1E–37 to 1E+37.
    C – sizeof() function:
    sizeof () operator is used to find the memory space allocated for each C data types.

    Output:

    Storage size for int data type:4
    Storage size for char data type:1
    Storage size for float data type:4
    Storage size for double data type:8

    C – Modifiers

    The amount of memory space to be allocated for a variable is derived by modifiers. Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
    For example, storage space for int data type is 4 byte for 32 bit processor.
    We can increase the range by using long int which is 8 byte.
    We can decrease the range by using short int which is 2 byte.
    • There are 5 modifiers available in C language. They are,
          1. short
          2. long
          3. signed
          4. unsigned
          5. long long
    • Below table gives the detail about the storage size of each C basic data type in 16 bit processor.
      Please keep in mind that storage size and range for int and float will vary depend on the CPU processor (8,16, 32 and 64 bit)
    S.No C Data types storage Size Range
    1 char 1 –127 to 127
    2 int 2 –32,767 to 32,767
    3 float 4 1E–37 to 1E+37 with six digits of precision
    4 double 8 1E–37 to 1E+37 with ten digits of precision
    5 long double 10 1E–37 to 1E+37 with ten digits of precision
    6 long int 4 –2,147,483,647 to 2,147,483,647
    7 short int 2 –32,767 to 32,767
    8 unsigned short int 2 0 to 65,535
    9 signed short int 2 –32,767 to 32,767
    10 long long int 8 –(2power(63) –1) to 2(power)63 –1
    11 signed long int 4 –2,147,483,647 to 2,147,483,647
    12 unsigned long int 4 0 to 4,294,967,295
    13 unsigned long long int 8 2(power)64 –1

    2. Enumeration data type:

               Enumeration data type consists of named integer constants as a list. It start with 0(zero) by default and value is incremented by 1 for the sequential identifiers in the list.2. Enumeration data type:
    Enum syntax in C:
    enum identifier [optional{ enumerator-list }]
    Enum example :
    enum month { Jan, Feb, Mar }; or
    /* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
    enum month { Jan = 1, Feb, Mar };
    /* Feb and Mar variables will be assigned to 2 and 3 respectively by default */
    enum month { Jan = 20, Feb, Mar };
    /* Jan, Feb and Mar variables will be assigned to 20, 21 and 22 respectively by default */
    The above enum functionality can also be implemented by “#define” preprocessor directive as given below. Above enum example is same as given below.
    #define Jan 20;
    #define Feb 21;
    #define Mar 22;

    C – enum example program:

    Output:
    Month is March

    3. Derived data type:

               Array, pointer, structure and union are called derived data type. To know more about derived data types, please visit C – Array topic.

    4. Void data type:

               Void is an empty data type that has no value. This can be used in functions and pointers. Please visit C – Function topic to know how to use void data type in function with simple call by value and call by reference example programs.

No comments:

Post a Comment