Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming II & Formatted Input/Output Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.

Similar presentations


Presentation on theme: "Introduction to C Programming II & Formatted Input/Output Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013."— Presentation transcript:

1

2 Introduction to C Programming II & Formatted Input/Output Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013

3 3 Learning Outcomes At the end of this session, student will be able to: Define element and structure of C programming language (LO1 & LO2) T0016 - Algorithm and Programming

4 4 Variable Identifier for storing data/information Each variable has its name, address (L-value), type, size and data (R-value) Data or variable value can be modified at run time Declaration format: ; = ; Example: int a, b, c, total; float salary, bonus; int num_students = 20; T0016 - Algorithm and Programming

5 5 Variable Example: char ch=65 T0016 - Algorithm and Programming 65 Memory 123456 address ch name value Range of value: -128 – 127

6 6 Variable Variable Declaration: –Variable can be declared at every statement block –Block statement or compound statement is statement exists between { and } sign –Example: T0016 - Algorithm and Programming int x; int y; int z; or: int x, y, z; or: int x; int y; int z;

7 7 Data Type In C, there are 5 data types and 4 modifiers Data types: –Character  char –Integer  int –Floating point  float –Double floating point  double –Void  void Modifiers: -signed -unsigned -long -short T0016 - Algorithm and Programming

8 8 Data Type Data type in C is a combination of basic data type and its modifier Example: signed char unsigned int long int etc. T0016 - Algorithm and Programming

9 9 Data Type Data type and its range on TURBO C 2.0 (DOS) T0016 - Algorithm and Programming DATA TYPESYNTAXMEMORYRANGE characterunsigned char1 byte0 to 255 char1 byte-128 to 127 integerunsigned int2 byte0 to 65535 int2 byte-32768 to 32767 short int1 byte-128 to 127 unsigned long4 byte0 to 4294967295 long4 byte-2147483648 to 2147483647 float 4 byte3.4E-38 to 3.4E+38 double8 byte1.7E-308 to 1.7E+308 long double16 byte3.4E-4932 to 1.1E+4932

10 10 Data Type Why char data range between -128 to 127 ? 1 Byte = 8-bit 00000000 to 01111111 (MSB=>0 = Positive value) 10000000 to 11111111 (MSB=>1 = Negative value) T0016 - Algorithm and Programming MSB = Most Significant Bit (most left) -128 64 32 16 8421 Total = -1Total = -128 -128

11 11 Data Type Beside used in function identifier type as no return value, keyword void also used as data type in variable. Void data type: is data type that can be transform into any data type (will be discussed later in pointer) T0016 - Algorithm and Programming

12 12 Casting Casting is a process to convert data type in C Syntax : (data type) Example : int x; float f = 3.134; x = (int) f; T0016 - Algorithm and Programming casting

13 13 Symbolic Constant Symbolic constant is an identifier which only have R- Value, and its value is unchangeable at runtime Symbolic constant does not have address (L-Value) Symbolic constant declaration does not need memory allocation To declare symbolic constant, can be done by using pre- processor directive #define or keyword const Example : const float Pi=3.14; #define Pi 3.14 T0016 - Algorithm and Programming

14 14 Symbolic Constant T0016 - Algorithm and Programming #define Pi 3.14 int main() { Pi=3.1475; //Error return 0; } int main() { const float Pi=3.14; Pi=3.1475; //Error return 0; } #define Pi 3.14 int main() { float PHI=3.14; PHI = 3.1475; //OK (variable) Pi=3.1475; //Error return 0; }

15 15 Constant Constant / symbolic constant does not have address (only value) and its value can not be changed at run time. Constant type:  Integer constant  -5  Floating-point constant  3.14  Character constant  'C' '1' '$'  Escape sequence  \n \t \''  String constant  ''BiNus'' † Symbolic constant  #define PHI 3.14  const float PHI=3.14;  'H‘is a character constant  ''H'‘is a string constant  1is a integer constant  '1‘is a character constant  const float Pi= 3.1415926; Pi is a symbolic constant T0016 - Algorithm and Programming

16 16 Program Example Addition of two numbers Data has put on the memory. Result of the addition saved on the memory (variable) T0016 - Algorithm and Programming /* Addition Program */ /*comments */ int x,y,z;/*Global variable*/ int main() {/*start main program*/ x = 20;/*Statement 1*/ y = 30;/*Statement 2*/ z = x + y;/*Statement 3*/ return 0;/*Statement 4*/ }/*end of main program*/

17 17 Program Example Calculating area of a circle Radius value input from keyboard. Print out the result. T0016 - Algorithm and Programming /*---------------------------------- Program circle area ----------------------------------*/ #include const float Pi = 3.14; /*Constant declaration*/ int main() /*start main program*/ { float r; /*local variable*/ float area; scanf(“%f”,&r); /*r input from keyboard*/ area = Pi * r * r; printf(“ Circle area = %5.2f”, area);/*print out to screen*/ return (0); }/*end of main program*/

18 18 Sizeof sizeof is an operator to find out size of a data type in C language Syntax: sizeof expression Example : sizeof(int) = 4 => Dev-V (Windows) sizeof(int) = 2 => Turbo C ver 2.0 (DOS) T0016 - Algorithm and Programming

19 19 Suffix C provides suffix for floating point constant: – F or f for float data type – L or l for long double data type – Default double data type Example : – 3.14  (double) – 3.14f  (float) – 3.14L  (long double) T0016 - Algorithm and Programming

20 20 Suffix C provides suffix for a constant integer: –U or u for unsigned integer –L or l for long integer –UL or ul or LU or lu for unsigned long integer –Default integer Example : – 174  (integer) – 174u  (unsigned integer) – 174L  (long integer) – 174ul  (unsigned long integer) T0016 - Algorithm and Programming

21 21 Suffix Some compilers will give warning for differ in data type, as can be seen from the following example Visual C++: Example : float x; x = 3.14; warning: truncation from 'const double' to 'float’ How to deal with the issue? You may use casting or suffix float x; x = (float)3.14;// casting x = 3.14f;// or suffix T0016 - Algorithm and Programming

22 22 Suffix T0016 - Algorithm and Programming #include int main() { printf(“Size of Floating Point Constant :\n"); printf(" – using suffix f = %d\n",sizeof(3.14f)); printf(" – without suffix = %d\n",sizeof(3.14)); printf(" – using suffix L = %d\n",sizeof(3.14L)); getch(); return 0; } Output: Size of Floating Point Constant : - using suffix f= 4 - without suffix= 8 - using suffix L= 12

23 23 Output Operation To show data on the display screen/monitor. Some of standard library function in C : printf(); putchar(); putch(); puts(); etc. T0016 - Algorithm and Programming

24 24 Output Operation: printf function To display some data on the standard output, using certain format Standard output is the monitor. Syntax: printf(const char *format[,argument, …]); Header file : stdio.h T0016 - Algorithm and Programming

25 25 Output Operation: printf() function T0016 - Algorithm and Programming /* A first program in C */ #include void main() { printf (“Welcome to C!\n”); } /*Printing on one line with two printf statements*/ #include int main(void){ printf (“Welcome”); printf (“to C!\n”); return 0; }

26 26 Output Formatting Output also has formatted specification: %[flags][width][.precision] type T0016 - Algorithm and Programming width : number of columns provided precision : digit number flags : can be changed into: none: right justify - : left justify -+: for positive & negative value type : d –or- i : signed decimal o: unsigned octal u: unsigned decimal x: unsigned hexadecimal f: floating point e: floating point (exponent) c: single character s: string %: % character p: pointer

27 27 Output Formatting For long data type, add l at the front of data type: –long double  ( “ %lf “) –unsigned long int  ( “ %lu “) –long int  ( “ %ld “) T0016 - Algorithm and Programming

28 28 Output Example Example 1: printf (“%6d”, 34);….34 printf (”%-6d”, 34);34…. Example 2 printf (“%10s”, “BINUS”);…..BINUS printf (“%-10s”, “BINUS”);BINUS….. printf (“%8.2f”, 3.14159 );….3.14 printf (“%-8.3f”, 3.14159 );3.141… T0016 - Algorithm and Programming

29 29 Output Example Example 3: printf ("%c\n",65); // print A printf ("%x\n",'A'); // print 41 printf ("%o\n",65); // print 101 printf ("%+d\n",34);// print +34 printf ("%+d\n",-45);// print -45 printf ("%e\n",3.14);// print 3.140000e+000 T0016 - Algorithm and Programming

30 30 Output Example Example 4: T0016 - Algorithm and Programming #include int main(){ char ss[]="Selamat Datang"; printf("123456789012345678901234567890\n"); printf("%.10s di Binus\n",ss); printf("%10s di Binus\n",ss); printf("%-10s di Binus\n",ss); printf("%.20s di Binus\n",ss); printf("%20s di Binus\n",ss); printf("%-20s di Binus\n",ss); printf("%20.10s di Binus\n",ss); printf("%-20.10s di Binus\n",ss); return 0; }

31 31 Output Example Example 4: T0016 - Algorithm and Programming Output: 123456789012345678901234567890 Selamat Da di Binus Selamat Datang di Binus Selamat Da di Binus

32 32 Output Operation: putchar() function Syntax: int putchar(int c) Functionality: –Displaying character on the monitor at cursor position. After display, cursor will move to the next position –Return EOF if error, and return the displayed character after successfully done –putchar is a macro similar to : putc(c, stdout ) –Header File : stdio.h Example : char ch=’A’; putchar(ch); T0016 - Algorithm and Programming

33 33 Output Operation: putch() function Syntax: int putch(int ch) Functionality: –Display ASCII character to the monitor without moving cursor to its next position –Return EOF if error, and return the displayed character after successfully done –Header file : conio.h Example : char ch=’b’; putch(ch); T0016 - Algorithm and Programming

34 34 Output Operation: puts() function Syntax: int puts(const char *str); Functionality : –Display string to the monitor and move the cursor to new line –Return non-negative value when successful and EOF if error –Header file: stdio.h Example : puts(”Welcome”); puts(”to Binus”); Output on monitor: Welcome to Binus T0016 - Algorithm and Programming

35 35 Input Operation Standard library function that is related to input operations are: scanf(); getchar(); getch(); getche(); gets(); etc. Input operation: function/operation of getting the data into the memory using standard I/O devices (keyboard, disk, etc.) T0016 - Algorithm and Programming

36 36 Input Operation: scanf() function Header file: stdio.h Format: int scanf( const char *format [,argument]... ); All the argument type are pointers (address of a variable) To get the address of a variable use “&” sign Example : int aValue; scanf(”%d”,&aValue); Input format: ”%type” where type can be substituted with one of the following list: (next page) T0016 - Algorithm and Programming

37 37 Input Operation: scanf() function Format Type: T0016 - Algorithm and Programming TypeUsed to scan d u x e, f, g C integer unsigned integer hexadecimal floating point single character s O […] [^..] string ended with whit space data unsigned octal string ended with non of the value inside [...] string ended with the value inside [...]

38 38 Input Operation: scanf() function If exist an x integer variable, state the difference of x and &x? Answer: x : 234 &x : 45678 T0016 - Algorithm and Programming Variable Name X Address 45678 Value 234

39 39 Input Operation: scanf() function scanf() function returns an integer that stated how many fields are successfully assigned Example : int x,y,z,w; x = scanf("%d %d %d",&y,&z,&w); –Input three values of integer 6 7 8, then x = 3; –Input four values 6 7 8 9 then x = 3 (successfully assign 3 variables y z w) T0016 - Algorithm and Programming

40 40 Input Operation: scanf() function T0016 - Algorithm and Programming /* Program Calculating rectangle area v1*/ #include int main(){ int width, height, area; scanf(”%d”,&width); scanf(”%d”,&height); area = width*height; return(0); } /* Program Calculating rectangle area v2*/ #include int main(){ int width, height, area; scanf(“%d %d”,&width, &height); area = width * height; return(0); } scanf() function can use more than one argument

41 41 Input Operation: scanf() function T0016 - Algorithm and Programming /* Program different argument*/ #include int main() { int number; char initial; float money; scanf(“%d %c %f”,&number, &initial, &money); //other statements return(0); } Data type for each variable in the function argument can be different

42 42 Input Operation: scanf() function Getting string data from keyboard using scanf() using format: %s Example : char ss[40]; scanf(”%s”, ss); Note for the example above, as the ss variable is a pointer than we need not putting extra & sign (&ss) in the function argument (pointer will be discussed later separately) String takes only till the first whitespace found T0016 - Algorithm and Programming

43 43 Input Formatting Space char, tab, linefeed, carriage-return, form-feed, vertical-tab, and new-line entitle ”white-space characters” Example : –Using previous example, if a string “good morning every one” entered then ss value will only contain “good” T0016 - Algorithm and Programming

44 44 Input Formatting & Example To get string that ended with certain character for example Enter, use scanf() with format: [^\n] Example 1: char ss[40]; scanf(”%[^\n]”,ss); Using the previous example, if a string “good morning every one” then ENTER, the ss variable will contain “good morning every one” T0016 - Algorithm and Programming

45 45 Input Formatting & Example Example 2: char ss[40]; scanf(”%[a-z]”, ss); Using the example above, if the string is: http://binusmaya.binus.ac.id then ENTER, ss variable will only contain: http. This is caused by character (:) is not within a to z, thus (:) accounted as the end of the string T0016 - Algorithm and Programming

46 46 Input Formatting & Example Example 3 : int x; scanf("%o", &x); Using the above code, if input value: 44 followed by enter then x will contain: 36 in decimal, as 44 is an octal number system T0016 - Algorithm and Programming

47 47 Input Formatting & Example Example 4: int x; scanf("%x", &x); Using the above code, if input value: 44 followed by enter then x will contain: 68 in decimal, as 44 is a hexadecimal number system T0016 - Algorithm and Programming

48 48 Input Operation: getchar() function Syntax: int getchar(void); Functionality: –Return the next ASCII character from keyboard buffer –Shown on the monitor screen –Awaiting for ENTER pressed –Header file: stdio.h Example : char ch; ch = getchar(); T0016 - Algorithm and Programming

49 49 Input Operation: getch() function Syntax: int getch(void); Functionality: –return a character from keyboard buffer –not shown on the monitor (no echo) –not wait for ENTER pressed –suitable for password –Header file: conio.h Example : char ch; ch = getch(); T0016 - Algorithm and Programming

50 50 Input Operation: getche() function Syntax: int getche(void) Functionality : –return a character from keyboard buffer –character shown on monitor (echo) –not wait for ENTER –Header file: conio.h Example : char ch; ch = getche(); T0016 - Algorithm and Programming

51 51 Input Operation: gets() function Syntax: char *gets(char *buffer) Functionality : –read a string from keyboard till find new-line and save in buffer –new-line will later on replace with null character –will return NULL if error and return its argument (buffer) if success Example : char buffer[40]; char *ptr; ptr = gets(buffer); T0016 - Algorithm and Programming

52 52 Exercise 1.int x,y,z,w; x=scanf("%d %d %d",&y,&z,&w); a.What happen if input was 2 integer values from the keyboard? b.What is x value if 3 character values given as the input? 2.char ss1[40]; char ss2[40]; x=scanf(”%s %s”,ss1,ss2); a.What is ss1 and ss2, if the input from keyboard is ”Good morning everyone” ? b.What is x if the input : ”Class 1PAT” ? T0016 - Algorithm and Programming

53 53 Exercise 3.char ss[40]; scanf(”%4s”, ss); What is ss value, if the input : ”Good morning” ? 4.char ch; ch = getchar(); What is ch value, if the input : Binus ? 5.char ch1, ch2; ch1 = getchar(); // input word “Binus” here! ch2 = getchar(); What is the value of ch1 and ch2, if the input : Binus ? T0016 - Algorithm and Programming

54 54 Exercise 6.Create a program in C to receive input from standard input (keyboard) for the following data: –Assignment Score –Mid Exam Score –Final Exam Score Calculate and display Final Score using : Final Score = 20%*Assignment + 30%*Mid + 50%*Final T0016 - Algorithm and Programming

55 55 Exercise 7. #include int main() { char name[40]; int nim; char gender; printf(“Name:"); scanf("%[^\n]",name); printf(“StudentNum :"); scanf("%d",&nim); printf(“Gender (M/F):"); gender=getchar(); return 0; } After entering name and student number, program will exit to prompt. gender=getchar() seems to be never executed. Explain why? T0016 - Algorithm and Programming

56 56 Exercise 8. #include int main(){ char ss[]="10 % 3 = 1\n"; char str[]= “ Welcome to Binus everyone\n"; printf(ss); printf("%s",ss); printf(str); printf("%s",str); return 0; } What is the output of the above code ? T0016 - Algorithm and Programming

57 57 Summary Syntax for Output: printf, putchar, putch, puts Syntax for Input: scanf, getchar, getch, getche, gets The screen (DOS mode) divided into row and column, normally max column = 80 and max row = 25 T0016 - Algorithm and Programming

58 58 References Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 1 & 2 Writing Your First C Program: http://aelinik.free.fr/c/ch02.htmhttp://aelinik.free.fr/c/ch02.htm Data Types and Names in C: http://aelinik.free.fr/c/ch04.htmhttp://aelinik.free.fr/c/ch04.htm T0016 - Algorithm and Programming

59 59 References Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 9 Reading from and Writing to Standard I/O: http://aelinik.free.fr/c/ch05.htm http://aelinik.free.fr/c/ch05.htm Intro to File Input/Output in C: http://www.cs.bu.edu/teaching/c/file-io/intro/ http://www.cs.bu.edu/teaching/c/file-io/intro/ T0016 - Algorithm and Programming

60 60 END T0016 - Algorithm and Programming


Download ppt "Introduction to C Programming II & Formatted Input/Output Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013."

Similar presentations


Ads by Google