The basic elements used to construct a simple C program are: the C character set, identifiers and keywords, data types, constants, arrays, declarations , expressions and statements. Let us see how these elements can be combined to form more comprehensive program components-
Key Takeaways
- C programming uses a case-sensitive character set, identifiers and keywords, data types, constants, and arrays to construct a program. Identifiers are names given to program elements, while keywords are reserved words with predefined meanings.
- C programming supports various data types, each with different memory requirements. These include integer (int), character (char), floating point (float), double precision floating point (double), void, _Bool, and _Complex. These data types can be modified by data type qualifiers.
- Arrays in C programming refer to a collection of data items with the same name and data type. Each data item is represented by its array element, distinguished by their subscripts. Constants, on the other hand, are identifiers whose value remains unchanged throughout the program.
- C programming supports structured programming, lexical variable scope, and recursion. It also provides a wide range of operators for mathematical or logical operations, control statements for program flow, and functions for breaking down complex programs into simpler ones.
The C Character Set:
C uses letters A to Z in lowercase and uppercase, the digits 0 to 9, certain special characters, and white spaces to form basic program elements (e.g variables , constants, expressions etc.)
The special characters are:
+ – * / = % & # ! ? ^ ” ‘ / | < > ( ) [ ] { } : ; . , ~ @ !
The white spaces used in C programs are: blank space, horizontal tab, carriage return, new line and form feed.
Identifiers and Keywords:
Identifiers are names given to various program elements such as variables, functions, and arrays. Identifiers consist of letters and digits, in any order, except that the first character must be a letter. Both uppercase and lowercase letters are permitted and the underscore may also be used, as it is also regarded as a letter. Uppercase and lowercase letters are not equivalent, thus not interchangeable. This is why it is said that C is case sensitive. An identifier can be arbitrarily long.
The same identifier may denote different entities in the same program, for example, a variable and an array may be denoted by the same identifier, example below.
int sum, average, A[10]; // sum, average
and the array name A
are all identifiers.
The _ _func_ _ predefined identifier:-
The predefined identifier __func__
makes a function name available for use within the function. Immediately following the opening brace of each function definition, _ _func_ _
is implicitly declared by the compiler in the following way:
static const char _ _func_ _[] = "function-name";
where function-name is the name of the function.
consider the following example
#include <stdio.h> void func1(void) { printf("%sn",__func__); return; } int main() { myfunc(); }
The output would be
func1
Keywords
Keywords are reserved words that have standard predefined meanings. These keywords can only be used for their intended purpose; they cannot be used as programmer defined identifiers. Examples of some keywords are: int
, main
, void
, if
.
Data Types
Data values passed in a program may be of different types. Each of these data types are represented differently within the computer’s memory and have different memory requirements. These data types can be augmented by the use of data type qualifiers/modifiers.
The data types supported in C are described below:
int:
It is used to store an integer quantity. An ordinary int can store a range of values from INT_MIN to INT_MAX as defined by in header file <limits.h>. The type modifiers for the int data type are: signed, unsigned, short, long and long long.
- A short int occupies 2 bytes of space and a long int occupies 4 bytes.
- A short unsigned int occupies 2 bytes of space but it can store only positive values in the range of 0 to 65535.
- An unsigned int has the same memory requirements as a short unsigned int. However, in case of an ordinary int, the leftmost bit is reserved for the sign.
- A long unsigned int occupies 4 bytes of memory and stores positive integers in the range of 0 to 4294967295.
- By default the int data type is signed.
- A long long int occupies 64 bits of memory. It may be signed or unsigned. The signed long long int stores values from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and the unsigned long long ranges from 0 to 18,446,744,073,709,551,615.
char:
It stores a single character of data belonging to the C character set. It occupies 1 byte of memory, and stores any value from the C character set. The type modifiers for char are signed
and unsigned
.
Both signed and unsigned char occupy 1 byte of memory but the range of values differ. An unsigned char can store values from 0 to 255 and a signed char can store values from -128 to +127. Each char type has an equivalent integer interpretation, so that a char is really a special kind of short integer. By default, char is unsigned.
float:
It is used to store real numbers with single precision i.e. a precision of 6 digits after decimal point. It occupies 4 bytes of memory. The type modifier for float is long
. It has the same memory requirements as double.
double:
It is used to store real numbers with double precision. It occupies 8 bytes of memory. The type modifier for double is long
. A long double occupies 10 bytes of memory.
void:
It is used to specify an empty set containing no values. Hence, it occupies 0 bytes of memory.
_Bool:
A boolean data type, which is an unsigned integer type, that can store only two values, 0 and 1. Include the file <stdbool.h> when using _Bool.
_Complex:
It is used to store complex numbers. There are three complex types: float _Complex
, double _Complex
, and long double _ComplexIt
is found in the <complex.h> file.
Arrays:
An array is an identifier that refers to a collection of data items of that have the same name. They must also have the same data type (i.e. all characters, all integers etc.). Each data item is represented by its array element. The individual array elements are distinguished from one another by their subscripts.
Syntax for array declaration:
Suppose arr
is a 5 element integer array which stores the numbers 5, 4, 2, 7, 3 in that order. The first element is referred to as arr[0]
which stores the data value 5, the second element is arr[1]
which stores the value 4 and so on. The last element is arr[4]
which stores the value 3. The subscript assosciated with each element is shown in square braces. For an n-element array the subscripts will range from 0 to n-1.
In case of a character array of size n, the array will be able to store only n-1 elements as a null character is automatically stored at the end of the string to terminate it. Therefore, a character array letter that stores 5 elements must be declared of size 6. The fifth element would be stored at letter[4]
and letter[5]
will store the null character.
Constants:
A constant is an identifier whose value remains unchanged throughout the program. To declare any constant the syntax is:const datatype varname = value;
where const
is a keyword that declares the variable to be a fixed value entity.
There are four basic types of constants inC. They are integer constants, floating point constants, character constants and string constants. Integer and floating point constants cannot contain commas or blank spaces; but they can be prefixed by a minus sign to indicate a negative quantity.
Integer Constants:
An integer constant is an integer valued number. It consists of a sequence of digits. Integer constants can be written in the following three number systems:
Decimal(base 10): A decimal constant can consist of any combination of digits from 0 to 9. If it contains two or more digits, the first digit must be something other than 0, for example: const int size =50;
Octal(base 8): An octal constant can consist of any combination of digits from 0 to 7. The first digit must be a 0 to identify the constant as an octal number, for example: const int a= 074; const int b= 0;
Hexadecimal constant(base 16): A hexadecimal constant can consist of any combination of digits from 0 to 9 and a to f (either uppercase or lowercase). It must begin with 0x or oX to identify the constant as a hexadecimal number, for example: const int c= 0x7FF;
Integer constants can also be prefixed by the type modifiers unsigned and long. Unsigned constants must end with u
or U
, long integer constants must end with l
or L
and unsigned long integer constants must end with ul
or UL
. Long long integer constants end with LL
or ll
. Unsigned long long end with UL
or ul
Floating Point Constant:
Its a base 10 or a base 16 number that contains a decimal point or an exponent or both. In case of a decimal floating point constant the exponent the base 10 is replaced by e or E. Thus, 1.4 *10^-3 would be written as 1.4E-3 or 1.4e-3.
In case of a hexadecimal character constant, the exponent is in binary and is replaced by p or P. For example:
const float a= 5000. ; const float b= .1212e12; const float c= 827.54;
Floating point constants are generally double precision quantities that occupy 8 bytes. In some versions of C, the constant is appended by F
to indicate single precision and by L
to indicate a long floating point constant.
Character Constants:
A character constant is a sequence of one or more characters enclosed in apostrophes. Each character constant has an equivalent integer value that is determined by the computer’s character set. It may also contain escape sequences. A character literal may be prefixed with the letter L
, u
or U
, for example L'c'
.
A character literal without the L prefix is an ordinary character constant or a narrow character constant. A character literal with the L
prefix is a wide character constant. The type of a narrow character constant and a multicharacter constant is int
. The type of a wide character constant with prefix L
is wchar_t
defined in the header file <stddef.h>
.A wide character constant with prefix u
or U
is of type char16_t
or char32_t.
These are unsigned character types defined in <uchar.h>
.
An ordinary character literal that contains more than one character or escape sequence is a multicharacter constant, for example: const char p= 'A';
Escape Sequences are also character constants that are used to express certain non printing characters such as the tab or the carriage return.An escape sequence always begins with a backward slash and is followed by one or more special characters. for eg. b will represent the bell, n will represent the line feed.
String Literals:
A string literal consists of a sequence of multibyte characters enclosed in double quotation marks. They are of two types, wide string literal and UTF-8 string literal. A UTF-8 string literal is prefixed by u8
and a wide string literal by L
, u
or U
.
The compiler recognizes and supports the additional characters (the extended character set) which you can meaningfully use in string literals and character constants. The support for extended characters includes the multibyte character sets. A multibyte character is a character whose bit representation fits into one or more bytes.
Symbolic Constants:
A symbolic constant is a name that substitutes for a numeric constant, a character constant or a string constant throughout the program. When the program is compiled each occurrence of a symbolic constant is replaced by its actual value.
A symbolic constant is defined at the beginning of a program using the # define feature. The # define feature is called a preprocessor directive, more about the C preprocessor in a later article.
A symbolic constant definition never ends with a semi colon as it is not a C statement rather it is a directive, for example:
#define PI 3.1415 //PI is the constant that will represent value 3.1415 #define True 1 #define name "Alice"
These were the some of the basic elements of C. Elements like scope of an identifier, declarations, statements, expressions, etc., will be explained in future articles with the help of practical examples. The next article will discuss the operators provided in the C language.
FAQs About Fundamentals of C Programming
What are the basic concepts of C programming?
C programming is a general-purpose, procedural computer programming language that supports structured programming, lexical variable scope, and recursion. It was developed in the early 1970s by Dennis Ritchie at Bell Labs. The basic concepts of C programming include variables, data types, operators, control statements, arrays, functions, pointers, and file handling. Understanding these concepts is crucial to mastering C programming.
How does C programming support structured programming?
Structured programming is a programming paradigm that improves the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, and loops. C programming supports structured programming by allowing complex programs to be broken down into simpler ones, known as functions. Each function performs a specific task and can be called when needed, reducing code redundancy and making the program easier to understand and manage.
What are the different data types in C programming?
C programming supports several data types, including integer (int), character (char), floating point (float), and double precision floating point (double). Each data type requires a different amount of memory and has a specific range of values that it can represent. Understanding these data types is essential for writing efficient and effective C programs.
How are operators used in C programming?
Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C programming supports a wide range of operators, including arithmetic operators (+, -, *, /, %), relational operators (>, <, ==, !=, >=, <=), logical operators (&&, ||, !), and bitwise operators (&, |, ^, ~, <<, >>). These operators can be used to manipulate data and control the flow of the program.
What are control statements in C programming?
Control statements are used to control the flow of execution of the program. They include decision-making statements (if, if-else, switch), looping statements (for, while, do-while), and jump statements (break, continue, goto). These statements allow the program to make decisions and repeat actions based on certain conditions.
How are arrays used in C programming?
An array is a collection of elements of the same data type that are stored in contiguous memory locations. Arrays are used in C programming to store and manipulate large amounts of data efficiently. They can be one-dimensional (a single row or column of elements) or multi-dimensional (a table of elements).
What are functions in C programming?
A function is a group of statements that performs a specific task. Functions are used in C programming to break down complex programs into simpler ones, reducing code redundancy and making the program easier to understand and manage. Each function has a name, a list of parameters, a return type, and a body.
How are pointers used in C programming?
A pointer is a variable that stores the address of another variable. Pointers are used in C programming to access memory directly, allowing for efficient manipulation of data. They are also used to create dynamic data structures, such as linked lists and trees.
What is file handling in C programming?
File handling is the process of creating, reading, writing, and closing files. C programming provides a number of functions for file handling, including fopen, fclose, fgetc, fputc, fscanf, and fprintf. These functions allow the program to interact with files, storing data for later use or retrieving data for processing.
How can I start learning C programming?
The best way to start learning C programming is to understand the basic concepts, such as variables, data types, operators, control statements, arrays, functions, pointers, and file handling. Once you have a good grasp of these concepts, you can start writing simple programs and gradually move on to more complex ones. Practice is key to mastering C programming, so try to write code every day and solve problems on coding platforms.
My name is Surabhi Saxena, I am a graduate in computer applications. I have been a college topper, and have been awarded during college by the education minister for excellence in academics. I love programming and Linux. I have a blog on Linux at www.myblogonunix.wordpress.com.