Unit 1
Unit 1
History of C language
C language has evolved from three different structured language ALGOL, BCPL and B
Language. It uses many concepts from these languages while introduced many new concepts
such as datatypes, struct, pointer etc. In 1988, the language was formalised by American
National Standard Institute(ANSI). In 1990, a version of C language was approved by
the International Standard Organisation(ISO) and that version of C is also referred to as
C89.
The idea behind creating C language was to create an easy language which requires a simple compiler
and enables programmers to efficiently interact with the machine/system, just like machine
instructions. C language compiler converts the readable C language program into machine instruction
1 | Page By SwatiPhalak
BCA-104 Programming in C
2 | Page By SwatiPhalak
BCA-104 Programming in C
3 | Page By SwatiPhalak
BCA-104 Programming in C
Character set
A character set is a set of alphabets, letters and some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
0123456789
Special Characters
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
C 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. For
example:
1. int money;
4 | Page By SwatiPhalak
BCA-104 Programming in C
Here, int is a keyword that indicates money is a variable of type int (integer).
As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of
all keywords allowed in ANSI C.
Do if static while
C Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it
during the execution of the program. For example:
1. int money;
2. double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different from keywords. You cannot use int as an
identifier because int is a keyword.
Rules for naming identifiers
1. An Identifier can only have alphanumeric characters (a-z , A-Z , 0-9) and
underscore(_).
5 | Page By SwatiPhalak
BCA-104 Programming in C
2. The first character of an identifier can only contain alphabet(a-z , A-Z) or
underscore (_).
3. Identifiers are also case sensitive in C. For example name and Name are two
different identifiers in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters, such as semicolon, period, whitespaces, slash or comma are
permitted to be used in or as Identifier.
6 | Page By SwatiPhalak
BCA-104 Programming in C
Character type
Character types are used to store characters value.
Size and range of Integer type on 16-bit machine
Constants
If you want to define a variable whose value cannot be changed, you can use
the const keyword. This will create a constant. For example,
1. const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be changed.
1. const double PI = 3.14;
2. PI = 2.9; //Error
7 | Page By SwatiPhalak
BCA-104 Programming in C
Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location. For example:
1. int playerScore = 95;
Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.
The value of a variable can be changed, hence the name variable.
1. char ch = 'a';
2. // some code
3. ch = 'l';
Rules for naming a variable
1. A variable name can have only letters (both uppercase and lowercase letters), digits and
underscore.
2. The first letter of a variable should be either a letter or an underscore.
3. The length variable name can be upto 32 character
Note: You should always try to give meaningful names to variables. For
example: firstName is a better variable name than fn.
C is a strongly typed language. This means that the variable type cannot be changed once it
is declared. For example:
1. int number = 5; // integer variable
2. number = 5.5; // error
3. double number; // error
Assignment Statement:
C provides an assignment operator for this purpose, assigning the value to a variable using
assignment operator is known as an assignment statement in C.
The function of this operator is to assign the values or values in variables on right hand side
of an expression to variables on the left hand side.
The syntax of the assignment expression
Variable = constant / variable/ expression;
The data type of the variable on left hand side should match the data type of
constant/variable/expression on right hand side with a few exceptions where automatic type
conversions are possible.
Examples of assignment statements,
8 | Page By SwatiPhalak
BCA-104 Programming in C
b = c ; /* b is assigned the value of c */
a = 9 ; /* a is assigned the value 9*/
b = c+5; /* b is assigned the value of expr c+5 */
9 | Page By SwatiPhalak
BCA-104 Programming in C
Input / Output
Formatted Unformatted
Input Output Input Output
getch() putch()
getche() putchar(),
scanf() printf()
getchar() puts().
gets()
Format Specifier:
Type Format specifier
Integer %d
Float %f
Char %c
Double %lf
Unsigned %u
Long %l
String %s
Octal %o
Hexadecimal %x
Formatted Input/Output Function:
The printf() and scanf() functions are inbuilt library functions declared and defined in
“stdio.h” header file. printf() is used to display the output and scanf() is used to read the
inputs.
1. printf() Function In C Language:
10 | Page By SwatiPhalak
BCA-104 Programming in C
The printf() function is used for output. It prints the given statement to the console.
Syntax: printf("format string",argument_list);
11 | Page By SwatiPhalak
BCA-104 Programming in C
getch() Function:
The getch() function reads the alphanumeric character input from the user. But, that the
entered character will not be displayed.
getch.c
Output:
Hello, press any alphanumeric character to exit
getche() Function
getche() function reads the alphanumeric character from the user input. Here, character you
entered will be echoed to the user until he/she presses any key.
getche() C Program
getche.c
Output:
Hello, press any alphanumeric character to exit
K
getchar() Function
12 | Page By SwatiPhalak
BCA-104 Programming in C
The getchar() function reads character type data form the input. The getchar() function reads
one character at a time till the user presses the enter key.
getchar() C Program
getchar.c
#include <stdio.h>
void main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("\nEntered character : %c ", c);
}
Output:
Enter a character : x
Entered character : x
gets() Function
The gets() function can read a full string even blank spaces presents in a string. But, the
scanf() function leave a string after blank space space is detected. The gets() function is used
to get any string from the user.
gets() C Program
gets.c
Output:
13 | Page By SwatiPhalak
BCA-104 Programming in C
Enter String : C Language
C Language is awesome
putch() Function
The putch() function prints any alphanumeric character.
putch() C Program
putch.c
Output:
Press any key to continue
Input : d
putchar() Function
putchar() function prints only one character at a time.
putchar() C Program
putchar.c
14 | Page By SwatiPhalak
BCA-104 Programming in C
K
Note:
Here, variable c is assigned to a character 'K'. The variable c is displayed by the putchar().
Use Single quotation mark ' ' for a character.
Operator Description
15 | Page By SwatiPhalak
BCA-104 Programming in C
% remainder of division
2. Increment/decrement operator
Operator Description
> Check if operand on the left is greater than operand on the right
4. Logical operators
C language supports following 3 logical operators. Suppose a = 1 and b = 0,
|| Logical OR (a || b) is true
5. Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bits from right to left. Bitwise operators are not applied to float or double
(These are datatypes, we will learn about them in the next tutorial).
Operator Description
16 | Page By SwatiPhalak
BCA-104 Programming in C
| Bitwise OR
^ Bitwise exclusive OR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
The bitwise shift operator, shifts the bit value. The left operand specifies the value to be
shifted and the right operand specifies the number of positions that the bits in the value have
to be shifted. Both operands have the same precedence.
Example : 1
a=8 and b=3
a= 0 0 0 1 0 0 0
a<<b 1 0 0 0 0 0 0
17 | Page By SwatiPhalak
BCA-104 Programming in C
a=11 and b=2
a>>b 0 0 0 0 0 1 0
6. Assignment Operators
= assigns values from right side operands to left side operand a=b
+= adds right operand to the left operand and assign the result to a+=b is same as
left a=a+b
-= subtracts right operand from the left operand and assign the a-=b is same as
result to left operand a=a-b
*= mutiply left operand with the right operand and assign the a*=b is same as
result to left operand a=a*b
/= divides left operand with the right operand and assign the a/=b is same as
result to left operand a=a/b
%= calculate modulus using two operands and assign the result to a%=b is same as
left operand a=a%b
7. Conditional operator
The conditional operators in C language are known by two more names
1. Ternary Operator
2. ? : Operator
It is actually the if condition that we use in C language decision making, but using
conditional operator, we turn the ifcondition statement into a short and simple operator.
18 | Page By SwatiPhalak
BCA-104 Programming in C
Explanation:
● The question mark "?" in the syntax represents the if part.
● The first expression (expression 1) generally returns either true or false, based on
which it is decided whether (expression 2) will be executed or (expression 3)
● If (expression 1) returns true then the expression on the left side of " : " i.e
(expression 2) is executed.
● If (expression 1) returns false then the expression on the right side of " : " i.e
(expression 3) is executed.
8. Special operator:
& Returns the address of an variable &x ; return address of the variable x
Precedence of operators
If more than one operators are involved in an expression, C language has a predefined rule of
priority for the operators. This rule of priority of operators is called operator precedence.
Example of precedence
19 | Page By SwatiPhalak
BCA-104 Programming in C
(1 > 2 + 3 && 4)
then, first part of the expression (1 > 5) executes resulting into 0 (false)
Output is 0
Associativity of operators
Example of associativity
1 == 2 != 3
((1 == 2) != 3)
Output is 1
The table below shows all the operators in C with precedence and associativity.
20 | Page By SwatiPhalak
BCA-104 Programming in C
Associativit
Operator Meaning of operator y
() Functional call
[] Array element reference
-> Indirect member selection
. Direct member selection Left to right
Logical negation
! Bitwise(1 's) complement
~ Unary plus
+ Unary minus
- Increment
++ Decrement
-- Dereference
& Operator(Address)
* Pointer reference
sizeof Returns the size of an object
(type) Type cast(conversion) Right to left
* Multiply
/ Divide
% Remainder Left to right
+ Binary plus(Addition)
- Binary minus(subtraction) Left to right
== Equal to
!= Not equal to Left to right
21 | Page By SwatiPhalak
BCA-104 Programming in C
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
Printf function
When the c program needs to communicate with the user, We should use printf
function.
22 | Page By SwatiPhalak
BCA-104 Programming in C
So if we want to use the printf function in our c program, we should
include stdio.h header file.
Syntax of printf
printf("");
It will print characters to the screen which is given inside the double quotes "" .
And it should end up with a semicolon ;.
Example
printf("Hello World");
printf("Bye");
Sample Program
Example
return 0;
}
23 | Page By SwatiPhalak
BCA-104 Programming in C
Run it
Example
#include<stdio.h>
int main()
{
printf("Correct me!)
return 0;
}
Run it
24 | Page By SwatiPhalak