1 / 19

An Overview of Programming in C

This overview provides an introduction to programming in C, covering topics such as compiling and running programs, if statements, loops, comments, and working with characters.

mjoel
Download Presentation

An Overview of Programming in C

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. An Overview of Programming in C by Erin Chambers CS140: Intro to CS

  2. Getting started • Please log in to the computer, click on the startup menu (located in bottom left corner) • Select “Utilities” -> “Kate” to open a text editor

  3. Running our program • We need to compile our C program using the compiler called cc • The compiler then outputs an executable file which will run our program, usually called a.out • To run it, open up a Konsole (the little black screen icon at the bottom), type “cc filename.c”, then type “./a.out” at a command prompt

  4. Input #include <stdio.h> main(void) { int x, y, divide, remainder; printf(“Enter two integers: “) scanf(“%d%d”, &x, &y); divide = x / y; remainder = x % y; printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder); return 0; }

  5. If statements • For more control, might want to specify some code be executed if some condition is true • Use the if-else statement and boolean expressions • Boolean expressions in C: <, >, <=, >=, ==, != • ! for 'not', && for 'and', || for 'or' • Syntax: if (boolean expression) { statements } else { statements }

  6. If statements – an example #include <stdio.h> main(void) { int x, y, z; printf(“Enter two integers: \n”); scanf(“%d%d”, &x, &y); if (x > y) max = x; else max = y; printf(“The maximum is %d \n”, max); return 0; }

  7. If statements – another example #include <stdio.h> main(void) { float x; printf(“Enter a number:”); scanf(“%f”, &x); if (x < 0) printf(“You entered a negative number\n”); else if (x == 0) printf(“You entered zero.\n”); else printf(“You entered a positive number.\n”); return 0; }

  8. Loops • While loops are a way of performing the same code multiple times • While the boolean expression evaluates to true, the code in the while loop will be performed; as soon as it is false, the while loop ends and the program continues

  9. While loop - example #include <stdio.h> main(void) { int i = 10; while (i >0) { printf(“T minus %d and counting\n”, i); i = i – 1; } }

  10. Another while loop #include <stdio.h> main(void) { float number, sum; printf(“Enter a number:”); scanf(“%f”, &number); while (number != 0) { sum = sum + number; printf(“Enter another number, or 0 to stop:”); } printf(“The sum is %f\n”, sum); return 0; }

  11. Exercise for you • Modify the last program to return the average of the numbers entered, instead of the sum

  12. Comments in a program • In C, any line beginning with // will be ignored by the compiler. • Since code can be difficult to decipher, this provides a great way to comment your code, or describe what each line does.

  13. A program with comments // Written by Erin Chambers #include <stdio.h> main(void) { //print my first message printf(“Hello, World!\n”); return 0; }

  14. More about Comments • Any program should be commented, so that you can determine what your code should be doing • In reality, this can be the most important part of the program, since it is what helps people decipher what is happening • Also, use meaningful variable names – sum, average, number – to help you remember what is stored in that variable

  15. Characters • The data type char stores a single character • Each character is actually represented as a number, just like with ASCII • To read or write a character variable, use %c

  16. Fun with char #include <stdio.h> main(void) { char letter; \\initialize letter to be a character \\Read in a character printf(“Enter a character:”); scanf(“%c”, &letter); \\Print out the character and its associated number printf(“The character you entered is %c \n”, letter); printf(“Its C number is %d”, letter); return 0; }

  17. Char tricks #include <stdio.h> main(void) { char letter; int number; //Prompt user to enter a character printf("Enter a letter:"); scanf("%c", &letter); //Find the next letter in the alphabet and print it number = letter; number = number + 1; printf("The next letter is %c\n", number); return 0; }

  18. A shortcut • The c command “getchar()” reads the next character from the input • So letter = getchar(); is equivalent to scanf(“%c”, &letter);

  19. Count the length of a message #include <stdio.h> main(void) { char ch; int length = 0; //Prompt the user for a message //Read the first character of the message //while loop to count how the message is //print length of message return 0; }

More Related