0% found this document useful (0 votes)
17 views57 pages

Int105identifires Keywords Datatypes1

The document provides an overview of the C programming language, covering essential components such as character sets, identifiers, keywords, and data types. It explains the structure of C programs, the rules for naming identifiers, and the classification of data types, including basic, derived, and user-defined types. Additionally, it includes examples of C programs and common programming concepts like input/output and format specifiers.

Uploaded by

mdkarif123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views57 pages

Int105identifires Keywords Datatypes1

The document provides an overview of the C programming language, covering essential components such as character sets, identifiers, keywords, and data types. It explains the structure of C programs, the rules for naming identifiers, and the classification of data types, including basic, derived, and user-defined types. Additionally, it includes examples of C programs and common programming concepts like input/output and format specifiers.

Uploaded by

mdkarif123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

INT105

Components of C
Identifiers and Keywords
Data types
OUTLINE
• In this lecture we will cover
• Character set
• Identifiers
• Keyword
• Data types
Language: its influence in our life
• Let us look to what we are doing since our childhood,
how did we learnt ENGLISH- A recap

A B C D …… X Y Z Characters

RAT BAT CAT COW Words

COW EAT GRASS Statements

ESSAY ON COW Programs


Introduction to C
• Like every language C programming language requires basic building
blocks to communicate with the computer.
• So we require Characters
• Character set
• Words(keywords and identifiers)
• Statement (instructions) Words
• Program
Statements

Programs
Character Set
• The character set of C represents alphabet, digit or any symbol used
to represent information.
Types Character Set

Uppercase Alphabets A, B, C, … Y, Z

Lowercase Alphabets a, b, c, … y, z

Digits 0, 1, 2, 3, … 9
~‘!@#%^&*()_-+=|\{}
Special Symbols []:;"'<>,.?/
White spaces Single space, tab, new line.
Meaningfulness
• Let us look to some words
• saslc, enp, keib, rac, llab

• Rearrange
• Class, pen, bike, car, ball

• This is the influence of adding meaning by logical and


sensible grouping in mode of communication through
language
Token
• Every single element in a C Program is Token
Token
• Smallest unit in a program/statement.
• It makes the compiler understand what is written in the
program.
• Example: main, printf , name,), etc.
• Tokens are broadly classified as:
• Identifiers
• Keywords
• Constants
• Variables
• Strings
• Operators
• Special character
Lets Identify the following:
square circle

ellipse square

circle ellipse
Identifiers
• So to identify things we have some name given to
them .
• Identifiers are the fundamental building blocks of a
program
• Used to give names to variables, functions, constant,
and user defined data.
• They are user-defined names and consist of a
sequence of letters and digits
Rules for naming an Identifier

1. An identifier name is any combination of 1 to 31


alphabets, digits or underscores.

2. The first character in the identifier name must be an


alphabet or underscore.

3. No blanks or special symbol other than an


underscore can be used in an identifier name.

4. Keywords are not allowed to be used as identifiers.


Some Identifiers
Tool_spanner;
both are different
tool_spanner;
FORMULA1;
engine_1;

Wrong identifiers name


1_engine;
break;
@car-roof;
C Keywords
• Keywords are the reserved words whose meaning
has already been explained to the C compiler.
• We cannot use these keywords as variables.
• Each keyword is meant to perform a specific function
in a C program.
• There are 32 keywords in C language.
• All keywords are written in lowercase only

Eg: The name of person can never be home, eat,


sleep, run, etc because these words have some
predefined meaning to perform some task.
List of C Keywords
auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while
Data Types
• Data type means the type of value a variable will have.
• It also defines memory space for a particular variable in computer.
• The type of value of variable can be alphabets or numbers.
• The numbers can be further divided as the integer or rational number.
• Lets see a mathematics problem:
My-Car
1. If the radius of car wheel is 15inch then what will the
distance traveled after one rotation of that wheel?
Sol: Given-
15 Integer( int in C )
radius = 15
dist_travelled = ?
So, Circumference of circle = 2 * pi * r
3.14 Real (float in C)
dist_travelled = 2 * 3.14 * radius
dist_travelled = 6.28 * 15 94.2 Real (float in C)
dist_travelled = 94.2 Ans.
My-Grades
2. Five students have appeared for Mathematics exam and their
respective marks are
84,34,97,58,64
consider the rank bands and their respective grades as
80 to 100 –A
60 to 79 –B
40 to 59 –C
less than 40 – D
So find the grade for each students?
Marks as integer Grades as character
Sol: Given-
char in C
M1=84, G1=? 84 A

M2=34, G2=? 34 D
char in C

M3=97, G3=? 97 A char in C

M4=58, G4=? C char in C


58
M5=64, G5=?
64 B char in C
Classification of Data Types
• In C data type is broadly classified as:
• Basic data types
• Derived data types
• User defined data types
Derived Data
Type
• Pointers
• Functions
• Array
Basic Data Type User Defined
• Integer Data Type
• Character • Structure
• Float • Union
• Double • Enumeration

Data
Type
List of Data Types
Type Size Minimal range
(bytes)
char 1 -128 to 127
unsigned char 1 0 to 255
int 2 or 4 -32768 to 32767
unsigned int 2 or 4 0 to 65535
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+38 with 6 digits of
precision
double 8 1.7e-308 to 1.7e+308 with 15 digits
of precision
long double 10 3.4e-4932 to 1.1e+4932 with 20
digits of precision
Integer
• It is used to store positive and negative counting Quick ya
numbers, as well as zero. Some in
used in
{...,-2,-1,0,1,2,...} commo
• No. o
in st
• Whe
• The numbers written in green box of My-Car problem auto
are the integers.

15 84 34 97
• The type modifiers for the integer data type are: signed, unsigned,
short, long .
• Signed types represent positive and negative numbers.
• Unsigned represent zero and positive numbers only.
• Long and short represent the range of integer number
Short Integer Long Integer
• Occupies 2 bytes in • Occupies 4 bytes in
memory. memory.
• Format specifier is %d or • Format specifier is %ld.
%i.
• Range is -2147483648 to
• Range is -32768 to
2147483647
32767.
• By default int variable is
short signed int. long radius=123456;
int cost=100; long int value;
short int si;
Signed Integer Unsigned Integer
• Occupies 2 bytes in • Occupies 2 bytes in memory
memory • Format specifier is %u.
• Format specifier is %d or %i
• There are also long signed • There are also long unsigned
integers having range from int with range 0 to
-2147483648 to 4294967295
2147483647
• Example:
• Example:
int firstvalue=10;
unsigned long count=567898;
long int WaterLevel; unsigned short int page;
Float
• Floating point numbers are real numbers that, unlike integers, may
contain fractional parts of numbers, like 1.446, -112.972, 3.267e+27.
• It is used to store real numbers with single precision i.e. a precision of
6 digits after decimal point.
• Format specifier is %f.
• The type modifier for float are float, double and long double.

• The rational number written in red box of My-Car problem are the
float numbers.

3.14 94.2
Type Float Double Long double

Storage Size 4 byte 8 byte 10 byte

Value range 3.4e-38 to 3.4e+38 1.7e-308 to 1.7e+308 3.4e-4932 to 1.1e+4932

Precision 6 decimal places 15 decimal places 20 decimal places

Example pi=3.141592 3.141592741012573 3.14159265358979323846


Character
• It stores a single character of data belonging to the C
character set.

• The alphabets written in blue box of My-Grades


problem are the character.

A D A B C
• It occupies 1 byte of memory.
• Format specifier is %c.
• The range is 0 to 255 for unsigned char.
• The range is -127 to 127 for signed char.
• Each char type has an equivalent integer interpretation, ASCII value,
so that a char is really a special kind of short integer.
char choice=‘y’;
Format Specifier
• Specifies the format according to which the value will be printed on screen in
C.
Example:
• %d : signed integer
• %ld: long integer
• %u : unsigned integer
• %c : single character
• %f : float
• %s : string
• %i : int
• Remember car example?

15
%d
Program 90
%d
3.14
%d

15
%d
Program 94.2
%f
3.14
%f
• Grade example:

84 %d %c A

34 %c D
%d
97 %d Program %c A

58 %d %c C

64 %d %c B
First C program
• #include <stdio.h>
• #include<conio.h>
• int main()
•{
• // printf() displays the string inside quotation
• printf("Hello, World!");
• return 0;
•}
How to input and output in C
• #include <stdio.h>
• int main() {
• int number;

• printf("Enter an integer: ");

• // reads and stores input
• scanf("%d", &number);

• // displays output
• printf("You entered: %d", number);

• return 0;
• }
PROGRAM
• #include <stdio.h>
• int main() {
• double a, b, product;
• printf("Enter two numbers: ");
• scanf("%lf %lf", &a, &b);

• // Calculating product
• product = a * b;

• // %.2lf displays number up to 2 decimal point


• printf("Product = %.2lf", product);

• return 0;
• }
programs
• #include<stdio.h>
• int main() {
• double first, second, temp;
• printf("Enter first number: ");
• scanf("%lf", &first);
• printf("Enter second number: ");
• scanf("%lf", &second);

• // value of first is assigned to temp


• temp = first;

• // value of second is assigned to first


• first = second;

• // value of temp (initial value of first) is assigned to second


• second = temp;

• // %.2lf displays number up to 2 decimal points


• printf("\nAfter swapping, first number = %.2lf\n", first);
• printf("After swapping, second number = %.2lf", second);
• return 0;
• }
program3
• #include<stdio.h>
• int main() {
• int intType;
• float floatType;
• double doubleType;
• char charType;

• // sizeof evaluates the size of a variable


• printf("Size of int: %d bytes\n", sizeof(intType));
• printf("Size of float: %d bytes\n", sizeof(floatType));
• printf("Size of double: %d bytes\n", sizeof(doubleType));
• printf("Size of char: %d byte\n", sizeof(charType));

• return 0;
• }
• #include <stdio.h>
• int main() {
• char c;
• printf("Enter a character: ");
• scanf("%c", &c);

• // %d displays the integer value of a character
• // %c displays the actual character
• printf("ASCII value of %c = %d", c, c);

• return 0;
• }
• Find out on which line no . you will get an error ?
• Line 1: int main ( )
• Line 2: {
• Line 3: print(“\n Hello World”)
• Line 4: }
• A] Line 1 B] Line 2 C] Line 3 D] Line 4
• What will be the output of the following program ?
• int main ( ) {
• int x=10,y=20;
• printf (“\n %d”,x,y); }
• A] 10
• B] 20
• C] 10 20
• D] None of these
• Which of the following language is predecessor to C Programming
Language?
• A] A
• B]B
• C] BCPL
• D]C++
• C was developed in the year ___
• A] 1970
• B] 1972
• C] 1976
• D] 1980
• C is a ___ language
• A] High Level
• B] Low Level
• C] Middle Level
• D] Machine Level
• C language is available for which of the following Operating
• Systems?
• A] DOS
• B] Windows
• C]Unix
• D] All of these
• Which of the following symbol is used to denote a pre-processor
statement?
• A] !
• B]#
• C] ~
• D];
• What is the valid range of numbers for int type of data?
• A] 0 to 256
• B] -32768 to +32767
• C] -65536 to +65536
• D] No
• specific range
• What is the output of the program
• #include<stdio.h>
• int main()
• {
• int x = 10, y = 20, z = 5, i;
• i = x < y < z;
• printf("%d\n", i);
• return 0;
• }

• 0
• 1
• Error
• None of these
• What is #include <stdio.h>?
• a) Preprocessor directive
• b) Inclusion directive
• c) File inclusion directive
• d) None of the mentioned
• What is the sizeof(char) in a 32-bit C compiler?
• a) 1 bit
• b) 2 bits
• c) 1 Byte
• d) 2 Bytes
Two ways to define constant in C

• There are two ways to define constant in C programming.


• 1. const keyword
• 2. #define preprocessor
C const keyword

• The const keyword is used to define constant in C programming.


• 1. const float PI=3.14;
• Now, the value of PI variable can't be changed.
• 1. #include <stdio.h>
• 2. #include <conio.h>
• 3. int main(){
• 4. const float PI=3.14;
• 5.
• 6. printf("The value of PI is: %f",PI);
• }
C #define preprocessor
• The #define preprocessor directive is used to define constant. n. It can
use any basic data type
• #include <stdio.h>
• 1. #define PI 3.14
• 2. int main()
• {
• 3. printf("%f",PI);
• 4. }
FILL THE MISSING
• #include<stdio.h>
_____ main()
• {
• float r,aoc, coc ;
• printf("Enter the radius : ");
• scanf(“____",&r);

• aoc=3.14*(r*r);
• coc = 2*3.14*r;

• printf("Area of Circle is : %f",_____);


• printf("\nCircumference of Circle is :_____",coc);

• }
• #________<stdio.h>
• int main()
• {
• int p,r,t;
• float i;
• printf("Enter the Principal, Rate and Time\n");
• scanf("%d_____",_p,_r,_t);

• /*Formula for calculating simple interest*/

• i=p*r*t/100;

• printf("simple interest is : %f",i);


• }

You might also like