The inception of C
- Designed and developed in AT&T’s Bell Labs in 1972 by Dennis Ritchie.
- It is a reliable, fast, simple, and easy to use language.
- It is a middle-level language which just close enough to the machine as well as to the programmer. This gives the programmer fine control over machine properties such as memory and processer.
- UNIX, Linux, Android, PHP, Python, C++ and so many other products and languages are written in C making it the most influential and powerful language for a professional to learn.
- C is everywhere. Smartphones, computers, enterprise machines, dish TV set-up boxes, cameras, microwaves, washing machines and 99% of smart devices run C or a morphed version of C. So, C is a basic skill to have if ones want to become a professional in computers.
Let’s get started
- C is a language just like English and Kannada which is used to communicate.
- English and other natural languages are used to communicate with other humans and we use C to communicate with computers.
- Just the way we learn natural languages by starting with the alphabet followed by words that form sentences finally constituting a paragraph, we learn C from alphabets, digits, and special symbols followed by constants, variables, and keywords which form instructions finally constituting a program.
The C Character Set
- A character denotes any alphabet, digit or special symbol used to represent information.
- In C,
- Alphabets are: from A to Z and from a to z. (Obviously)
- Digits are from 0 to 9 (Duh!)
- Special symbols are anything printable on your keyboard other than the above two groups.
C Tokens
Tokens are the result of a combination of C character set and form the fundamental building blocks of the C programming language. There are 5 types of C tokens. They are:
Keywords:
- These are reserved words that have a specific meaning in the language.
- Each keyword has its dedicated functionality.
- English has over 4,70,000 words and we have learnt it. Guess how many keywords does C have? Just 32. Do you still think C is difficult?
- Here are the keywords of C:
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
Identifiers
- The computer consists of millions of cells in which data is stored. To make the retrieval, storage and usage of the values easy, these memory cells are given names. Since the value in a particular cell might change or vary, these locations are called variables/identifiers.
- Here are rules for naming identifiers:
- The first character of an identifier should either be an alphabet or an underscore and nothing else. And then it could be followed by characters or digits or underscores.
- Identifiers in C are case sensitive. I.e., abc is a different identifier than Abc.
- Keywords should not be used as identifiers.
- The length of the identifiers should not be more than 31 characters.
- Tip: Identifiers should be written in such a way that it is meaningful, short and easy to read.
- Examples of valid and invalid identifiers:
Valid | Invalid |
num sum_of_array sumOfArray _len first side1 side_2 Side2 | X’mas switch main() sum of array 2nd $dollars period. percent% |
Constants:
- Contrary to variables, constants are the entities that remain unchanged throughout the execution of the program.
- Essentially the values which variables hold are constants.
- Consider ‘num’ variable holding the value 3 as shown in the following figure:
- Here, 3 is the constant. The value ‘3’ never changes. ‘num’ is the variable that holds the constant 3. The constant in ‘num’ may change over the course of the execution; hence, it is a variable.
Types of constants:
- Primary constants:
- Integer constant
- Real constant
- Character constant
- Void
- Secondary / Derived constants:
- Array: Collection of similar primary constants under a single name.
- Pointer: Constant which is the location of another constant
- Structure: Collection of similar or dissimilar primary constants.
- Union: Memory overlapping collection of similar or dissimilar primary / secondary constants.
- User-defined:
- Enum: It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
- Typedef: can be used to give a type a new name.
We shall currently focus on primary constants for now and pick up the 2ndand 3rd types in upcoming modules.
Primary constants:
Integer constant;
- An integer constant is a positive or negative non-decimal integer value.
- Must not have decimal point.
- If no sign is present, it is assumed positive.
- No commas or blanks are to be present within an integer constant.
- There are classifications of integer constants based on their base value. They are listed as follows:
- Decimal Integer Constants
- Integer constants consist of a set of digits, through 9, preceded by an optional – or + sign.
- Example of valid decimal integer constants 341, -341, 0, 8972
- Octal Integer Constants
- Integer constants consisting of a sequence of digits from the set 0 through 7 starting with 0 are said to be octal integer constants.
- Example of valid octal integer constants 010, 0424, 0, 0540
- Hexadecimal Integer Constants
- Hexadecimal integer constants are integer constants having a sequence of digits preceded by 0x or 0X. They may also include alphabets from A to F representing numbers 10 to 15.
- Example of valid hexadecimal integer constants 0xD, 0X8d, 0X, 0xbD
- Decimal Integer Constants
Real constant
- Real constants also called Floating Point constants, are numbers with the addition of decimal values to integers.
- The real constants could be written in two forms—Fractional form and Exponential form.
- Following rules must be observed while constructing real constants expressed in the fractional form:
- A real constant must have at least a one-digit, decimal point. (Yes! ‘5.’ is a valid floating-point number!)
- It could be either positive or negative.
- If no sign is mentioned, by default, it is positive.
- No commas or blanks are allowed within a real constant.
- In the exponential form, the real constant is represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called the exponent. Thus 0.000342 can be written in exponential form as 3.42e-4 (which in normal arithmetic means 3.42 x 10 -4).
- Following rules must be observed while constructing real constants expressed in exponential form:
- The mantissa part and the exponential part should be separated by the letter e or E.
- The mantissa part may have a positive or negative sign.
- The default sign of the mantissa part is positive.
- The exponent must have at least one digit, which must be a positive or negative integer. The default sign is positive.
Character constant:
- A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
- Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
- Ex.: ‘A’, ‘I’, ‘5’, ‘=’
Void
- A void is an empty data type that has no value. We use void data type in functions when we don’t want to return any value to the calling function and we also use it in pointers to create a generic pointer. We shall see functions and pointers in upcoming modules.
Operators:
- Operators in C are special symbols used to perform the functions. The data items on which the operators are applied are known as operands. Operators are applied between the operands. Depending on the number of operands, operators are classified as follows:
Unary Operator
- A unary operator is an operator applied to a single operand. For example: increment operator (++), decrement operator (–), sizeof, (type)*, address-of operator (&).
Binary Operator
- The binary operator is an operator applied between two operands. The following is the list of the binary operators:
- Arithmetic Operators (+, -, *, /, %)
- Surprisingly, there are no operator for exponentiation, but there is a function named pow() which we shall see later.
- Relational Operators (<, >, <=, >=, ==, !=)
- Shift Operators (<<, >>)
- Logical Operators (!, &&, ||) Logical ‘!’ operator is unary
- Bitwise Operators (~, &, |, ^) Bitwise ‘!’ is unary
- Conditional Operators / Ternary operator
- Assignment Operator (=)
Special symbols:
- The following are the special symbols in C: ~ ‘ ! @ # % ^ & * ( ) _ – + = | \ { } [ ] : ; ” ‘ < > , . ? / $
- They have special meaning and are used to achieve concepts through length and breadth of C. (They will reveal themselves along with their purpose as and when the topics are rolled out)
Qualifiers:
There are several qualifiers such as short, long, signed, unsigned that can be applied to the primary data types. The possible qualifiers for the basic type are shown in the table:
Data type | Qualifier |
Char | signed, unsigned |
int | short, long, signed, unsigned |
float | No qualifier |
double | long |
void | No qualifier |
Each compiler is free to choose the appropriate size for its hardware with restrictions that short and int are at least 16 bits and long is at least 32 bits and the size of short < int < long.
Char is the same as an integer. If a char is stored in a variable, it stores its corresponding ASCII value which ranges from (0, 255). Hence, qualifier signed or unsigned may be applied to char or any integer.
Unsigned numbers are always positive or zero and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. E.g., char is 8 bits so unsigned char variables have values between 0 and 28 i.e., values between 0 and 255
- If we do not specify either signed or unsigned, most compilers will assume the type to be signed.
- So, signed int x;
can be written as: int x; - short and long can be used alone as type specifiers.
short = short int
long = long int - short int x;
can be written as short x;
First C program!
It’s a tradition that the first program should be “Hello world!”. Following the same tradition, let’s step into our first program.
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Let us now understand this program in detail.
Structure of a C Program
- Each instruction in a C program is written as a separate statement.
- The statements in a program must appear in the same order in which we wish them to be executed.
- Blank spaces may be inserted between two words to improve the readability of the statement.
- All statements should be in lower case letters.
- C has no specific rules for the position at which a statement is to be written in a given line. That’s why it is often called a free-form language. But it is advisable to indent with a tab for every progressive nested block.
- Every C statement must end with a semicolon (;). Thus; acts as a statement terminator.
Comments in a C Program
- Comments are used in a C program to clarify either the purpose of the program or the purpose of some statements in the program.
- It is not considered by the compiler while creating an executable file. It is solely for the reader of the program.
- It is a good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written.
- Here are a few things that you must remember while writing comments in a C program:
- Comment about the program should be enclosed within /* */
- When the purpose of a statement(s) isn’t obvious, it is worthwhile mentioning the purpose using a comment. For example:
/* formula for simple interest */
si = p * n * r / 100 ;
- Any number of comments can be written at any place in the program. For example, a comment can be written before the statement, after the statement or within the statement as shown below.
/* formula */
si = p * n * r / 100;
si = p * n * r / 100 ;
/* formula */
si = p * n * r / /* formula */ 100 ;
- The normal language rules do not apply to text written within /* .. */. Thus, we can type this text in a small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and are completely ignored by the compiler.
- Comments cannot be nested. This means one comment cannot be written inside another comment. The following example is invalid.
/*Cal of SI /* Author: QM date: 25/06/2016 */ */
- A comment can be split over more than one line, as in,
/* This comment
has three lines
in it */
Such a comment is often called a multi-line comment.
- ANSI C permits single-line comments to be written as follows:
// Calculation of simple interest
// Formula
But note that ‘//’ comments a single line starting from where it was written.
Si = p * t * r / 100 // simple interest
// Formula
So, in this example, the comments are considered from ‘//’ and everything before it till the margin is considered for compilation.
What is main()?
- main() is a function. A function is nothing but a container for a set of statements.
- In a C program, there can be multiple functions.
- The execution of statements starts with the main() function
- All statements that belong to main() are enclosed within a pair of braces {} as shown below.
int main()
{
statement 1;
statement 2;
statement 3;
}
- A function may or may not return a value. The main() function always returns an integer value, hence there is an int before main().
- The integer value that we are returning is 0. 0 indicates success. If for any reason the statements in main() fail to do their intended work we can return a non-zero number from main(). This would indicate failure.
- As there could be many reasons for failure, we could place a return code standard concerning the program. One of such codes could be returning -1 for entering a floating-point value instead of an integer value.
- Some compilers like Turbo C/C++ even permit us to return nothing from main(). In such a case we should precede it with the keyword void. But this is a non-standard way of writing the main() function.
Variables and their Usage
Consider the following C program:
/* Calculation of simple interest */
/* Author: QM Date: 25/06/2016 */
# include <stdio.h>
int main()
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
return 0 ;
}
Any variable used in the program must be declared before using it. For example,
int p, n ; /* declaration */
float r, si ; /* declaration */
si = p * n * r / 100 ; /* usage */
In the statement,
si = p * n * r / 100 ;
* and / are the arithmetic operators.
printf() and its Purpose
printf()is used to display contents onto the user screen. Concerning the above program, we can note the following points:
- Once the value of si is calculated it needs to be displayed on the screen. We have used printf() to do so.
- For us to be able to use the printf() function, it is necessary to use #include at the beginning of the program. #include is a pre-processor directive. It has to be used every time we use printf(). The purpose and definition of pre-processor directives will be covered in later modules.
- Syntax of printf( ) function is as follows:
printf ( "<format specifier>",<list of variable> ) ;
- The following table is a reference for data type and their corresponding format specifiers:
Format Specifier | Type |
%c | Character |
%d | Signed integer |
%e or %E | Scientific notation of floats |
%f | Float values |
%g or %G | Similar to %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned Integer (short) |
%i | Unsigned Integer |
%l or %ld or %li | Long |
%lf | Double |
%Lf | Long Double |
%lu | Unsigned int or unsigned long |
%lli or %lld | Long long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Pointer |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
%n | Prints nothing |
%% | Prints % character |
In addition to format specifiers like %f, %d and %c, the format string may also contain any other characters. These characters are printed as they are when printf() is executed.
- Given below are some more examples of usage of printf() function:
printf ( "%f", si ) ;
printf ( "%d %d %f %f", p, n, r, si ) ;
printf ( "Simple interest = Rs. %f", si ) ;
printf ( "Principal = %d \nRate = %f", p, r ) ;
The output of the last statement would look like this…
Principal = 1000
Rate = 8.500000
‘\n’ is called the newline character and it takes the cursor to the next line. Therefore, you get the output split over two lines. ‘\n’ is one of the several Escape Sequences available in C.
Here are some more escape sequences and we shall see them in detail in upcoming modules.
- An expression is nothing but a valid combination of constants, variables and operators. printf() can also print the result of an expression. Examples of valid expressions are as follows:
3
3 + 2
c
a + b * c – d
The results of these expressions can be printed as shown below.
printf( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d );
Note that using expressions in printf() is optional. We can print a statement without any expression just as well.
Receiving Input
- In the program we saw earlier, the values of p, n and r were hard-coded.
- Often C programs are required to read input from the user to make the program more resourceful and portable.
- To achieve this motive, scanf() is one of the programs we use. It reads constants from the user and stores them into the address of a mentioned variable.
- It can be considered as a complementary to printf() function. • The syntax of scanf() is as follows:
scanf("<format -specifier>", &<variable_name>);
This is illustrated in the program given below.
/* Calculation of simple interest */
/* Author gekay Date 25/06/2016 */
# include <stdio.h>
int main( )
{
int p, n ;
float r, si ;
printf ( "Enter values of p, n, r" ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
return 0 ;
}
- Note the use of the ampersand (&) before the variables in the scanf() function is a must.
- ‘&’ is an ‘Address of’ operator. It gives the location number (address) used by the variable in memory.
- When we say &a, we are telling scanf() at which memory location should it store the value supplied by the user from the keyboard.
- Note that a blank, a tab or a new line must separate the values supplied to scanf().
- A blank is created using a spacebar, a tab using the Tab key and a new line using the Enter key. This is shown below. Ex.: The three values are separated by blank:
1000 5 15.5
Ex.: The three values separated by tab:
1000 5 15.5
Ex.: The three values separated by a newline:
1000
5
15.5
The following is another example stitching all the theories discussed:
/* Just for fun. Author: QM */
# include <stdio.h>
int main( )
{
int num ;
printf ( "Enter a number" ) ;
scanf ( "%d", &num ) ;
printf ( "Now I’m letting you on a secret...\n" ) ;
printf ( "You have just entered the number %d\n",
num ) ;
return 0 ;
}
Programming challenges
- Shradha has a basic salary of n rupees. 10% of the basic salary is her house rent allowance and 40% of the basic salary is her dearness allowance. Print the gross salary she gets.
- INPUT:
- First line: An integer n
- Output:
- Her gross salary
- INPUT: