Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.

Similar presentations


Presentation on theme: "C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems."— Presentation transcript:

1 C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems

2 About C C is a structural (also called imperative) programming language Each program is viewed as a group of operations or actions Programs may be modularized statements -------- ------ ----

3 Programming process Specify a task (design) Find an algorithm to complete the task Write code Test your code Repeat from appropriate point

4 Preprocessor Directives Like import statements in Java these allow you to include code that already exists #include #include //Standard input/output #include "myheader.h" //homemade header file //The printf function comes from stdio.h printf("Hi my name is...\n");

5 Preprocessor Directives You can also use preprocessor directives to create constants or macros #define // <-- use to create constants or a macro #define PI 3.14 //Never use PI 3.14; <-- semicolons should not be used when defining //a constant

6 Compilation Compiler Translates C code to machine code Linker takes object code (intermediate code) and joins it together

7 Compilation At $ prompt gcc prog1.c –std=c99 -o prog1.c file - source code -std=c99 – Use C99 standard -o - output to a specific file prog1 - executable file Without an executable specified with –o, a.out is the executable

8 Comments and a Simple C Program /* a multiline comment */ //a one line comment #include int main(int argc, char ** argv) { printf("Hello, world!\n"): return 0; }

9 Primitive Variable Declarations Variable declarations similar to Java int centimeters, meters, kilometers; double watts; char y_or_n = ‘y’;

10 Blocks Blocks similar to Java //A BLOCK { declarations statements }

11 Assignment Assignment statements comparable to Java int a; a = 1 + 2 * 5; result --> a = 11 Results of RHS stored in LHS

12 printf Print statements may be performed using printf similar to, but not exactly the same as, Java Format: printf(control string, variable list); variable list -> variable | variable list

13 Example Program #include int main(int argc, char ** argv) { int meters = 8; printf(“%d meters\n”, meters); } Result: 8 meters

14 Another Example #include int main(int argc, char ** argv) { int kilometers = 10, meters = 7, centimeters = 8; printf(“%d %d %d\n”, kilometers, meters, centimeters); } Result: 10 7 8 What if we reorder the variable list?

15 Common Conversion Specifications %d - int (often 4 bytes) %lf - double (often 8 bytes) %c - ASCII character or char (often 1 byte) %f - float (often 4 bytes) %s - string ( NULL terminated)

16 Escape Codes Used to represent characters that aren't readily visible in strings \n new line (in Linux this represents return) \r return (in Windows \n\r represents return) \t tab \' single quote \" double quote \\ backslash

17 Reserved words signed, unsigned, struct union, char, int, float, double while, for, if, do, short long, static, auto, extern register Remember that C is case sensitive!

18 Arithmetic Expressions in C *multiplication /division %modulus (remainder) +addition -subtraction

19 Order of Operations 1. () 2. unary negation 3. * / % 4. + - Example: 2 + 3 % 7 3 % 7 = 3 2 + 3 = 5

20 Example Program #include int main(int argc, char ** argv) { int kilometers, meters, centimeters; kilometers = 4; meters = 1000*kilometers; centimeters = 100*meters; printf("The length of a bridge :\n"); printf("%s%s\n", "The length", " of a bridge"); printf(”The length in different units:\n"); printf("%d kilometers\n", kilometers); printf("%d meters\n", meters); printf("%d centimeters\n", centimeters); return 0; }


Download ppt "C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems."

Similar presentations


Ads by Google