Documents

cmpe150_lecture_notes.docx

Description
4. variables and constants 1. Variable declaration Variables are data objects which keep values in the memory. A variable is declared by using the following syntax. dataType variableNameList; o Each variable must have a data type and have an unique name, i.e., you can not define different variables with the same name. o A variable must be declared before it is used. o Variables can only be declared at the beginning of any block. o In a single declaration statement, there can be mor
Categories
Published
of 77
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  4. variables and constants 1.   Variable declaration Variables are data objects which keep values in the memory. A variable is declared by using the following syntax. dataType variableNameList; o   Each variable must have a data type and have an unique name, i.e., you can not define different variables with the same name. o   A variable must be declared before it is used. o   Variables can only be declared at the beginning of any block. o   In a single declaration statement, there can be more than one variable name which are separated by commas. But you can only define the variables of the same data type in a single declaration statement. o   When you define a variable, its value is garbage (its value can not be predicted), but there are exceptions. But it is also possible to initialize the value of a variable when you define it. Note that initializer must be constant value. o   For printf() and scanf() functions, the type of variable should match up with the corresponding conversion specifier, otherwise an undefined behavior occurs. o   x ≠ 'x' ≠ x  x: it is the name of a variable. 'x': it is a character x. x : it is a string x. o   9 ≠ '9' ≠ 9  9: it is the integer constant 9. '9': it is a character 9, and it has the value of 57 in ASCII code. 9 : it is a string 9. FILE:variables.c Load Previous Saves/Versions:0-1-2-4-8-16-32-64-128-256-512 Empty  SAVE (Ctrl-S)COMPILE (Ctrl-F7) 1 2 #include<stdio.h> int  main(){   UNDO (Ctrl-Z)REDO (Ctrl-Y) { beautify(); }LOAD DEFAULT CODE 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 int num1 = 0, num2, sum ; float  Num1; long double tax = 4.3; double second ; long factorial = 40L; char  myName = 'a', t; char first = '\n'; double no1 = 3.4, no2 = 5.6, no3, no4 = -132; float last; scanf( %d%lf%ld%c ,&num1,&second,&factorial,&f irst); scanf( %f ,&Num1);  printf( Different numbers: %d\t%ld\t%Lf\n ,num2,factorial,tax);  printf( More than this, %c %c ,myName,first); anykey(); return 0; }  2.   Rules for variable names These rules are also valid for identifier names, e.g., variable names, function names, symbolic constant names, etc. 1.   They must consist of letters and digits (The underscore character is counted as a letter). The names of variables do not need to be meaningful. 2.   The names of variables must begin letters. It is also possible to begin with the underscore character, but it is not recommended since library routines often use such names. 3.   Do not forget that C is a case sensitive, i.e., x and X are different.  4.   At least 31 characters of an internal name are significant (it depends on a compiler). 5.   Keywords like int, main, float, return, long, unsigned, const, if, etc are reserved and can not be used as identifier names. Here are some valid identifier names:  My_Name8 number_  _number CMPE fdjkfhdg34ffs x9875424 And some invalid identifier names are listed as follows: 8Name_ 4 4563 number+ data\% return  3.   Constants They are values defined as constants, you can not change their values in the execution of the program. We can define constants by two different ways. 0.   By using reserved word const. It must be defined in the declaration part, note that a memory location is allocated for the constant. const type name var name = constant value; const double PI = 3.14; 1.   By using define. Actually it is a substitution and no memory place is allocated. #define PI 3.14 If you want to define PI as long double use #define PI 3.14L Note that we can use define for other purposes (look at subject macros). 4.   Enumeration constants It is for defining a list of constant integer values, remember that character constant values are actually small integer values. It is alternative to multiple #define's.  enum  Month{JAN = 1, FEB, MAR}; enum  BOOLEAN{no, yes}; enum  my_numbers{three = 3, one, FOUR = 2, five}; enum  specialCharacters{TAB = '\t', NEWLINE = '\n', BACKSLASH = '\\'}; o   They behave like integers. o   Unless it is specified, the values of enumeration constants begins with 0. The other unspecified ones continues from the last specified one. o   Names in different enumerations must be distinct. o   Values do not need to be distinct in the same or different enumeration. enum  boolean{FALSE,TRUE}; int main (){ int a; a = FALSE; } enum  boolean{FALSE,TRUE}; int main (){ enum  boolean b;  b = 9; /* it gives warning */  b = TRUE; } int main(){ enum  boolean{FALSE,TRUE}; int a; enum  boolean b; a = FALSE;  b = 9; /* it gives warning */  b = TRUE; } 5.   Assignment and type conversions When a narrower type is assigned to a wider one, the value is assigned without losing information. When a wider type is assigned to a narrower one, we may lose some information. int a; float b; char c; a=3.14; /* a = 3 */  b=8; /* b = 8.0 */ c='0'; /* c = '0' */   b=9.2; /* b = 9.2 */ a=b; /* a = 9 */  b=a; /* b = 9.0 */ a=c; /* a = 48, since ASCII code of '0' is 48 */  b=c; /* b = 48.0 */ c=49; /* c = '1', since ASCII code of '1' is 49 */ c=50.2; /* c = '2' */  2. Introduction To C
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks