Lecture 2 Unit1 DataTypes
Lecture 2 Unit1 DataTypes
2
Introduction to C
• The Unix operating system and virtually all Unix applications are
written in the C language. C has now become a widely used
professional language for various reasons.
• Easy to learn
• Structured language.
• It can handle low-level activities.
• It can be compiled on a variety of computers.
3
Facts about C
4
Why to use C
• C was initially used for system development work, in particular the programs that make-up the operating system.
Some examples of the use of C might be:
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Data Bases
• Language Interpreters
5
C-
Program
Structure
6
Process of Compiling a ‘C’ Program
8
Important Points
• White Spaces (ie tab space and space bar ) are ignored.
• Statements can continue over multiple lines
• Comments: are used to give additional useful information inside a
C Program. All the comments will be put inside /*...*/ (Multi-line
Comment) or //(End-of-line Comment)
9
Preprocessor directive
#include <stdio.h>
The "#include" is called a preprocessor directive. A preprocessor
directive begins with a # sign, and is processed before compilation.
The directive "#include <stdio.h>" tells the preprocessor to include
the "stdio.h" header file to support input/output operations.
10
int main() { ...... }
11
C Program Template
12
Program
• A program is a sequence of
instructions (called programming
statements), executing one after
another usually in
a sequential manner
13
Online C Compiler
https://www.onlinegdb.com/online_c_compiler
14
First Sample Program
15
printf("Hello, world!\n");
• Invoke the function printf() to print the string "Hello, world!"
followed by a newline (\n) to the console.
• The newline (\n) brings the cursor to the beginning of the next
line.
16
Return statement
• return 0;
terminates the main() function and returns a value of 0 to the
operating system. Typically, return value of 0 signals normal
termination; whereas value of non-zero (usually 1) signals abnormal
termination. This line is optional. C compiler will implicitly insert a
"return 0;" to the end of the main() function.
17
Case Sensitive
18
Variable
19
Data Types
20
Basic Data Types
Int (Integer)
Store integer type of data.
• 2 bytes (16 bits) on a 16-bit machine.
• The modern systems have 32 or 64-bit configurations. The size of
an integer in such environment is 4 bytes with a starting value of -
2,147,483,648 and an ending value as 2,147,483,647.
21
Basic Data Types
Float
• This datatype is also numeric but allows numbers with decimal
places. It has a size of 4 bytes and has a range of 3.4E-38 to
3.4E+38.
• The float type has a precision up to 7 digits.
22
Basic Data Type
Double
• It is same as float but with a higher precision range that is 8
bytes which gives it an ability to store more numerals after the
decimal places, double to be exact.
• It has a length of 8 bytes and has a range of 1.7E +/- 308. The
double data type has a precision up to 15 digits.
23
Basic Data Types
Char
Char data type by default allows a single character such as a letter
from the alphabet. It has the size of just 1 byte with a range of -128
to 127 or 0 to 255.
24
TYPE SIZE (Bits) Range
char or signed char 8 -128(-27) to 127(27-1)
unsigned char 8 0 to 255
int or signed int 16 -32768(-215) to 32767 (215 - 1)
unsigned int 16 0 to 65535
short int or signed short 8 -128 to 127
int
unsigned short int 8 0 to 255
long int or signed long int 32 -2147483648 to 2147483647
or long
unsigned long int 32 0 to 4294967295
float 32 3.4 e-38 to 3.4 e+38
double 64 1.7e-308 to 1.7e+308
long double 80 3.4 e-4932 to 3.4 e+4932
int main() {
return 0;
27
Program
WAP to multiply
floating point numbers
28
WAP to find area of circle
• #include <stdio.h>
• int main(void) {
• float pie = 3.14;
• int radius = 6;
• printf("The radius of the circle is %d " , radius);
• float area = (float)(pie* radius * radius);
• printf("The area of the given circle is %f", area);
• return 0;
• }
29
• #include <stdio.h>
• int main(void) {
• float pie = 3.14;
• int radius = 6;
• printf("The radius of the circle is %d " , radius);
• float area = (float)(pie* (pow(radius,2)));
• printf("The area of the given circle is %f", area);
• return 0;
• }
30
• #include < stdio.h >
• #include < conio.h > #define PI 3.141
• int main()
• {
• float radius, area;
• printf("Enter radius of circle\n");
• scanf("%f", & radius);
• area = PI * radius * radius;
• printf("Area of circle : %0.4f\n", area);
• getch();
• return 0;
• }
31
WAP to divide two numbers
#include<stdio.h>
int main()
{
int num1,num2,div;
printf("\tEnter Two Numbers\n");
printf("---------------------------\n");
printf("Enter First Number : ");
scanf("%d", &num1);
printf("\nEnter Second Number : ");
scanf("%d",&num2);
div=num1/num2;
printf("\nDivision of %d & %d is = %d",num1,num2,div);
return 0;
}
32
WAP to print ASCII value
#include <stdio.h>
In C programming, a character int main() {
variable holds ASCII value (an
char c;
integer number between 0 and 127)
rather than that character itself. This printf("Enter a character: ");
integer value is the ASCII code of the scanf("%c", &c);
character.
33
WAP to multiply floating point
numbers
#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
return 0;
}
34