You are on page 1of 33

Programming Fundamentals

Lecture No. 2
Short review
Terminologies

• Programmer
• Source Code (BIN Folder, CPP & BAK)
• Object Code
• End- User
• EXE file (Source folder)
Review of first C Program
#include <stdio.h>
#include <conio.h>
int main()
{
Printf(“Hello World”);
getche();
return 0;
}
Comment
• What is a comment ?
– Programmer defined.
– Ignored by compiler.
– Used to document the program.
– Increase understandability.
• Single line comment
• Multiline comment
//This is a single line comment

/*
This is a multiple line comment
*/
Data and Data Types
1. Int
2. Short
3. Long
4. Float
5. Double
6. Char
Integer
• Written as int
• Size of 2 bytes #include <stdio.h>
• Used to store numbers #include <conio.h>
int main()
-32,767 to 32,767 {
int abc;
abc = 0;
printf("The size of int variable is
%d", sizeof(abc));
getche();
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
int abc;
abc = 65536;
printf("The size of int variable is
%d", sizeof(abc));
getche();
return 0;
}
Char
• Use to store character value
• Character is ‘a’, ‘b’, ‘c’
• Declare as: char c_v = ‘a’;
• Placeholder is %c
• Size is 1 byte
#include <stdio.h>
#include <stdio.h> #include <conio.h>
#include <conio.h> int main()
int main() {
{ char abc;
char abc; abc = ‘abcd’;
abc = ‘a’; printf("The character variable is
printf("The character variable is %c", abc);
%c", abc); getche();
getche(); return 0;
return 0; }
}
#include <stdio.h> #include <stdio.h>
#include <conio.h> #include <conio.h>
int main() int main()
{ {
int abc; int abc;
abc = ‘!’; abc = ‘!$#’;
printf("The character variable is printf("The character variable is
%c", abc); %c", abc);
getche(); getche();
return 0; return 0;
} }
long
4 bytes -2,147,483,648 to 2,147,483,647

Short
Short 2 bytes -32,768 to 32,767
Float
4 bytes 6 decimal places

Double
8 bytes 15 decimal places
What is Constant ?
• A value that cannot be changed during the
execution of a program is called as constant.
• How to declare constant in C program ?
• data type, const name_of_constant
• const data type name_of_constant
• Int const GRAVITY = 6.67
• Const int PI = 3.14
• Use UPPER CASE to name constants
Sample Program to demonstrate
Constant
• #include<stdio.h>
• #include<conio.h>
• Int main()
• {
– int const PI = 3.14;
– const int GRAVITY= 9.7;
• }
Placeholders
• Output place holder
• Input place holder

• Type of values that will be displayed or used to


receive input.
• Common placeholders are:
Data type..............Placeholder
int.......................... %d
char....................... %c
double................... %lf
float....................... %f
string.................... %s
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
const float pi= 3.14;
float const gravity=9.7;
int int_value = 10;
char ch = 'a';

printf("The value of PI is %lf \n",pi);


printf("The integer value is %d \n",int_value);
printf("The chracter value is %c \n",ch);

getche();
return 0;

}
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
const float pi= 3.14;
float const gravity=9.7;
int int_value = 10;
char ch = 'a';
printf("Enter value of int value");
scanf("%d",&int_value);

printf("The integer value is %d \n",int_value);

getche();
return 0;
}
Placeholder: address variable
#include<stdio.h>
#include<conio.h>
int main()
{clrscr();
const float pi= 3.14;
float const gravity=9.7;
int a, b, c;
a=b=c=1;

printf("\n");
printf("%x",&b);
printf("\n");
printf("%x",&c);
getche();
return 0; }
Achievement
We have understood Placeholders, constants,
keywords and character set
Variables
• A value that can be changed during the
execution of a program is called as constant.

• Declare
– data_type name_variable;
– Int a;
– Int a, b, c;
– Int a, b, c;
Variables
• Initializing variable.
• What happen when a variable is not initialized.
• a=b=c=0;
– 10=a ?
– a=10?
• Rules to declare variables
– Variable name can start with _ ?
– Variable name with 112_?
– Variable name with _123?
– Variable name with abc$#@?
– Variable name as pi value ?
– Variable name as auto, double, int ?
PART III
Mathematical Expression
Operators
• What is an operator ?
An operator is a character/ symbol that
represents some action.
• What is operand ?
Quantity on which action needed to be done
Types of Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
Arithmetic operators
Plus +

Minus -

Multiply *

Divide /

Modulus %
Arithmetic operators

i+j
x*y
a/b
a%b
% = Remainder

5%2=1
2%2=0
4/2=2
5/2=?
Precedence
• Highest: ()
• Next: *,/,%
• Lowest: +,-
3*10 + 15 = ?
3/2+1=?
10+2*10/10=?
(1+2)*10/2=?
Q:1 Writing a program to calculate
Average of marks.

Q:2 Write a Program as declare


three variables of integer type and
print address of variables, and
values in variables.
END

You might also like