C CONCEPTS

C – Language History


  • C language is a structure oriented programming language, was developed at Bell Laboratories in 1972 by Dennis Ritchie
  • C language features were derived from earlier language called “B” (Basic Combined Programming Language – BCPL)
  • C language was invented for implementing UNIX operating system
  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition  “The C Programming Language” and commonly 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.


Features of C language:

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



Uses of C language:

C language is used for developing system applications that forms major portion of operating systems such as Windows, UNIX and Linux. 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

C – Basic Program


We are going to learn a simple “Hello World” C program in this section. Also, all the below topics are explained in this section which are the basics of a C program.

#include
int main()
{
     /* Our first simple C basic program */
     printf("Hello World! ");
     getch();
     return 0;
}


Output:

Hello World!

Explanation for above C basic Program:


Let’s see all the sections of the above simple C program line by line.
S.noCommandExplanation
1#include This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
2int main()This is the main function from where execution of any C program begins.
3{This indicates the beginning of the main function.
4/*_some_comments_*/whatever is given inside the command “/*   */” in any C program, won’t be considered for compilation and execution.
5printf(“Hello_World! “);printf command prints the output onto the screen.
6getch();This command waits for any character input from keyboard.
7return 0;
This command terminates C program (main function) and returns 0.
8}
This indicates the end of the main function.

 Basic structure of C program:


     Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned  below.

    1. Documentation section
    2. Link Section
    3. Definition Section
    4. Global declaration section
    5. Function prototype declaration section
    6. Main function
    7. User defined function definition section


C – Printf and Scanf

                                                                                                    

  • printf() and scanf() functions are inbuilt library functions in C which are available in C library by default. These functions are declared and defined in “stdio.h” which is a header file.
  • We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions.

1. C printf() function:

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

2. C scanf() function:

    • scanf() function is used to read character, string, numeric data from keyboard
    • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
    • Then, user enters a string and this value is assigned to the variable ”str” and then displayed.

 C tokens:
    • C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
    • Each and every smallest individual units in a C program are known as C tokens.
    • C tokens are of six types. They are,
    1. Keywords               (eg: int, while),
    2. Identifiers               (eg: main, total),
    3. Constants              (eg: 10, 20),
    4. Strings                    (eg: “total”, “hello”),
    5. Special symbols  (eg: (), {}),
    6. Operators              (eg: +, /,-,*)

 Identifiers in C language:

    • Each program elements in a C program are given a name called identifiers.
    • Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name given to integer variable in above program.

Rules for constructing identifier name in C:

    1. First character should be an alphabet or underscore.
    2. Succeeding characters might be digits or letter.
    3. Punctuation and special characters aren’t allowed except underscore.
    4. Identifiers should not be keywords.

Keywords in C language:

    • Keywords are pre-defined words in a C compiler.
    • Each keyword is meant to perform a specific function in a C program.
    • Since keywords are referred names for compiler, they can’t be used as variable name.

C language supports 32 keywords which are given below


C – Constant


  • C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined.
  • Constants refer to fixed values. They are also called as literals
  • Constants may be belonging to any of the data type.
  • Syntax:
const data_type variable_name; (or) const data_type *variable_name;

  1. Rules for constructing C constant:

    1. Integer Constants in C:

      • An integer constant must have at least one digit.
      • It must not have a decimal point.
      • It can either be positive or negative.
      • No commas or blanks are allowed within an integer constant.
      • If no sign precedes an integer constant, it is assumed to be positive.
      • The allowable range for integer constants is -32768 to 32767.

    2. Real constants in C:

      • A real constant must have at least one digit
      • It must have a decimal point
      • It could be either positive or negative
      • If no sign precedes an integer constant, it is assumed to be positive.
      • No commas or blanks are allowed within a real constant.

    3. Character and string constants in C:

      • A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
      • The maximum length of a character constant is 1 character.
      • String constants are  enclosed within double quotes.

    4. Backslash Character Constants in C:

      • There are some characters which have special meaning in C language.
      • They should be preceded by backslash symbol to make use of special function of them.
      • Given below is the list of special characters and their purpose.

How to use constants in a C program?


#include
void main()
{
   const int  height = 100;                /*int constant*/
   const float number = 3.14;              /*Real constant*/
   const char letter = 'A';                /*char constant*/
   const char letter_sequence[10] = "ABC"; /*string constant*/
   const char backslash_char = '\?';      /*special char cnst*/
   printf("value of height    : %d \n", height );
   printf("value of number : %f \n", number );
   printf("value of letter : %c \n", letter );
   printf("value of letter_sequence : %s \n", letter_sequence);
   printf("value of backslash_char  : %c \n", backslash_char);
}




C – Variable



  • C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.

Rules for naming C variable:

    1. Variable name must begin with letter or underscore.
    2. Variables are case sensitive
    3. They can be constructed with digits, letters.
    4. No special symbols are allowed other than underscore.
    5. sum, height, _value are some examples for variable name

There are three types of variables in C program They are,

    1. Local variable=These variables are declared within the function and can’t be accessed outside the function.
    2. Global variable=This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.
    3. Environment variable=Environment variable is a variable that will be available for all C  applications and C programs.We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.
  1. There are 3 functions which are used to access, modify and assign an environment variable in C. They are,
  2. 1. setenv()
    2. getenv()
    3. putenv()

     Example program for setenv() function in C:

          This function sets the value for environment variable. Let us assume that environment variable “FILE” is to be assigned “/usr/bin/example.c”

    Example program for getenv() function in C:

          This function gets the current value of the environment variable. Let us assume that environment variable DIR is assigned to “/usr/bin/test/”.

    Example program for putenv() function in C:

          This function modifies the value for environment variable. Below example program shows that how to modify an existing environment variable value.
    #include
    #include
    int main()
    {
       setenv("DIR","/usr/bin/example/",50);
       printf("Directory name before modifying = " \
              "%s\n", getenv("DIR"));
     
       putenv("DIR=/usr/home/");
       printf("Directory name after modifying = " \
                 "%s\n", getenv("DIR"));
       return 0;
    }