Elements of Programming
Elements of Programming
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
Examples are
• backspacing
• moving to a newline or ringing a bell they are used in output statements.
#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)
2. Octal integer constants consists of any combination of digits from the set 0
through 1, preceded with O. E.g. O31, 0435, O551
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
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;
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
return 0;
}
Input, Processing and Output
Computer programs perform three-steps:
• taking input
• producing output
Input can be sent to the program from the user, who is entering data at the
keyboard. Example:
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);
return 0;
} Output = ?