C programming slide c02

29
Prepared by:- PRADEEP DWIVEDI (pur. B.TECH-IT) FROM- HINDUSTAN COLLEGE OF SCIENCE &TECHNOLOGY Mob-+919027843806 [email protected] C-PROGRAMMING SLIDE-2 6/7/22 1 PRADEEP DWIVEDI [pur.B.TECH-IT]

description

 

Transcript of C programming slide c02

Page 1: C programming slide c02

Monday, April 10, 2023

1

PRADEEP DWIVEDI [pur.B.TECH-IT]

Prepared by:-PRADEEP DWIVEDI (pur. B.TECH-IT)FROM-HINDUSTAN COLLEGE OF SCIENCE &[email protected]

C-PROGRAMMING SLIDE-2

Page 2: C programming slide c02

Monday, April 10, 2023

2

PRADEEP DWIVEDI [pur.B.TECH-IT]

Sample program.Format of simple c program.Constant ,variable and data types.C tokens.Some program and explanations.

TOPIC:-

Page 3: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

3

SAMPLE PROGRAM(SEE-SAMPLE.C)#include<stdio.h>#include<conio.h>void main(){clrscr();printf(“hello, I am pradeep”);getch();}

Page 4: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

4

#include directve

c programs are divided in to modules or functions.

some functions are written by users, like us , and many other are stored in the c library.

library functions are grouped category-wise and stored in different files known as header files.

if we want to access the function stored in the library , it is necessary to tell the compiler about the file to be accessed.

this is achieved by using the preprocessor directive #include.

Page 5: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

5

TERMs RELATED TO PROG

stdio.h:- standard input output header file

scanf() printf() conio.h:-consol input output header file. clrscr() getch()

(clear screen) (pause screen)

.h extension for header file.

Page 6: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

6

TERMs RELATED TO PROG

void is a return type that return nothing (blank return type)

main() is a predefine function that start the execution of our program.

with out main() function no program can be executed.

NOTE:- each and every function always followed

by parentheses sign. ()

Page 7: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

7

SAMPLE PROGRAM(SEE-SAMPLE.C) this program when executed will produce the

following output. hello, I am pradeep the first inform that the execution of our program

begin at this line. the main is a predefine(special) function used by the

c system to tell the compiler where the program starts.

every program must have exactly one main function. if we use more than one main function the compiler

can not understand which one marks the beginning of the program.

Page 8: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

8

SAMPLE PROGRAM(SEE-SAMPLE.C) the opening brace { in the second line marks the

beginning of the function main() and the closing brace } in the last line indicates the end of the function.

printf() is a standard predefine c function for printing output.

predefined means that it is a function that has already been written and compiled and linked together with program at the time of linking.

the printf function every thing between the starting and the ending quotation marks to be printed out.

semicolon(;) sign is called terminator. every statement in c should end with a semicolon.

Page 9: C programming slide c02

Monday, April 10, 2023

9

TERMs RELATED TO PROG

PRADEEP DWIVEDI [pur.B.TECH-IT]

clrscr(); is a predefine function that clear the output screen.

it comes from conio.h header file. getch(); is also a predefine function

which is used to pause the output screen.

Page 10: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

10

FORMAT OF SIMPLE C PROGRAMmain() function name

{ start the program

} end the program

program statements

(body)

Page 11: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

11

CHARACTER SET

The character in c are grouped into the following categories.

1. Letters2. Digits3. Special characters4. White space

Page 12: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

12

C TOKENS

in a passage of text individual words and punctuation are called tokens.

Similarly, in c program the smallest individual units are known as c tokens.

C TOKENS

KEYWORDS

IDENTIFIERS

CONSTANT

SPECIAL SYMBOLS

OPERATORS

Page 13: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

13

CONSTANTS

CONSTANTS

NUMERIC CONSTANTS

INTEGERCONSTANT

REAL CONSTANT

CHARACTERCONSTANTS

SINGLE CHARACTER

STRING CONSTANT

Page 14: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

14

VARIABLES

Variables are a place holder that holds the place in computer memory for the value.

Because its value may be varied that’s why it is known as variable.

int a; (this is called variable declaration). data type variable. int a=10;(this is called initialization of

variable.

Page 15: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

15

DATA TYPES

data type decide that what kind of value should be held by the variable.

in c data types are classified into three categories.

I. primary(or fundamental data) types.II. derived data types.III. user defined data types.

Page 16: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

16

DATA TYPES

PRIMARY DATA TYPES

INTEGERCHARACTE

RFLOATING

POINT TYPEVOID

Page 17: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]17

DATA TYPESGROUP DATA TYPE SIZE(in bits) RANGE

characster char or signed char

8 -128 to 127

- unsigned char 8 0 to 255

integer int or signed int

16 -32768 to 32767

- unsigned int 16 0 to 65535

- short int or signed short int

8 -128 to 127

- unsigned short int

8 0 to 255

- long int or signed long int

32 -2,147,483,648 to +2,147,483,647

floating point float 32 3.4e-38 to 3.4e38

- double 64 1.7e-308 to 1.7e+308

- long double 80 3.4e-4932 to 1.1 e+4932

Page 18: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

18

DATA TYPES

void type:- void has no value. this is usually used to specify the type of

function.

Page 19: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

19

KEYWORDS AND IDENTIFIERS KEYWORDS are the reserve words those

are having some predefine meaning and those are only use for their predefine meaning.

all keywords in c are in lower case. in c we have 32 keywords. eg, if, else, for, while, char, int etc. IDENTIFIERS refers to the names of

variable, function and arrays.

Page 20: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

20

NAMING RULES AND CONVENTIONS FOR THE VARIABLE NAME(IDENTIFIERS)

a variable name can be anything but it should be short and meaningful.

a variable name can not start with a digit. it must be started with an alphabet letter. but a variable name can be followed by n number of digits.

it can be started with an underscore sign(_) also. we can not use any special symbol like @,#,*,& in a

variable name. we can not use any space in a variable name. if we

are having mare than one word in a variable name and space is required that time we can use the underscore(_) sign between them.

my_name ,myName. any keywords can not be a variable name.

Page 21: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

21

prog:1

/*WRITE A PROGRAM TO ACCEPT A NUMBER FROM THE USER AND PRINT THEM*/

#include<stdio.h>#include<conio.h>void main(){int number;clrscr();printf("Enter a number");scanf("%d",&number);printf("the number is: %d",number);getch();}

Page 22: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

22

TERMs RELATED TO PROG

printf() is a predefine function with the help of that we can print the data in any specified format or agates if it is in double quotes. (“ “);

scanf() is again a predefine function that can accept data in any specified format.

Page 23: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

23

FORMAT STRING

format string is used to specify the format of the data .

these are following type of:- %d (decimal) %f (float) %c (character) %s (string) %x (hexadecimal)

Page 24: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

24

ADDRESS OPERATOR(&)

It is used to keep the address of that place where the value is placed.

Page 25: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

25

prog4

/*write a program to convert a character to its ascii value*/#include<stdio.h>#include<conio.h>void main(){char alph;clrscr();printf("Enter a character");scanf("%c",&alph);printf("value of character is %c\n",alph);printf("value of character equivalent is %d\n",alph);getche();}

Page 26: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

26

SOME POINTS

note:- each and every character is internally treated as an integer.

getch(); is a predefine function that accept a character on the output screen . it just wait for a character.

getche(); is having same functionality like getch() function but different is that it displayed the accepted character on the consol screen.

getchar(); is also a predefine function that accept a character but that character must be followed by the enter key.

note:-drawback of scanf function is that it never accept the space.

Page 27: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

27

COMMENTS

commets are used to give the better readability of a program.

at the time of program execution comments are ignored by the compiler.

these are two types of:1. //single line comments2. /* multiple line comments */note:-we can not use nested comments.

Page 28: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]

28

RETURN TYPE

It decide what kind of value should be returned by the function.

if we don’t use any return type before the function that time by default there is int.

return is a keyword with the help of that we can return a value and a value is always return where the function is being called.

Page 29: C programming slide c02

Monday, April 10, 2023PRADEEP DWIVEDI [pur.B.TECH-IT]29

THANKS