PSP Unit 1 Overview of C 12 7 2022 2pm
PSP Unit 1 Overview of C 12 7 2022 2pm
THROUGH PROGRAMMING
Course Instructor
Dr. Umadevi V
Department of CSE, BMSCE
Webpage:https://sites.google.com/site/drvumadevi/
Essential Part
Optional
main () function section: Every C program must have one main function section. This section
contains two parts; declaration part and executable part
Declaration part: The declaration part declares all the variables used in the executable
part.
Executable part: There is at least one statement in the executable part. These two parts
must appear between the opening and closing braces. The program execution begins at the opening
brace and ends at the closing brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a semicolon.
Subprogram section
If the program is a multi-function program then the subprogram section contains all the user-defined
functions that are called in the main () function. User-defined functions are generally placed
immediately after the main () function, although they may appear in any order.
All section, except the main () function section may be absent when they are not required.
Invalid Identifiers:
float -> It is a keyword.
I am -> It has space
123_Abc -> It is starting with number
N1+n2 -> It contains special symbol (+)
Signed Unsigned
0 12 856 456844
47.00 47,00
0.4 05
2. String Constant:
A group of characters together form
string.
Strings are enclosed within double
quotes.
They end with NULL character (\0).
Ex: “BMS”
B M S \0
#include <stdio.h>
main()
{
printf("BMS College of Engineering\n");
printf("BMS \"College\" of Engineering\n");
printf("BMS College of Engineering\?\n");
printf("\\BMS College of Engineering\\");
}
Output
BMS College of Engineering
BMS "College" of Engineering
BMS College of Engineering?
\BMS College of Engineering\
Void
Integer type:
It is used to store whole numbers.
The keyword int is used to declare variable of integer
type.
Note:
Double: It is used to store double precision floating point numbers.
Void:
It is a special data type that has no value.
It doesn’t have size.
It is used for returning the result of function
that returns nothing.
Integertype.c
#include <stdio.h>
main()
{
int a = 8;
printf("The value of a = %d ",a);
}
The value of a = 8
floatrtype.c
#include <stdio.h>
main()
{
float a = 8.976;
printf("The value of a = %f",a);
program.c
#include <stdio.h>
main()
{
sum.c
#include <stdio.h>
main()
{
int a = 8;
int b = 10
printf("The sum = %d ",a+b);
The sum = 18
sum.c
#include <stdio.h>
main()
{
int a = 8;
int b = 10
int c;
c=a+b;
printf("The sum= %d ",c);
The sum = 18
unsigned char %c 1
char %c 1
signed char %c 1
int %d, %i 2
unsigned int %u 2
signed int %d, %i 2
short int %hd 1
unsigned short int %hu 1
float %f 4
double %lf 8
Syntax of scanf()
scanf("format specifier" , &variable1, &variable2,…..);
Note:
How to get the address of a variable ?
Using ampersand (&) operator, we can get the address of a variable. In C,
we will refer & as address of operator.
#include <stdio.h>
main()
{
int a;
float b;
}
February 25, 2024 CSE, BMSCE 58
Derived Data Types
Data types that are derived from fundamental data types are called derived data
types.
Derived data types do not create new data types. Instead, they add some
functionality to the existing data types.
Derived data types are derived from the primitive data types by adding some
extra relationships with the various elements of the primary data types. The
derived data type can be used to represent a single value or multiple values.
typdef
enum
Example Program
#include <stdio.h>
void main() {
typedef int Tutorials; //statement-1
Tutorials a = 17;
printf("Given value =%d\n", a);
}
In statement – 1, the keyword typedef is used to create Tutorials as the alias for the
int data type. From this statement onwards, Tutorials will be the new name for int in
this program and the variables declared as Tutorials type will also behave like int
variables for all practical purposes.
The enumerated data types basically allow a user to create symbolic names of their
own for a list of all the constants that are related to each other.
For instance, you can create the enumerated data type for the false and true
conditions this way:
enum Boolean {false, true};
In case we don’t assign values explicitly to the enum names; the compiler, by default,
would assign the values that start from 0.
In this case, false and true would be automatically assigned to 0 and 1 respectively. In
simpler words, the first field of the enum value here will be replaced by 0, and then
the next field will be replaced with 1, and so on.
#include <stdio.h>
main() {
enum week {MON = 1, TUE, WED, THURS, FRI, SAT, SUN};
enum week day = FRI;
printf(“Week Day = %d”, day);
}
Here, the field name MON will be assigned with the value 1. Thus, the next
field name TUE will be assigned with the value 2 automatically, and so on.
The program mentioned above will print the following output:
Week Day = 5
#include <stdio.h>
main() {
enum months {January = 11, February, March, April, May, June, July,
August, September, October, November, December};
enum months birthday = October;
printf(“Surprise Birthday = %d”, birthday);
}
A. Surprise Birthday = 15
B. Surprise Birthday = 12
C. Surprise Birthday = 20
D. Error
#include <stdio.h>
main() {
enum months {January = 11, February, March, April, May, June, July,
August, September, October, November, December};
enum months birthday = October;
printf(“Surprise Birthday = %d”, birthday);
}
A. Surprise Birthday = 15
B. Surprise Birthday = 12
C. Surprise Birthday = 20
D. Error
Example Program
#include <stdio.h>
main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d", a);
printf("\nThe value of variable b : %d", b);
}
Example Program
#include <stdio.h>
main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d", a);
printf("\nThe value of variable b : %d", b);
}
Output
The default value of variable a : 0
The value of variable b : 12
Answer: Option D
Explanation: Constant variable has to be declared and defined at the same time.
Trying to change it later results in error.
#include<stdio.h>
main()
{
int p,r,t, si;
printf(“Enter Principle, Rate of interest & Time to find simple interest: \n");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("Simple interest = %d",si);
}
#include <stdio.h>
main() {
float Num1, Num2, Sum;
printf("Enter two floating point values: ");
scanf("%f%f", &Num1, &Num2);
Sum=Num1+Num2;
printf("Sum = %f", Sum);
}
Output:
31.40 78.50
1. Integer Arithmetic
2. Real Arithmetic
3. Mixed mode Arithmetic
Integer Arithmetic: It is an expression evaluation where
all the operands are of integer data type. Let us take an
example:
int a=10,b=5,c;
c=a/b;
printf(“%d”, c);
Example-2:
int A=5, B=10, X=20, Y=10;
What is the truth value of the following expression:
(A+B) >= (X-Y)
Example:
large = (4 > 2) ? 4: 2; large = 4
Example Program
#include <stdio.h>
main() {
int age;
// take input from users
printf("Enter your age: ");
scanf("%d", &age);
#include <stdio.h>
main() {
int a,b;
int large;
#include <stdio.h>
main() {
int a,b,c;
int large, final_large;
large=(a>=b)?a:b;
final_large=(large>=c)?large:c;
#include <stdio.h>
main() {
int a, remainder;
printf("Enter an integer number: ");
scanf("%d", &a);
remainder=a%2;
(remainder == 0) ? printf("Even") : printf("Odd");
}
#include <stdio.h>
main() {
int N1, N2, N3, large;
printf("Largest=%d",large);
}
a+=4;
printf("\na=%d",a);
a-=13;
printf("\na=%d",a);
a*=6;
printf("\na=%d",a);
a/=5;
printf("\na=%d",a);
a%=5;
printf("\na=%d",a);
}
a+=4;
printf("\na=%d",a);
a-=13;
Output
printf("\na=%d",a); a=24
a=11
a*=6; a=66
printf("\na=%d",a); a=13
a=3
a/=5;
printf("\na=%d",a);
a%=5;
printf("\na=%d",a);
}
Pre-increment
First value of the operand
is incremented (added) by 1
then, it is used for
evaluation.
Example: ++a
++a is equivalent to a=a+1
Pre-increment Post-increment
First value of the operand First value of the operand
is incremented (added) by 1 is used for evaluation then, it
then, it is used for is incremented (added) by 1.
evaluation.
Pre-increment
or
Prefix increment
#include <stdio.h>
main()
{
int a=0, b=5;
Pre-increment
or
Prefix increment
#include <stdio.h>
main()
{
int a=0, b=5;
Value of a= 0 b=5
Value of a= 6 b=6
Pre-increment Post-increment
or or
Prefix increment Postfix increment
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;
Value of a= 0 b=5
Value of a= 6 b=6
Pre-increment Post-increment
or or
Prefix increment Postfix increment
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;
printf("%d\n", var1++);
printf("%d\n", ++var2);
}
// 5 is displayed
// Then, var1 is increased to 6.
printf("%d\n", var1++);
// var2 is increased to 6
// Then, it is displayed.
printf("%d\n", ++var2);
}
Answer:
5
6
Pre-increment Post-increment
First value of the operand First value of the operand
is decremented (subtracted) is used for evaluation then, it
by 1 then, it is used for is decremented (subtracted)
evaluation. by 1.
Pre-decrement
or
Prefix decrement
#include <stdio.h>
main()
{
int a=0, b=5;
Pre-decrement Post-decrement
or or
Prefix decrement Postfix decrement
#include <stdio.h> #include <stdio.h>
main() main()
{ {
int a=0, b=5; int a=0, b=5;
a) Comma Operator
b) sizeof operator
x = 40,50,60;
y = (40,50,60);
printf(“x= %d, y= %d\n”,x,y);
}
Output:
x= 40, y= 60
Let us say: B=5, C=7, D=9 and F=3, then value of A is: A= 5+7*9/3
This expression is evaluated in following steps:
A = 5 + (7*9)/3
A= 5+(63/3)
A=5+21
A=26
Example:
int a=25;
float x=5,z;
z=x/a;
printf(“%f”,z);
In this example ‘x’ is float and ‘a’ is integer, a gets automatically converted to float
and answer is: z= (5.0/25.0) = 0.2
In the example given above we are using modulus or remainder (%) operator. Here ‘a’
is integer type and ‘x’ is float. But the catch is % can be used only with integers. So
‘x’ has to be automatically converted to float. But it is impossible as float is bigger
than integer. As a result Complier gives syntax error.
If we have to convert a variable of bigger size to smaller type we have to use manual
conversion (explicit conversion). Here is how we can use explicit conversion and
overcome the syntax error in previous example:
#include<stdio.h>
main()
{
int a , b, temp;
#include<stdio.h>
void main()
{
int a , b, temp;
printf("Enter the values of a and b:\n");
scanf("%d%d", &a,&b);
printf(“ a=%d and b=%d\n” , a ,b);
printf(“Exchanging Using a temporary variable\n”);
temp = a;
a = b;
b = temp;
printf(“a=%d and b=%d\n”,a,b);
printf(“Without using a temporary variable\n”);
a=a+b;
b=a-b;
a=a-b;
printf(“a=%d and b=%d\n”,a,b);
}
Output:
Enter the values of a and b:
5
10
a = 5 and b = 10
Exchanging Using a temporary variable\n”);
a = 10 and b = 5
Without using a temporary variable
a = 5 and b = 10
#include<stdio.h>
void main()
{
float ct,ft;
Printf(“Enter the temperature in Fahrenheit\n");
scanf(“%f”,&ft);
ct=(ft-32.0)/1.8;
printf(“Fahrenheit temperature = %f\n”, ft);
printf(“Celsius temperature = %f\n”, ct);
}
Output:
Enter the temperature in Fahrenheit
50
Fahrenheit temperature = 50.00
Celsius temperature = 10.00
#include<stdio.h>
void main()
{
int length,breadth,area,peri;
printf(“Enter the length and breadth of a rectangle\n”);
scanf(“%d%d”,&length,&breadth);
area=length*breadth;
peri=2*(length+breadth);
printf(“Area=%f\n Perimeter=%f\n”,area,peri);
}
Output:
enter the length and breadth of a rectangle
5
6
Area=30
Perimeter=22
My contact details
Dr. Umadevi V, Associate Professor, Department of CSE, BMSCE
Staff Room: 4th floor, New Academic block
8762742909