History, Basic Structure, C Token, Data Type, operators, printf() and scanf()


The C Language is developed for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc.
C programming is considered as the base for other programming languages, that is why it is known as mother language.
It can be defined by the following ways:
  1. Mother language
  2. System programming language
  3. Procedure-oriented programming language
  4. Structured programming language
  5. Mid-level programming language
History of C Language:
  •   C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A.
  •   Dennis Ritchie is known as the founder of C language.
  •   It was developed to overcome the problems of previous languages such as B, BCPL etc.
  •   Initially, C language was developed to be used in UNIX operating system. It inherits many 
  •  features of previous languages such as B and BCPL 



General Structure of C Program



Documentation Section
Link Section
Definition Section
Global Declaration Section
Main() Function section
{

Declaration part    
Executable part

}
Subprogram Section
                
Function 1
Function 2


Function n



Documentation Section: The documentation section consists of set of comments. The documentation in the program can be given by comment lines. The documentation done by comment lines are not the program statement, hence it will be ignore by the compiler.
There two types of comment lines.
1.      Single line comment -             //
2.      Multiline comment-                 /*         */

Link Section: The link section provides an instruction to the compiler to link the library function with the program. Link section in program is given by header files.
Example:
 #include<stdio.h>
#include<conio.h>

Definition Section: The definition defines all the symbolic constants. Example value of PI (3.14)

Global Declaration Section: There are some variable which are used in more than one function, then those variables must be declare in global section.

main() function: Every C program must have main( ) function. It’s the compulsory function of C program. It is user defined function. This section contains two parts
1.      Declaration part: local variable declaration
2.      Executable part:  consists of program statements

Subprogram Section: the subprogram section contains all the user defined functions that are called in the main function.

Standard Header files:
There are many header files in c Language. The some of the basic header files which we normally used in the program are

<stdio.h>         standard Input/ output header file
<conio.h>        console input/output header file
<math.h>         common mathematical functions header file
<string.h>        string function header files (String handling function)
<stdlib.h>        General utilities: memory managementprogram utilitiesstring conversionsrandom numbers

Features of C Language:
  1.   C language is a portable language
  2.   C is a middle level language
  3.   C is a robust language
  4.   C has an ability to extend itself
  5.   C is well suited for structured programming. Hence easy to debug, maintain and test.
  6.   C language support the concept of memory management
  7.   Program written in C language is faster and efficient








C Token


The smallest individual unit of a C program is called C Tokens ie a small things needed to create a C program is called tokens.


Keywords:


  •   Keywords are the reserved words of c language.
  •   Every keyword has a specific meaning and is used for the specific purpose.
  •   Keywords cannot be used as a variable or function name.
  •   The keywords are defined in small letters.
  •   Total there are 32 keywords in C language.





Identifiers and variables:
Identifiers refer the name of variables, functions and array. They are user defined names consists of letters and digits.
Variable:
Variable is just a data name to store a data value.
The following are rules for defining the variables and identifiers
  • A variable can have alphabets, digits and underscore.
  • A variable name can start with alphabet and underscore only. It can't start with digit.
  • No white space is allowed within variable name.
  • A variable name must not be any reserved word or keyword e.g. int, float etc.
  • Only first 31 characters are significant.

Constants: Constants in C refers to fixed values that do not change during the execution of a program.



Interger constant: consists of 0-9 digits preceded by + or – sign.
                               Example: 12, -20, +38
Real Constant: constants which contain the fractional parts. Such are called real or floating numbers.
                               Example: 12.75, -17.5, +.5
Character constant: single character is called character constant. In C language character constant are represented in single quotations.
                               Example: ‘c’
String Constant: more than one character is considered as string. They are represented in double quotations.
                               Example: “shailesh”







Data Types:  C language is rich in its data types. Variety of data types allows the programmer to select the appropriate types as per the requirement of application.

ANSI C supports three classes of data types. They are

  • Primary or fundamental data types 
  •  Derived data types
  •  User-defined data types

Primary Data types:

C supports five fundamental data types. They are


  Integer (int)
 Character (char)
                  Floating point (float)
                  Double precision floating point (double)
                Generic type (void)
 
REFER DIAGRAM FROM ANSI C BOOK




In order to provide the control over the range of values and storage classes C supports three classes of integer namely int, short int, long int in both signed and usigned forms. The use of qualifer signed on integer is optional because the default declaration assumes a signed number. It also provide three classes of floating types namely float, double and long double.




 



Variable Declaration and Initilization:

Variable is a data name to store a data value. Variable can be used to stroe a value of any type. The syntax for declaring the variable is as follows.

data-type v1, v2, …….vn;
Where data type is the type of variable and v1, v2, ….. vn are the name of variable separated by commas and the declaration ends with semicolon.
int a;
int a, b,c;
float average, ratio;
double ratio;
Variable initialization can be done in two ways
1.      Compile time initialization
2.      Run time initialization

Compile time initialization means assigning the value to variable during writing of the program (assigning a value to a variable in the program using the assignment operator =)
int a=10;
int a = 10, b = 20, c = 30;
double ratio = 10.5;
                                                                        int number1, number2, sum;
                                                                        number1 = 10;
                                                                        number2 = 20;
                                                                        sum = number1+number2;
Run time initialization means assigning a value to a variable from the keyboard during the execution of the program. This is done by using the input function ie scanf( ).





C supports a rich set of operators. An operator is simply a symbol that tells the computer to perform certain mathematical and logical manipulations. Operators are used to manipulate data and variables.
There are different types of C operators. They are
  • Arithmetic Operators
There are five types of arithmetic operators. They are
Addition                     +          a + b
Substraction                -           a - b
Multiplication              *          a * b
Division                       /           a / b
Modulo                       %         a % b
All the arithmetic operators except modulo operators can be used with integer and real constants.
Modulo operators can be used with integer operands.

  • Relational Operators
The relational operators are
<          less than operator
>          greater than operator
<=        less than or equal to operator
>=        greater than or equal to operator
==        equal-equal to operator or comparison operator
!=         Not equal to operator

Relational operators are used within the condition for comparing the values.
Example: (a>b) , (a==b), (a>=b)

  • Logical Operators
There are three types of logical operators. They are
Logical AND              &&      it returns true when all condition are true and false when all or one is false
Logical OR                 | |          It returns true when all or any one condition is true else returns false          
Logical NOT               !           It is used for negation
Logical operators are used to combine more than one condition in the single statements.
            If (a>b) && (a>c)

  • Increment and decrement operators
Increment Operator     ++
Decrement operator    - -
Increment and decrement operators are used to increment or decrement the value of a variable. Default it increment or decrement by 1. Both are unary operator.
++m (prefix operators means it will increment the value first and then assign)
m++ (postfix operators means it will assign first and then increment the value)
same for – (decrement operator)
example:          m=10;                          m=10;
                        x=++m;                       x=m++;
                        x=11 and m=11           x=10 and m=11

  • Assignment Operator
Assignment operator               = (equal sign)
It is used assign a value to variable. Everything on the right side will be assigned to the left side variable by assignment operator.
Example: a=1;             a=a+1;

a=a+1; can also be written using shorthand operator ie a+=1;
           
  • Ternary or Conditional Operators
It is denoted by (? :)
It is known as ternary operator because it uses three operands. The syntax of ternary operator is
exp1    ?      exp2   :     exp3;
Here exp1 is a condition, if the exp1 is true then it will execute exp2 after the (?) Else execute exp3 after the (:)
a=10;
b=32;
X=(a>b) ?  a :  b;


  • Special operators
C supports some special operators such as comma operator, sizeof operator, pointer operators (& and *), and member selection operators ( . and ->).
  • Bitwise Operators:
C supports bitwise operators for the manipulation of the data at bit level. Bitwise operators are not applied to float and double.

Operators                               Meaning

&                     -                       bitwise AND
|                       -                       bitwise OR
^                      -                       bitwise exclusive OR
<<                    -                       shift left
>>                    -                       shift right
 

  












printf() and scanf() in C
The printf() is an output function and scanf() is an input functions used for output and input in C language. Both functions are library functions, which are already defined in the header file stdio.h (standard input output header file).

printf() function
The printf() function prints on output screen. Everything that is in double quotation in printf() is a message which will be printed on output screen by printf(). It also prints the output ie the value of the variable given in the argument list.

Syntax of printf() function:

printf("format string",argument_list); 

The format string can be %d (integer), %c (character), %s (string), %f (float) based on the argument list etc. Argument is the list of variable names separated by comma.

 

 

 

 

scanf() function


The scanf() function is an input function. It is used to read the value from the keyboard during the execution of program means it used to assign the value to a variable at runtime. The value assign to a variable at runtime is known as runtime initialization.
scanf("format string",argument_list);

Comments

Popular posts from this blog

Practical 1

Practical 2