structured programming unit2 elements of the c language [3hrs] · basic elements of c language they...

25
Structured Programming Unit2 Elements of the C Language [3hrs] http://genuinenotes.com

Upload: others

Post on 05-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Structured ProgrammingUnit2

Elements of the C Language [3hrs]

http://genuinenotes.com

Page 2: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Basic Elements of C language ❖ They are basic building blocks used to cre

ate a C program.1. Character set

2. Data types

3. Identifiers and Keywords

4. Variables and constants

5. Special symbols

6. Operators

7. Expressions and statements

8. Arrays

9. Function http://genuinenotes.com

Page 3: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

C Character Setalphabets A to Z, letters a to z, the digits 0 to 9 and certain special characters as

+ - * / = % & #! ? ^ “ ‘ ~ \ | < > () [ ]{ } : ; . , _ @$ (blank space)

http://genuinenotes.com

Page 4: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

C Data Types

Data type Description Size Range

char single character 1 byte 0 - 255

int integer number 4 bytes

-2147483648 to 21474836

47

float

single precision floatin

g point number (numbe

r containing fraction &

or an exponent) 4 bytes 3.4E-38 to 3.4E+38

double

double precision floatin

g point number 8 bytes 1.7E-308 to 1.7E+308

http://genuinenotes.com

Page 5: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

C Data Types

Data type Size Range

short int 2 bytes -32768 to 32767

long int 4 bytes -2147483648 to 2147483647

unsigned short int 2 bytes 0 to 65535

unsigned int 4 bytes 0 to 4294967295

unsigned long int 4 bytes 0 to 4294967295

long double (exten

ded precision) 8 bytes 1.7E-308 to1.7E+308

http://genuinenotes.com

Page 6: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Identifiers ◼ Identifiers are names given to identify various items in the

program, such as variables, functions and arrays.

◼ An identifier consists of letters and digits, in any order, exc

ept that the first character must be a letter.

◼ Both upper and lowercase letters are permitted. Upper and

lowercase letters are however not interchangeable (i.e., an

uppercase letter is not equivalent to the corresponding low

ercase letter).

◼ The underscore character (_) can also be included, and it is

treated as a letter. Keywords like if, else, int, float, etc., hav

e special meaning and they cannot be used as identifier na

mes.

http://genuinenotes.com

Page 7: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Identifiers Naming Rules

◼ Identifier names must consists of combination of only letters, digits, underscore and must begin with an alphabet (or underscore)

◼ An identifier should not contain a space.

◼ Underscore is permitted between two digits and must begin with a letter

◼ An identifier name should have less than 31 characters. Only first 31 characters are significant.

◼ Any standard C language keyword cannot be used as a identifier

◼ It is case sensitive, i.e. uppercase and lowercase letters are not interchangeable.

http://genuinenotes.com

Page 8: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Examples of valid identifier

◼ X

◼ Y1

◼ name123

◼ stud_name

◼ _row

◼ circumference

◼ volume

◼ TOTAL

http://genuinenotes.com

Page 9: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Examples of invalid identifier

1st - the first character must be a letter

"Jamshedpur" - illegal characters (")

‘Y’ - illegal characters (‘)

stud-name - illegal character (-)

stud name - illegal character (blank space)

http://genuinenotes.com

Page 10: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

C Keywords◼ Keywords are predefined words and they have

fixed meanings in C and their meaning cannot be changed.

◼ These keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers.

◼ All keywords are lowercase. Since uppercase and lowercase characters are not equivalent in C language, it is possible to utilize an uppercase keyword as an identifier.

http://genuinenotes.com

Page 11: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Some standard keywords in C are

auto extern sizeof break floaten static case forstruct char goto switchconst if typedef continueint union default longunsigned do rgister voiddouble return volatile elseshort while enum signed

http://genuinenotes.com

Page 12: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Variable◼ Variables are simply names used to refer to some dat

a item (value with which we are working) in a C program. Initially, data item must be assigned to the variable at some point in the program.

◼ The data item can then be accessed later in the program simply by referring to the variable name.

◼ A variable declaration consists of a data type, followed by one or more variable names, ending with a semicolon.

◼ Variable declaration example:

◼ int m, n;

◼ short a,b;

◼ float x;

◼ char name[20];http://genuinenotes.com

Page 13: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Initialing variables◼ A variable can be declared and initialized with values a

t the time of declaration.

int c = 5;

char reply = 'Y';

double d = 4.64386237445675;

char state[] = "ANDHRA PRADESH";

float eps = 1.0e-5;

We can also assign a variable the value of another variable.int a=5;int b=a;

http://genuinenotes.com

Page 14: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Constants

◼ Constants in C refer to fixed values that do not change during the execution of a program. The constants in C can be classified into four categories

1. integer constants, [5]

2. floating point constants, [10.991]

3. character constants and ['A']

4. string constants. [“Kathmandu”]

http://genuinenotes.com

Page 15: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Literal Constants

◼ Anytime within a program in which you specify a value explicitly instead of referring to a variable or some other form of data, that value is referred to as a literal. In the initialization example below, 3 is a literal. Literals can either take a form defined by their type or directly insert data into a variable regardless of its type.

◼ int somenumber=3;

http://genuinenotes.com

Page 16: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Symbolic constants◼ A symbolic constant is a name that substitutes for a se

quence of characters.

◼ A symbolic constant allows a name to appear in place of a numeric constant, a character constant or a string constant.

◼ They are usually defined at the beginning of a program.

◼ Example,

◼ #define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is preprocessed, all occurrences of the symbolic constant PI are replaced with the replacement text 3.141593.

http://genuinenotes.com

Page 17: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Preprocessor Directives◼ Preprocessor directives are special statements that ar

e processed before compilation of any other source code in the program (executed at the beginning of a compilation process). They are placed in the source program before main function.

Preprocessor directives Meaning

#include<stdio.h> Used for linking header file

#define PI 3.1416 Used for defining symbolic constant PI

#define TRUE 1 Used for defining TRUE as 1

http://genuinenotes.com

Page 18: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Solved Example1

#include <stdio.h>

#define PI 3.141593

#define RADIUS 10

int main(void)

{

float area;

area = 2 * PI * RADIUS * RADIUS;

printf("\nArea of the circle: %f", area);

return 0;

}

http://genuinenotes.com

Page 19: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Escape Sequences◼ An Certain nonprinting characters, such as the backslash (\) an

d apostrophe ( ‘ ), can be expressed in terms of escape sequenc

es in C. An escape sequence always begins with a backward sl

ash and is followed by one or more special characters.

Character value Escape Sequence ASCII

bell (alert) \a 007

backspace \b 008

horizontal tab \t 009

vertical tab \v 011

newline (line feed) \n 010

form feed \f 012

carriage return \r 013

quotation mark (“ \” 034

apostrophe (‘) \’ 039

question mark (?) \? 063

backslash (\) \\ 092

null \0 000

http://genuinenotes.com

Page 20: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Type Conversion &Type Casting◼ The process of converting entity of one data type to another da

ta type is called as type casting or type conversion.

◼ There are two categories of conversion:

◼ A) Implicit type conversion: In implicit type conversion com

piler automatically converts one data type into another and it i.

e. generally obtained through arithmetic expression.

◼ B) Explicit type conversion: When a user explicitly convert t

he one data type into another then it is called as the type castin

g or explicit type conversion in which programmer specifies s

pecial programming instruction what data type to convert.

http://genuinenotes.com

Page 21: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Example of Conversion

Implicit type Conversion Exampleint a;a=5/2; //will produce integer value as 1float x = 7/5; //will produce value as 1.0

Explicit type Conversion Examplex = (float) 7/5; //will produce value as 1.40

http://genuinenotes.com

Page 22: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Things to Remember◼ In C language type conversion is an arithmetic expre

ssion will be done automatically. This is called implic

it conversion.

◼ If we want to store a value of one type into a variable

of another type, we must caste the value to be stored

by preceding it with the type name in parenthesis. Thi

s is called explicit conversion or type casting.

◼ Remember the type Conversion is performed by the c

ompiler but a casting is done by the programmer.

http://genuinenotes.com

Page 23: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Type Conversion Rules

◼ In type conversion, the data type is promoted from lo

wer to higher because converting higher to lower invo

lves loss of precision and value.

◼ Integer types are lower than floating point types

◼ Signed types are lower than unsigned types

◼ Short whole number types are lower than longer types

◼ double>float>long>int>short>char

http://genuinenotes.com

Page 24: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Solved Example2#include<stdio.h>

int main(void)

{

int a, b, c;

float x, y;

a=5;

b=2;

x=a/b; //c=2.0 Automatic (or) implicit conversion

y=(float) a/b; //d=2.50 Type casting (or) explicit conversion

printf ("\n a = %d, b=%d, x = %f , y=%f", a,b,x,y);

return 0;

}

http://genuinenotes.com

Page 25: Structured Programming Unit2 Elements of the C Language [3hrs] · Basic Elements of C language They are basic building blocks used to cre ate a C program. 1. Character set 2. Data

Type Definitions◼ C supports a feature known as “type definition” that allow

s users to define an identifier that would represent an existi

ng data type.

◼ A type definition is used to create an alias name for anothe

r data type.

◼ In C programming languages they are introduced using th

e typedef reserved keyword and takes the following general

form:

◼ typedef type identifier;

◼ Some example of type definition are:

◼ typedef int unit;

◼ typedef float marks;

◼ Here the type unit symbolizes as a synonym of the type int

and marks symbolizes float.

http://genuinenotes.com