Professional Documents
Culture Documents
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Learning Objectives
Ref. Page 395 Chapter 21: Introduction to C Programming Language Slide 2/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Features
Ref. Page 395 Chapter 21: Introduction to C Programming Language Slide 3/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
C Character Set
Uppercase alphabets A, B, C, …, Z 26
Lowercase alphabets a, b, c, …, z 26
Digits 0, 1, 2, …, 9 10
~ `! @ # % ^ & * ( ) _ −
Special characters 31
+ = | \ {}[ ]:; " ' < > , . ? /
93
Ref. Page 396 Chapter 21: Introduction to C Programming Language Slide 4/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Constants
Ref. Page 396 Chapter 21: Introduction to C Programming Language Slide 5/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 397 Chapter 21: Introduction to C Programming Language Slide 6/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 397 Chapter 21: Introduction to C Programming Language Slide 7/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 397 Chapter 21: Introduction to C Programming Language Slide 8/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Variables
Ref. Page 398 Chapter 21: Introduction to C Programming Language Slide 9/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 398 Chapter 21: Introduction to C Programming Language Slide 10/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 399 Chapter 21: Introduction to C Programming Language Slide 11/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
int count;
short index;
long principle;
float area;
double radius;
char c;
Ref. Page 399 Chapter 21: Introduction to C Programming Language Slide 12/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Standard Qualifiers in C
Sign signed + or –
unsigned + only
Ref. Page 399 Chapter 21: Introduction to C Programming Language Slide 13/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Lifetime and Visibility Scopes of
Variables
Ref. Page 399 Chapter 21: Introduction to C Programming Language Slide 14/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Keywords
Ref. Page 400 Chapter 21: Introduction to C Programming Language Slide 15/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Comments
Ref. Page 400 Chapter 21: Introduction to C Programming Language Slide 16/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Operators
Ref. Page 401 Chapter 21: Introduction to C Programming Language Slide 17/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Arithmetic Operators
Arithmetic Operators
+ Addition; x+y L→R 4
- Subtraction; x-y L→R 4
∗ Multiplication; x∗y L→R 3
/ Division; x/y L→R 3
% Remainder (or Modulus); x%y L→R 3
++ Increment;
x++ means post-increment (increment L→R 1
the value of x by 1 after using its value);
Ref. Page 401 Chapter 21: Introduction to C Programming Language Slide 18/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Arithmetic Operators
Associativit
Operator Meaning with Example Precedence
y
Arithmetic Operators
-- Decrement;
x-- means post-decrement (decrement L→R 1
the value of x by 1 after using its value);
--x means pre-decrement (decrement R→L 2
the value of x by 1 before using its value)
Ref. Page 401 Chapter 21: Introduction to C Programming Language Slide 19/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Logical Operators
Logical Operators
! Reverse the logical value of a single variable; R→L 2
!x means if the value of x is non-zero, make it
zero; and if it is zero, make it one
> Greater than; x>y L→R 6
< Less than; x<y L→R 6
>= Greater than or equal to; x >= y L→R 6
<= Less than or equal to; x <= y L→R 6
== Equal to; x == y L→R 7
!= Not equal to; x != y L→R 7
&& AND; x && y means both x and y should be L→R 11
true (non-zero) for result to be true
|| OR; x || y means either x or y should be true L→R 12
(non-zero) for result to be true
Ref. Page 402 Chapter 21: Introduction to C Programming Language Slide 20/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Bitwise Operators
Operator Meaning with Example Associativity Precedence
Bitwise Operators
~ Complement; ~x means R→L 2
All 1s are changed to 0s and 0s to 1s
& AND; x & y means x AND y L→R 8
| OR; x | y means x OR y L→R 10
^ Exclusive OR; x ^ y means x y L→R 9
⊕
<< Left shift; x << 4 means shift all bits in x L→R 5
four places to the left
>> Right shift; x >> 3 means shift all bits L→R 5
in x three places to the right
&= x &= y means x = x & y R→L 14
|= x |= y means x = x | y R→L 14
^= x ^= y means x = x ^ y R→L 14
<<= x <<= 4 means shift all bits in x four places R→L 14
to the left and assign the result to x
>>= x >>= 3 means shift all bits in x three R→L 14
places to the right and assign the result to x
Ref. Page 402 Chapter 21: Introduction to C Programming Language Slide 21/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 402 Chapter 21: Introduction to C Programming Language Slide 22/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Miscellaneous Operators
Associativit Precedenc
Operator Meaning with Example
y e
Miscellaneous Operators
Ref. Page 403 Chapter 21: Introduction to C Programming Language Slide 23/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Statements
Ref. Page 403 Chapter 21: Introduction to C Programming Language Slide 24/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Examples of Statements
Ref. Page 403 Chapter 21: Introduction to C Programming Language Slide 25/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 403 Chapter 21: Introduction to C Programming Language Slide 26/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 404 Chapter 21: Introduction to C Programming Language Slide 27/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Basic Format Specifiers for
scanf() and printf()
Format
Data Types
Specifiers
%d integer (short signed)
integer (short
%u
unsigned)
%ld integer (long signed)
%lu integer (long unsigned)
%f real (float)
%lf real (double)
%c character
%s string
Ref. Page 404 Chapter 21: Introduction to C Programming Language Slide 28/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 405 Chapter 21: Introduction to C Programming Language Slide 29/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Output:
Maths marks = 92
Science marks = 87
English marks = 78
Percentage marks obtained = 85.66
Ref. Page 405 Chapter 21: Introduction to C Programming Language Slide 30/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Preprocessor Directives
Ref. Page 405 Chapter 21: Introduction to C Programming Language Slide 31/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Examples of Preprocessor
Directives
#include <stdio.h>
#define PI 3.1415
#define AND &&
#define ADMIT printf (“The candidate can be admitted”);
#ifdef WINDOWS
.
.
.
Code specific to windows operating system
.
.
.
#else
.
.
.
Code specific to Linux operating system
.
.
.
#endif
.
.
.
Code common to both operating systems
Ref. Page 406 Chapter 21: Introduction to C Programming Language Slide 32/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Standard Preprocessor
Directives in C
Preprocessor Directive Meaning Category
# Null directive
#error message Prints message when processed
Simple
#line linenum filename Used to update code line number and filename
#pragma name Compiler specific settings
#include filename Includes content of another file File
#define macro/string Define a macro or string substitution
#undef macro Removes a macro definition Macro
Ref. Page 407 Chapter 21: Introduction to C Programming Language Slide 33/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Pointers
Ref. Page 407 Chapter 21: Introduction to C Programming Language Slide 34/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
1000 62 i
Address of i = 1000
Value of i = 62
int i = 62;
int ∗p;
int j;
p = &i; /∗ p becomes 1000 ∗/
j = ∗p; /∗ j becomes 62 ∗/
j = 0; /∗ j becomes zero ∗/
j = ∗(&i) /∗ j becomes 62 ∗/
Ref. Page 408 Chapter 21: Introduction to C Programming Language Slide 35/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Array
Ref. Page 408 Chapter 21: Introduction to C Programming Language Slide 36/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
1010 92 Y
1005
1008 63 A
1004
1006 82 1012 10.25
1003 B
1004 66 1008 250.00
1002 M
1002 84 1004 155.50 O
1001
1000 45 1000 82.75 B
1000
Ref. Page 409 Chapter 21: Introduction to C Programming Language Slide 37/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
String
Ref. Page 410 Chapter 21: Introduction to C Programming Language Slide 38/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 410 Chapter 21: Introduction to C Programming Language Slide 39/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 411 Chapter 21: Introduction to C Programming Language Slide 40/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Structure
Ref. Page 411 Chapter 21: Introduction to C Programming Language Slide 41/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Structure (Examples)
Ref. Page 411 Chapter 21: Introduction to C Programming Language Slide 42/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Union
Ref. Page 412 Chapter 21: Introduction to C Programming Language Slide 43/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Union Example
unionNum
{
int intNum;
unsigned
unsNum’
};
union Num Number;
Ref. Page 412 Chapter 21: Introduction to C Programming Language Slide 44/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 412 Chapter 21: Introduction to C Programming Language Slide 45/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Control Structures
Ref. Page 413 Chapter 21: Introduction to C Programming Language Slide 46/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 413 Chapter 21: Introduction to C Programming Language Slide 47/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
§ if (i <= 0) § if (i <= 0)
i++; i++;
else if (i >= 0)
§ if (i <= 0) j++;
i++; else
else k++;
j++;
Ref. Page 413 Chapter 21: Introduction to C Programming Language Slide 48/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 414 Chapter 21: Introduction to C Programming Language Slide 49/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 415 Chapter 21: Introduction to C Programming Language Slide 50/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 415 Chapter 21: Introduction to C Programming Language Slide 51/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Loop Structures
Ref. Page 415 Chapter 21: Introduction to C Programming Language Slide 52/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 415 Chapter 21: Introduction to C Programming Language Slide 53/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Ref. Page 415 Chapter 21: Introduction to C Programming Language Slide 54/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Posttest Loop Construct
“do…while”
§ It has a loop condition only that is tested after each
iteration to decide whether to continue with next
iteration or terminate the loop
§ Example of do…while is:
do {
printf(“i = %d”, i);
i++;
}while (i < 10) ;
Ref. Page 416 Chapter 21: Introduction to C Programming Language Slide 55/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Functions
Ref. Page 416 Chapter 21: Introduction to C Programming Language Slide 56/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Example of a Function
Ref. Page 417 Chapter 21: Introduction to C Programming Language Slide 57/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
# include <stdio.h>
void main()
{
int number, remainder;
clrscr(); /∗ clears the console screen ∗/
printf (“Enter an integer: ”);
scanf (“%d”, &number);
remainder = number % 2;
if (remainder == 0)
printf (“\n The given number is even”);
else
printf (“\n The given number is odd”);
getch();
}
Ref. Page 418 Chapter 21: Introduction to C Programming Language Slide 58/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
# include <stdio.h>
# include <conio.h>
void main()
{
int day;
clrscr();
printf (“Enter an integer in the range 1 to 7”);
scanf (“%d”, &day);
switch(day)
Ref. Page 418 Chapter 21: Introduction to C Programming Language Slide 59/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
{
Case 1:
MON;
break;
Case 2:
TUE;
break;
Case 3:
WED;
break;
Case 4:
THU;
break;
Case 5:
FRI;
break;
Case 6:
SAT;
break;
Case 7:
SUN;
break;
defautl:
OTH;
}
getch();
}
Ref. Page 418 Chapter 21: Introduction to C Programming Language Slide 60/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
# include <stdio.h>
# include <conio.h>
# define PI 3.1415
void main()
{
float radius, area, circum;
clrscr();
printf (“Enter the radius of the circle: ”);
scanf (“%f”, &radius);
area = PI ∗ radius ∗ radius;
circum = 2 ∗ PI ∗ radius;
printf (“\n Area and circumference of the circle are %f
and %f respectively”, area, circum);
getch();
}
(Continued on next slide)
Ref. Page 419 Chapter 21: Introduction to C Programming Language Slide 61/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main()
{
char input_string[50]; /∗ maximum 50 characters ∗/
int len;
int i = 0, cnt = 0;
clrscr();
printf (“Enter a string of less than 50 characters: \n”);
gets (input_string);
len = strlen (input_string);
for (i = 0; i < len; i++)
{
switch (input_string[i])
Ref. Page 420 Chapter 21: Introduction to C Programming Language Slide 62/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U’:
cnt++
}
}
printf (“\n Number of vowels in the string are: %d”, cnt);
getch();
}
Ref. Page 420 Chapter 21: Introduction to C Programming Language Slide 63/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
/∗ Program to illustrate use of a user defined function. The program initializes an array of n elements
from 0 to n-1 and then calculates and prints the sum of the array elements. In this example n = 10 ∗/
#include <stdio.h>
#define SIZE 10
Ref. Page 421 Chapter 21: Introduction to C Programming Language Slide 64/65
Computer
Computer Fundamentals:
Fundamentals: Pradeep
Pradeep K.
K. Sinha
Sinha &
& Priti
Priti Sinha
Sinha
Key Words/Phrases
§ Arithmetic operators § Macro expansion
§ Arrays § Main function
§ Assignment operators § Member element
§ Bit-level manipulation § Null statement
§ Bitwise operators § Operator associativity
§ Branch statement § Operator precedence
§ Character set § Pointer
§ Comment statement § Posttest loop
§ Compound statement § Preprocessor directives
§ Conditional branch § Pretest loop
§ Conditional § Primitive data types
compilation § Reserved words
§ Constants § Simple statement
§ Control structures § Statement block
§ Format specifiers § Strings
§ Formatted I/O § Structure data type
§ Function § Unconditional branch
§ Keywords § Union data type
§ Library functions § User-defined data types
§ Logical operators § Variable name
§ Loop structures § Variable type declaration
§ Variables
Ref. Page 421 Chapter 21: Introduction to C Programming Language Slide 65/65