Elements of C Programming
The elements of C programming consist of basic building blocks that form the structure of C programs. Understanding these elements is crucial for writing efficient and functional C code. This overview also supports problem-solving with computers. The key elements include:
Table of Contents
ToggleCharacter Set
The set of characters used for words, numbers, and expressions in C is called the c character set. The combination of these characters forms words, numbers, and expressions in C. The characters in C are grouped into the following four categories.
- Letters or Alphabets
- Digits
- Special Characters
- White space
1. Letter or Alphabets
- Uppercase alphabets – A…Z
- Lowercase alphabets – a…z
2. Digits
All decimal digits – 0 1 2 3 4 5 6 7 8 9
3. Special Characters
Symbol | Meaning | Symbol | Meaning |
, | comma | & | ampersand |
. | period | ^ | caret |
; | semicolon | * | asterisk |
etc.
4. White Spaces
- Blank space
- Horizontal tab
- Vertical tab
- carriage return
- New line or line feed
- Form feed
Keywords
Keywords are predefined words for a C programming language. All keywords have fixed meanings and these meanings cannot be changed. They serve as basic building blocks for program statements. ANSIC keywords are listed below.
auto, double, int, struct, break, else, long switch, case, enum, register, typedef, char, extern, return, union, const, float, short, unsigned, continue, signed, void, for, default, goto, sizeof, volatile, do, if, static, while.
Thus, the keywords are also called reserved words.
Identifiers
Every word used in a C program to identify the names of variables, functions, arrays, pointers, and symbolic constants is known as an identifier. They are names given by the user and consist of a sequence of letters and digits, with letters as the first character. The underscore character can also be used to link between two words in long identifiers.
Data Types
ANSIC supports three classes of data types.
- Primary data types
- User-defined data types
- Derived data types
(A) Primary Data Types
Primary data types are categorized into five types:
- Integer type (int)
- Floating point type (float)
- Double precision floating point type (double)
- Character type (char)
- void type (void)
The words in parentheses indicate the keyword used in C to indicate the data type.
i. Integer Types
Integers are whole numbers, i.e. non-fractional numbers. Generally, integers require 16 bits of storage. C has further three classes of integer – integer (int), short integer (short int), and long integer (long int), in both signed and unsigned forms. The appropriate data type is used according to our requirement i.e. what type of number is to be used.
ii. Floating Point Types
Floating point types are fractional numbers (i.e. real numbers). They are defined in C by the keyword float. Floating numbers reserve 32 bits (i.e. 4 bytes) of storage, with 6 digits of precision.
iii. Double Precision Floating Point Types
When the accuracy provided by a float number is not sufficient, the type double can be used to define the number. A number data type number used 64 bits giving a precision of 14 digits. These are known as double-precision numbers. to extend the precision further, a long double can be used which uses 80 bits giving 18 digits of precision.
iv. Character Type
A single character can be defined as a character type data. characters are stored in 8 bits. The qualifier is char. The qualifier signed or unsigned may be used with char. The unsigned char has values between 0 and 255. The signed char has values from -128 to 127. The conversion character for this type is c.
v. void Type
The void type has no values. This is usually to specify a type of function when it does not return any value to the calling function.
B. User-Defined Data Types
C supports a feature called type definition which allows users to define an identifier that would represent an existing data type. The typedef statement is used to give a new name to an existing data type. It allows users to define new data types that are equivalent to existing data types. It takes the general form
typedef existing_ data_ type new_ name_ for_ existing_ data_ type;
Here, the existing_ data_ type refers to any one of the fundamental or user-defined data types; the new_ for_ existing_ data_ type refers to a new identifier name.
C. Derived Data Types
Some data types are derived from the existing primary data types. For example:
Here ‘struct’ is a derived data type ‘structure’. It consists of integer, float, and character variables. structures, unions, and enumerations are the derived data types used in C.
Constants
A constant is a quantity that does not change during the execution of a program. C constants can be divided into different categories. constant is divided into numeric and character constants. Integer and real constants lie in numeric constants while character and string constants lie in character constant types.
Variables
A variable is a symbolic name that is used to store data item, i.e. a numerical quantity or a character constant. Variables are defined in computer programs to represent an item of data input by the user, any intermediate calculation, or the end results. Declaring variables requires specifying their data type (e.g., int x;
).
Tokens in C
The basic elements recognized by the C compiler are the “tokens”. A token is source program text that the compiler does not break down into component elements. The keywords, identifiers, constants, string literals, operators, the special symbols: brackets([ ]), braces ({ }), parentheses (()), and commas(,) are examples of tokens.
Example: 1 Let’s understand it
Write a program that will convert the temperature in Centigrade into Fahrenheit.
Example: 2
Write a program to find interest for a given principle amount ‘p’, time ‘t’ years, and rate ‘r’%.