Top Banner
Variables, Datatype, Constants, Keywords, Comments, Rules for Writing C program Part 2 By: Er. Aman Kumar
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Variables in C and C++ Language

Variables, Datatype, Constants, Keywords,

Comments, Rules for Writing C program

Part 2By: Er. Aman Kumar

Page 2: Variables in C and C++ Language

Variables

• Variable is simply name given to memory location which acts as placeholder or container for storing data. It may help to think of variables as a container for a value.

Page 3: Variables in C and C++ Language

Data type

• We can store variables in our computer memory. Since each type of data takes different amount of memory, like integer takes 2 bytes, decimal numbers take 4 byte, and SINGLE character takes 1 byte. So we have to tell computer about the type of data we are going to store in our variable

Page 4: Variables in C and C++ Language

• Predefined/inbuilt :These datatypes are inbuilt. eg : char, int, float, double

• Drived Datatype These datatypes are inherited from predefined datatype. eg : arrays, pointer.

• Userdefined datatype : These are created by user for his/her own purpose

• eg : typedef, enum, structure, union

Page 5: Variables in C and C++ Language
Page 6: Variables in C and C++ Language

Predefined Datatypes

Page 7: Variables in C and C++ Language
Page 8: Variables in C and C++ Language

Character set• Character set: every character is assigned unique

number called character set. C/C++ follows ASCII character set.

• ASCII: American standard code for information interchange

• Here lowercase, uppercase characters (English), special symbols, digits have been assigned unique integer number.

• Total 256 characters are there. • Range is: -128 to 127 • Here limitation is only English is used.

Page 9: Variables in C and C++ Language

Identifiers

• Identifier is simply a name given to your variable, class, union, structure, function, etc.

• Rules for creating valid identifiers: • 1. An identifier can be any combination of

alphabets, digits, underscore. • 2. Neither spaces nor special symbol other

then underscore can be used. • 3. It won‘t begin with a digit

Page 10: Variables in C and C++ Language
Page 11: Variables in C and C++ Language

Declaration and initialization of variable

• How to declare variable?• Declaration of variable means specify data type

of variable and name assigned (identifier).• A variable declaration provides assurance to the

compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable.

Page 12: Variables in C and C++ Language

• syntax :datatype varible_name;

eg :int a;float b; char c;double first;int i, j, k;

Page 13: Variables in C and C++ Language

Initialization

• It means assigning the values to variable

Page 14: Variables in C and C++ Language

Declaration and initialization in single step

int a = 10;

float b = 30.05;

char c = 'a'; (Value to characters always assigned in single

quotes)

Page 15: Variables in C and C++ Language

Keywords

• Are simply the reserved words whose meaning has been explained in library of C language.

• There are 32 keywords in c language. • We can’t use keywords as identifiers or

variables because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer

Page 16: Variables in C and C++ Language

List of Keywords

Page 17: Variables in C and C++ Language

int float; char if; Int void; Above will give error as we are using keywords

as identifier or variable name

Page 18: Variables in C and C++ Language

Constants

• Is an identity which does not change its value. Once the value is initialized, that can’t be changed.

• Constants are just like variables only condition is that their value doesn’t change.

• We will use const keyword for declaring constants

Page 19: Variables in C and C++ Language

Comments

• Comments are that part of program, which are ignored by compiler. These are used for better readability of user. Compiler doesn‘t compile the text written inside comments.

• The comment describes the purpose of the code in the file, and might include some license or copyright information.

• It is not compulsory to have comments in our program, but its good practice and what most C programmers will expect to find.

Page 20: Variables in C and C++ Language

Single Line Comment

// is used to make single line comment. All the text in a line after // will become part of comment and will be ignored by compiler.

• E.g. int a; // this is ignored

Page 21: Variables in C and C++ Language

Multiline Comment

This starts from /* and ends at */. All the text in between these will be part of

comment. E.g. /* This Is Multiline Comment */

Page 22: Variables in C and C++ Language

Rules for writing c program• Every instruction in c program is written as separate

statement because C program is collection of statements • There is no particular hard and fast rule for placing the

statements in our program. We can place it anywhere in our program, that why C is known as free form language.

• Every statement will end with ; (semi-colon). So it is also known as statement terminator.

• Proper comments and indentation should be given in c program for better readability

• C is case sensitive language. It differs in lower and upper case characters.

• We can‘t declare variable with same name multiple times in single block.

Page 23: Variables in C and C++ Language

FB Page : www.facebook.com/way2itechFB ID : fb /amankumar29

Like, Subscribe & ShareDo comment, if you have any doubt!