Unit 1.2 C Notes by Prof. Shahid Masood
Unit 1.2 C Notes by Prof. Shahid Masood
Data Types
In
C
CABSMJ1001 [2024-25]
Data Types in C
A data type defines:
a set of values and
the operations that can be performed on these values.
1. Integer types
2. Character types
4. void type
CABSMJ1001 [2024-25]
2. Character types
char data type - used to store a single character.
In C, every character is represented in memory by its ASCII
(American Standard Code for Information Interchange) value.
For example:
'A' is represented by 65, 'B' by 66, 'C' by 67
'a' by 97, 'b' by 98, 'c' by 99.
/* charexample.c */
#include<stdio.h>
main()
{ char c1='A', c2='a';
printf("ASCII value of %c is %d\n",c1,c1);
printf("ASCII value of %c is %d\n",c2,c2);
}
Output
ASCII value of A is 65
ASCII value of a is 97
CABSMJ1001 [2024-25]
'
CABSMJ1001 [2024-25]
Back
DEL is also a
control character
CABSMJ1001 [2024-25]
4. void type
It denotes no data type.
It can be used:
to specify return type of a function (if a function’s return
type is void, it means it does not return any value).
to specify that a functions’s argument list is empty, e.g.
void f1(void)
{ ……
……
}
A pointer may also be of type void. Such a pointer may
contain the address of an object, but not its type.
Memory size of void type: Since void type means nothing,
hence it doesn’t have any memory size.
CABSMJ1001 [2024-25]
How to Get the Exact Size of Basic data types on your machine?
To get the exact size of basic data types on a particular
machine, you can use the expression sizeof(data type) that
yields the memory size of the specified data type in bytes.
/* memorysize.c */
#include<stdio.h>
main()
{ printf("Memory size of char = %d\n",sizeof(char));
printf("Memory size of short = %d\n",sizeof(short));
printf("Memory size of int = %d\n",sizeof(int));
printf("Memory size of long = %d\n",sizeof(long));
printf("Memory size of float = %d\n",sizeof(float));
printf("Memory size of double= %d\n",sizeof(double));
printf("Memory size of long double= %d\n",sizeof(long double));
}
CABSMJ1001 [2024-25]
Managing
Input and Output
Operations
CABSMJ1001 [2024-25]
Streams in C Programming
In C, all input and output is
handled with streams -
regardless of where input is
coming from, or where output
is going to.
A stream is a sequence of characters (bytes of data) flowing
into a program or flowing out of a program.
C has three predefined streams, known as (i) standard input
stream, (ii) standard output stream and (iii) standard error
stream.
These streams are:
automatically opened when a C program’s execution starts
automatically closed when the program terminates.
CABSMJ1001 [2024-25]
Streams in C Programming
The standard streams and the
devices they are connected
with:
Name Stream Device
stdin Standard Input Keyboard
stdout Standard Output Screen
stderr Standard Error Screen
Example:
char var1;
var1 = getch(); /* it will read the entered character */
/* from stdin and return it */
CABSMJ1001 [2024-25]
Example:
char var1;
var1 = getche(); /* it will read the entered character */
/* from stdin and return it */
CABSMJ1001 [2024-25]
Prob
General form:
scanf("format string",&var1,&var2,…..);
Once the input values are typed and <Enter> key is pressed,
the values are read in the variables num1, num2 and the
computer proceeds to the next statement.
CABSMJ1001 [2024-25]
char ch;
%c A single character scanf("%c",&ch); /* input: A */
printf("%c",ch); /* output: A */
int x;
A decimal integer
%d scanf("%d",&x); /* input: -32526 */
(signed)
printf("%d",x); /* output: -32526 */
unsigned int x;
A decimal integer
%u scanf("%u",&x); /* input: 40501 */
(unsigned)
printf("%u",x); /* output: 40501 */
long int x;
A long decimal
%ld scanf("%ld",&x); /* input: -4535075 */
integer (signed)
printf("%ld",x); /* output: -4535075 */
CABSMJ1001 [2024-25]
General form:
printf("format string",var1,var2,…..);
Format string:
contains format specifiers which specify what type of data
(stored in the specified variables) is to be printed.
may also optionally contain string constants that are
printed as they appear in the format string.
It returns the total number of characters written, if successful.
On failure, a negative number is returned.
CABSMJ1001 [2024-25]
printf() Function
Following flags can also be included in the format specification:
printf("%6d", i); 9 8 7 6
printf("%2d", i); 9 8 7 6
printf("%-6d", i); 9 8 7 6
printf("%06d", i); 0 0 9 8 7 6
printf("%#X", i); 0 X 2 6 9 4
CABSMJ1001 [2024-25]
printf("%7.2f",y) ; 9 8 . 7 7
printf("%-7.2f",y); 9 8 . 7 7
printf("%f",y); 9 8 . 7 6 5 4 0 0
printf("%11.3E",y); 9 . 8 7 7 E + 0 0 1
printf("%g",y); 9 8 . 7 6 5 4
CABSMJ1001 [2024-25]
printf("%7.2lf",z) 9 8 . 7 7
printf("%le",z) 9 . 8 7 6 5 4 0 e + 0 0 1
printf("%lE",z) 9 . 8 7 6 5 4 0 E + 0 0 1
printf("%lg",z) 9 8 . 7 6 5 4
printf("%lG",z) 9 8 . 7 6 5 4
CABSMJ1001 [2024-25]
Prob
Write a C program that reads two integers and prints their sum
and average.
CABSMJ1001 [2024-25]
/* C program to read two integers and print their sum and average */
/*add2int.c */
#include<stdio.h>
main()
{ int num1, num2, sum;
float avg;
printf("Enter two integer numbers\n");
scanf("%d %d",&num1,&num2);
What will be the value of avg
sum = num1+num2;
if we write 2 instead of 2.0?
avg = sum/2.0;
printf("Sum= %d, Average= %f",sum,avg);
}
CABSMJ1001 [2024-25]
Prob
/* swap.c */
#include<stdio.h>
main()
{ int a, b, temp;
printf("Enter two integer numbers: ");
scanf("%d %d",&a,&b);
temp = a;
a = b;
b = temp;
printf("Values of a and b after swapping\n");
printf("a = %d and b = %d",a,b);
}
CABSMJ1001 [2024-25]
Prob
/* swap2.c */
#include<stdio.h>
main()
{ int a, b;
printf("Enter two integer numbers: ");
scanf("%d %d",&a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("Values of a and b after swapping\n");
printf("a = %d and b = %d",a,b);
}
CABSMJ1001 [2024-25]
Prob
C = ( F – 32 ) / 1.8
CABSMJ1001 [2024-25]
/* ftocelsius.c */
#include<stdio.h>
main()
{ float fahrenheit, celsius;
printf("Enter temperature in fahreheit\n");
scanf("%f",&fahrenheit);
celsius = (fahrenheit-32)/1.8;
printf("Temp. in Celsius = %6.2f",celsius);
}
CABSMJ1001 [2024-25]
Example: skip
Input data: 123 456 789 int type variables
Input stat1: scanf("%d %*d %d",&num1,&num2);
or Input stat2: scanf("%3d %*3d %3d",&num1,&num2);
will assign
123 to num1
456 skipped (because of *)
789 to num2
CABSMJ1001 [2024-25]
will assign:
31426 to num1
518 to num2.
CABSMJ1001 [2024-25]
Scope of
Variables
CABSMJ1001 [2024-25]
Scope of C Variables
The scope of a C variable means:
the section/region of the program where it has its
existence
i.e. it cannot be used/accessed beyond that region.
2. Global Variables
Variables declared outside of all functions (usually on top of
the program) and accessed inside the functions are called
global variables.
#define
and
const keyword
CABSMJ1001 [2024-25]
C
Operators
CABSMJ1001 [2024-25]
C Operators
Operators are symbols that perform different operations on
variables and values.
C supports a rich set of built-in operators that can be divided
into following EIGHT groups:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment & Decrement operators
6. Ternary (or Conditional) operator
7. Bitwise operators
8. Special operators
CABSMJ1001 [2024-25]
1. Arithmetic Operators
used to perform mathematical operations on numeric values.
They are:
Contd...
CABSMJ1001 [2024-25]
Prob
Write a C program that reads two integer values in variables
dividend and divisor and finds the quotient and remainder when
dividend is divided by divisor.
/* quotientrem1.c */
#include<stdio.h>
main()
{ int dividend, divisor, quotient, rem;
printf("Enter dividend and divisor (integer values): ");
scanf("%d %d", ÷nd, &divisor);
quotient = dividend / divisor;
rem = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", rem);
} Output
Enter dividend and divisor (integer values): 25 6
Quotient = 4
Remainder = 1
CABSMJ1001 [2024-25]
Prob
/* areaoftriangle.c */
#include<stdio.h>
main()
{ float base, height, area;
printf("Enter base and height of the triangle: ");
scanf("%f %f",&base,&height);
area = (base * height) / 2;
printf("Area of the triangle = %f",area);
}
Output
Enter base and height of the triangle: 10 5
Area of the triangle = 25.000000
CABSMJ1001 [2024-25]
Prob
Write a C program that reads time in seconds and converts it
into hours, minutes and seconds.
Prob
Write a C program to calculate the distance between two points
(x1,y1) and (x2,y2) on a x-y plane using the formula:
CABSMJ1001 [2024-25]
/* distance.c illustrates the use of sqrt() and pow() library functions */
#include<stdio.h>
#include<math.h>
main()
{ float x1, y1, x2, y2, dist;
printf("Input x1 and y1: ");
scanf("%f %f",&x1,&y1);
printf("Input x2 and y2: ");
scanf("%f %f",&x2,&y2);
/* Calculate the distance between the points */
dist = sqrt(pow((x2-x1),2)+pow((y2-y1),2));
printf("Distance between the points: %.2f",dist);
}
Output
Input x1 and y1: 5 6
Input x2 and y2: 15 16
Distance between the points: 14.14
CABSMJ1001 [2024-25]
Exercise #5.
Write a C program that reads weight in pounds and converts it
into kilograms. Weight conversion formula is:
kilogram = pound * 0.453592
Exercise #6.
Write a C program to read the following 4 numbers, round them
off to the nearest integers and print them in integer form:
35.7 50.21 -23.73 -46.45
Exercise #7.
Write a C program that reads number of days from the user and
converts it to years, weeks and days. Assume that:
1 year = 365 days (ignoring leap year)
1 week = 7 days
For example, 374 days = 1 year/s, 1 week/s and 2 day/s.
CABSMJ1001 [2024-25]
Exercise #8.
Write a C program to read the amount value and break it into
the smallest possible indian currency notes. Assume that 500,
200, 100, 50, 20, 10, 5, 2 and 1 rupee notes are in use.
For example, Rs.5888 should be broken as below:
500 note(s) = 11
200 note(s) = 1
100 note(s) = 1
50 note(s) = 1
20 note(s) = 1
10 note(s) = 1
5 note(s) =1
2 note(s) = 1
1 note(s) =1
CABSMJ1001 [2024-25]
Exercise #9.
Write a C program that reads a floating point number and then
displays the right-most digit of the integral part of the number.
Exercise #10.
Write a C program that reads a 4-digit integer number and
displays the number as follows:
First line : all digits
Second line : all except first digit
Third line : all except first two digits
Fourth line : the last digit.
For example, the 4-digit number 5678 should be displayed as:
5678
678
78
8
CABSMJ1001 [2024-25]
End of
Unit-I