History of c language
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons:
Easy to learn
Structured language
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms
The basic elements used to construct a simple C program are: the C character set, identifiers and keywords, data types, constants, arrays, declarations , expressions and statements.
Basic Structures of C-Programming
C language has a bundle of protocols that define its programming activities. The C programming language came into existence when its developers were working on the development of the Unix operating system. The features constituted the part of the C program upon which it was is built.
Part of c programs
1. # include <stdio.h> – This command is a preprocessor directive in C that includes all standard input-output files before compiling any C program so as to make use of all those functions in our C program.
2. int main() – This is the line from where the execution of the program starts. The main() function starts the execution of any C program.
3.{ (Opening bracket) – This indicates the beginning of any function in the program (Here it indicates the beginning of the main function).
4./* some comments */ – Whatever is inside /*——-*/ are not compiled and executed; they are only written for user understanding or for making the program interactive by inserting a comment line. These are known as multiline comments. Single line comments are represented with the help of 2 forward slashes “//——”.
5.printf(“Hello World”) –The printf() command is included in the C stdio.h library, which helps to display the message on the output screen.
6.getch() – This command helps to hold the screen.
7. return 0 –This command terminates the C program and returns a null value, that is, 0.
8.} (Closing brackets)- This indicates the end of the function. (Here it indicates the end of the main function)
9. ; (terminater)- This indicates is used to terminate the program.
simple example of c programs
With the help of this example, we can easily understand the basic structure of a C program.
#include<stdio.h>
int main()
{
// Our first basic program in C
printf ("Hello World!\n\n");
return 0;
}
Character Set
The C language has a rich character set. It can be described under 3 main headings-
1. Alphabets- These are alphabets of English language and can be in upper or lower case. (E.g. A,B,C…..Z || a,b,c,…z)
2. Digits- 0,1,2,…,9.
3. Special Symbols- The C language also allows the use of special symbols such as →(),[],{},~!@#%^&$*_-+=`\|;:’”
Constants
An entity that does not change or have a fixed value is a constant. For e.g. the digit 5 is a constant.
Types of Constants in C →
1. Primary Constants →
a)Integer Constants,
b)Real Constants,
c)Character Constants.
2. Secondary Constants →
a)Array,
b)Pointers,
c)Structures, etc.
Variables
A variable is an entity which can hold a particular type of constant and this value can change as per user’s wish.For e.g. x=10; here, x is a variable and 5 is a constant.
Any variable can hold only a particular type of constant. The type of constant decides the data type of the variable.
Keywords
Keywords are used to run a program and display the output, a programming language makes use of a compiler.There are certain words that have a predefined meaning to a compiler. Thus the use of these words(keywords or reserved words) as a variable name is not allowed, as it will lead to an ambiguous situation and resulting in an error.
In C language there are 32 keywords.They are given below-
auto | break | case | char | const | continue | default | do |
double | else | enum | extern | float | for | goto | if |
int | long | register | return | short | signed | sizeof | static |
struct | switch | typedef | union | unsigned | void | volatile | while |
Data type in c language
The data type in C defines the amount of storage allocated to variables ,the values that they can accept ,and the operation that can be performed on those variables.
C is rich in data types. The verity of data type allow the programmer to select appropriate data type to satisfy the need of application as well as the needs of different machine
There are three classes of Data-Type
Primary Data Type
Derived Data Type
User Defined Data Type
Primary Data Types(Fundamental Data Types)
All C compiler support five type of fundamental data type
1. Integer int 2,768 to 32,768
2. Character char -128 to 127
3. Floating Point float 3.4e-38 to 3.4e+38
4. Double Precision Floating Point double 1.7e-308 to 1.7e+308
5. Void Data Type void(used for function when no value is to be return)
User define Data type
By using a feature known as "type definition" that allows user to define an identifier that would represent a data type using an existing data type.
Derived Data Type
Those data types which are derived from fundamental data types are called derived data types.
There are basically three derived data types .
1. Array: A finit collection of data of same types or homogenous data type.
2. String: An array of character type.
3. Structure: A collection of related variables of the same or different data types.
Escape Sequences in C
In C programming language, there are 256 numbers of characters in character set. The entire character set is divided into 2 parts i.e. the ASCII characters set and the extended ASCII characters set. But apart from that, some other characters are also there which are not the part of any characters set, known as ESCAPE characters.
List of some Escape Sequences in C
Escape Sequence | Meaning |
---|---|
\a | Alarm or Beep or alert |
\t | Tab (Horizontal) |
\v | Vertical Tab |
\n | New Line |
\f | form feed |
\0 | null |
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Unary Operators
Arithmetic Operators
The Arithmetic operators are some of the C Programming Operator, which are used to perform arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division and Modulus. All these Arithmetic operators in C are binary operators which means they operate on two operands.
Arithmetic operands are listed below:
Here assume variable A holds 10 and variable B holds 20, then −
Operator | Description | Example |
---|---|---|
+ | Adds two operands. | A + B = 30 |
− | Subtracts second operand from the first. | A − B = -10 |
* | Multiplies both operands. | A * B = 200 |
/ | Divides numerator by de-numerator. | B / A = 2 |
% | Modulus Operator and remainder of after an integer division. | B % A = 0 |
++ | Increment operator increases the integer value by one. | A++ = 11 |
-- | Decrement operator decreases the integer value by one. | A-- = 9 |
Relational operator
A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. ... In languages such as C, relational operators return the integers 0 or 1, where 0 stands for false and any non-zero value stands for true.
Relational operands are listed below:
Here assume variable A holds 10 and variable B holds 20, then −
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |
Logical Operators
Those operators which are used to perform logical operations on the given expressions is known as logical operaters.
Logical operaters are listed below
Here assume variable A holds 1 and variable B holds 0, then −
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Assignment Operators
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
Assignment operands are listed below:
Operator | Description | Example |
---|---|---|
= | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
Unary Operators
The operators which operates on single operand (i.e. to perform an operation through these operators, we need only one operand) is known as unary operaters.
Unary operands are listed below:
SrNo | Operators | Symbols |
---|---|---|
1 | Unary plus | + |
2 | Unary minus | - |
3 | Increment operator | ++ |
4 | Decrement operato | -- |
5 | Address of Operator | & |
6 | Size of Operator | sizeof() |
7 | Dereferencing Operator | * |
8 | Logical NOT | ! |
9 | Bitwise NOT/ Bitwise Negation/ One's Compliment | ~ |
Tags:
c