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 the U.S.A.
Dennis Ritchie is known as
the founder of the C language.
It was developed to overcome the
problems of previous languages such as B, BCPL, etc.
Features of C Language

C is the widely used language. It
provides many features that are given below.
- Simple
- Machine Independent or Portable
- Mid-level programming language
- structured programming language
- Rich Library
- Memory Management
- Fast Speed
- Pointers
- Recursion
- Extensible
1) Simple
C
is a simple language in the sense that it provides a structured
approach (to break the problem into parts), the rich set of
library functions, data types, etc.
2) Machine Independent or Portable
Unlike
assembly language, c programs can be executed on different machines with
some machine specific changes. Therefore, C is a machine independent language.
3) Mid-level programming language
Although,
C is intended to do low-level programming. It is used to develop
system applications such as kernel, driver, etc. It also supports the
features of a high-level language. That is why it is known as mid-level
language.
4) Structured programming language
C
is a structured programming language in the sense that we can break the
program into parts using functions. So, it is easy to understand and
modify. Functions also provide code reusability.
5) Rich Library
C provides
a lot of inbuilt functions that make the development fast.
6) Memory Management
It
supports the feature of dynamic memory allocation. In C language,
we can free the allocated memory at any time by calling the free() function.
7) Speed
The
compilation and execution time of C language is fast since there are lesser
inbuilt functions and hence the lesser overhead.
8) Pointer
C
provides the feature of pointers. We can directly interact with the memory by
using the pointers. We can use pointers for memory, structures,
functions, array, etc.
9) Recursion
In
C, we can call the function within the function. It provides code
reusability for every function. Recursion enables us to use the approach of
backtracking.
10) Extensible
C
language is extensible because it can easily adopt new features.
Tokens in C
Tokens are the smallest individual
unit in C. For `example, we cannot create a sentence without using words;
similarly, we cannot create a program in C without using tokens in C. tokens in
C is the building block or the basic component for creating a program in C
language
.Classification of tokens in C
·
Keywords
·
Identifiers
·
Constants
·
Strings
·
Special
Symbols
·
Operators
Keywords
A keyword can
be defined as the pre-defined or the reserved words. It cannot be used it as a variable name, constant name,
etc. There are only 32 reserved words (keywords) in the C language.
A list of 32 keywords in the c language
is 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 |
Identifiers in C
Identifiers
are used for naming variables, functions, arrays, structures, etc. Identifiers
in C are the user-defined words.
Rules
for constructing identifiers in C are given below:
Ø The
first character should be either an alphabet or an underscore
Ø It
should not begin with any numerical digit.
Ø In
identifiers, both uppercase and lowercase letters are distinct. Therefore, we
can say that identifiers are case
sensitive.
Ø No
special characters are allowed except underscore.
Ø Keywords
cannot be represented as an identifier.
Ø The
length of the identifiers should not be more than 31 characters.
Constants
A constant is a fixed
value or variable that can't be changed in the program, for example: 10, 20,
'a', 3.4, "c programming" etc.
There are different
types of constants in C programming.
|
Constant |
Example |
|
Decimal Constant |
10, 20, 450 etc. |
|
Real or Floating-point Constant |
10.3, 20.2, 450.6 etc. |
|
Octal Constant |
021, 033, 046 etc. |
|
Hexadecimal Constant |
0x2a, 0x7b, 0xaa etc. |
|
Character Constant |
'a', 'b', 'x' etc. |
|
String Constant |
"c", "c program", "c in
javatpoint" etc. |
Two ways to define constant in C
There are two ways to
define constant in C programming.
- const keyword
- #define preprocessor
1) C const keyword
The const keyword is
used to define constant in C programming.
Ø
const float PI=3.14;
Now, the value of PI variable can't be
changed.
#include<stdio.h>
int main()
{
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
The value of PI is: 3.140000
#include<stdio.h>
int main()
{
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
Compile Time Error: Cannot modify a const object
2) C #define preprocessor ( Symbolic
Constant )
Symbolic constant is name that substitute for a sequence of
character that cannot be changed. The character may represent a numeric
constant, a character constant, or a string. When the program is compiled, each
occurrence of a symbolic constant is replaced by its corresponding character
sequence. They are usually defined at the beginning of the program.
The #define
preprocessor is also used to define constant.
#include<stdio.h>
#define
a=10;
#define
pi=3.14;
int main()
{
pi=5.5;
printf("The value of PI is: %f",PI);
return 0;
}
Strings in C
-are always represented as an array
of characters having null character '\0' at the end of the string. This null
character denotes the end of the string. Strings in C are enclosed within
double quotes, while characters are enclosed within single characters. The size
of a string is a number of characters that the string contains.
Now,
we describe the strings in different ways:
char a[10] = "javatpoint"; // The
compiler allocates the 10 bytes to the 'a' array.
har a[] = "javatpoint";
// The compiler allocates the memory at the run time.
char a[10] =
{'j','a','v','a','t','p','o','i','n','t','\0'}; // String is represented in the
form of characters.
Operators in C
-is a special symbol used to perform
the functions. The data items on which the operators are applied are known as
operands. Operators are applied between the operands. Depending on the number
of operands, operators are classified as follows:
Unary Operator
A unary operator is an operator
applied to the single operand. For example: Positive sign (+), negative sign
(-), increment operator (++), decrement operator (--), sizeof, (type)*.
Binary Operator
`The binary operator is an operator
applied between two operands. The following is the list of the binary operators:
- Arithmetic Operators
- Relational Operators
- Shift Operators
- Logical Operators
- Bitwise Operators
- Conditional Operators
- Assignment Operator
- Misc Operator
Special characters in C
Some special
characters are used in C, and they have a special meaning which cannot be used
for another purpose.
- Square brackets [ ]: The
opening and closing brackets represent the single and multidimensional
subscripts.
- Simple brackets ( ): It is
used in function declaration and function calling. For example, printf()
is a pre-defined function.
- Curly braces { }: It is
used in the opening and closing of the code. It is used in the opening and
closing of the loops.
- Comma (,): It is
used for separating for more than one statement and for example,
separating function parameters in a function call, separating the variable
when printing the value of more than one variable using a single printf
statement.
- Hash/pre-processor (#): It is
used for pre-processor directive. It basically denotes that we are using
the header file.
- Asterisk (*): This
symbol is used to represent pointers and also used as an operator for
multiplication.
- Tilde (~): It is
used as a destructor to free memory.
- Period (.): It is
used to access a member of a structure or a union.
Variables in C
A variable is
a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times. It is a way to represent memory
location through symbol so that it can be easily identified.
Syntax
datatype variable_list;
Eg:
int a;
float b;
char c;
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
Here, a, b, c are variables. The int, float,
char are the data types.
Assigning values to the variable
Values can be assigned to variables
using the assignment operator = as follows.
Syntax
Variable_name=constant;
Eg:
ini_valu=0;
balance=75.64;
Yes=’x’;
Rules for defining variables
- A variable can have alphabets, digits, and
underscore.
- A variable name can start with the alphabet, and
underscore only. It can't start with a digit.
- No whitespace is allowed within the variable
name.
- A variable name must not be any reserved word or
keyword, e.g. int, float, etc.
Valid variable names:
int a;
int _ab;
int a30;
Invalid variable names:
int 2;
int a b;
int long;
Comments
Post a Comment