• CHAPTER 2 | Part 1 | The C Declarations




    Chapter 2.1 » The C Declarations

    In this chapter, we basically intend to learn how to provide a proper structure to our C program. At the end of this chapter, you will be able to create small useful programs such as creating a contact list, calculating areas and perimeters of quadrilaterals, help your younger sibling complete homework with division & multiplication solvers, and many more! So, learn not just to crack your college exams, but also to build something useful!

    The C programming language is designed to support certain kinds of data to get useful output.The output is obtained using certain words and characters according to syntax rules. These syntax rules are similar to ‘grammar’ of a language. Every program must accurately follow these ‘syntax rules’ supported by that language. These syntax rules can be better understood, by means of practice.

    In English language, we have Nouns, Pronouns, Adverbs, etc. Similarly in C Programming Language we have syntax rules that include the following: C Character Set, Delimiters, Tokens, Identifiers, Constants, Variables . These are ‘The C Declarations’. It is similar to ‘Author’ and ‘Edition’ in the example given in the previous chapter (Part 4), where you need to declare the exact book you are looking for( that is, the exact task your program should perform.) These declarations should be added according to the syntax rules.

    (The syntax rules are explained in detail in my YouTube lecture.)


    Part 2.2 » The C Character Set

    In order to get the required output, you need to add few characters in between the program (according to syntax rules). These characters include Letters, Digits, White Spaces and Special Characters. Look at the ‘Hello World’ Program below. We need to put the semicolon (;), open and closed braces ({ } ), and other characters at the correct places, in-order to run the program successfully.
     
    »» The mindmap below shows the classification on The C Character set.
    Classification of the Character Set in C

    The special characters include:

     Symbol
     Meaning
     ~  Tilde
     ! Exclamation mark 
     # Number sign 
     $ Dollar sign 
    Percent sign  
     ^ Caret
     & Ampersand 
      * Asterisk 
    (  Lest parenthesis 
     ) Right parenthesis 
    Underscore  
     + Plus sign 
     |  Vertical bar
      \  Backslash
     ` Apostrophe
     -  Minus sign
     =  Equal to sign
      {  Left brace
      }  Right brace
     [  Left bracket
     Right bracket
     :  Colon
     "  Quotation mark
     ;  Semicolon
     <  Opening angle bracket
     >  Closing angle bracket
      ?  Question mark
     ,  Comma
     . Period
      /  Slash


    Part 2.3 » Delimiters

    Symbols used in the coding are called Delimiters. The symbols or Delimiters used in programming along with their uses are given in below. Click to enlarge.
    Classification of Delimiters



    Part 2.4 » Tokens

    Remember, every compiler has few syntax rules of its own kind. No two compilers set same rules. So, if you run a program on Ubuntu (LINUX), Turbo-C, CodeBlocks etc. they have got few rules different than the other.
    The smallest unit in a program/statement is called a token, and the compiler you are using, identifies them as tokens. In short individual words or punctuation marks are called tokens. They can be of Six Types: 

    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: +, /,-,*) 



    Example:

    1. #include<stdio.h>  
    2.  void main()   
    3.       {   
    4.          int x, y;  
    5.          x = 10, y = 20;   
    6.          total = x + y;   
    7.          printf ("Total = %d \n", total);  
    8.       }   
    9.  
    10. OUTPUT : 
    11.       Total = 30
       
    where,
    • main – identifier
    • {,}, (,) – delimiter
    • int – keyword
    • x, y, total – identifier
    • main, {, }, (, ), int, x, y, total – tokens
    • x, y – variables
    • 10,20 – constants
    • %d, \n – operators
    • int x, y, total; - string


    Part 2.5 » The C Keywords

    The table shows 32 C Keywords. Each Keyword has different functions based on the position and time you use it. However, we will not be going in depth of these keywords. 
    The C Keywords are reserved words by the compiler. All the C Keywords have been assigned fixed meanings and they cannot be used as variable names
    autodoubleintstruct
    breakelselongswitch
    caseenumregistertypedef
    charexternreturnunion
    constfloatshortunsigned
    continueforsignedvoid
    defaultgotosizeofvolatile
    doifstaticwhile


    Part 2.6 » Identifiers

    Suppose that in a class of 30 students you need to know the number of students scoring 10/10 in a Test. So, in order to get good appearance in the output, you set an identifier to 10/10 scoring students and name them as ‘BEST’. We wish to get BEST – 15/30 in the output screen.(if 15 out of a class of 30 students score 10/10)
    Identifiers set a user-defined name to a variable, constant, function, structure, union etc.
    Please Note: There shouldn’t be any blank space, punctuations and signs in C Identifiers. However, you can use Uppercase letters and underscore (_). Programmers use underscore to link between two words for the long identifiers. Best Students of the Class : NOT AN IDENTIFIER (spaces)
    “Best” : NOT AN IDENTIFIER ( symbols)
    Best--> : NOT AN IDENTIFIER (signs)
    Best_students_of _the_class: IDENTIFIER. 

    Classification of Identifiers



    Part 2.7 » Constants

    Let was consider this example: ‘sinx’ and ‘sin45’. Here ‘sinx’ can be any value. It depends on what value we assign to ‘x’. However ‘sin45’ is a constant. It cannot assume any value. Similar is the case with ‘Constants’ in Prgramming.
    Constants are classified as follows: 

    Classification of C Constants
    Let us now learn how to use these constants in programming.
    In-order to use these constants you’ll have to assign fixed values to all the variables you use. It’s just like you assign ‘45’ (a constant), for the variable ‘x’, of the function ‘sin’.

    1. #include<stdio.h>  
    2.  void main()   
    3.       {   
    4.          int x;  
    5.          float y;  
    6.          char z;  
    7.          double p;  
    8.          x=20;  
    9.          y=2e1;  
    10.          z='a';  
    11.          p=3.2e20;  
    12.          printf(\n" %d %10f %c %f", x,y,z,p);  
    13.       } 
    Explanation:
    So, here we have assigned a value of ‘20’ for the variable ‘x’ of ‘Integer’ function and displayed the same in the output screen.


    Part 2.8 » Variables

    As stated in the previous topic, the terms that we assign to the functions are called variables. They don’t have any fixed values. It differs on the type of input given, or the value that is predefined by the user.
    The rules for Defining variables is given below.
    RULES FOR DEFINING VARIABLES : pg31
    Let us understand this using two examples.
    User-defined Variables:
    Here, the user has assigned values to the variables in the Text editor itself.
    Inputted variables:
    Here, the user needs to input values to the variables, after compiling and running the program.


    Part 2.9 » C Data Types

    C Data Types are classified into three categories. These are:
    1) Derived Data Type
    2) Basic Data Type
    3) User-Defined Data Type
    Currently, we will focus only on Basic Data Type. The Derived and User-Defined Data Types are explained in the later chapters of this tutorial.
    The classification tree is given below:
    Classification of C Data Types
    The different types of Data types and their control strings are given below:
    Fig: 2.10 : Data Types and their control strings
    Data Types are declared in the following manner:
    Fig. 2.6 Value assignemment.
    Programs on ‘The C Declarations’.


    Group-1

    Group-1 Body
  • You might also like

Comment Section

Popular Posts

Leave a Message