Tejalal Choudhary “Computer Programming” Fundamentals of “C” Store Data Operate on Data Perform I/O Control the sequence “Computer Programming” Fundamentals of “C” VARIABLES, CONSTANTS & DATA TYPES Tejalal Choudhary Department of Computer Science & Engg SDBCT, Indore tejalal.choudhary@sdbct.ac.in
Contents Need of variable, constant and data types Character set Constants Variables Data types
What is C ? Programming Language Simple and easy to use Many other languages are based on C Developed at AT & T’s Bell Laboratories of USA in 1972. Major parts of OS like Windows, UNIX, Linux is still written in C. Device driver are written in C Many mobile devices, consumer devices are based on C Popular gaming frameworks have been built using C Closely interact with the hardware devices. and so on…..
Analogy b/w English & C Steps in learning English language: Steps in learning Programming language: Alphabets Words Sentences Paragraphs Alphabets, Digits, Special Symbols Constants, Variables, Keywords Instructions Program
Identifier Any user defined name Can be a name of Variable, array, structure, Union ,Function etc. Should follow the naming rules of a language Naming Rules The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within identifier No special symbol other than an underscore
Constants An entity that doesn’t change Types Integer (10, 20) Real (10.56) Character (‘a’, ‘A’)
Variables Variable is an entity that may change. It’s a Named memory location Each variable has some address Store constant value If not initialized constrains garbage value Can store one value at a time i.e. x=10 x=20 X –> Variable 10, 20 –> constant
Variable A B
Data-type variable-name; Data Types Define Type for variable Range(max and min value it can store) Size(space required to store value) Data Type char short int long float double General Syntax for Variable Declaration Data-type variable-name; Char ch;
Byte Binary digit -> bit(0 or 1) One byte 0/1 One Byte can represent 28 =256 distinct values 0/1
Char(1 Byte) Char ch=‘A’; //65 64 +1 26+20 1 26 20 27 +26 +25 +24 +23 +22 +21 +20
Short(2 Byte) Short s=519; 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 29 + 22 + 21 + 20 512 + 4 + 2 +1=519
Short(2 Byte) 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20 +…….+ 214 1+2+4+8+16+ 32+64+128+256+ 512+ 1024+ 2048+ 4096+ 8192+ 16384= 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32767 + 1
Negative Numbers (Stored in 2’s complement) Binary of -32768 is same as 32768 When we try to store 32768 it becomes -32768 Binary of -32768 = 1’s Complement of 32768 +1 Binary of 32768 is : 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1’s complement 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 2’s complement 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Binary of -32768
Assigning one type to another Char ch=‘A’; //65 Short s=ch; 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 65
Assigning one type to another Short s=300; 28+25+23+22=256+32+8+4 Char ch=s; 25+23+22 = 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 1 0 0 44
Lab Assignment 2 WAP which accepts a lower case character from the user and display the corresponding upper case character.(Hint: use <string.h> header file and its functions) Take length & breadth of rectangle from user. WAP to calculate area of rectangle.