3_Data Type_Variables_Constants_Data Input.pdf
Document Details
Uploaded by DoctorVisionaryAngel
Seneca Polytechnic
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- University of Santo Tomas Pre-Laboratory Discussion of LA No. 1 PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
Full Transcript
PRG 155 – Programming Fundamentals Using C 3. Variables Variable is a named storage location (memory location), that stores a value of a particular data type (Hock-Chuan). Declaration syntax: data_type variable_name; where data_type represents the variable data type variable_name is a...
PRG 155 – Programming Fundamentals Using C 3. Variables Variable is a named storage location (memory location), that stores a value of a particular data type (Hock-Chuan). Declaration syntax: data_type variable_name; where data_type represents the variable data type variable_name is a variable name Multiple variables of the same data type may be declared in the same statement where names are separate by commas. Example: int number1, number2, sum; Variables can be initiated in the declaration statement by using the assignment operator (=). Example: int number1 = 1; Important to remember: Every variable must be declared before it is used. Variable can only store a value of a specified data type. A variable may take different values during the program execution. Each declaration statement must end with semi-colon (;). Variable name Since the variable names are identifiers, the naming convention must follow the rules given for identifiers. 1 PRG 155 – Programming Fundamentals Using C Data types Data Types Basic User-defined Data (Primitive/Primary) Derived Data Types Types Data Types Character (char) Array Structure Integer (int) Pointer Union Single precision floating point Function Enumeration (float) Double precision floating point (double) No value available (void) 2 PRG 155 – Programming Fundamentals Using C “Primary Data Type.” n.d. Online Image. Studytonight. 23 Jan, 2017. http://www.studytonight.com/c/datatype-in-c.php Note: signed, unsigned, short, and long are type modifiers. Data Type Keyword Storage Value range size Character char 1 byte -128 to 127 or 0 to 255 Unsigned character unsigned char 1 byte 0 to 255 Signed character signed char 1 byte -128 to 127 Integer int 2 OR -32,768 to 32,767 OR 4 bytes -2,147,483,648 to 2,147,483,647 Unsigned integer unsigned int 2 OR 0 to 65,535 OR 4 bytes 0 to 4,294,967,295 Short integer short int 2 bytes -32,768 to 32,767 Unsigned short integer unsigned short int 2 bytes 0 to 65,535 Long integer long int 4 bytes -2,147,483,648 to 2,147,483,647 Unsigned long integer unsigned long int 4 bytes 0 to 4,294,967,295 Float float 4 bytes -1.2E-38 to 3.4E+38 Precision: 6 decimal places Double double 8 bytes 2.3E-308 to 1.7E+308 Precision: 15 decimal places Long double long double 10 bytes 3.4E-4932 to 1.1E+4932 Precision: 19 decimal places The sizes and ranges of different variable types are compiler dependent. To get the exact size of a data type, the operator sizeof() can be used. The expression sizeof(data_type) will return a number of bytes required to store a particular data type. 3 PRG 155 – Programming Fundamentals Using C Constants (Literals) A named memory location which holds a fixed value that cannot be modified by the program during its execution Types of constants: Integer o Decimal – Example: 123 o Octal – Using prefix 0. Example: 0123 o Hexadecimal – Using prefix 0x or 0X. Example: 0x2A Floating point – Examples: 123.45, -0.2E-2 Character o Examples: ‘A’, ‘1’, ‘&’ o Special Backslash character constants – Example: ‘\n’ String – Example: “Seneca” Defining Constants There are two ways to define a constant in C: using #define preprocessor Examples: o #define PI 3.14 o #define NEWLINE ‘\n’ using const keyword Examples: o const int SIZE = 100; o const float PI = 3.14; o const char NEWLINE = ‘\n’; Example: #include #define SIZE 10 int main() { const float PI = 3.14; const char letter = 'A'; Output: printf("pi=%.2f\n", PI); pi=3.14 printf("Section: %c\n", letter); printf("size = 2 x %d = %d\n", SIZE, 2*SIZE); Section: A return 0; size = 2 x 10 = 20 } 4 PRG 155 – Programming Fundamentals Using C Function scanf() scanf() function reads data from the input device (usually keyboard) and store it in a variable. To use scanf() function, you will have to include header (same as for printf()). Syntax: scanf(“format_string”, &variable1, &variable2, …); where format_string – Specifies the data type of each variable from the list. Common format specifiers: Data Type Format Specifier int %d float %f double %lf char %c string %s Ampersand sign (&) – “Address of” operator o Tells scanf() where (in memory) to store the new value entered by the user o Missing & in scanf() is a common error; it leads to abnormal program termination. Example: int number; printf("Enter a number: "); //User prompted to enter a number scanf("%d", &number); After scanf() is called, the program waits for user to enter a value and press the “Enter” key. scanf() can be used to enter multiple values, of different or same data types, as shown in the example below: int age; float height; printf("Enter your age and height: "); scanf("%d%f", &age, &height); // %d is used for variable age, %f for height 5 PRG 155 – Programming Fundamentals Using C References Tan, H.H., and T.B. D’Orazio. C Programming for Engineering & Computer Science. USA: WCB McGRaw-Hill. 1999. Print. Hock-Chuan, Chua. C programming Tutorial. Programming notes, n.d. Web. 23 Jan, 2017. Tutorialspoint.com. "C Data Types." Www.tutorialspoint.com. N.p., n.d. Web. 02 Mar. 2017.. Tutorialspoint.com. "C Constants and Literals." Www.tutorialspoint.com. N.p., n.d. Web. 02 Mar. 2017.. 6