190 likes | 200 Views
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.
E N D
An Overview of Programming in C by Erin Chambers CS140: Intro to CS
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
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
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; }
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 }
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; }
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; }
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
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; } }
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; }
Exercise for you • Modify the last program to return the average of the numbers entered, instead of the sum
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.
A program with comments // Written by Erin Chambers #include <stdio.h> main(void) { //print my first message printf(“Hello, World!\n”); return 0; }
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
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
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; }
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; }
A shortcut • The c command “getchar()” reads the next character from the input • So letter = getchar(); is equivalent to scanf(“%c”, &letter);
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; }