Every language has some set of grammatical rules and basic elements. Before starting it is mandatory to know the primary elements of the language. These basic components of C language are variables, character sets, constants, keywords (reserved words), database, expressions, variable declaration, statements, etc. These are the basic components to construct a C program.
Table of Contents
Data in C
In life of a file execution,
- The data which does not change with time are constants. Eg. Name, Date of birth, Year of passing.
- The data which changes with time are variables. (Which are also called Identifiers, Means must be declared) Eg. Age, Contact no.
Character constants: ‘A’, ‘*’, ‘5’, ‘8’, …
Integer constant: 567, -1, 8, 0x123, 0125, …
Real constant: 5.23, -0.001, 5.6E+3, …
String constant: “Electronics1010”, “A”, …
basic components of c language
These are the character are allowed in C programs
Uppercase and Lowercase letters
A, B, C, D ……….. Z
a, b, c, d ……….. z
Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Graphic characters
Character | Name | Character | Name |
---|---|---|---|
+ | Plus sign | - | Minus sine (Hyphen) |
\ | Backward slash | / | Forward slash |
% | Percent sign | * | Asterisk |
{ | Left Braces | } | Right Braces |
[ | Left Bracket | ] | Right Bracket |
( | Left Parenthesis | ) | Right Parenthesis |
Less than sign | = | Equal to sign | |
> | Grater than sign | _ | Underscore |
, | Comma | . | Period |
' | Single Quotes | " | Double Quotes |
: | Colon | ; | Semicolon |
? | Question mark | ! | Exclamation sign |
& | Ampersand | ^ | Caret sign |
~ | Tilde sign | | | Vertical bar |
# | Hash |
Whitespace characters
Newline character, Space character, Vertical tab, Horizontal tab, and form feed are whitespace characters.
Other characters
Backspace, Carriage return, alerts, and null character.
Escape Sequences
Some character sets newline, tab, backspace are cannot be displayed on a screen like normal characters.
C supports the combinations of backslash ( / ) and characters to represents these characters in our program. These combinations are known as escape sequence.
Some escape sequences are given below,
Escape Sequence | Meaning | Purpose |
---|---|---|
\b | backspace | Moves the cursor to the previous position of the current line. |
\a | alert | Produces an audible or visible alert |
\r | carriage return | moves curser to the beginning of the line |
\n | new line | moves curser to the beginning of the next line |
\f | form feed | Moves the curser to the initial position of the next logical page |
\0 | null character | Used for termination of character string |
\v | vertical tab | Moves the cursor to next vertical tab position |
\t | horizontal tab | Moves the cursor to next horizontal tab position |
\\ | backslash | Presents a character with backslash ( \ ) |
Blank, vertical tab, horizontal tab, carriage return, newline, form feed are known as whitespace in C language.
Trigraph Character
Some keyboards are not having all the characters from the C character set. So as a solution C supports the facility of “trigraph sequence” to print these characters.
Trigraph Sequence | Symbol |
---|---|
?? | { Left brace |
??> | } Right brace |
??( | [ Left bracket |
??) | ] Right bracket |
??! | | Vertical bar |
??/ | \ Backslash |
??= | # Hash sign |
??- | ~ Tilde |
??' | ^ Caret |
Delimiters
Delimiters are very important and they are used for syntactic purpose in C.
: | Colon | used for label |
; | Semicolon | end of the statement |
( ) | Parentheses | used in expression |
[ ] | Square brackets | used in array |
{ } | Curly braces | used for block of statements |
# | Hash | preprocessor directive |
, | Comma | variable delimiter |
NOTE: In C language every word we write, it is either a keyword or Identifier.
Keywords
- The word which is meaningful to the compiler is known as a keyword.
- Keywords are having its specific meaning, and the meaning can not be changed.
- In C language we are having 32 keywords.
- It is always written in lowercase only.
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 |
NOTE: Functions are not keywords.
Identifiers
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.
Eg.
Valid identifiers:
- Result
- sum
- a
- total_value
- _temp
- DATA
Invalid identifiers:
- 2data : First character should be an alphabet or underscore.
- int : int is a keyword.
- sum# : # is a special character.
- total value : blank space is not permitted.
Token
Token: Whatever known to compiler OR meaningful to the compiler is known as a token.
TOKEN is the smallest unit in a ‘C’ program.
It is each and every word and punctuation that you come across in your C program.
The compiler breaks a program into the smallest possible units (tokens) and proceeds to the various stages of the compilation.
A token is divided into six different types, Keywords, Operators, Strings, Constants, Special Characters, and Identifiers.
Data type
Every datatypes are keywords but not vice-versa
Different datatype is different in memory.
- Primary (Built-in) data type
- Derived data type
All character and integer types are known as integral types, and the types float, double, long double are known as floating point type.
The size and range of different datatypes are machine and compiler dependent.
Syntax:
datatype variable_name;
Eg.
char v1;
int _count;
We can use sizeof() operator to find out the size of different datatype on our system.
Primary (Built-in) data type
char
Sine qualifiers: Signed, Unsigned
Size: 1 byte
Size of character variable always will be 1 byte.
int
Integer variable size will be 2 (or) 4 bytes, which depends upon the platform.
Sine qualifiers: Signed, Unsigned
Size qualifiers: Short, long, long long
Size:
- short int : 2 bytes
- long int : 4 or 8 bytes
- long long int : 8 or 16 bytes
float
Size : 4 bytes
double
Size Variations : long
Size :
- double : 8 bytes
- long double : 8 or 10 bytes
void
Void indicates that no value is available. void should be used to declare only pointers not for variables.
Basic data types | Data types with type qualifiers | Size (bytes) | Range |
---|---|---|---|
char | signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 | |
int | int or signed int | 2 | - 32768 to 32767 |
unsigned int | 2 | 0 to 65535 | |
short int or signed short int | 1 | -128 to 127 | |
unsigned short int | 1 | 0 to 255 | |
long int or signed long int | 4 | -2147483648 to 2147483647 | |
unsigned long int | 4 | 0 to 4294967295 | |
float | float | 4 | 3.4E-38 to 3.4E+38 |
double | double | 8 | 1.7E-308 to 1.7E+308 |
long double | 10 | 3.4E-4932 to 1.1E+4932 |
Derived data type
Derived data type we look in detail in other article but for your reference just look for its types.
Enum, Pointer, array, structure, union are derived datatype
NOTE: int the next tutorial we will learn how to check of your system's variable size.