Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-...
Try Your First C Program <ul><ul><ul><li>#include <stdio.h>  /* I/O header file */ </li></ul></ul></ul><ul><ul><ul><li>mai...
Identifiers <ul><li>Variable identifiers </li></ul><ul><ul><li>Begin with a letter or underscore:  A-Z ,  a-z , _ </li></u...
Fundamental Data Type <ul><li>Four Data Types   (assume 2’s complement, byte machine) </li></ul><ul><li>Note: 2 7  = 128, ...
Variable Declarations <ul><ul><ul><li>type  v 1 ,v 2 ,v 3 , …, v n </li></ul></ul></ul><ul><ul><ul><li>Example : </li></ul...
Numeric, Char, String Literals <ul><li>Literal </li></ul><ul><ul><li>Numeric literal </li></ul></ul><ul><ul><ul><li>fixed-...
<ul><li>Character literal (covered later) </li></ul><ul><ul><ul><li>American Standard Code for Information Interchange (AS...
<ul><li>String Literal </li></ul><ul><ul><li>will be covered in Array section </li></ul></ul><ul><ul><li>String is a array...
Numeric, Char, String Literals <ul><ul><ul><li>Character literals & ASCII codes: </li></ul></ul></ul><ul><ul><ul><ul><ul><...
Initialization <ul><li>If a variable is not initialized, the value of variable may be either  0 or garbage  depending on t...
Memory Concepts <ul><li>Each variable has a name, address, type, and value </li></ul><ul><ul><li>int x; </li></ul></ul><ul...
Sample Problem <ul><li>Write a program to take two numbers as input data and print their sum, their difference, their prod...
Sample Problem  (cont.) <ul><li>Pseudo Code: </li></ul><ul><ul><ul><li>Declare variables of x and y; </li></ul></ul></ul><...
Example Program <ul><li>#include <stdio.h> </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li></ul><ul><li>float x,...
Upcoming SlideShare
Loading in...5
×

T02 a firstcprogram

271

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
271
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

T02 a firstcprogram

  1. 1. Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu A First C Program
  2. 2. Try Your First C Program <ul><ul><ul><li>#include <stdio.h> /* I/O header file */ </li></ul></ul></ul><ul><ul><ul><li>main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>printf(“Hello world ”); </li></ul></ul></ul><ul><ul><ul><li>printf(“Welcome to CSCI230n“); </li></ul></ul></ul><ul><ul><ul><li>printf(“I am John Smithn”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>A C program contains one or more functions </li></ul></ul><ul><ul><li>main() is the function name of your main (root) program </li></ul></ul><ul><ul><li>{ }: braces (left & right) to construct a block containing the statements of a function </li></ul></ul><ul><ul><li>Every statement must end with a ; </li></ul></ul><ul><ul><li> is called an escape character </li></ul></ul><ul><ul><li>n is an example of an escape sequence which indicates newline </li></ul></ul><ul><ul><li>Other escape sequences are: t r a ” </li></ul></ul><ul><ul><li>Exercise : Use any editor to type and then save your first program as main.c </li></ul></ul><ul><ul><li>% gcc main.c </li></ul></ul><ul><ul><li>% a.out and observe its result. </li></ul></ul>header file – contains I/O routines pre-processor directive one statement main must be present in each C program statement terminator Indicates a program building block called function comment
  3. 3. Identifiers <ul><li>Variable identifiers </li></ul><ul><ul><li>Begin with a letter or underscore: A-Z , a-z , _ </li></ul></ul><ul><ul><li>The rest of the name can be letters, underscore, or digits </li></ul></ul><ul><ul><li>Guarantee that east least the first 8 characters are significant (those come after the 8th character will be ignored) while most of C compiler allows 32 significant characters. </li></ul></ul><ul><ul><ul><li>Example : </li></ul></ul></ul><ul><ul><ul><ul><li>_abc ABC Time time _a1 abcdefgh </li></ul></ul></ul></ul><ul><ul><ul><ul><li>abcdefghi (may be the same as abcdefgh ) </li></ul></ul></ul></ul><ul><ul><li>Case sensitive </li></ul></ul><ul><ul><li>Keywords: reserved names (lexical tokens) </li></ul></ul><ul><ul><li>auto double if static break else int struct </li></ul></ul><ul><ul><li>case entry long switch char extern register </li></ul></ul><ul><ul><li>typedef float return union do go sizeof continue … </li></ul></ul>
  4. 4. Fundamental Data Type <ul><li>Four Data Types (assume 2’s complement, byte machine) </li></ul><ul><li>Note: 2 7 = 128, 2 15 =32768, 2 31 = 2147483648 </li></ul><ul><ul><li>Complex and double complex are not available </li></ul></ul>8 4 4 4 2 2 2 or 4 2 or 4 1 1 Size (byte) float double 0 ~ 2 32 -1 unsigned long unsigned long int -2 31 ~ 2 31 -1 long long int 0 ~ 65535 unsigned short unsigned short int -32768 ~ 32767 short short int 0 ~ 65535 or 0 ~ 2 32 -1 unsigned unsigned int -2 15 ~ 2 15 -1 or -2 31 ~ 2 31 -1 int int 0 ~ 255 unsigned char -128 ~ 127 char char Range Abbreviation Data Type
  5. 5. Variable Declarations <ul><ul><ul><li>type v 1 ,v 2 ,v 3 , …, v n </li></ul></ul></ul><ul><ul><ul><li>Example : </li></ul></ul></ul><ul><ul><ul><ul><li>int i; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int j; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>float k; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>char c; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>short int x; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>long int y; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>unsigned int z; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int a1, a2, a3, a4, a5; </li></ul></ul></ul></ul>
  6. 6. Numeric, Char, String Literals <ul><li>Literal </li></ul><ul><ul><li>Numeric literal </li></ul></ul><ul><ul><ul><li>fixed-point </li></ul></ul></ul><ul><ul><ul><ul><li>octal O32 (= 24 D ) (covered later) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>hexadecimal OxFE or Oxfe (=254 D ) (covered later) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>decimal int 32 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>long (explicit) 32L or 32l </li></ul></ul></ul></ul><ul><ul><ul><li>an ordinary integer literal that is too long to fit in an int is also too long for long </li></ul></ul></ul><ul><ul><ul><li>floating-point </li></ul></ul></ul><ul><ul><ul><ul><li>No single precision is used; always use double for literal </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Example : </li></ul></ul></ul></ul><ul><ul><ul><ul><li>1.23 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>123.456e-7 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>0.12E </li></ul></ul></ul></ul>
  7. 7. <ul><li>Character literal (covered later) </li></ul><ul><ul><ul><li>American Standard Code for Information Interchange (ASCII) </li></ul></ul></ul><ul><ul><ul><li>Printable: single space 32 </li></ul></ul></ul><ul><ul><ul><ul><li>‘ 0’ - ‘9’ 48 - 57 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ A’ - ‘Z’ 65 - 90 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ a’ - ‘z’ 97 - 122 </li></ul></ul></ul></ul><ul><ul><ul><li>Nonprintable and special meaning chars </li></ul></ul></ul><ul><ul><ul><ul><li>‘ n’ new line 10 ‘t’ tab 9 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ ’ back slash 9 ‘’’ single quote 39 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ 0’ null 0 ‘b’ back space 8 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ f’ formfeed 12 ’r’ carriage return 13 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ ”’ double quote 34 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ ddd’ arbitrary bit pattern using 1-3 octal digits </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ Xdd’ for Hexadecimal mode </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ 017’ or ‘17’ Shift-Ins, ^O </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ 04’ or ‘4’ or ‘004’ EOT (^D) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ 033’ or ‘X1B’ <esc> </li></ul></ul></ul></ul>Numeric, Char, String Literals
  8. 8. <ul><li>String Literal </li></ul><ul><ul><li>will be covered in Array section </li></ul></ul><ul><ul><li>String is a array of chars but ended by ‘0’ </li></ul></ul><ul><ul><li>String literal is allocated in a continuous memory space of Data Segment, so it can not be rewritten </li></ul></ul><ul><ul><ul><li>Example : “ ABCD ” </li></ul></ul></ul>Numeric, Char, String Literals Ans : 13+1 = 14 bytes Question : “I am a string” takes ? Bytes 4 chars but takes 5 byte spaces in memory ... A B C D ‘0’
  9. 9. Numeric, Char, String Literals <ul><ul><ul><li>Character literals & ASCII codes: </li></ul></ul></ul><ul><ul><ul><ul><ul><li>char x; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>x=‘a’; /* x = 97*/ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>Notes: </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ a’ and “a” are different; why? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>‘ a’ is the literal 97 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>“ a” is an array of character literals, { ‘a’, ‘0’} or {97, 0} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>“ a” + “b” +”c” is invalid but ‘a’+’b’+’c’ = ? (hint: ‘a’ = 97 in ASCII) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>if the code used is not ASCII code, one should check out each value of character </li></ul></ul></ul></ul>1 38 ‘ a’ + ‘b’ + ‘c’ = 97 + 98 + 99 = 294 = 256 + 38 in the memory
  10. 10. Initialization <ul><li>If a variable is not initialized, the value of variable may be either 0 or garbage depending on the storage class of the variable. </li></ul><ul><ul><ul><li> int i=5; </li></ul></ul></ul><ul><ul><ul><ul><li>float x=1.23; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>char c=‘A’; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int i=1, j,k=5; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>char c1 = ‘A’, c2 = 97; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>float x=1.23, y=0.1; </li></ul></ul></ul></ul>
  11. 11. Memory Concepts <ul><li>Each variable has a name, address, type, and value </li></ul><ul><ul><li>int x; </li></ul></ul><ul><ul><li>scanf(“%d”, &x); </li></ul></ul><ul><ul><li>user inputs 10 </li></ul></ul><ul><ul><li>x = 200; </li></ul></ul><ul><ul><li>After the execution of (1) x </li></ul></ul><ul><ul><ul><li>After the execution of (2) x </li></ul></ul></ul><ul><ul><ul><li>After the execution of (3) x </li></ul></ul></ul><ul><ul><ul><li>After the execution of (4) x </li></ul></ul></ul><ul><ul><li>Previous value of x was overwritten </li></ul></ul>10 200
  12. 12. Sample Problem <ul><li>Write a program to take two numbers as input data and print their sum, their difference, their product and their quotient. </li></ul><ul><ul><li>Problem Inputs </li></ul></ul><ul><ul><li>float x, y; /* two items */ </li></ul></ul><ul><ul><li>problem Output </li></ul></ul><ul><ul><li>float sum; /* sum of x and y */ </li></ul></ul><ul><ul><li>float difference; /* difference of x and y */ </li></ul></ul><ul><ul><li>float product; /* product of x and y */ </li></ul></ul><ul><ul><li>float quotient; /* quotient of x divided by y */ </li></ul></ul>
  13. 13. Sample Problem (cont.) <ul><li>Pseudo Code: </li></ul><ul><ul><ul><li>Declare variables of x and y; </li></ul></ul></ul><ul><ul><ul><li>Prompt user to input the value of x and y; </li></ul></ul></ul><ul><ul><ul><li>Print the sum of x and y; </li></ul></ul></ul><ul><ul><ul><li>Print the difference of x and y; </li></ul></ul></ul><ul><ul><ul><li>Print the product of x and y; </li></ul></ul></ul><ul><ul><ul><li>If y not equal to zero, print the quotient of x divided by y </li></ul></ul></ul>
  14. 14. Example Program <ul><li>#include <stdio.h> </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li></ul><ul><li>float x,y; </li></ul><ul><li>float sum; </li></ul><ul><li>printf(“Enter the value of x:”); </li></ul><ul><li>scanf(“%f”, &x); </li></ul><ul><li>printf(“ n Enter the value of y:”); </li></ul><ul><li>scanf(“%f”, &y); </li></ul><ul><li>sum = x + y; </li></ul><ul><li>printf(“ n the sum of x and y is:%f”,sum); </li></ul><ul><li>printf(“ n the sum of x and y is:%f”,x+y); </li></ul><ul><li>printf(“ n the difference of x and y is:%f”,x-y); </li></ul><ul><li>printf(“ n the product of x and y is:%f”,x*y); </li></ul><ul><li>if ( y != 0 ) </li></ul><ul><li>printf(“ n the quotient of x divided by y is:%f”,x/y); </li></ul><ul><li>else </li></ul><ul><li>printf(“ n quotient of x divided by y does not exist!n”); </li></ul><ul><li>return(0); </li></ul><ul><li>} </li></ul><ul><li>function </li></ul><ul><ul><li>name </li></ul></ul><ul><ul><li>list of argument along with their types </li></ul></ul><ul><ul><li>return value and its type </li></ul></ul><ul><ul><li>Body </li></ul></ul>inequality operator
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×