Professional Documents
Culture Documents
1
Introduction to C Programming & components (CO2) Features of C(CO2)
Stable language.
Features of C
Introduction to C Programming Simple Language.
Extensible Language
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 7 11/12/2022 10
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 8 11/12/2022 11
ANSI/ISO 1990 ANSI standard adopted by ISO and known as C89. Digits(0-9)
C
C95 1995 Some minor changes made in C89
Special
C99 1999 Significant changes were made to C95. Symbols(@,.,;,:)
Operators(+,-,/,%)
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 9 11/12/2022 12
2
C Tokens (CO2) Identifiers (CO2)
Declaration of variable:
• datatype var_name;
• e.g. int x;
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 13 11/12/2022 16
3
Rules for Naming Identifiers (CO2) Real or Floating Constants(CO2)
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 19 11/12/2022 22
known as
Character constants
literals. A character constant consists of a single character enclosed in
single quotes.
Types of For example ‘a’ , ‘D’ and ‘@’.
Constants are constants: Characters are stored using machine characters sets using ASCII
values that codes.
can’t change
during the Character Integer Floating Strings All escapes sequences are also characters sequences.
execution. constants constants constants constants
String constants
A string constant is a sequence of characters enclosed in double
quotes.
For example “a”, “ABC”.
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 20 11/12/2022 23
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 21 11/12/2022 24
4
Type of Operators(CO2) Characteristics of escape sequences(CO2)
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 25 11/12/2022 28
Documentation Section
An escape sequence is a sequence of characters that does not
represent itself when used inside a character or string literal, Preprocessor Directives
but is translated into another character. Global Declaration Section
void main()
used in formatting the output in output functions. {
Local Declaration Section
Statements
It represents only one character. However, it consists of two
characters. }
Other functions as required
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 26 11/12/2022 29
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 27 11/12/2022 30
5
Fundamental Data types(CO2) Data Type(CO2)
enum, typedef
User Defined data
,structure,
types
union
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 31 11/12/2022 34
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 33 11/12/2022 36
6
Primary Data Types(CO2) Explicit Type Conversion or Type Casting(CO2)
Basic DATA DATA TYPE with type FORMAT SIZE RANGE
TYPE modifier SPECIFIER
long 4 Byte -2,147,483,648 to
long int %ld 2147,483,647
x=(int) 8.5 • 8.5 is converted to integer 8 by
int long signed int %li truncation
unsigned long %lu 4 Byte 0 to 4,294,967,295
long unsigned int
a = (int) 23.6/(int)7.2 • Evaluated as 23/7 and the result
float %f 4 Byte +(1.17*10-38 to 3.4*1038 ) would be 3
float %e or %E And
%g or %G -(1.17*1038 to 3.4*1038 )
b= (double)sum/n • Division is done in floating point
mode
double %lf 8 Byte +(2.22*10-308 to 1.79*10308)
And
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
float z;
OUTPUT :
printf("x = %d, z = %f", x, z);
x = 107, z = 108.000000
return 0;
}
7
Implicit Type Conversion(CO2) Standard I/O(CO2)
x=(int) 8.5 • 8.5 is converted to integer 8 by • putchar() function can be used to display a single character
truncation on the screen
Syntax:
a = (int) 23.6/(int)7.2 • Evaluated as 23/7 and the result
would be 3 putchar(var_name);
8
Standard I/O(CO2) Errors(CO2)
scanf(“control string”,&arg1,&arg2..);
void main()
Example: {
printf(“ value of x floataverage(floata,floatb)
int a;
#include<stdio.h> %d”,x): {
printf("type an integer");
void main() { /* x is not declared and return a+b/2;
int var1; semicolon missing*/ scanf("%d", a);.
/* should be (a + b) / 2 */
scanf("%d ", &var1, ); } }
}
11/12/2022 Devanshu Dube BCA 103 Unit 1 49 11/12/2022 Devanshu Dube BCA 103 Unit 1 52
Syntax:
printf(“control string”,arg1,arg2..);
Source code Object code Executable Code
Example:
• The input program of • The output program • Executable code is
compiler is known as of compiler is generated after
#include<stdio.h>
source code. known as object linking all the object
void main() { code module after
• It can be in any high
int var1= 60; compilation.
level programming • It is machine
printf("%d ", var1, ); language language code.
} • It is human readable • It is machine
code. readable code
11/12/2022 Devanshu Dube BCA 103 Unit 1 50 11/12/2022 Devanshu Dube BCA 103 Unit 1 53
11/12/2022 Devanshu Dube BCA 103 Unit 1 51 11/12/2022 Devanshu Dube BCA 103 Unit 1 54
9
Variables and Memory allocation(CO2) Program(CO2)
/* Area of Circle*/
#include<stdio.h>
A variable is a name which is assigned to a data value at storage #include<conio.h>
location in computer’s memory. void main()
{
OUTPUT:
float r, area;
Enter radius:
Variables are used to store various kinds of data during program clrscr(); 2
executions. printf(“enter radius\n”); area of circle is =6.28
scanf(“%f”,&r);
area=22*r*r/7;
Variables name assigned before it use and it can be letter , digit printf(“area of circle is=%f”,area);
and an underscore(_). getch();
}
11/12/2022 Devanshu Dube BCA 103 Unit 1 55 11/12/2022 Devanshu Dube BCA 103 Unit 1 58
Programs(CO2) Program(CO2)
10
Program(CO2) Type of operators(CO2)
Multiply * a*b 27
Other
Increment Bitwise operator(con
/Decrement Assignment Divide / a/b 3
Operators Operators ditional ,
operators comma and
size) Addition + a+b 12
Operator
precedence Operator quiz Subtraction - a-b 6
and
Precedence quiz Modulus % a%b 0
associativity
Divide / a/b -3
An operator is a symbol that specifies the mathematical, logical
or relational operation to be performed.
Modulus % a%b -1
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 63 11/12/2022 66
11
Relational operators(CO2) Assignment operators(CO2)
Greater than > 7>9 0 Assignment = Assign values to the left variable a=6
LESS THAN OR EQUAL TO <= 100<=100 1
Not Equal to != 20!=20 0 6 is rvalue can be variable ,constants and constant expression.
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 67 11/12/2022 70
Bitwise OR | 5|6 7
Logical OR || (6>5) || (7>9) 1
Bitwise XOR ^ 5^6 3
Logical NOT ! !(6>5) 1
Bitwise Left Shift << 5<<1 10
Note1: If first operand in False in case of logical AND operation than second
operand will not evaluated Bitwise Right Shift >> 5>>1 2
Note2: If first operand in True in case of logical OR operation than second
operand will not evaluated Bitwise Not /One’s Complement ~ ~6 -7
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 68 11/12/2022 71
Increment operator ++ Add one to the current value a++/++a Bitwise AND & 5&6 4
The result of AND is 1 only if both bits are 1
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 69 11/12/2022 72
12
More Bitwise Operators(CO2) More Bitwise Operators(CO2)
Let us suppose the bitwise AND operation of two integers 12 and 25.
Result: 3--------0000000000000011
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 73 11/12/2022 76
Result: 7--------0000000000000111
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 74 11/12/2022 77
Result: 10--------0000000000001010
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 75 11/12/2022 78
13
More Bitwise Operators(CO2) More Bitwise Operators(CO2)
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 79 11/12/2022 82
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 80 11/12/2022 83
int main()
{
int x = 19; Condition ?: exp1?exp2:exp3 12>6?1:0 1
printf("x << 1 = %d\n", x << 1);
printf("x >> 1 = %d\n", x >> 1);
return 0;
} Note: if exp1 is true then exp2 will evaluates
if exp1 is false then exp3 will evaluates
Output:
x << 1 = 38
x >> 1 = 9
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 81 11/12/2022 84
14
sizeof() operators(CO2) Comma operator(CO2)
Comma Operator (,)
Using Comma Operator along with Assignment
Operation Operator Example Result #include<stdio.h>
int main()
{
Determines memory size size() size(int) 2 int i;
size(5) 2 i = 1,2,3;
printf("i:%d\n",i);
size(5+6) 2 return 0;
}
Note: Evaluates the memory size of variable, constant and Output :
expression passed to the operator. i:1
Explanation :
i = 1,2,3;
Above Expression contain 3 comma operator and 1 assignment operator.
If we check precedence table then we can say that “Comma” operator has lowest
precedence than assignment operator
So Assignment statement will be executed first .
Devanshu Dube BCA 103 Unit 1 1 is assigned to variable “i”.
11/12/2022 85
11/12/2022 Devanshu Dube BCA 103 Unit 1 88
Comma operator(CO2)
• Comma Operator (,)
– Comma Operator has Lowest Precedence i.e it is having lowest priority so it
is evaluated at last.
– Comma operator returns the value of the rightmost operand when multiple
comma operators are used inside an expression.
– Comma Operator Can acts as –
• Operator : In the Expression
• Separator : Declaring Variable , In Function Call Parameter List
11/12/2022 Devanshu Dube BCA 103 Unit 1 87 11/12/2022 Devanshu Dube BCA 103 Unit 1 90
15
Operator Precedence and Associativity(CO2) Operator Precedence and Associativity(CO2)
Operator Description Associativity Precedence Level
+ Addition operator
Left-to-right 4
- Subtraction operator
• Operator Precedence >> Right shift operator Left-to-right
5
<< Left shift operator
< Less than
<= Less than or equal operator
Left-to-right 6
and >
>=
Greater than
Greater than or equal to
== Equal to
Left-to-right 7
!= Not equal to
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 91 11/12/2022 94
*Note 1:
Parentheses are also used to group sub-expressions to force a
different precedence; such parenthetical expressions can be nested
The associativity defines the direction ,left to right or right to and are evaluated from inner to outer.
**Note 2:
left , in which the operator acts upon its operands Postfix increment/decrement have high precedence, but the actual
increment or decrement of the operand is delayed (to be
accomplished sometime before the statement completes execution).
So in the statement y = x * z++; the current value of z is used to
evaluate the expression (i.e., z++ evaluates to z) and z only
Devanshu Dube BCA 103 Unit 1 incrementedDevanshu
after allDube
else isBCA
done.
103 Unit 1
11/12/2022 92 11/12/2022 95
16
Operator Precedence and Associativity(CO2)
• Example:
y=12*3%5-6+7/2
y=12*3%5-6+7/2
y=36%5-6+7/2
y=1-6+7/2
y=-5+7/2
y=-5+3
y=-2
Devanshu Dube BCA 103 Unit 1 Devanshu Dube BCA 103 Unit 1
11/12/2022 97 11/12/2022 100
Summary
• scanf() and printf() are the library functions are used for input
and output resp.
References
• The C programming by Kernighan Brain W. and Ritchie Dennis
M., Pearson Education .
• Computer Basics and C Programming by V.Rajaraman , PHI
Learning Pvt. Limited – 2015.
• Computer Concepts and Programming in C by Vikas Gupta,
Wiley India Publication
• Computer Fundamentals and Programming in C. Reema
Thareja, Oxford Publication
• Computer Concepts and Programming in C, E Balaguruswami,
McGraw Hill
17