CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES Mingon Kang The University of Texas at Arlington C History ANSI C, also known C89(ANSI X3.159-1989) and C90 (ISO/IEC 9899:1990) ANSI: American National Standards Institute ISO: International Organization for Standardization C95 (ISO/IEC 9899/AMD1:1995) Supports multi byte/wide character char sets C99 (ISO/IEC 9899:1999) One-line comments Intermingled variable declarations C11(ISO/IEC 9899:2011) Support Multi-threading Support Unicode (e.g., UTF-8) 1
Overview of Source code Components Comment Library declaration Variables Functions int main(void): a program starts from here User-defined functions C standard library provides very fundamental functions See overview.c in the course web 2
Comment Starts with /* symbol and ends with */ Can be anywhere in the source code A compiler ignores the block /* comment goes here */ // comment goes here This is not correct in C89 Or /* Author: Bill Gates * Purpose: To show a comment that spans multiple lines * Created on Aug. 2014 */ Library declaration Declare libraries that need to load in the program C standard library User-defined library #include <library_name.h> Or #include library_name.h 3
C standard Library Specified in the ANSI C standard Provides macros, type definition, and functions for string handling, mathematical computation, input/output processing, memory allocation, and etc. C standard Library Name <stdio.h> <math.h> <stdlib.h> <string.h> <time.h> Description Defines core input and output functions Defines common mathematical functions. Defines numeric conversion functions, pseudo-random numbers generation functions, memory allocation, process control functions Defines string handling functions. Defines date and time handling functions See http://www.tutorialspoint.com/c_standard_library/ 4
Functions Main() int main(void) int main(int argc, char **argv) User-defined function Functions that the C standard library provides E.g., printf: print formatted data to standard output Variables A name used to refer to a certain location in memory C programming requires that variables must be declared with the variable type int: an integer, typically reflecting the natural size of integers on the host machine char: a single byte, capable of holding one character float: single-precision floating point double: double-precision floating point Data Type Ranges: http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx 5
Variables Data Types in C short (unsigned short) int (unsigned int) long (unsigned long) char float double Fundamentals of Computer Systems Bit: zero or one Morse Code, Punched Card, Electrical Switch, Two distinct Voltage or current levels 1 Byte = 8 bits historically for a single character A unit to access a memory Data can be accessed 8 bits or multiples of 8 bits at a time 16 bit vs 32 bits vs 64 bits computers? Processor registers (small amount of storage in a CPU) Data, Address, General purpose, Conditional, and so on.. 6
Character (1byte) ASCII code (American Standard Code for Information Interchange) DEC OCT HEX BIN Symbol HTML Number HTML Name Description 32 040 20 00100000 Space 33 041 21 00100001!! Exclamation mark 35 043 23 00100011 # # Number 36 044 24 00100100 $ $ Dollar 37 045 25 00100101 % % Procenttecke n ASCII code Table: http://www.ascii-code.com/ 48 060 30 00110000 0 0 Zero 49 061 31 00110001 1 1 One 50 062 32 00110010 2 2 Two 51 063 33 00110011 3 3 Three 65 101 41 01000001 A A 66 102 42 01000010 B B 97 141 61 01100001 a a 98 142 62 01100010 b b Uppercase A Uppercase B Lowercase a Lowercase b 7
Integer 2 bytes in 16 bits systems 4 bytes in 32/64 bits systems Unsigned integer: 0 ~ 2 n 1 Signed integer (two's complement): 2 n 1 ~ 2 n 1 1 Integer: http://en.wikipedia.org/wiki/integer_(computer_science) Two s complement: http://en.wikipedia.org/wiki/two%27s_complement Float: single-precision floating-point format A bit for sign (0: positive, 1: negative) 8 bits for exponent 23 bits for fraction http://en.wikipedia.org/wiki/single-precision_floating-point_format 8
Double: double-precision floating-point format http://en.wikipedia.org/wiki/double-precision_floating-point_format Print formatted data printf function from <stdio.h> int printf (char * format, ); Specified character at format d : signed decimal integer u : unsigned decimal integer f : decimal floating point e : scientific notation (e.g., 3.8254e+2) c : character s : string of characters http://www.cplusplus.com/reference/cstdio/printf/ 9