1 / 17

Department of Computer and Information Science, School of Science, IUPUI

Department of Computer and Information Science, School of Science, IUPUI. CSCI 230. A First C Program. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Try Your First C Program. #include <stdio.h> /* I/O header file */ main() { printf(“Hello world ”);

gazit
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI CSCI 230 A First C Program Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. Try Your First C Program #include <stdio.h> /* I/O header file */ main() { printf(“Hello world ”); printf(“Welcome to CSCI230\n“); printf(“I am John Smith\n”); } • A C program contains one or more functions • main() is the function name of your main (root) program • { }: braces (left & right) to construct a block containing the statements of a function • Every statement must end with a ; • \ is called an escape character • \n is an example of an escape sequence which indicates newline • Other escape sequences are: \t \r \a \\ \” Exercise:Use any editor to type and then save your first program asmain.c % gcc main.c % a.out and observe its result. comment header file – contains I/O routines pre-processor directive main must be present in each C program Indicates a program building block called function one statement statement terminator

  3. Identifiers • Variable identifiers • Begin with a letter or underscore: A-Z, a-z, _ • The rest of the name can be letters, underscore, or digits • Only the first 8 characters are significant on most compilers (those come after the 8th character will be ignored) while most of C compiler allows 32 significant characters. Example: _abc ABC Time time _a1 abcdefgh abcdefghi (may be the same as abcdefgh) • Case sensitive • Keywords: reserved names auto double if static break else int struct case entry long switch char extern register typedef float return union do go sizeof continue …

  4. Fundamental Data Type • Four Data Types(assume 2’s complement, byte machine) • Note: 27 = 128, 215 =32768, 231 = 2147483648 • Complex and double complex are not available

  5. Variable declaration type v1,v2,v3, …, vn Example: int i; int j; float k; char c; shortint x; long int y; unsigned int z; int a1, a2, a3, a4, a5;

  6. Numeric, Char, String Constant • Constant • Numeric constant • fixed-point • octal O32 (= 24D) • hexadecimal OxFE or Oxfe (=254D) • decimal int 32 • long (explicit) 32L or 32l • an ordinary integer constant that is too long to fit in an int is also to be long • floating-point • No single precision is used; always use double for constant Example: 1.23 123.456e-7 0.12E

  7. Numeric, Char, String Constant • Character constant • American Standard Code for Information Interchange (ASCII) • Printable: single space 32 ‘0’ - ‘9’ 48 - 57 ‘A’ - ‘Z’ 65 - 90 ‘a’ - ‘z’ 97 - 122 • Nonprintable and special meaning chars ‘\n’ new line 10 ‘\t’ tab 9 ‘\\’ back slash 9 ‘\’’ single quote 39 ‘\0’ null 0 ‘\b’ back space 8 ‘\f’ formfeed 12 ’\r’ carriage return 13 ‘\”’ double quote 34 ‘\ddd’ arbitrary bit pattern using 1-3 octal digits ‘\Xdd’ for Hexadecimal mode ‘\017’ or ‘\17’ Shift-Ins, ^O ‘\04’ or ‘\4’ or ‘\004’ EOT (^D) ‘\033’ or ‘\X1B’<esc>

  8. ... A B C D ‘\0’ Numeric, Char, String Constant • String Constant • will be covered in Array section • String is a array of chars but ended by ‘\0’ • String constant is allocated in a continuous memory space of Data Segment, so it can not be rewritten Example: “ABCD” 4 chars but takes 5 byte spaces in memory Question: “I am a string” takes ? Bytes Ans:13+1 = 14 bytes

  9. Numeric, Char, String Constant • Character constants & ASCII codes: • char x; • x=‘a’; /* x = 97*/ • Notes: • ‘a’ and “a” are different; why? • ‘a’ is the constant 97 • “a” is an array of character constants, { ‘a’, ‘\0’} or {97, 0} • “a” + “b” +”c” is invalid but ‘a’+’b’+’c’ = ? (hint: ‘a’ = 97 in ASCII) • if the code used is not ASCII code, one should check out each value of character ‘a’ + ‘b’ + ‘c’ = 97 + 98 + 99 = 294 = 256 + 38 in the memory 1 38

  10. Initialization • If a variable is not initialized, the value of variable may be either 0 or garbage depending on the storage class of the variable. int i=5; float x=1.23; char c=‘A’; int i=1, j,k=5; char c1 = ‘A’, c2 = 97; float x=1.23, y=0.1;

  11. Memory Concepts • Each variable has a name, type, and value • int x; • scanf(“%d”, &x); • user inputs 10 • x = 200; After the execution of (1) x After the execution of (2) x After the execution of (3) x After the execution of (4) x Previous value of x was overwritten 10 200

  12. Sample Problem • Write a program to take two numbers as input data and print their sum, their difference, their product and their quotient. Problem Inputs float x, y; /* two items */ problem Output float sum; /* sum of x and y */ float difference; /* difference of x and y */ float product; /* product of x and y */ float quotient; /* quotient of x divided by y */ • Pseudo Code: Declare variables of x and y; Prompt user to input the value of x and y; Print the sum of x and y; Print the difference of x and y; Print the product of x and y; If y not equal to zero, print the quotient of x divided by y

  13. Example Program #include <stdio.h> int main(void) { float x,y; float sum; printf(“Enter the value of x:”); scanf(“%f”, &x); printf(“\nEnter the value of y:”); scanf(“%f”, &y); sum = x + y; printf(“\nthe sum of x and y is:%f”,sum); printf(“\nthe sum of x and y is:%f”,x+y); printf(“\nthe difference of x and y is:%f”,x-y); printf(“\nthe product of x and y is:%f”,x*y); if (y != 0) printf(“\nthe quotient of x divided by y is:%f”,x/y); else printf(“\nquotient of x divided by y does not exist!\n”); return(0); } • function • name • list of argument along with their types • return value and its type • Body inequality operator

  14. Data Type Conversion • Rule #1 char, short int float  double • Rule #2(double > long > unsigned > int) • If either operand is double, the other is converted to double, and the result is double • Otherwise, if either operand islong, the other is converted tolong, and the result is long • Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned • Otherwise, the operand must beint

  15. Examples Example: here c: char, u: unsigned, i: int, s: short, l: long, Expression Final Data Type Explanation c – s / i intshortint, int/int, charint, int-int u * 3 – i unsignedint(3)unsigned, unsigned*unsigned=unsigned, intunsigned, unsigned-unsigned=unsigned u * 3.0 – i double unsigneddouble, double*double, intdouble, double-double=double c + i int charint c + 1.0 double charint (rule 1), intdouble(rule 2) 3 * s * 1 long

  16. Data Type Conversion (cont.) • Note: • Conversion of char to int may be signed extension (Most machine like IBM PC, VAX, 68000, 68020) or zero filled • Conversion of int to long is signed extension, so does short • unsigned to long is zero filled • Var = expr f = d; /* round off */ i = f; /* truncates fractions part, if the number is too big to fit, the result is undetermined */ i = l; s = i; and c = i /* eliminates high order bits */

  17. If a specific type is required, the following syntax may be used, called cast operator. (type) expr Example: float f=2.5; x = (int)f + 1; /* the result is 3, Q:will f value be changed? */ • Unsigned int to int: there is not actual conversion between int and unsigned int. Example:(Assuming 2’s complement machine and int is 2 bytes long) unsigned i = 65535; int j = i;/* j will be –1 */ int j = -2; unsigned i = 1 + j;/* i= 65535 */

More Related