.
A program is a set of instructions that advise the computer to do various tasks. A programming language is a vocabulary and set of grammatical rules for instructing a computer or computing device to perform specific tasks. Programming languages can be broadly classified into the following three categories.
Imagine them as the “native tongue” of the computer, the language closest to the hardware itself. Each unique computer has a unique machine language. A machine language program is made up of a series of binary patterns (e.g., 01011100) which represent simple operations that can be accomplished by the computer (e.g., add two operands, and move data to a memory location). Machine language programs are executable, meaning that they can be run directly. Programming in machine language requires memorization of the binary codes and can be difficult for the human programmer. A nice and interactive example is present here.
They represent an effort to make programming easier for the human. The machine language instructions are replaced with simple Mnemonic abbreviations (e.g., ADD, MOV). Thus assembly languages are unique to a specific computer (machine). Before execution, an assembly language program requires translation to machine language. This translation is accomplished by a computer program known as an Assembler. Assemblers are written for each unique machine language.
High-level languages, like C, C++, JAVA etc., are more English-like and, therefore, make it easier for programmers to “think” in the programming language. High-level languages also require translation to machine language before execution. This translation is accomplished by either a compiler or an interpreter. Compilers translate the entire source code program before execution. Interpreters translate source code programs one line at a time. Interpreters are more interactive than compilers.
As explained earlier, a high-level language (HLL) is a programming language such as C, FORTRAN, or Pascal that enables a programmer to write programs that are more or less independent of a particular type of computer. Such languages are considered high-level because they are closer to human languages and further from machine languages. The main advantage of high-level languages over low-level languages is that they are easier to read, write, and maintain.
The first high-level programming languages were designed in the 1950s. Now there are dozens of different languages, including Ada, Algol, BASIC, COBOL, C, C++, FORTRAN, LISP, Pascal, and Prolog etc.
The father of C language is Dennis Ritchie at Bell Labs. He coded this language during the 1972s. This language has been closely associated with the UNIX operating system for which it was developed – since the system and most of the programs that run it are written in C. It was developed to overcome the problems of previous languages such as B, BCPL etc.
In 1983 a committee was formed by the American National Standards Institute (ANSI) to develop a modern definition for the programming language C (ANSI X3J11). In 1988 they delivered the final standard definition ANSI C.
The standard ANSI C made little changes to the original design of the C language. (They had to make sure that old programs still worked with the new standard). Later on, the ANSI C standard was adopted by the International Standards Organization (ISO). The correct term should therefore be ISO C, but everybody still calls it ANSI C.
The main features of the C language are
Now Let us discuss the program style of the C program. To write the first c program, open the C console and write the following code
#include <stdio.h>
#include <conio.h>
void main()
{
printf(“Hello from PravySoft”);
getch();
}
The explanation of the above program is as follows
#include <stdio.h> includes the standard input-output library functions. The printf() function is defined in stdio.h .
#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.
The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.
The printf() function is used to print data on the console. In this case it will print “Hello from PravySoft”
The getch() function asks for a single character. Until you press any key, it blocks the screen.
C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program. C variable might belong to any of the data types like int, float, char etc. (We will discuss these data types in the following section)
Following are some valid variable names in c language
Myname, pRAVYsOFT, velocity,_value ,flag3
Following are some invalid variable names in the c language
Pravy Soft //space is not permitted
3idiots // don’t start with a digit
love&loveonly // don’t use special character
A scope is a region of a program. Variable Scope is a region in a program where a variable is declared and used. So, we can have three types of scopes depending on the region where these are declared and used
Variables that are declared inside a function or a block are called local variables and are said to have local scope. These local variables can only be used within the function or block in which these are declared. We can use (or access) a local variable only in the block or function in which it is declared. It is invalid outside it. Local variables are created when the control reaches the block or function containing the local variables and then they get destroyed after that.
A complete program is incorporated in section 4.29.1
Variables that are defined outside of all the functions and are accessible throughout the program are global variables and are said to have a global scope. Once declared, these can be accessed and modified by any function in the program. We can have the same name for a local and a global variable but the local variable gets priority inside a function.
A complete program of global variables is incorporated in section 4.29.2
Formal Parameters are the parameters which are used inside the body of a function. Formal parameters are treated as local variables in that function and get priority over the global variables.
Demonstration of formal parameters is incorporated in section 4.29.3.
A constant is a value or an identifier whose value cannot be altered in a program.
For example: 99, 62.5, “I love PravySoft”, etc.
As mentioned, an identifier also can be defined as a constant.
const double PI = 3.14
Here, PI is a constant. Basically what it means is that PI and 3.14 is the same for this program.
Below are the different types of constants you can use in C.
An integer constant is a numeric constant (associated with a number) without any fractional or exponential part. There are three types of integer constants in C programming:
Examples
Decimal constants: 0, -10, 44 etc.
Octal constants: o21, o77, o33 etc.
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc.
In C programming, the octal constant starts with a 0 and the hexadecimal constant starts with a 0x.
A floating point constant is a numeric constant that has either a fractional form or an exponent form.
Examples
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
A character constant is a constant which uses a single quotation around characters. For example: ‘a’, ‘l’, ‘m’, ‘F’
Sometimes, it is necessary to use characters which cannot be typed or have special meaning in C programming.
For example: newline (enter), tab, question mark etc. To use these characters, an escape sequence is used.
For example, \n is used for newline. The backslash (\) causes “escape” from the normal way the characters are interpreted by the compiler.
Table 4.6.1: Escape sequence in C language
Escape Sequences | Character |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Return |
\t | Horizontal tab |
\v | Vertical tab |
\\ | Backslash |
\’ | Single quotation mark |
\” | Double quotation mark |
\? | Question mark |
\0 | Null character |
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
“good” //string constant
“” //null string constant
” ” //string constant of six white space
“x” //string constant having a single character.
“Earth is round\n” //prints string with newline
Keyword enum is used to define enumeration types. For example:
enum colour {yellow, green, black, white};
Here, colour is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators
Table 4. 1 shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20 then
Table 4. 1: Arithmetic operators in C
Operator | Description | Example |
+ | Adds two operands. | A + B = 30 |
− | Subtracts the second operand from the first. | A − B = -10 |
* | Multiplies both operands. | A * B = 200 |
/ | Divides numerator by de-numerator. | B / A = 2 |
% | Modulus Operator and the remainder of after an integer division. | B % A = 0 |
++ | The increment operator increases the integer value by one. | A++ = 11 |
— | The decrement operator decreases the integer value by one. | A– = 9 |
Table 4. 2 shows all the relational operators supported by C. Assume variable A holds 10 and variable B holds 20 then
Table 4. 2: Relational operators in C
Operator | Description | Example |
== | Check if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |
!= | Check if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |
> | Check if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true. | (A > B) is not true. |
< | Check if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true. | (A < B) is true. |
>= | Check if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true. | (A >= B) is not true. |
<= | Check if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true. | (A <= B) is true. |
Table 4. 3 shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then
Table 4. 3: Logical operators in C
Operator | Description | Example |
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. | !(A && B) is true. |
The bitwise operator works on bits and performs bit-by-bit operations.
Table 4. 4 lists the bitwise operators supported by C. Assume variable ‘A’ holds 60 and variable ‘B’ holds 13, then
Table 4. 4: Bitwise operators supported by C
Operator | Description | Example |
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, i.e., 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, i.e., 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, i.e., 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. | (~A ) = -61, i.e,. 1100 0011 in 2’s complement form. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 i.e., 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 i.e., 0000 1111 |
The truth tables for &, |, and ^ is as follows
Table 4. 5: Bitwise operations in C
p | q | p & q | p | q | p ^ q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Assume A = 60 and B = 13 in binary format, they will be as follows
A = 0011 1100
B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the assignment operators supported by the C language −
Table 4. 6: Assignment operators supported by the C
Operator | Description | Example |
= | Simple assignment operator. Assigns values from right-side operands to left-side operands | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C – A |
*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |
^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |
Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.
We will discuss conditional statements in section 4.11.3 of this book.
Table 4. 7 Misc. Operators in C
Operator | Description | Example |
sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 4.(in latest systems integer size is 4 byte not 2 byte) |
& | Returns the address of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; |
? : | Conditional Expression. | If Condition is true? then value X: Otherwise value Y |
Operator precedence describes the order in which C reads expressions. For example, the expression y=m*2+c contains two operations, an addition and a multiplication. Does the C compiler evaluate 2+c first, then multiply the result by m, or does it evaluate m*2 first, then add c to the result?
The operator precedence chart (Table 4. 8) contains the answers. Operators higher in the chart have a higher precedence, meaning that the C compiler evaluates them first.
Operators on the same line in the chart have the same precedence, and the “Associativity” column on the right gives their evaluation order.
In C, precedence of arithmetic operators ( *, %, /, +, -) is higher than relational operators(==, !=, >, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).
Example of precedence
(1 > 2 + 3 && 4)
This expression is equivalent to:
((1 > (2 + 3)) && 4)
i.e., (2 + 3) executes first resulting into 5
Then, first part of the expression (1 > 5) executes resulting into 0 (false)
Then, (0 && 4) executes resulting into 0 (false)
Output
0
If two operators of the same precedence (priority) is present in an expression, the Associativity of operators indicates the order in which they execute.
Example of associativity
1 == 2 != 3
Here, operators == and != have the same precedence. The associativity of both == and != is left to right, i.e., the expression on the left is executed first and moves towards the right.
Thus, the expression above is equivalent to:
((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false)
Then, (0 != 3) executes resulting into 1 (true)
Output
1
1) Associativity is only used when there are two or more operators of the same precedence. The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated.
2) All operators with the same precedence have the same associativity
This is necessary, otherwise, there won’t be any way for the compiler to decide the evaluation order of expressions which have two operators of the same precedence and different associativity. For example + and – have the same associativity.
3) Precedence and associativity of postfix ++ and prefix ++ are different
The precedence of postfix ++ is more than prefix ++, their associativity is also different. The associativity of postfix ++ is left to right and the associativity of prefix ++.
4) Comma has the least precedence among all operators and should be used carefully For example consider the following statement, the output is 1.
a = 1, 2, 3; // Evaluated as (a = 1), 2, 3
5) There is no chaining of comparison operators in C
In Python, an expression like “c > b > a” is treated as “a > b and b > c”, but this type of chaining doesn’t happen in C.
For example consider the values of a, b and c are 10, 20, 30 respectively. The output of the following statement will be “FALSE”.
if (c > b > a)
printf(“TRUE”);
else
printf(“FALSE”);
(c > b > a) is treated as ((c > b) > a), associativity of ‘>’ is left to right. Therefore the value becomes ((30 > 20) > 10) which becomes (1 > 20)
Table 4. 8 shows all operators in C with precedence and associativity. The precedence of operators decreases from top to bottom in the given table.
Table 4. 8: Summary of C operators with precedence and associativity
Operator | Meaning of operator | Associativity |
() [] -> . | Functional call Array element reference Indirect member selection Direct member selection | Left to right |
! ~ + – ++ — & * sizeof (type) | Logical negation Bitwise(1 ‘s) complement Unary plus Unary minus Increment Pointer reference Returns the size of an object Type cast(conversion) | Right to left |
* / % | Multiply Divide Remainder | Left to right |
+ – | Binary plus(Addition) Binary minus(subtraction) | Left to right |
<< >> | Left shift Right shift | Left to right |
< <= > >= | Less than Less than or equal Greater than Greater than or equal | Left to right |
== != | Equal to Not equal to | Left to right |
& | Bitwise AND | Left to right |
^ | Bitwise exclusive OR | Left to right |
| | Bitwise OR | Left to right |
&& | Logical AND | Left to right |
|| | Logical OR | Left to right |
?: | Conditional Operator | Right to left |
= *= /= %= -= &= ^= |= <<= >>= | Simple assignment Assign product Assign quotient Assign remainder Assign sum Assign difference Assign bitwise AND Assign bitwise XOR Assign bitwise OR Assign left shift Assign right shift | Right to left |
, | Separator of expressions | Left to right |
Let variables a, b, c and d contain the values 20, 10, 15 and 5 then what will be the answers for the following expressions?
Solution
Answers are commented (//) as shown below
e = (a + b) * c / d; // (30 * 15 ) / 5=90
e = ((a + b) * c) / d; // (30 * 15) / 5=90
e = (a + b) * (c / d); // (30) * (15/5) =90
e = a + (b * c) / d; // 20 + (150/5)=50
END OF THE PART-4 PREVIEW
For full self-study materials, please visit
https://pravysoft.org/eduserver/course-category/hsst-cs-kerala-psc/
After conducting a comprehensive survey of businesses operating in the UAE, we have compiled a…
Flutter Development in Calicut: Why PravySoft is Your Best Choice In today’s fast-paced digital world,…
Are you exploring modern web development tools to build a stunning website for your business…
Question Paper Code: 98/2011Exam: HSST Computer ScienceCategory No: 111/2010Date of Test: 01-09-2011Alpha code: B 1.Which…
For simplicity, we can start with an example. Suppose you have a dataset of 100…
PravySoft Calicut: Your Top Choice for Apple App Developers in Calicut In the vibrant city…