0% found this document useful (0 votes)
4 views

Elements of Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Elements of Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Elements of

Programming
Definition
"Elements of Programming" refers to the fundamental components and building
blocks that are essential for developing and understanding computer programs.
These elements are the foundational concepts and tools that programmers use
to write, execute, and maintain software.
Character Set in C
The set of characters, including letters, digits, and special symbols, that a programming
language recognizes and uses to write code.

C has its own Vocabulary and Grammar. The Characters that can be used to form
words, numbers and expression depend upon the Computer on which the program
is run.
The Character in C are grouped into the following two categories:

Character Set

Source character set Execution


character set
The source character set are grouped into the following categories.
Source character set

Alphabet Digits Special White space


s character

Alphabets: Alphabets are divided into two group in C language.


They are Uppercase letter
Lowercase letter
Uppercase letter
Alphabets ASCII Value N 78
A 65 O 79
B 66 P 80
C 67 Q 81
D 68 R 82
E 69 S 83
F 70
T 84
G 71
U 85
H 72
V 86
I 73
W 87
J 74
X 88
K 75
L 76 Y 89
M 77 Z 90
Lowercase letter:
Alphabets ASCII Value n 110
a 97 o 111
b 98 p 112
c 99 q 113
d 100 r 114
e 101 s 115
f 102
t 116
g 103
u 117
h 104
v 118
i 105
w 119
j 106
x 120
k 107
y 121
l 108
z 122
m 109
Digits
Digits ASCII Value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
Special characters:
Special character ASCII Value Character represented
! 32 Exclamation mark
” 34 Quotation mark
# 35 Number sign
$ 36 Dollar Sign
% 37 Percent sign
& 38 Ampersand
( 40 Left parenthesis
) 41 Right parenthesis
* 42 Asterisk
+ 43 Plus sign
Cont.….
, 44 Comma
- 45 Mins sign
. 46 Period
/ 47 Slash
: 58 Colon
; 59 Semicolon
< 60 Opening angle bracket
(or) less then sign
= 61 Equal sign
> 62 Closing angle bracket (or)
Greater than sign
? 63 Question mark
Whitespace Character:
Constant Character represented
\b Blank space
\\ Back slash
\n New line
\f Form feed
\0 Null
\v Vertical tab
\t Horizontal tab
\’ Single quote
\r Carriage return
\a Alarm(bell)
\? Question mark
Execution Character set:
• Certain ASCII character are unprintable which means they are not displayed
on the screen or printer.
• Those character performed other function aside from displaying text.

Examples are
• backspacing
• moving to a newline or ringing a bell they are used in output statements.

• Execution character set are always represented by a combination of


backslash(\) followed by a character.
• These characters combination are called as Escape sequence.
Example:
Program to convert from ASCII value to Equivalent character

#include <stdio.h>
#include <ctype.h>
int main()
{
int myvar;
printf("Enter the integer value to get its ASCII character: ");
scanf("%d",&myvar);
printf("\n Character Value is: %c",myvar);
return 0;
}
Example
Program to convert from Lower case to upper case
#include <stdio.h>
#include <ctype.h>
int main() {
char a;
printf("Enter the character from a-z or A-z for change the case: ");
scanf("%c",&a);
if(a>=97 && a<=122)
{
a=a-32;
}
else
{
a=a+32;
}
printf("value after changing case be: %c ",a);
return 0;
}
Keywords in C
Reserved words in a programming language with predefined meanings. Keywords are
used to define the structure and logic of a program.
• The keywords are all also identifiersPrograming
but cannot be user defined since they are reserved
Languages
words.
• The following words are reserved for use as keywords.
• In C they have fixed meaning and these meanings cannot be changed. We should not
use them as variables or identifiers.
• Keywords must be written Low-level languages High-level languages
in lower case letters.
auto do goto signed unsigned
break double if sizeof void
case
First Generation
else int
Second Generation
static volatile
char enum long struct while
const extern register switch
continue float return typedef
default for short union
Identifiers in C
• Identifiers: User-defined names for various program elements such as
variables, functions, and constants. Identifiers help make code more readable
and maintainable.
• Identifiers can be defined as the names of the variables and some other
program elements using the combination of the following characters.
• Alphabets : a….z, A…Z
• Numerals : 0…9
• Underscore : _
The rules for defining Identifiers:
• First character must be an alphabet. pay 1pay (wrong)

• Must consist of only letters, digits or underscore. wat_bill, page120, a123

• Identifier cannot be a keyword. goto

• No special characters are allowed. pays$

• Must not contain white space. reg no


Differences between Keyword and Identifier
1. int main()
Keyword Identifier
2. {
Keyword
3. is a pre-defined word.
int a=10; The identifier is a user-defined word
It
4. must be written in a lowercase letter.
int A=20; It can be written in both lowercase and
5. printf("Value of a is : %d",a); uppercase letters.
6.
Its printf("\nValue
meaning isof Apre-defined
is :%d",A); in the c Its meaning is not defined in the c
compiler.
7. return 0; compiler.
8. }
It is a combination of alphabetical It is a combination of alphanumeric
characters. characters.
It does not contain the underscore It can contain the underscore character.
character.
Example
int main ()
{
int h = 78;
int H = 43;
printf( “ Value stored in h is : %d”, h);
printf( “ \nValue stored in H is : %d”, H);
return 0;
}
Output of the following program will be :
Value stored in h is : 78
Value stored in H is : 43
The output shows that the identifier is case sensitive.
Constants
Constants in C refer to fixed values that do not change during the execution of the
program. The constants can be defined in two ways.
1. const < datatype> <constant name> = <init value>;
e.g. const float PI=3.14;
2. # define CONSTNAME literal
e.g. #define MAX 100
Types of C Constant:
1. Integer constants
2. Real or Floating point constants
3. Character constants
4. String constants
5. Backslash character constants
C supports several kinds of constants.
Integer Constants:
There are three types of integer constants namely, decimal, octal and hexadecimal.

1. Decimal integers consists of a set of digits , 0 through 9, preceded by an optional


– or + sign. E.g. 123, -321, 0, +18
Embedded spaces, commas, and non – digit characters are not permitted
between digits.

2. Octal integer constants consists of any combination of digits from the set 0
through 1, preceded with O. E.g. O31, 0435, O551

3. Hexadecimal integer constants consists of any combination of digits from 0 – 9


and a – f or A – F preceded by 0x or 0X.
Constants
Real Constants
Real constants consist of two parts namely integral part and fractional part
separated with a decimal point. E.g. 0.0083, -0.15, 435.36, +241.0.

Single Character Constants


A single character constant contains a single character enclosed within a
pair of single quotes. E.g. ‘5’, ‘X’, ‘;’, ‘ ‘

String Constants
It is a sequence of characters enclosed in double quotes. E.g. “Hello!”,
“1981”, “Well done”, “?.......!”, “5+3”, “X”.
Two ways to define constant in C
There are two ways to define constant in C programming.
• 1. const keyword
• 2. #define preprocessor
Example using ‘const’ Keyword
#include <stdio.h>
#include <ctype.h>
int main() {
const int a=1;
printf("%d",a);
a=2;
return 0;
} Compiler time error: assignment of read-only variable ‘a’
Using ‘#define’ Keyword
Example1: Example2:
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#define a 1 #define a 1
int main() { int main() {
printf("%d",a); printf("%d",a);
return 0; a=2;
} return 0; Compiler time error:
Output : 1 } Lvalue required as left operand of assignment
Output :
Variables in C
Variables: Named memory locations used to store and manipulate data. Variables
can hold different values during the execution of a program.
Variable declaration
The syntax for variable declaration is as follows −
type variable_name;
or
type variable_name, variable_name, variable_name;
For example,
int a,b;
float c;
double d;
Here, a, b, c, d are variables. The int, float, double are the data types.
Variables

Must start with • Names may consist of alphanumeric or underscore


a letter double this_is_122_OK;
char SO_81_is_THIS;
int 12but_this_isnot;
• You can declare and initialize at same time
char ch=‘a’;
int zero=0;
double zero=0.0;
x=y=z=0;
Multiple assignments at
same time
Declarations
As seen in Structure of C program, declarations are done in the declaration part,
before the execution part of the main() function.
#include <stdio.h>
int main()
{
Declaration part

Execution part
}
Declaration part consists of declaration of all variables used
Execution part contain one or more C statements separated by ;
#include <stdio.h>
int main()
Declaration part
{
// declaration part
int a=5, b=10, c;

// execution part int a=5, b=10, c;


c = a + b;
printf(“Sum of given number is
%d”,c); Execution part
} c = a + b;
printf(“Sum of given number is %d”,c);
Assigning Values to Variables
Puts a specified value into a specified variable
Syntax is
<variable name> = <expression/constant>;
E.g.:
int item;
item=0;

item=(134*60)/100;
C program for variable assignment
#include <stdio.h>
int main (){
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 40;
b = 50;
c = a + b;
printf("value of c : %d
", c);
return 0;} Output:Value of c: 90
Differentiating Data Types
#include <stdio.h>

int main() {
int age; // Declaration
float price; // Declaration
char initial; // Declaration

age = 25; // Assignment


price = 19.99; // Assignment
initial = 'A'; // Assignment

printf("Age: %d\n", age);


printf("Price: %.2f\n", price);
printf("Initial: %c\n", initial);

return 0;
}
Input, Processing and Output
Computer programs perform three-steps:

• taking input

• doing some processing on input taken and then,

• producing output

Input can be sent to the program from the user, who is entering data at the
keyboard. Example:

scanf(“%d”,&hours); /* read an ‘integer’ value from keyboard */

scanf(“%f”,&rate); /* read a ‘float’ value from keyboard */

scanf is the standard input function in C that is used to read character, string and
Input, Processing and Output
Processing: Once the input is taken from the user through keyboard, a program
usually processes it in some manner, like:
pay = hours * rate;
Output is information that a program sends to the outside world. It can be
anything displayed on a screen, or, data sent to the printer.
printf is the standard output function in C to print the text or output onto the
console.
Example:
printf(“You have earned %f“,pay);
Input functions
Use the scanf function to read a value from standard input.
Standard input is read word by word (words are separated by spaces, tabs, or
newlines).
Examples:
scanf(“%c”,&st_let); // reads a character from keyboard and stores in
variable ‘st_let’
scanf(“%s”,name); // reads a string from keyboard and stores in variable
‘name’
scanf(“%d”,&num); // read an integer from keyboard and stores in variable
‘num’
scanf(“%f”,&rate); // read an float value from keyboard and stores in
Output functions
printf statement is used to display all the output statements or prompt messages
Examples:
• printf(“Hi there!”);
• printf(“ I have “+(3+5)+” classes today.”);
• printf(“ goodbye!\n”); // [‘\n’ takes the cursor to the beginning of new
line]
Type Conversion and Casting in C
In a c programming language, the data conversion is performed in two different
methods as follows.

• Type Conversion
• Type Casting
Type Conversion:
The type conversion is the process of converting a data value from one data type
to another data type automatically by the compiler. Sometimes type conversion is
also called implicit type conversion. The implicit type conversion is
automatically performed by the compiler.
int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;
Example
#include<stdio.h>
#include<conio.h>

void main(){
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;

i=x;
printf("i value is %d\n",i);
x=i;
printf("x value is %f\n",x);
i = ch ;
printf("i value is %d\n",i);

} Output: i=90, x=90.000000, i=65


Typecasting
Typecasting is also called an explicit type conversion. Compiler converts data
from one data type to another data type implicitly. When compiler converts implicitly,
there may be a data loss. In such a case, we convert the data from one data type to
another data type using explicit type conversion. To perform this we use the unary
cast operator.

The general syntax of typecasting is as follows.


(TargetDatatype) DataValue
Example
int totalMarks = 450, maxMarks = 600 ;
float average ;
Example
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
int main()
{
float a = 1.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
return 0;
} Output: Value of a is 1.200000
Value of b is 2
Practice Question
#include<stdio.h>
#include<conio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
x = x + y;
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output = ?
Program to demonstrate explicit type
int main()
casting
{
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

printf("sum = %d", sum);

return 0;
} Output = ?

You might also like