19
CS140: Intro to CS An Overview of Programming in C by Erin Chambers

CS140: Intro to CS An Overview of Programming in C by Erin Chambers

Embed Size (px)

Citation preview

Page 1: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

CS140: Intro to CS

An Overview of Programming in C

by Erin Chambers

Page 2: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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

Page 3: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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

Page 4: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 5: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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 }

Page 6: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 7: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 8: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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

Page 9: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

}

Page 10: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 11: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

Exercise for you

● Modify the last program to return the average of the numbers entered, instead of the sum

Page 12: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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.

Page 13: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

A program with comments

// Written by Erin Chambers

#include <stdio.h>

main(void)

{

//print my first message

printf(“Hello, World!\n”);

return 0;

}

Page 14: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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

Page 15: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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

Page 16: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 17: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;

}

Page 18: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

A shortcut

● The c command “getchar()” reads the next character from the input

● So

letter = getchar();

is equivalent to

scanf(“%c”, &letter);

Page 19: CS140: Intro to CS An Overview of Programming in C by Erin Chambers

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;}