ICS 103: Computer Programming in C

Handout-02

Topic: Overview of C Language

 

Instructor: M. Waheed Aslam.

 

Objective:

  

C LANGUAGE ELEMENTS:

 

(1) Preprocessor Directives:

 

 

Syntax:

      #include <standard header file>

e.g

              #include <stdio.h>

              #include <math.h>

 

 

Syntax:

               #define    NAME     value

e.g

              #define  PI   3.141593

              #define  MILES_PER_KM   0.62137

              #define  MAX_LENGTH   100

 

In this declaration, each identifier NAME is replaced by the constant value and this value will remain fixed (constant) throughout the whole program.

 

 /* This is a comment: Could be side comment, stand alone or in between */

 

 

(2) Function main:

 

 

Syntax:

    int main (void)

    {

        function body /* it has two parts: declarations and executable statements */

    }

 

 

Example:

    int                    /* function returns an integer value (0) to the OS when terminates normally.*/

    main (void)       /* void indicates that main function receive no input data from the OS.*/

    {                         /* Open brace symbol marks start of function body. */

        printf ("Assalamu Aleikum\n");

        return (0);

    }                       /*Closing brace symbol marks end of function body*/

 

 

Outline of  a C Program:

 

Then the general format of a C program is:

 

preprocessor directives

int main (void )

{

variable declarations;

statements;                                       /* Executable statements are derived from the algorithm */

}

 

 

Example: C program to find addition of two numbers is:

 

 /* To find sum of two numbers */

 

#include <stdio.h>                                              /* preprocessor directive*/

main ()                                                                  /* main function*/

{

 

int number1, number2, sum;                                 /* variable declarations*/

printf ("Please input two numbers: ");                   /* to display message*/

scanf ("%d %d",&number1, &number2);           /* to read 2 numbers*/

sum = number1+number2;                                            /* to calculate sum*/

printf ("The sum of two numbers is: %d " ,sum);      /* to print sum*/

return (0);

}                                                                                    /*end of main*/

 

Overview of C Program:

 

Complete overview of  C Program is explained in following example:

 

   /* Displays the user's nickname and the current year in a welcoming message. */

 

   #include <stdio.h> /* printf, scanf definitions */

   int  main(void) // main function

   {

         char letter_1,  letter_2,  letter_3 , letter_4;      /* four letters */

         int  year;    /* current year  */

 

         printf("Enter a 4-letter name and press return: ");

         scanf("%c %c %c %c", &letter_1, &letter_2, &letter_3, &letter_4); //to read 4 letters from keyboard

 

         printf("Enter the current year and press return:");

 

         scanf("%d", &year); // to read year from keyboard

 

         printf("Welcome, %c%c%c%c. %d is a great year to study C!\n", letter_1, letter_2, letter_3,letter_4, year);

            return (0);

   } // end of main

 

Sample Output:

  

   Enter a 4-letter name and press return: Omar

   Enter the current year and press return: 2004

   Welcome, Omar. 2004 is a great year to study C!

 

(3) Reserved Words:

 

All reserved words in C language appears in lower case ; they have special meaning in C and cannot be used or redefined for any other purpose at any place .

 

In our previous programs reserved key words are: int, char, void, double, return  etc.

 

 

A complete list of ANSI C reserved words is present in your text book at Appendix H .

 

 

 

 (4) Standard Identifiers:

 

 

(5) User Defined Identifiers:

 

Rules for Making  User Defined Identifier/Variable name:

·       An identifier name cannot begin with a digit.

·       An identifier name  must consist only  of letters, digits, or underscores.

·       A reserved word in C language (e.g.,  return , double , int etc) cannot be used as an identifier name.

·       An identifier defined in a C standard library (e.g., printf , scanf) should not be redefined. This is an advice rather than ANSI C syntax.

 

 

Valid User-Defined Identifiers:   PI, radius, area, letter_1, circum, year, main etc.

 

 

Invalid User-Defined Identifiers:  4 name,  int,  two-by-four,  scanf.

 

Syntax rules for identifiers do not place a limit on identifier length.

 

However, some ANSI C compilers put a limit of 31 characters as significant ones.

 

Uppercase and Lowercase Letters:

 

In C – Language there is big difference between lowercase and uppercase letters.

e.g., 

    int num;

    int NUM;

    int Num

are all considered to be different.

§       It is recommended that you should follow a consistent pattern in using uppercase and lowercase letters in your program. This facilitate reading the program by users.

§       All reserved words in C are in lowercase.

§       All names of all standard library functions are in lowercase letters.

§       Common practice adopted by professional programmers is to use only uppercase letters in the names of constant macros.

§       Other identifiers generally use lowercase letters.

 

 

Variable declarations and Standard Data Types in C Programs:

 

The memory cells used for storing a program’s input data and its computational results are called variables because the values stored in variables may change (and usually do) as the program executes.

 

 

In a C program, the variable declaration tells the C compiler:

 

Therefore, a variable declaration in C begins with an identifier (e.g., double) that tells the C compiler, the type of data (e.g., an integer, Character, real, etc.) stored in the associated variable location.

 

 

Variable names are also known as identifiers. Use meaningful identifier names!!!

 

Syntax for variable Declaration:

 

                                                      int   variable_list;

                                                      double   variable_list;

                                                      char  variable_list;

                                                      float   variable_list;

 

                                                e.g.,

 

                                                    int count, large;

                                                    double x, y, z;

                                                    char first_initial, second_initial;

                                                    float a, b, c;

 

 

Basic Data Types in C:

 

Data Type int:

 

Data Type double:

 

 

ANSI C provides for three floating-point types:

 

 

 

Data Type char:

 

 

Basic data types in C language are int, char, float and double:

 

Data Type

Bytes required for storing it in computer memory.

Placeholder / Conversion specifier required with scanf or printf .

int

2

%d

char

1

%c

float

4

%f

double

8

%lf