Lecture 7 Constants Variables and Data types in
Lecture 7 Constants, Variables and Data types in C
The C character Set A character denotes any alphabet, digit or special symbol used to represent information. Alphabets A, B, …. , Y, Z a, b, …. . , y, z Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Special Symbols ~‘!@#%^&*( )_-+= |{}[] : ; “‘< > , . ? /
Identifiers �Identifiers are names given to various program elements, such as variables, functions and arrays �A variable name is any combination of alphabets, digits or underscores �The first character in the variable name must be an alphabet
Variable �It is a data name which is used to store data and may change during program execution. It is opposite to constant. Variable name is a name given to memory cells location of a computer where data is stored. �* Rules for varibales: �First character should be letter or alphabet. �Keywords are not allowed to use as a variable name. �White space is not allowed. �C is case sensitive i. e. UPPER and lower case are significant. �Only underscore, special symbol is allowed between two characters. �The length of indentifier may be upto 31 characters but only the first 8 characters are significant by compiler.
Types of C Constants �C constants can be divided into two major categories � Primary Constants � Secondary Constants C Constants Primary Constants Integer Constant Real Constant Character Constant Secondary constants Array, Pointer Structure, Union Enum
Integer Constants �An integer constant must have at least one digit �It must not have a decimal point �It could be either positive or negative �If no sign precedes an integer constant, it is assumed to be positive �No commas or blanks are allowed within an integer constant 1990, 194, -394
Real Constants �Real constants(RC) must have at least one digit �It must have a decimal point �It could be either positive or negative �Default sign is positive �No commas or blank are allowed within a RC 194. 143, -416. 41
Character Constants �A character constant is either a single alphabet, a single digit or a single special symbol enclosed within single inverted commas �The maximum length of a character constant can be 1 character Eg ‘a’, ‘ 1’, ‘ 5’, ‘=‘ (Valid) ‘asd’, ‘ 12’ (Invalid)
String Constants �A String Constant consists of any number consecutive characters enclosed in double quotation marks �Escape sequence can be embedded within the string.
Keywords are the words whose meaning has already been explained to the C compiler They cannot be used as variable names. There are only 32 keywords available in c auto break case char const continue short double else enum extern near float far void if int long static do struct goto switch signed typedef while register union default return unsigned for
C Instructions � There are basically four types of instructions in C �Type declaration instruction Eg int sum; float ave; char name, code; �Input/Output instruction Eg scanf(), printf() �Arithmetic Instruction �Control instruction
Data Types C Supports several different types of data, each of which may be represented differently within the computers memory. Basic data types are listed below: Data Type int char float double Description integer quantity single character floating point number double-precision floating point number Typical Memory 2 bytes 1 bytes 4 bytes 8 bytes
Escape Sequences in C �Certain non printing characters can be expressed in terms of escape sequences Character bell backspace horizontal tab vertical tab newline quotation mark (“) question mark(? ) backslash () null Escape Sequence a b t v n ” ? \ ASCII Value 007 008 009 001 010 034 063 092 000
First C Program /* library function for input and output */ #include<stdio. h> main() { int a, b, sum; /*variable declaration*/ printf(“enter the first number: : n”); scanf(“%d”, &a); /*receiving data from user */ printf(“enter the second no: n “); scanf(“%d”, &b); sum =a+b; /* calculating the sum*/ printf(“the sum of two numbers: %dn“, sum); }
Preparing and Running a Complete C Program. �Planning a program �Design the algorithm for the given program �Statements must be selected and sequenced in the most effective manner �Writing a C program �Translate the algorithm into equivalent C instructions �Comments should be included within a C program �A well written program should generate clear, legible output. The program should be user interactive
Entering, Compiling and Executing �The program can be entered into the computer using an EDITOR. (In Unix vi editor is used) Eg. vi filename. c �Compiling and executing the program cc filename. c �The object code a. out is generated �The program can be executed by entering a. out in the prompt �Program asks for required input and generates the output
Assignment 01 Q 1 : Which of the following are invalid variable names and why ? � interestpaid � si-int � AVERAGE � Percent. � 123 � dist in km � ot pay � Name � FLOAT
ASSIGNMENT QUESTIONS Question 1. explain various data types available in C? Question 2. Explain the keywords and Constants in C ? What restrictions apply to their use?
Programming Assignment 01 Q 2 : Point out the errors, if any, in the following C statements: � int =314. 562 *150 ; � name = ‘Ajay’; � 3. 14 * r*r = area; � k=a*b+c(2. 5 a+b); � m_inst =rate of interest * numberofyears /100; � area = 3. 14 * r **2;
Assignment 01 Q 3 : A C program contains the following statements: #include<stdio. h> int i, j, k; write appropriate scanf and printf functions to enter and print the numerical values for i, j and k Q 4 : Each new c instruction has to be written on a separate line. (True/false)
- Slides: 20