What is C?
- It is a programming language developed at AT & T’s Bell Laboratories of USA in 1972.
- It is designed and written by Dennis Ritchie.
- C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C.
- The evolution of C took place in the following way:
What is C?
- It is a programming language developed at AT & T’s Bell Laboratories of USA in 1972.
- It is designed and written by Dennis Ritchie.
- C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C.
- The evolution of C took place in the following way:
Why should we use C?
1.C language is a building block for many other currently known languages.
2.There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs.
3.C language is a structured programming language. This makes user to think of a problem in terms of function modules or blocks. Collection of these modules makes a complete program. This modular structure makes program debugging, testing and maintenance easier.
What is ANSI C?
1.ANSI C, ISO C and Standard C refer to the successive standards for the C programming language published by the American National Standards Institute (ANSI) and the International Organization for Standardization (ISO).
2.The first standard for C was published by ANSI. Although this document was subsequently adopted by International Organization for Standardization (ISO) and subsequent revisions published by ISO have been adopted by ANSI, the name ANSI C (rather than ISO C) is still more widely used.
Characters used in C:
•Alphabets: A,B,C….,Y,Z
•Digits: 0,1,…9
•Special Symbols: ~ ‘! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; “ ‘ < > , . ? / $
•Alphabets: A,B,C….,Y,Z
•Digits: 0,1,…9
•Special Symbols: ~ ‘! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; “ ‘ < > , . ? / $
Constants in C:
• Constant is an entity that does not change its value and hold a fixed value defined by user.
Types of Constants in C:
A)Primary Constants
i)Integer Constants:
•An Integer constant must have at least one digit.
•It should not contain a decimal point. No commas or blank space are allowed within an integer constant.
•It can be positive or negative. Default sign is positive.
•Range is -32768 to 32767.
ii)Real Constants/Floating point constants:
It is written in two forms i.e. Fractional form and Exponential form.
Fractional form:
•It must have a decimal point. No commas or blank space are allowed within an integer constant.
•Can be either positive or negative. Default is positive.
Exponential form:
•In this form the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa whereas the part following ‘e’ is called exponent. So, 0.000342 can be written as 3.42e-4.The mantissa and exponent should be separated by the letter e or E.
•Mantissa and exponent part may be positive or negative. Default sign is positive.
•Range is -3.4e38 to 3.4e38.
iii)Character Constants:
•It can be a letter, digit or any single symbol enclosed within single inverted commas.
•Maximum length is one.
B)Secondary Constants
Will be discussed in brief in later chapters.
Variables in C:
1.An entity that may vary during program execution is termed as Variable.
2.A variable name is a combination of 1 to 31 alphabets.
3.First character of a variable should be an underscore or alphabet.
4.No commas or special symbol except underscore are not allowed for a variable name.
5.Eg:si_int,m_hra,pope_89
C Keywords or Reserved words:
1.Keywords are those whose meanings has already been explained to the C compiler (or in a broad sense to the computer).
2.The keywords cannot be used to assign variable names because doing so we are assign a new function to the predefined words which is not acceptable.
3.There are 32 keywords in C given below:
First C Program:
Some instructions before writing a C program:
1. Each instructions in C starts with a different instructions.
2. Every C statements must end with ; .
3. All statements must be in small letter.
Program 1:
/* Calculate Simple Interest */
#include<stdio.h>
int main()
{
int principal,time;
float si,rate;
principal=1000;
time=3;
rate=8.5;
si=principal*rate*time/100;
printf("%f\n",si);
return 0;
}
Output:
Explanation of Program 1:
1.Comments should be enclosed in /* */.
2.main ( ) is nothing but a function. A function consists of series of statements. A C program consists of multiple functions but to begin with there has to be some function and that is main. It is already defined by C developers. All statements inside main should be in braces.
3.Every functions returns a value. Similarly, main( ) returns an integer value so int main( ) is written. If the function does not return any value then we should use void but for main it always has to be int. Here we have returned 0 as a returning value.
4.A variable in C has to be declared first before using it.
Such as:
int principal, time;
float si,rate;
5.* and / are multiplication and division operators respectively. These are called Arithmetic Operators. C have +(addition),-(subtraction),*(multiplication),/(division) as Arithmetic operators.
6.C have 45 operators and will be discussed later.
7.printf( ) is used for displaying the output results.
8.In order to use printf ( ) properly we have to write #include<stdio.h> at the start. #include is a pre-processor directive and will be discussed later.
9.Syntax of printf is:
printf(“<format string>”,<list of variables>);
<format string> can contain:
%f for printing real values
%d for printing integer values
%c for printing character values
10.‘\n’ is used to move the cursor to the new line.
Compile and Run in C:
Compiler vendor provides IDE (Integrated Development Environment) which consists of an Editor as well as a Compiler.
Receiving user Input in C:
1.To make the program more general, the program should ask user to give input.
2.This function will be achieved with the help of function scanf( ).printf( ) is used to print the values on screen and scanf( ) is used to receive user input. It is shown below:
Program 2:
/* Calculate Simple Interest */
#include<stdio.h>
int main()
{
int principal,time;
float si,rate;
scanf("%d %d %f",&principal,&time,&rate);
si=principal*rate*time/100;
printf("%f\n",si);
return 0;
}
Output:
Explanation of Program 1:
1.Comments should be enclosed in /* */.
2.main ( ) is nothing but a function. A function consists of series of statements. A C program consists of multiple functions but to begin with there has to be some function and that is main. It is already defined by C developers. All statements inside main should be in braces.
3.Every functions returns a value. Similarly, main( ) returns an integer value so int main( ) is written. If the function does not return any value then we should use void but for main it always has to be int. Here we have returned 0 as a returning value.
4.A variable in C has to be declared first before using it.
Such as:
int principal, time;
float si,rate;
5.* and / are multiplication and division operators respectively. These are called Arithmetic Operators. C have +(addition),-(subtraction),*(multiplication),/(division) as Arithmetic operators.
6.C have 45 operators and will be discussed later.
7.printf( ) is used for displaying the output results.
8.In order to use printf ( ) properly we have to write #include<stdio.h> at the start. #include is a pre-processor directive and will be discussed later.
9.Syntax of printf is:
printf(“<format string>”,<list of variables>);
<format string> can contain:
%f for printing real values
%d for printing integer values
%c for printing character values
10.‘\n’ is used to move the cursor to the new line.
Compile and Run in C:
Receiving user Input in C:
1.To make the program more general, the program should ask user to give input.
2.This function will be achieved with the help of function scanf( ).printf( ) is used to print the values on screen and scanf( ) is used to receive user input. It is shown below:
Program 2:
/* Calculate Simple Interest */
#include<stdio.h>
int main()
{
int principal,time;
float si,rate;
scanf("%d %d %f",&principal,&time,&rate);
si=principal*rate*time/100;
printf("%f\n",si);
return 0;
}
Output:
Explanation of Program 2:
1.&(ampersand) before the variable in the scanf( ) is must.
2.& is an address of the operator. It gives the location number used by the variable in memory.
3.When we are using &a, we are telling scanf( ) at which memory location should it store the value supplied by the user.
![]() |
A memory block |
Here &a means we are storing data in 1028(suppose) and & is the address of variable a.
Modulus Operator(%)in C:
1.Modulus operator helps in providing remainder.
15/5 gives 3 whereas 15%5 gives 0
-3%2 gives -1
3%2 gives 1
**In modulus operator the sign of the remainder is always the sign of the numerator**
Implicit Conversion in C:
1.An arithmetic operation between two integers produce integer result.
2.An arithmetic operation between two real number yields a real output.
3.An arithmetic operation between a real and integer produces a real output. The integer gets promoted to real number implicitly and operation is performed. As, the real number range is higher than integer so conversion causing implicitly whereas vice versa has to be done explicitly.
4.In some cases, we may find the variable at the left hand side of the assignment operator(=) and right hand side is different.
Eg: int a=6.5
Here we have integer value on the left side of the assignment operator and float on the right side.
5.In such case the value on the right side of the assignment operator is promoted or demoted depending on the type of variable on the left side.
So, considering the above case integer a will store 6 instead of 6.5.
We can say 6.5 is demoted to 6.Whereas if we consider another example
Such as float a=6.Here the value of 6 is stored as 6.000000.So,here 6 is promoted to 6.000000.
Operators Hierarchy in C:
While executing an arithmetic operation we often gets confused as to which operators should function first and here comes the hierarchy of operation. Given below is a pic of hierarchy of operation.
**Always start performing operation with the innermost parenthesis**
Consider an example:
i=2*3/4+4/4+8-2+5/8 where i, is an integer
Without knowing hierarchy we will get confuse as to whose operation we should perform first but now it can be performed very easily following the table above.
Sol:i=6/4+4/4+8-2+5/8
/*first performed multiplication*/
=1+1+8-2+0
/*divison is perfomed.Another thing 6/4 is 1 and not 1.5 since I ias an integer and it will demote 1.5 to 1 */
=10-2
/*addition is perfomed*/
=8
/*subtraction*/
**Here one may ask why we performing multiplication first and then division since both have same priority. This is explained in the section below.**
Associativity of operators in C:
Associativity is not about what evaluates first, it is about how the expression is parsed. Here is given a list of operators and their associativity.
Associativity indicates in which order two operators of same precedence (priority) executes. Let us suppose an expression:
a= =b! =c
Here, operators = = and! = have same precedence.
The associativity of both = = and! = is left to right, i.e., the expression in left is executed first and execution take pale towards right.
Thus, a= =b! =c equivalent to :( a= =b)! =c.
So, considering previous example first precedence is given to multiplication, division. Since both enjoy same priority and same left to right associativity and the equation is parsed in the following way.
i=((2*3)/4)+(4/4)+8-2+(5/8)
*End of chapter 1*
C-Decision Making Next chapter >>
Comments
Post a Comment