# C PROGRAMS
C Basics
➤ Introduction to C
➤ Compile and Execute
➤ Hello, World! in C
➤ Identifiers & Keywords
➤ Data types in C
➤ Variables in C
➤ Comments in C
➤ Operators in C
➤ Bitwise Operators
➤ Printf() in C
➤ Input using scanf()
C Basic Programs
➤ Size & Range of Data Types
➤ C Programs for Beginners
➤ Add Subtract Multiply & divide
➤ Sum & Average of 3 Numbers
➤ Simple and Compound Interest
➤ Distance between two Points
➤ Find Circle Triangle Area
➤ ASCII Value of Character
➤ Find Square root of Number
➤ Different Swapping Programs
In this tutorial, we will learn about constants in the C language. What are the character set and tokens in the C language? How many types of constants? What are integer constants, floating-point constants, character constants, string constants, boolean constants, complex constants? What are identifiers and keywords in the C language? What are the rules for naming identifiers in C? Find the valid and invalid keywords?
Character Set in C
C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9, and certain special characters as building blocks to form basic programming elements. Example of basic programming elements:- Constants, Variables, Operators, Expressions e.t.c.
- Digits:- 0 1 2 3 4 5 6 7 8 9
- Alphabets:-
- A B C D E F G H ………………. X Y Z
- a b c d e f g h i ……………………..x y z
- Special characters:-
, | Comma |
| ^ | Caret |
. | Period |
| * | Asterisk |
; | semicolon |
| – | Minus sign |
: | colon |
| + | Plus sign |
# | Number sign |
| < | Open angle bracket |
‘ | Apostrophe |
| > | Closing angle bracket |
“ | Quotation mark |
| ( | Left parenthesis |
! | Exclamation mark |
| ) | Right parenthesis |
| | Vertical bar |
| [ | Left bracket |
~ | Tilde |
| ] | Right bracket |
_ | Underscore |
| { | Left brace |
$ | Dollar sign |
| } | Right brace |
% | Percent sign |
| / | Slash |
? | Question mark |
| \ | Back slash |
& | Ampersand |
|
|
|
Tokens in C
The smallest individual words we are using in developing a C program are called tokens. Tokens are the basic lexical building blocks of source code. Characters are combined into tokens according to the rules of the programming language. The compiler checks that the tokens can be formed into legal strings according to the syntax of the language. Tokens can be divided into five parts:-
- Constants:- A constant is an entity that doesn’t change.
- Identifiers:- Name of a programming elements (like variables, functions, arrays e.t.c) are called identifiers.
- Keywords:- The system pre-defined reserved words are called Keywords. Each keywords having a certain meaning and we can’t change it’s meaning.
- Operators:- These tokens are used to indicate an action to be taken.
- Separators:- They are used to separate other tokens.
We will discuss every token in detail. In this tutorial, we will discuss constants, identifiers, and keywords. Operators in C will be discussed later. First, we start with constants.
Constants in C
A constant is an entity that doesn’t change, So we can’t change a constant value during program execution. The constant value should be provided at the time of declaration only, further initialization is not allowed. Constants can be divided into six types.
- Integer constants
- Floating-point constants or Real constants
- Single character constants
- String constants
- Boolean constant
- Complex constants
Boolean and Complex constants were introduced in C99.
Integer Constants in C
An integer constant is an integer-value number. It consists of a sequence of digits. Integer constants can be written in three different number systems, those are decimal, octal, and hexadecimal number system.
Decimal Integer Constants
It can be a combination of digits taken from 0 to 9. The base of decimal integer constants is 10. Example of decimal integer constants are 10, 25519, -565 e.t.c. Some invalid decimal integer constants are:- $5 (invalid due to $), 5 00 (invalid due to space) 10,000 (illegal character ‘,’), 9.9 (invalid due to ‘.’).
Octal Integer Constants
An octal integer constant must begin with 0 (zero) after that It can be any combination of digits from 0 to 7. The base of octal integer constants is 8. Example:- 055, 0357, 067771 e.t.c. Some invalid octal integer constants are:- 123 (should begin with 0), 0538 (can’t have 8), 562.389 (shouldn’t have ‘.’)
Hexadecimal Integer Constants
Hexadecimal integer constant must begin with 0x or 0X and after that, it can be any combination of digits taken from the set 0 through 9 and a through f (either upper or lower case). The base of hexadecimal integer constants is 16. The letters A through F represent the decimal quantities 10 through 15, respectively. Example:- 0x78A, 0Xabcd8, 0xFF e.t.c. Some invalid hexadecimal integer constants are- 0xad.7 (invalid due to ‘.’), 0xFGD (should not have ‘G’), 0FF (should begin with 0x or 0X).
The largest integer value that can be stored is machine-dependent. It is 32767 on 16-bit machine and 2,147,483,647 on 32-bit machines. It is also possible to store larger integer constants on these machines by appending qualifiers such as U, L, and UL to the constants. By default the type of an integer constant is int. Example:-
4546 | Integer constant |
46545U | Unsigned integer constant |
46545L | Long integer constant |
46545UL | An unsigned long integer constant |
Floating-point Constants or Real constants in C
A floating-point constant is a base 10 number that contains either a decimal point or an exponent. A real number may also be expressed in exponential or scientific notation. The exponential form of representation of real constants is usually used if the value of the constants is either too small or too large.
The part before ‘e‘ is called mantissa, the part after ‘e‘ is called exponent.
Syntax:- mantissa e exponent
Rules:-
- The mantissa part and the exponential part should be separated by a letter e or E.
- The mantissa part may have a +ve or -ve sign. Default sign of the mantissa part is +ve.
- The exponent must have at least one digit, which must be a +ve or -ve integer. Default sign is +ve.
- The mantissa is either a real number or an integer. An exponent is an integer number.
- The range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Examples:- +32e-5 , 4.1e8 , -0.2E+3 , -3.2e-5
By default real constants are assumed to be double. Suffixes ‘f’ or ‘F’ can be used to specify the float value. L can be used to specify long double values. Example:-
- 0.955 double constant
- 0.955f float constant
- 0.955L long double constant
The following rules apply to all numeric-type (integer, real) constants.
- The constants can be preceded by a minus (-) or plus (+) sign. The plus (+) sign is optional.
- Constants should not have any comma (,) and blank spaces.
Character Constants in C
A character constant is a single character, enclosed in apostrophe’s i.e. single quotation marks. Character constants have integer values that are determined by computers. Those integer values are called ASCII values. ASCII stands for American Standard Code for Information Interchange. Examples of character constants are:- ‘A’, ‘b’, ‘@’, ‘9’, ‘?’
C has named the critical character values so that we can refer to them symbolically. Note that these control characters use the escape character followed by a symbolic character.
ASCII Character | Symbolic Name |
Null character | ‘\0’ |
Horizontal tab | ‘\t’ |
Newline | ‘\n’ |
Vertical tab | ‘\v’ |
Backslash | ‘\\’ |
String Constants in C
A string constant is a sequence of zero and more characters enclosed within double quotes. It is alpha-numeric. The characters may consist of letters, numbers, escape sequences, and spaces. In C there is no string data type. Examples of string constants are:-
- “KnowProgram@20XX”
- “Hi, welcome to Programming World.”
- “”
- “987654321”
- “X”
Type | Example | Specification |
Decimal | 99 | nil |
Octal | 077 | Begins with 0 |
Hexadecimal | 0x98F | Begins with 0X or 0x |
Unsigned integer | 55U | Ends with U or u |
Long | 645644L | Ends with L or l |
Long Long | 4566464LL | Ends with LL or ll |
Unsigned long | 485UL | Ends with UL or w |
Floating-point | 953.35f | Ends with f or F |
Character | ‘A’ | Enclosed within single quote |
String | “Know” | Enclosed within double quote |
C89 added the suffixes U and u to specify unsigned numbers. C89 adds LL to specify long long.
Difference Between ‘X’ and “X”
‘X’ is a character constant and it is enclosed within a single quote but the second “X” is a string constant and it is enclosed within the double quote. The character constant occupying 1 byte containing the ASCII code of the character ‘X’. On another hand, a string constant “X” occupies 2 bytes, one byte for ASCII code of ‘X’ and another for the null character (‘\0’) with the value 0, which terminates all strings.
Boolean constants
A Boolean data type can take only two values. Therefore, we expect that we have only two symbols to represent a Boolean type. The values are true and false. A Boolean value can have only one of the two values: 1 (false) and 0 (true).
Complex constants
Complex constants are coded as two parts, the real part, and the imaginary part, separated by a plus sign. The real part is coded using the real format rules. The imaginary part is coded as a real number times (*) the imaginary constant. If the complex library (complex.h) is included, the imaginary constant can be abbreviated as I. Example:- 12.8 + 15.5 * I
Identifiers in C
The Identifier is the name of the basic programming element. In other words, an identifier or name is a sequence of characters invented by the programmer to identify or name a specific object.
#include<stdio.h>
int main()
{
int num=10;
printf(“%d”,num);
printf(“\nHello, World!);
return 0;
}
In a C program, every word is either classified as an identifier or a keyword. Identifiers are user-defined words and are used to give names to entities like variables, functions, arrays, structures e.t.c. Keywords have predefined meanings and can’t be changed by the user.
Rules for naming identifier
- The identifier can only consist of alphabets [ (a to z) or (A to Z)], digits (0 to 9), and underscore sign (_).
- The Identifier shouldn’t start with a digit, but from the second character onwards digits are allowed.
- Special characters are not allowed except underscore (_).
- The Keyword can’t be used as an identifier.
- The Identifier is case sensitive. The uppercase and lowercase letters are considered different.
- Identifiers may be of any reasonable length; generally, eight to ten characters should suffice, though certain compilers may allow for much longer names (of up to sixty-three characters). Note that on ANSI C, two identifiers will be considered different if they differ up to their first 31 characters.
Also See:- Identifiers and Data Types Quiz in C
Keywords in C
The system’s pre-defined reserved words are called Keywords. Each keyword having a certain meaning and we can’t change its meaning. All keywords are in lowercase. C is case sensitive so, lowercase and uppercase are considered different.
Standard ANSI supports 32 keywords. Five new keyword given in C99. List of all keywords are:-
auto | break | case | char |
const | continue | default | do |
double | else | enum | extern |
float | for | goto | if |
inline * | int | long | register |
restrict * | return | short | signed |
sizeof | static | struct | switch |
typedef | union | unsigned | void |
volatile | while | _Bool * | _Complex * |
_Imaginary * |
|
|
|
* These new keywords are added in C99.
Q) Find invalid identifiers and valid keywords
1) if
2) 5x
3) structure
4) x5
5) array
6) string
7) struct
8) cases
9) volatile
10) restricted
11) imaginary
12) boolean
13) _temp
14) unsign
15) _
16) a_
17) const
18) Know_Program
19) total-sum
20) Names
21) KnowProgram
22) Know Program
23) constant
24) while
25) sizeOf
26) while_do
27) Enum
28) external
29) "x"
30) Goto
[bg_collapse view=”button-blue” color=”#fff” icon=”eye” expand_text=”Show Answer” collapse_text=”Show Less” ]
Invalid identifiers are :- 2, 7, 9, 17, 19, 22 and, 24
Valid keywords are:- 7, 9, 17 and, 24
[/bg_collapse]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!