Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Friday, January 6, 2017

What is C Programming Language

Before we can begin to write serious programs in C, it would be interesting to find out what really is C, how it came into existence and how does it compare with other programming languages. In this chapter, we would briefly outline these issues.
Four important aspects of any language are the way it stores data, the way it operates upon this data, how it accomplishes input and output and how it lets you control the sequence of execution of instructions in a program. We would discuss the first three of these building blocks here.

What is C Programming Language?
C is a programming language developed at AT and T's Bell laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. it wasn't made the 'official' Bell Labs language. Thus, without any advertisement, C's reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened.
Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than three decades has to be really good.
An opinion that is often heard today is --"C has been already superseded by languages like C++, C# and Java, so why bother to learn C today". I seriously beg to differ with this opinion. There are several reasons for this. These are as follows:

1. C++, C# or Java make use of a principle called Object Oriented Programming (OOP) to organize the program. This organizing principle has lots of advantages to offer. But even while using this organizing principle you would still need a good hold over the language elements of C and the basic programming skills. So it makes more sense to first learn C and then migrate to C++, C# and Java. Though this two-step learning process may take more time, but at the end of it you will definitely find it worth the trouble.

2. Major parts of popular operating systems like Windows, UNIX, and Linux are still written in C. This is because even today when it comes to performance (speed of execution) nothing beats C .Moreover, if one is to extend the operating system to work with new devices one needs to write device driver programs. These programs are exclusively written in C.

3. Mobile devices like smart phones and Tablets have become rage of today. Also, common consumer devices like microwave ovens, washing machines and digital cameras are getting smarter by the day. This smartness comes from a microprocessor, an operating system and a program embedded in these devices. These programs not only have to run fast but also have to work in limited amount of memory. No wonder that such programs are written in C. with these constraints on time a space, C is the language of choice while building such operating systems and programs.

4. you must have seem several professional 3D computer games where the user navigates some object, like say a spaceship and fires bullets at the invaders. The essence of all such games is speed. Needless to say, such games won't become popular if they take a long time to move the spaceship or to fire a bullet. To match the expectations of the player the game has to react fast to the user inputs. This is where C language scores over other languages. Many popular gaming frameworks (like DirectX) have been built using C language. 

5. At times one is required to very closely interact with the hardware devices. Since C provides several language elements that make this interaction feasible without compromising the performance, it is the preferred choice of the programmer.

I hope that these are very convincing reasons why you should adopt C as the first, and a very important step, in your quest for learning programming.

Getting started with C
Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is a close analogy between learning English language and learning C language. The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which, in turn, are combined to form sentences and sentences are combined to form paragraphs.
Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them, constants, variables and keywords are constructed and finally, how are these combined to form an instruction. A group of instructions would be combined later on to form a program. This is illustrated in the figure below:

similarities between learning English and C programming Language

The C character Set
A character denotes any alphabet, digit or special symbol used to represent information. Figure below shows the valid alphabets, numbers and special symbols allowed in C.

Character Sets
                                                            

Thursday, January 5, 2017

Fundamentals of C Programming Language



Fundamentals of C

Identifiers:
Identifier refers to the name of variables, function and array. These are user-defined names and consist of sequence of letters and digits with a letter as a first character. C is case sensitive (i.e. upper and lower case) letters are treated differently. Thus, the names rate, RATE and Rate denote different identifiers.

Keywords:
Keywords are the reserved words in programming language for doing specific task. These have predefined meaning and cannot be changed by the user.
There are 32 keywords in C language. Some are:
Int         for           else    
Float     do            void
Char      while      case
Double    if          default

Header files:
Header file ( also called as include file) contains declarations of data types, keywords, predefined identifiers, operators and the library functions. To include header file in the program, we have to use pre-processor directive
i.e.
#include
e.g.
#include<stdio.h>

It tells the compiler to include the additional source file. i.e. stdio.h (standard input output file) to the program. The header file must be included to the beginning of all programs that use input/output statements.

Data types:
Data can be of many types e.g. character, integer, real or string. Hence, data types specifies the size and type of data.
These are four fundamental/basic types in C, they are:

1. Integer data types:
Integers are whole numbers having no fractional parts such as 5, 8, -7. Integers are represented by int keyword. C supports three types of integer namely short int, int and long int in both signed and unsigned forms.

Types                    size (in bit)
Short int                   16
Int                            16
Long int                   32

2. Floating point data types:  A number having fractional part is a floating point numbers. Eg.3012 floating point number is represented by float keyword. Its size is 32 bits with 6 digits of precision (precision refers to the number of significant digits after the decimal point).

2. Double Data Types:
The double data type is also used for handling floating point numbers. But, it occupies twice as much memory as type float i.e. 64 bits with 14 digits of precision.
When the accuracy provided by a float number is not sufficient, the type double can be used. The keyword double is used for double precision floating point numbers.

4. Character Data Type:
A single character can be defined as a character type data which maximum size is 8 bits long. The char keyword is used to represent character data type.

Variable:
Variable is a memory location used to store data value. Every variable in C has a data types and it must be declared before it is used in a program. The value of variable may change during the execution of a program. There are certain rules regarding variable names which are listed below:
1. It should begin with letter not with digit
2. It can't be a keyword
3. Space not allowed
4. Uppercase and lower case are distinct.
5. It should not be of length more than 32 characters.

Declaration of Variable
The declaration of variables must be done before they are used in the program. The declaration statement defines the name and data type of variable.
The syntax for declaring a variable is:
Data_type variable_name1, variable_name2...  ;
e.g.
int a,b,c;
float d,e;
Note: variables of the same data type are separated by commas. A declaration must end with a semicolon.
Constant - (literal)
Constant is also a memory location used to store data values. Unlike variable, its value does not change during program execution.
Types:
1. Integer constant:
It is a whole number that has no decimal part e.g. 1, 50.
2. Floating point or Real constant:
It is a number having fractional part e.g. 12.50,50.100 etc.
3. Character constant:
A character enclosed within single quotes is known as character constant. E.g. a, A, b.
4. String Constant/ Literal:
A string literal is a sequence of characters enclosed within double quotes. E.g. "RAM", "C is the best".

Operator:
An operator is a symbol that tells the computer to perform certain mathematical or logical operation.
Unary operator requires single variable(operand) to calculate final answer whereas Binary Operator requires two variable (operand) to calculate a final answer.
C operators can be classified into a number of categories. They include:

1. Arithmetic Operator:
The operators used for arithmetic operation (i.e. addition, subtraction, multiplication) are called arithmetic operator. A list of arithmetic operators and their meaning are given below.
Operator                                             Meaning
+                                                             Addition
-                                                              subtraction
*                                                             multiplication
/                                                              division
%                                                            module division (remainder of an integer division)
Note: The module operator (%) produces the remainder of dividing the first by the second operand. But, both the operands must be integer data types. It can't be used with float or double data types.
e.g. if a & b are integers and a= 14 and b= 4 then
a%b = 2 (reminder of an integer division)

2. Relational operators:
The relational operators are used to compare the value between the two operands(variable) and return the appropriate result. If the comparison is true, it returns 1 and if the comparison is false, it returns false or 0.
Hence, depending on their relation, it helps to take certain decision after comparison.
Operator                                                             Meaning
>                                                                             greater than
<                                                                             less than
>=                                                                           greater than or equal to
<=                                                                           less than or equal to
==                                                                           equal to
!=                                                                            not equal to
e.g.
the expression 3>4 results false whereas the expression 5<6 results true.

        3.  Logical Operators:
Logical operators combine two or more relational expressions. C has the following three logical operators:
Operators                                           Meaning
&&                                                   logical AND
||                                                        logical OR
!                                                         ogical NOT
NOTE: The logical AND operator (&&) gives net result TRUE only if both the conditions have value TRUE otherwise give result FALSE.
The OR operator (||) gives net result TRUE even if one of the condition has value TRUE. But, if both the conditions have false value, it result FALSE.
The logical NOT operator (!) takes single expression and results to TRUE if the expression is false and results to FALSE if the expression is true. In other words, it just reverses the value of the expression.

4. Assignment Operator:
It is used to assign a value or result of an expression to a variable. The symbol "=" is used as assignment operator. There are arithmetic assignment operators which combine an arithmetic operator and an assignment operator they are:
+=, -=, /=, *=, %=
e.g.
a+=b; is equal to a=a+b
a-=b; is equal to a=a-b                                            
a*=b; is equal to a=a*b
a/=b; is equal to a=a/b
a%=b; is equal to a=a%b

            5. Increment & Decrement Operator:
Increment and decrement operator are unary operator because they operate on one operand(variable). Increment operator (++) adds 1 the operand while decrement operator (--) subtracts 1 from its operand. These operators can be used in two ways: postfix and prefix forms(or operators).

e.g. a++                      increment operator as a postfix operator
++aa                             increment operator as a prefix operator
a--                                 decrement operator as a postfix operator
--a                                 decrement operator as a prefix operator

           6. Conditional Operators:
Conditional operator is only the ternary operator i.e. it requires three expression as operands. The general form of conditional operator (?:)is:
Test expression? Expression1: expression2
If test expression is true, then the value of whole expression is the value of expression 2 otherwise the value of whole expression is the value of expression 3 e.g.
A=10;
B=15;
X=(a>b)?a:b;
In this case, x will be assigned the value of b i.e. 15.

C Statements:
Statements represent a set of declaration or step in sequence of actions. It is the instruction given to the computer to perform any kind of action. All the statement end with semicolon.
e.g.
int a,b,c;
c=a+b;
The value above statement causes the computer to carry out addition of the variable a and b and assign the result to c.