C Tokens, Keywords, Identifiers, Constants, Variables, Data Types
In this tutorial, you will learn
What is a Character set?
Token
Keywords and Identifiers
What is a Variable?
Integer data type
Floating point data type
Constants
What is a Character set?
Like
every other language 'C' also has its own character set. A program is a
set of instructions that when executed, generate an output. The data
that is processed by a program consists of various characters and
symbols. The output generated is also a combination of characters and
symbols.
A character set in 'C' is divided into,
Letters
Numbers
Special characters
White spaces (blank spaces)
A
compiler always ignores the use ofcharacters, but it is widely used for
formatting the data. Following is the character set in 'C' programming:
Letters
Uppercase characters (A-Z)
Lowercase characters (a-z)
Numbers
All the digits from 0 to 9
White spaces
Blank space
New line
Carriage return
Horizontal tab
Special characters
Special characters in 'C' are shown in the given table,
, (comma)
{ (opening curly bracket)
. (period)
} (closing curly bracket)
; (semi-colon)
[ (left bracket)
: (colon)
] (right bracket)
? (question mark)
( (opening left parenthesis)
' (apostrophe)
) (closing right parenthesis)
" (double quotation mark)
& (ampersand)
! (exclamation mark)
^ (caret)
|(vertical bar)
+ (addition)
/ (forward slash)
- (subtraction)
\ (backward slash)
* (multiplication)
~ (tilde)
/ (division)
_ (underscore)
> (greater than or closing angle bracket)
$ (dollar sign)
< (less than or opening angle bracket)
% (percentage sign)
# (hash sign)
Token
A token is the smallest unit in a 'C' program. A token is divided into six different types as follows,
Tokens in C
Keywords and Identifiers
In 'C' every word can be either a keyword or an identifier.
Keywords
have fixed meanings, and the meaning cannot be changed. They act as a
building block of a 'C' program. There are total 32 keywords in 'C'.
Keywords are written in lowercase letters.
Following table represents the keywords in 'C',
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
short
float
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
An
identifier is nothing but a name assigned to an element in a program.
Example, name of a variable, function, etc. Identifiers are the
user-defined names consisting of 'C' standard character set. As the name
says, identifiers are used to identify a particular element in a
program. Each identifier must have a unique name. Following rules must
be followed for identifiers:
The first character must always be an alphabet or an underscore.
It should be formed using only letters, numbers, or underscore.
A keyword cannot be used as an identifier.
It should not contain any whitespace character.
The name must be meaningful.
What is a Variable?
A
variable is an identifier which is used to store some value. Constants
can never change at the time of execution. Variables can change during
the execution of a program and update the value stored inside it.
A
single variable can be used at multiple locations in a program. A
variable name must be meaningful. It should represent the purpose of the
variable.
Example: Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person
A
variable must be declared first before it is used somewhere inside the
program. A variable name is formed using characters, digits and an
underscore.
Following are the rules that must be followed while creating a variable:
A variable name should consist of only characters, digits and an underscore.
A variable name should not begin with a number.
A variable name should not consist of whitespace.
A variable name should not consist of a keyword.
'C' is a case sensitive language that means a variable named 'age' and 'AGE' are different.
Following are the examples of valid variable names in a 'C' program:
height or HEIGHT
_height
_height1
My_name
Following are the examples of invalid variable names in a 'C' program:
1height
Hei$ght
My name
For example, we declare an integer variable my_variable and assign it the value 48:
int my_variable;
my_variable = 48;
By the way, we can both declare and initialize (assign an initial value) a variable in a single statement:
int my_variable = 48;
Data types
'C' provides various data types to make it
easy for a programmer to select a suitable data type as per the
requirements of an application. Following are the three data types:
Primitive data types
Derived data types
User-defined data types
There are five primary fundamental data types,
int for integer data
char for character data
float for floating point numbers
double for double precision floating point numbers
void
Array,
functions, pointers, structures are derived data types. 'C' language
provides more extended versions of the above mentioned primary data
types. Each data type differs from one another in size and range.
Following table displays the size and range of each data type.
Data type
Size in bytes
Range
Char or signed char
1
-128 to 127
Unsigned char
1
0 to 255
int or signed int
2
-32768 to 32767
Unsigned int
2
0 to 65535
Short int or Unsigned short int
2
0 to 255
Signed short int
2
-128 to 127
Long int or Signed long int
4
-2147483648 to 2147483647
Unsigned long int
4
0 to 4294967295
float
4
3.4E-38 to 3.4E+38
double
8
1.7E-308 to 1.7E+308
Long double
10
3.4E-4932 to 1.1E+4932
Note: In C, there is no Boolean data type.
Integer data type
Integer
is nothing but a whole number. The range for an integer data type
varies from machine to machine. The standard range for an integer data
type is -32768 to 32767.
An integer typically is of 2 bytes which
means it consumes a total of 16 bits in memory. A single integer value
takes 2 bytes of memory. An integer data type is further divided into
other data types such as short int, int, and long int.
Each data
type differs in range even though it belongs to the integer data type
family. The size may not change for each data type of integer family.
The
short int is mostly used for storing small numbers, int is used for
storing averagely sized integer values, and long int is used for storing
large integer values.
Whenever we want to use an integer data type, we have place int before the identifier such as,
int age;
Here, age is a variable of an integer data type which can be used to store integer values.
Floating point data type
Like
integers, in 'C' program we can also make use of floating point data
types. The 'float' keyword is used to represent the floating point data
type. It can hold a floating point value which means a number is having a
fraction and a decimal part. A floating point value is a real number
that contains a decimal point. Integer data type doesn't store the
decimal part hence we can use floats to store decimal part of a value.
Generally,
a float can hold up to 6 precision values. If the float is not
sufficient, then we can make use of other data types that can hold large
floating point values. The data type double and long double are used to
store real numbers with precision up to 14 and 80 bits respectively.
While
using a floating point number a keyword float/double/long double must
be placed before an identifier. The valid examples are,
float division;
double BankBalance;
Character data type
Character data types are used to store a single character value enclosed in single quotes.
A character data type takes up-to 1 byte of memory space.
Example,
Char letter;
Void data type
A void data type doesn't contain or return any value. It is mostly used for defining functions in 'C'.
Example,
void displayData()
Type declaration of a variable
int main() {
int x, y;
float salary = 13.48;
char letter = 'K';
x = 25;
y = 34;
int z = x+y;
printf("%d \n", z);
printf("%f \n", salary);
printf("%c \n", letter);
return 0;}
Output:
59
13.480000
K
We can declare multiple variables with the same data type on a
single line by separating them with a comma. Also, notice the use of
format specifiers in printf output function float (%f) and char (%c) and int (%d).
Constants
Constants
are the fixed values that never change during the execution of a
program. Following are the various types of constants:
Integer constants
An
integer constant is nothing but a value consisting of digits or
numbers. These values never change during the execution of a program.
Integer constants can be octal, decimal and hexadecimal.
Decimal constant contains digits from 0-9 such as,
Example, 111, 1234
Above are the valid decimal constants.
Octal constant contains digits from 0-7, and these types of constants are always preceded by 0.
Example, 012, 065
Above are the valid decimal constants.
Hexadecimal constant contains a digit from 0-9 as well as characters from A-F. Hexadecimal constants are always preceded by 0X.
Example, 0X2, 0Xbcd
Above are the valid hexadecimal constants.
The octal and hexadecimal integer constants are very rarely used in programming with 'C'.
Character constants
A
character constant contains only a single character enclosed within a
single quote (''). We can also represent character constant by providing
ASCII value of it.
Example, 'A', '9'
Above are the examples of valid character constants.
String constants
A string constant contains a sequence of characters enclosed within double quotes ("").
Example, "Hello", "Programming"
These are the examples of valid string constants.
Real Constants
Like
integer constants that always contains an integer value. 'C' also
provides real constants that contain a decimal point or a fraction
value. The real constants are also called as floating point constants.
The real constant contains a decimal point and a fractional value.
Example, 202.15, 300.00
These are the valid real constants in 'C'.
A real constant can also be written as,
Mantissa e Exponent
For example, to declare a value that does not change like the
classic circle constant PI, there are two ways to declare this constant
By using the const keyword in a variable declaration which will reserve a storage memory
#include <stdio.h>
int main() {
const double PI = 3.14;
printf("%f", PI);
//PI++; // This will generate an error as constants cannot be changed
return 0;}
By using the #define pre-processor directive
which doesn't use memory for storage and without putting a semicolon
character at the end of that statement
#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;}
Summary
A constant is a value that doesn't change throughout the execution of a program.
A token is the smallest unit in a program.
A keyword is reserved words by language.
There are total 32 keywords.
An identifier is used to identify elements of a program.
A variable is an identifier which is used to store a value.
There are four commonly used data types such as int, float, char and a void.
Each data type differs in size and range from one another.
A
comment is an explanation or description of the source code of the
program. It helps a developer explain logic of the code and improves
program readability. At run-time, a comment is ignored by the compiler.
There are two types of comments in C:
1)
A comment that starts with a slash asterisk /* and finishes with an
asterisk slash */ and you can place it anywhere in your code, on the
same line or several lines.
2) Single-line Comments which uses a double slash // dedicated to comment single lines
Example Single Line Comment
// single line comment example
Here is an example of comments type
// C program to demo
// Single Line comment
#include <stdio.h>
int main(void)
{
// This is a single line comment
printf("Guru99");
return 0; // return zero
}
Example Multi Line Comment
/* Sample Multiline Comment
Line 1
Line 2
….
…
*/
Example Multi Line Comment
#include <stdio.h>
int main() {
/* in main function
I can write my principal code
And this in several comments line */
int x = 42; //x is a integer variable
printf("%d", x);
return 0;}
Why do you need comments?
A good programmer
who writes codes understood by a human is better than a programmer who
generates codes understood only by the machine.
So, it is highly
recommended to insert comments to your code because it is good
programming practice. Comments do not affect a program because the
compiler ignores them.
Comments help the developer understand the logic/algorithm of the code if he revisits it after a long time.
#include<stdio.h> //Pre-processor directive
void main() //main function declaration
{
printf("Hello World"); //to output the string on a display
getch (); //terminating function
}
Here is the code explanation:
Pre-processor directive
#include is a pre-processor directive in 'C.' #include <stdio.h>, stdio is the library where the function printf is defined. printfis
used for generating output. Before using this function, we have to
first include the required file, also known as a header file (.h).
You
can also create your own functions, group them in header files and
declare them at the top of the program to use them. To include a file in
a program, use pre-processor directive
#include <file-name>.h
File-name
is the name of a file in which the functions are stored. Pre-processor
directives are always placed at the beginning of the program.
The main function
The main function is a part of every 'C' program. We can represent the main function in various forms, such as:
main()
int main()
void main()
main(void)
void main(void)
int main(void)
The
empty parentheses indicate that this function does not take any
argument, value or a parameter. You can also represent this explicitly
by placing the keyword void inside the parentheses. The keyword void
means the function does not return any value, in this case, the last
statement is always getch ().
#include<stdio.h> //Pre-processor directive
int main() //main function declaration
{
printf("Hello World"); //to output the string on a display
return 0; //terminating function
}
In the above example, the keyword int means the function will
return an integer value. In this case, the last statement should always
return 0.
The source code
After the main function has been declared, we have to specify the opening and closing parentheses. Curly brackets { },
indicate the starting and end of a program. These brackets must be
always put after the main function. All the program code is written
inside these brackets, such as declarative and executable part.
The printf function generates the output by passing the text "Hello World!"
The semicolon ; determines the end of the statement. In C, each statement must end with a semicolon.
So
we have successfully installed the compiler and now can begin working
in 'C.' We will write a simple program that will say hello to us. Let's
start.
How to run C Program
Step 1) Create a new Project
Step 2) In the pop-up,
Select File
Choose the "C/C++ Source"
Click "Go."
Step 3) Continue, by clicking on "Next."
Step 4) To create the new file ,select a "C" file then click on "Next" button to continue.
Step 5) Set the file path by clicking the "..." button, the explorer window permits to create the C file.
Step 6) Select the path of your new C File then its name which has .c extension and save it.
Step 7) Finally, to confirm the C file creation click "Finish."
Step 8) Enter the code, save it and compile it by clicking on the "Build & Run "button.
Here is the result:
Hello, World!
Summary
The main function is a mandatory part of every 'C' program.
To use the functionality of a header file, we have to include the file at the beginning of our program.
How to Download & Install GCC Compiler for C in Windows
In this tutorial, we will learn to install C in Windows, Mac, and Linux.
Install C on Windows
We will use an open-source Integrated Development environment named Code::Blocks which bundles a compiler (named gcc offered by Free Software Foundation GNU), editor and debugger in a neat package. Step 1) Go to http://www.codeblocks.org/downloads and click Binary Release.
Step 2) Choose
the installer with GCC Compiler, e.g., codeblocks-17.12mingw-setup.exe
which includes MinGW's GNU GCC compiler and GNU GDB debugger with
Code::Blocks source files.
Step 3) Run thedownloaded installer and accept the default options.
Step 4) Accept the Agreement
Step 5) Keep the component selection default and click Next.
Step 6) You may change the installation folder and click Next.
Step 7) To launch Code::Blocks double click on the icon.
Step 8) It will detect the gcc compiler automatically, set it as default.
Associate C/C++ files with code::blocks Step 9) You will see the IDE Home screen.
Conclusion:
'C'
program can be written and executed on any machine that has a suitable
environment to run the program. Its recommended using an IDE to run C
programs. An IDE includes a compiler, editor and debugger. Clanfg, MinGW
compiler (Minimalist GNU for Windows), Portable 'C' compiler, Turbo C
are popular compilers available.
What is C Programming Language? Basics, Introduction and History
What is C programming?
C
is a general-purpose programming language that is extremely popular,
simple and flexible. It is machine-independent, structured programming
language which is used extensively in various applications.
C
was the basics language to write everything from operating systems
(Windows and many others) to complex programs like the Oracle database,
Git, Python interpreter and more.
It is said that 'C' is a god's
programming language. One can say, C is a base for the programming. If
you know 'C,' you can easily grasp the knowledge of the other
programming languages that uses the concept of 'C'
It
is essential to have a background in computer memory mechanisms because
it is an important aspect when dealing with the C programming language.
The base or father of programming
languages is 'ALGOL.' It was first introduced in 1960. 'ALGOL' was used
on a large basis in European countries. 'ALGOL' introduced the concept
of structured programming to the developer community. In 1967, a new
computer programming language was announced called as 'BCPL' which
stands for Basic Combined Programming Language. BCPL was designed and
developed by Martin Richards, especially for writing system software.
This was the era of programming languages. Just after three years, in
1970 a new programming language called 'B' was introduced by Ken
Thompson that contained multiple features of 'BCPL.' This programming
language was created using UNIX operating system at AT&T and Bell
Laboratories. Both the 'BCPL' and 'B' were system programming languages.
In
1972, a great computer scientist Dennis Ritchie created a new
programming language called 'C' at the Bell Laboratories. It was created
from 'ALGOL', 'BCPL' and 'B' programming languages. 'C' programming
language contains all the features of these languages and many more
additional concepts that make it unique from other languages.
'C'
is a powerful programming language which is strongly associated with
the UNIX operating system. Even most of the UNIX operating system is
coded in 'C'. Initially 'C' programming was limited to the UNIX
operating system, but as it started spreading around the world, it
became commercial, and many compilers were released for cross-platform
systems. Today 'C' runs under a variety of operating systems and
hardware platforms. As it started evolving many different versions of
the language were released. At times it became difficult for the
developers to keep up with the latest version as the systems were
running under the older versions. To assure that 'C' language will
remain standard, American National Standards Institute (ANSI) defined a
commercial standard for 'C' language in 1989. Later, it was approved by
the International Standards Organization (ISO) in 1990. 'C' programming
language is also called as 'ANSI C'.
History of C
Languages
such as C++/Java are developed from 'C'. These languages are widely
used in various technologies. Thus, 'C' forms a base for many other
languages that are currently in use.
Where is C used? Key Applications
'C' language is widely used in embedded systems.
It is used for developing system applications.
It is widely used for developing desktop applications.
Most of the applications by Adobe are developed using 'C' programming language.
It is used for developing browsers and their extensions. Google's Chromium is built using 'C' programming language.
It is used to develop databases. MySQL is the most popular database software which is built using 'C'.
It
is used in developing an operating system. Operating systems such as
Apple's OS X, Microsoft's Windows, and Symbian are developed using 'C'
language. It is used for developing desktop as well as mobile phone's
operating system.
It is used for compiler production.
It is widely used in IOT applications.
Why learn 'C'?
As
we studied earlier, 'C' is a base language for many programming
languages. So, learning 'C' as the main language will play an important
role while studying other programming languages. It shares the same
concepts such as data types, operators, control statements and many
more. 'C' can be used widely in various applications. It is a simple
language and provides faster execution. There are many jobs available
for a 'C' developer in the current market.
'C'
is a structured programming language in which program is divided into
various modules. Each module can be written separately and together it
forms a single 'C' program. This structure makes it easy for testing,
maintaining and debugging processes.
'C' contains 32 keywords, various data types and a set of powerful built-in functions that make programming very efficient.
Another
feature of 'C' programming is that it can extend itself. A 'C' program
contains various functions which are part of a library. We can add our
features and functions to the library. We can access and use these
functions anytime we want in our program. This feature makes it simple
while working with complex programming.
Various compilers are available in the market that can be used for executing programs written in this language.
It
is a highly portable language which means programs written in 'C'
language can run on other machines. This feature is essential if we wish
to use or execute the code on another computer.
How 'C' Works?
C
is a compiled language. A compiler is a special tool that compiles the
program and converts it into the object file which is machine readable.
After the compilation process, the linker will combine different object
files and creates a single executable file to run the program. The
following diagram shows the execution of a 'C' program
Nowadays,
various compilers are available online, and you can use any of those
compilers. The functionality will never differ and most of the compilers
will provide the features required to execute both 'C' and 'C++'
programs.
Following is the list of popular compilers available online:
Clang compiler
MinGW compiler (Minimalist GNU for Windows)
Portable 'C' compiler
Turbo C
Summary
'C' was developed by Dennis Ritchie in 1972.
It is a robust language.
It is a low programming level language close to machine language
It is widely used in the software development field.
It is a procedure and structure oriented language.
It has the full support of various operating systems and hardware platforms.
Many compilers are available for executing programs written in 'C'.
A compiler compiles the source file and generates an object file.
A linker links all the object files together and creates one executable file.