Download presentation
Presentation is loading. Please wait.
Published byJulian Willis Modified over 8 years ago
1
CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua http://www.csse.monash.edu.au/~jjchua
2
Topics Basic structure of a C program C “baby-talk” phrases printf(“Da-da-da”);
3
0 or 1 Recall:
4
Machine Language 10100110 01110110 00100110 00000000 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...
5
High-Level Language Compilers and linkers translate a high level program into executable machine code. #include int main() { printf(“Da-da-da”); return 0; } Source code Executable code 10100110 01110110 00100110 00000000 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...
6
Why C? Structured language Standard library exists, allowing portability –See D&D 2/e Appendix B (web links in D&D 3/e) –Forouzan & Gilberg Appendix F Wide availability on a variety of computers Low level activities possible It can produce lean and efficient code Widely used
7
History of C CPL Combined Programming Language (Barron et al., 1963) BCPL Basic CPL (Richards, 1969) B (Thompson, 1970) C K&R C (Ritchie, 1972) ANSI C American National Standards Institute C (X3J11, 1989) C9X (JTC1/SC22/WG14, ISO/IEC 9899)
8
Basic Structure of a C Program output “Da-da-da” Instructions: #include int main() { printf(“Da-da-da”); return 0; } C Program: Example: Da-da-da
9
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: “Skeleton” Example: Da-da-da
10
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: void main() { } Also: Not recommended Example: Da-da-da
11
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: main() { return 0; } Also: Assumes int Example: Da-da-da
12
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: main() { return 0; } Also: Warning messages: “Return value expected” Example: Da-da-da
13
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Includes standard input / output library of functions. Read: “Hash-include” Example: Da-da-da
14
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Brackets mark the beginning and end of a block of instructions. Example: Da-da-da
15
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: Instruction (function call) to output “Da-da-da” Example: Da-da-da
16
Basic Structure of a C Program #include int main() { printf(“Da-da-da”); return 0; } C Program: “Statements” (lines of instructions) end with a semi-colon ( ; ) Example: Da-da-da
17
Simple C Phrases Example 1: Float let x be equal to 4.5 output the value of x Instructions: #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program:
18
Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Declares that x is a variable which can be assigned a numerical value.
19
Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Assigns the value 4.5 into the variable x. (Remember: “right value is assigned to left variable”)
20
Simple C Phrases Example 1: Float #include int main() { float x; x = 4.5; printf(“%f”, x); return 0; } C Program: Outputs the value of x. (“ %f ” for float)
21
Simple C Phrases Example 2: Sum input the value of x input the value of y output “The sum is “ output the value of x + y output a new line Instructions: #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program:
22
x and y are both numeric variables #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum
23
Input the values of x and y respectively (note the ampersand!) #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum
24
Outputs the string: “The sum is “ #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum
25
Outputs the sum x + y #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum
26
Outputs a newline #include int main() { float x; float y; scanf(“%f”, &x); scanf(“%f”, &y); printf(“The sum is “); printf(“%f”, x + y); printf(“\n”); return 0; } C Program: Simple C Phrases Example 2: Sum
27
Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Example 3: Mark
28
Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Declares that name is a variable which can contain a “string” (i.e. words, names, etc.)
29
/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Outputs a “prompt” so the user knows what the program wants.
30
/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Inputs the string content of the variable name. (Note: no ampersand!)
31
/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark (The usual. Note the ampersand for floats!)
32
/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Outputs the string name. (Note the “%s”)
33
/* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; } Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark Comments: everything between /* and */ are ignored by the compiler.
34
Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark This block of instructions is performed if the comparison is true. /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; }
35
Simple C Phrases: “if” #include int main() { char name[50]; float mark; printf("What is your name? "); scanf("%s", name); printf("What is your mark? "); scanf("%f", &mark); printf("\n"); printf("Hi, "); printf("%s", name); printf("!\n"); Example 3: Mark This block of instructions is performed if the comparison is false. /* The next output depends on the mark. */ if (mark < 50) { printf("Sorry. You failed.\n"); } else { printf("Yey! You passed!\n"); } return 0; }
36
Summary Basic structure of a C program Simple C “phrases” –Declaring numerical variables (floats) –Declaring string variables –Input (scanf) and Output (printf) –comments –The “if...else...” condition Readings: Forouzan & Gilberg, Chapter 1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.