Constants, Variables, and Data Types

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Fundamentals of Computer and programming in C
CSci 1130 Intro to Programming in Java
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Introduction to C Programming
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Data Types.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
The C Character Set: The Characters used to form words, numbers and Expression depends upon the computer on which program runs. Letters. Digits White spaces.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
C-Language Keywords(C99)
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
Week 1 Algorithmization and Programming Languages.
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Characters and Character Sets tMyn1 Characters and Character Sets The characters that can be used are the upper and lower case letters, A to Z and a to.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
CPS120: Introduction to Computer Science Variables and Constants.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Prepared By: K. U. Khimani Assistant Professor IT Department.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Basics of ‘ C ’ By Gaikwad Varsha P. Asst. Prof. Information Technology Dept. Govt. College of Engg. Aurangabad.
The Machine Model Memory
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Wel come.
C Language VIVA Questions with Answers
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
C AS A LANGUAGE Interface between computer & human being. ALPHABETS
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Chapter 2: Introduction to C++.
Lexical Elements & Operators
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Constants, Variables, and Data Types

Constants, Variables, and Data Types Like any other language, C has its own vocabulary and grammar. In this chapter, we will discuss the concepts of constants and variables and their types as they relate to C programming language.

Introduction The type of an object determines the set of values it can have and what operations can be performed on it. Variables and constants are the basic data objects manipulated in a program. Declarations list the Variables to be used, and state what type they have and perhaps what their initial values are.

Character Set The characters that can be used to form words, numbers and expressions depend upon the computer on which the program is run. However, a subset of characters is available that can be used on most personal, micro, mini and mainframe computers.

Character Set The characters in C are grouped into the following categories: 1. Letters 2. Digits 3. Special characters 4. White spaces The entire character set is given in Table 2.1 (page 23).

C tokens Fig. 2.1 C tokens and examples

Keywords and Identifiers Every C word is classified as either a keyword or an identifier. All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. The list of all keywords of ANSI C are listed in Table 2.3(page 24). All keywords must be written in lowercase. Some compilers may use additional keywords that must be identified from the C manual.

Identifiers refer to the names of variables, functions and arrays Identifiers refer to the names of variables, functions and arrays. These are user-defined names and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers.

Constants Constants in C refer to fixed values that do not change during the execution of a program. C supports several types of constants as illustrated in Fig. 2.2.

Constants See Page 25-26

Constants There are several constants in C,such as integer constant, floating-point constant, char constant, string constant, enumeration constant. Integer constant can be specified in decimal, octal, and hexadecimal.1234 is a decimal integer constant, 037 which has prefix ‘0’ is an octal integer constant, and 0x1f or 0x1F which has prefix‘0x’is a hexadecimal integer constant.

Constants /* exam2-3.c */ #include "stdio.h" main( ) { printf("Integer values\n\n"); printf("%d %d %d\n",32767,32767+1,32767+10); printf("\n"); printf("Long integer values\n\n"); printf("%ld %ld %ld\n", 32767L,32767L+1,32767L+10); getch( ); }

Constants char constant is an integer, written as one character within single quotes,such as ‘x’, including escape sequences like \n. These escape sequences look like two characters, but represent only one.

Constants In addition, an arbitrary byte-sized bit pattern can be specified by‘\ooo’, where ooo is one to three octal digits(0…7) or by‘\xhh’,where hh is one or more hexadecimal digits (0…9,a…f or A…F).So we might write : #define VTAB ‘\013’ /* ASCII vertical tab */ #define BELL ‘\007’ /* ASCII bell character */

Constants Or, in hexadecimal #define VTAB ‘\xb’ /* ASCII vertical tab */ #define BELL ‘\x7’ /* ASCII bell character */

Constants The complete of escape sequences is: \a Alert (bell) character \\ Backslash \b Backspace \? Question mark \f Form feed \‘ Single quote \n Newline \” Double quote \r Carriage return \ooo Octal number \t Horizontal tab \xhh Hexadecimal number \v Vertical tab

Constants The character constant ‘\0’ represents the character with value zero, the null character. A constant expression is an expression that involves only constants. Such expression may be evaluated during compilation rather than run-time, and accordingly may be used in any place that a constant can be occur.

Constants A string constant is a sequence of zero or more characters surrounded by double quotes, as in “it is a string”, or“” /* the empty string */ . The quotes are not the part of the string.

Constants Technically, a string constant is an array of characters. The internal representation of a string has a null character‘\0’at the end, so the physical storage required is one more than number of characters written between the quotes.

Constants This representation means that there is no limit to how long a string can be, but programs must scan a string completely to determine its length.

Constants Notice: ‘a’ is not the same as “a”. The former is an integer, used to produce the number value of the letter a in the machine’s character set. The latter is an array of characters that contains one character (the letter a) and the null character ( ‘\0’ ).

Variable In c, there are some restrictions on the names of variables and symbolic constants. Names are made up of letters and digits;the first character must be a letter. The underscore “_” counts as a letter; it is sometimes useful for improving the readability of long variable names.

Variable Don’t begin variable names with underscore, however, since library routines often use such names. Upper case and lower case letters are distinct, so x and X are two different names. Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants.

Variable ANSI standard recognizes a length of 31 characters. However, length should not be normally more than eight characters, since only the first eight characters are treated as significant by many compilers. .

Variable We tend to use short names for local variables, and longer names for external variables. Usually the length of variable names are less than 8 characters. Keywords like if, else, int, float, etc. are reserved: you can’t use them as variable names.

Data Types and Sizes C language is rich in its data types. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine. ANSI C supports three classes of data types: 1. Primary (or fundamental) data types 2. Derived data types 3. User-defined data types

Data Types and Sizes The primary data types and their extensions are discussed in this section. The user-defined data types are defined in the next section while the derived data types such as arrays, functions, structures and pointers are discussed as and when they are encountered.

Fig. 2.4 Primary data types in C

Primary data types and sizes char Range of values –128 to 127 int –32,768 to 32,767 float 3.4e–38 to 3.4e+e38 double 1.7e–308 to 1.7e+308

Data Types and Sizes In addition, there are a number of qualifiers that can be applied to there basic types. Such as short, long, signed, unsigned. short int sh; long int counter; /*here int can be omitted.*/

char or signed char 8 –128 to 127 unsigned char 0 to 255 int or signed int 16 –32,768 to 32,767 unsigned int 0 to 65535 short int or signed short int unsigned short int long int or signed long int 32 –2,147,483,648 to 2,147,483,647 unsigned long int 0 to 4,294,967,295 float 3.4E – 38 to 3.4E + 38 double 64 1.7E – 308 to 1.7E + 308 long double 80 3.4E – 4932 to 1.1E + 4932

Data Types and Sizes Different type objects have different storage bits. So the range of the represented value are not same. short is often 16 bits, long 32 bits, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.

Data Types and Sizes The qualifier signed or unsigned may be applied to char or any integer. Unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n,where n is the number of bits in the type.

Data Types and Sizes So, for instance, if chars are 8 bits, unsigned char variables have values between 0 to 255, while signed chars have values between –128 and 127. Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive. Look at the limits.h and float.h to get a reference.

Declarations All variables must be declared before use. A variable can be used to store a value of any data type.

Declarations A declaration specifies a type, and contains a list of one or more variables of that type: int lower, upper, step; char c, line[1000]; You can write it as : int lower; int upper; int step;

Declaration of Storage Class Variables in C can have not only data type but also storage class that provides information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized.

The storage class is another qualifier (like long or unsigned) that can be added to a variable declaration as shown below:

Table 2.10 Storage Classes and Their Meaning auto Meaning Local variable known only to the function in which it is declared. Default is auto. static Local variable which exists and retains its value even after the control is transferred to the calling function. extern Global variable known to all functions in the file. register Local variable which is stored in the register.

Assigning Values to Variables A variable may also be initialized in its declaration. char esc=‘\\’; float eps=1.0e-5; If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, and the initializer must be a constant expression.

Assigning Values to Variables An explicitly initialized automatic variable is initialized each time the function or block it is entered; the initializer may be any expression. External and static variables are initialized to zero or “”(for char array) by default.

Assigning Values to Variables Automatic variables for which there is no explicit initializer have undefined values(i.e., garbage). The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. It is can also be used with array arguments, to indicate that the function does not change that array argument.

/* ex1-3.c */ #include <stdio.h> /*print Fahrenheit-Celsius table for fahr=0,20,…,300*/ main( ) {float fahr,celsius; int lower,upper,step; lower=0; /* lower limit of temperature table */ upper=300; /* upper limit */ step=20; /* step size*/ fahr=lower; while (fahr<=upper) { celsius=5.0/9.0*(fahr-32); printf(“%3.0f %6.1f\n”,fahr,celsuis); fahr=fahr+step; } }