PPS Module-2
PPS Module-2
of
“C” language
What is C Language
The C Language is developed by Dennis Ritchie for creating system applications that directly
interact with the hardware devices such as drivers, kernels, etc.
C programming is considered as the base for other programming languages, that is why it is
known as mother language.
1. Mother language
2. System programming language
3. Procedure-oriented programming language
4. Structured programming language
5. Mid-level programming language
History of C Language
History of C language is interesting to know. Here we are going to discuss a brief history
of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
1. Alphabets
2. Digits
3. Special Characters
4. White Spaces
Character Set of “C” Language
Here is a table that represents all the types of character sets that we can use in the C language
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T,
U, V, W, X, Y, Z
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Some ANSI C Trigraph Sequences are shown the table next slide
ANSI C Trigraph Sequences
Keywords
C reserves a set of 32 words for its own use.
These words are called keywords (or reserved words), and each of these keywords has a special
meaning within the C language.
Identifiers
Identifiers are names that are given to various user defined program elements, such as variable,
function and arrays.
Constants
Constants refer to fixed values that do not change during execution of program.
C Tokens
The smallest individual unit of a program is known as token.
C has the following tokens:
Strings
Special Symbols
Symbols such as #, &, =, * are used in C for some specific function are called as special symbols.
Operators
An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.
Keywords
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler. Keywords are part of the syntax and they cannot be used
as an identifier
Identifiers
Identifier refers to name given to entities such as variables, functions, structures
etc.
For example:
int money;
double accountBalance;
Rules for Naming Identifiers
The first character of an identifier should be either an alphabet or an underscore,
and then it can be followed by any of the character, digit, or underscore.
It should not begin with any numerical digit.
In identifiers, both uppercase and lowercase letters are distinct. Therefore, we
can say that identifiers are case sensitive.
Commas or blank spaces cannot be specified within an identifier.
Keywords cannot be represented as an identifier.
The length of the identifiers should not be more than 31 characters.
Identifiers should be written in such a way that it is meaningful, short, and easy
to read.
Example of identifiers
1. Internal identifiers
2. External identifiers
Internal Identifier
If the identifier is not used in the external linkage, then it is known as an internal
identifier. The internal identifiers can be local variables.
External Identifier
Keyword Identifier
type variable_list;
The example of declaring the variable is given below:
int a;
float b;
char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
void main()
{
int x=10; //local variable (also automatic)
auto int y=20; //automatic variable
}
External Variables in C
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}
Data Types
Data types are defined as the data storage format that a variable can store a data.
It determines the type and size of data associated with variables.
Data types in C
void:
The void type has no value therefore we cannot declare it as variable as we did in case of int or
float or char.
The void data type is used to indicate that function is not returning anything.
Secondary Data Type
Secondary data types are not directly supported by the machine.
It is combination of primary data types to handle real life data in more convenient
way.
It can be further divided in two categories,
Derived data types: Derived data type is extension of primary data type. It is built-in system
and its structure cannot be changed. Examples: Array and Pointer.
Array: An array is a fixed-size sequenced collection of elements of the same data type.
Pointer: Pointer is a special variable which contains memory address of another variable.
User defined data types: User defined data type can be created by programmer using
combination of primary data type and/or derived data type. Examples: Structure, Union,
Enum.
Structure: Structure is a collection of logically related data items of different data types grouped
together under a single name.
Union: Union is like a structure, except that each element shares the common memory.
Enum: Enum is used to assign names to integral constants, the names make a program easy to read
and maintain.
There are the following data types in C language.
%X It is used to print the hexadecimal unsigned integer, but %X prints the alphabetical
characters in uppercase such as A, B, C, etc.
Format Specifiers in C
The Format specifier is a string used in the formatted input and output functions.
The format string determines the format of the input and output. T
he format string always starts with a '%' character.
%g It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the
value after the decimal in input would be exactly the same as the value in the output.
The full form of ASCII is the American Standard Code for information interchange.
It is a character encoding scheme used for electronics communication.
Each character or a special character is represented by some ASCII code, and each ascii
code occupies 7 bits in memory.
In C programming language, a character variable does not contain a character value itself
rather the ascii value of the character variable.
The ascii value represents the character variable in numbers, and each character variable
is assigned with some number range from 0 to 127.
For example, the ascii value of 'A' is 65.
In the above example, we assign 'A' to the character variable whose ascii value is 65, so 65
will be stored in the character variable rather than 'A'.
ASCII value in C
What is ASCII code?
The full form of ASCII is the American Standard Code for information interchange.
It is a character encoding scheme used for electronics communication.
Each character or a special character is represented by some ASCII code, and each ascii
code occupies 7 bits in memory.
In C programming language, a character variable does not contain a character value itself
rather the ascii value of the character variable.
The ascii value represents the character variable in numbers, and each character variable
is assigned with some number range from 0 to 127.
For example, the ascii value of 'A' is 65.
In the above example, we assign 'A' to the character variable whose ascii value is 65, so 65
will be stored in the character variable rather than 'A'.
Let's understand through an example. In the above code, the first user will
We will create a program which will display the ascii value of give the character input, and the
the character variable. input will get stored in the 'ch'
variable.
#include <stdio.h>
int main() If we print the value of the 'ch'
{ variable by using %c format specifier,
char ch; // variable declaration then it will display 'A' because we
printf("Enter a character"); have given the character input as 'A‘.
scanf("%c",&ch); // user input
printf("\n The ascii value of the ch variable is : %d", ch); If we use the %d format specifier
return 0; then its ascii value will be displayed,
} i.e., 65.
Constant Example
1) const keyword
Syntax:
#define token value
𝑥=5 𝑎=𝑏+𝑐
Special operators ()
Arithmetic Operators
Arithmetic operators are used for mathematical calculation.
Operator Meaning
&& logical AND (Both non zero then true, either is zero then false)
|| logical OR (Both zero then false, either is non zero then true)
! Is greater than
a b a&&b a||b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Assignment Operators
Assignment operators (=) is used to assign the result of an expression to a variable.
Assignment operator stores a value in memory.
C also supports shorthand assignment operators which simplify operation with
assignment.
Operator Meaning
= Assigns value of right side to left side
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a - 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1
Increment and Decrement Operators
Increment (++) operator used to increase the value of the variable by one.
Decrement (--) operator used to decrease the value of the variable by one.
Example Explanation
x=100; After the execution the
x++; value of x will be 101.
Example Explanation
x=100; After the execution the
x--; value of x will be 99.
Increment and Decrement Operators (cont…)
Operator Description
Pre increment operator (++x) value of x is incremented before assigning it to the variable
on the left
Operator Description
Post increment operator (x++) value of x is incremented after assigning it to the variable on
the left
Example
Example
m=2, n=3;
m=2, n=3; r=(m<n) ? m : n;
r=(m>n) ? m : n;
Explanation
Explanation
Value of r will be 2
Value of r will be 3
Bitwise Operators
Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left (shift left means multiply by 2)
>> shift right (shift right means divide by 2)
Bitwise Operators
8 = 1000 (In Binary) and 6 = 0110 (In Binary)
Example: Bitwise << (Shift Left) Example: Bitwise >> (Shift Right)
int a=8, b; int a=8, b;
b = a << 1; b = a >> 1;
printf("Output = %d", b); printf("Output = %d", b);
Output Output
16 (multiplying a by a power of two) 4 (dividing a by a power of two)
Special Operators
Operator Meaning
* Pointer operator, it is used to declare pointer variable and to get value from it.
answer = a + b * c;
When there are multiple operators in an expression, they are evaluated according
to their precedence and associativity.
Operator precedence
Precedence of an operator is its priority in an expression for evaluation.
The operator with higher precedence is evaluated first and the operator with the
least precedence is evaluated last.
We say that the multiplication operator (*) has higher "precedence" or "priority"
than the addition operator (+), so the multiplication must be performed first.
Operator associativity
Associativity is the left-to-right or right-to-left order for grouping operands to
operators that have the same precedence.
We say that the subtraction operator (-) is "left associative", so the left subtraction
must be performed first.
int a = 20;
double b = 20.5;
printf("%lf", a + b);
Output
40.500000
Explicit Type conversion
These conversions are done explicitly by users using the pre-defined functions.
printf(“ ”);
printf(“%d”, c); // 15
scanf() function is used to read character, string, numeric data from keyboard
Syntax of scanf
scanf("%X", &variable);
where %X is the format specifier which tells the compiler what type of data is in a
variable.
& refers to address of “variable” which is directing the input value to a address
returned by &variable.
scanf() Function
Output
Enter a character: a
Entered character is: a
gets and puts
Program
gets function reads a line from stdin
into the buffer pointed to by s until 1
2
#include <stdio.h>
void main( )
either a terminating newline or EOF 3 {
(End of File) occurs. 4 /*Character array of length 100*/
5 char str[100];
6 printf("Enter a string: ");
puts function writes the string 's' 7 /* Take a string as input */
and 'a' trailing newline to stdout. 8 gets( str );
9 /* Display the string */
10 printf("Entered string is: ");
11 puts( str );
12 }
Output
Enter a string: india
Entered string is: india
Preprocessor
Preprocessors are programs that process our source code before compilation.
There are a number of steps involved between writing a program and executing a
program in C.
Let us have a look at these steps before we actually start learning about
Preprocessors.
C Program
Object Executable
Are there No Code Code
preprocessor Compiler Linker
directive
Yes
Macros
File inclusion
Conditional compilation
Other directives
Macro
A macro is a fragment of code which has been given a name. Whenever the name
is used in program, it is replaced by the contents of the macro.
Macro definitions are not variables and cannot be changed by your program code
like variables.
Function-like Macros
Macro
Description Object-like Macros Function-like Macros
Definition The object-like macro is an identifier that The function-like macro looks like
is replaced by value. function call.
Use It is used to represent numeric constants. It is used to represent function.
Syntax #define CNAME value #define CNAME (expression)
Example #define PI 3.14 #define MIN(a,b) ((a)<(b)?(a):(b))
Program 1 #include <stdio.h> 1 #include <stdio.h>
2 #define PI 3.14 2 #define MIN(a,b) ((a)<(b)?(a):(b))
3 void main() 3 void main()
4 { int r=2; 4 {
5 float a; 5 printf("%d", MIN(2, 5));
6 a=PI*r*r; 6 }
7 printf("%f", a);
8 }