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

A First Program

#include <stdio.h>

main(void)

{

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

return 0;

}

First Program – Line by line

<-- Loads standard input/output options

#include <stdio.h>

main(void)

{

printf(“Hello, World!\n”)

return 0;

}

<-- starts the program: void means that

there is no return value or input	parameters

<-- prints a string, which is in between

quotation marks

<-- marks end of program

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

Remember data types?

Numbers: int, float

Strings: str

Characters: char

Booleans: bool

Other data types: a simple example

#include <stdio.h>

main(void)

{

int x, y, divide, remainder;

x = 10;

y = 13;

divide = x / y;

remainder = x % y;

printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder);

return 0;

}

Printing the value of a variable

%d – tells the compiler that we will be printing an integer in this spot, which will appear after the comma

Other possibilities

%c for a character

%f for a floating point number

Floats

#include <stdio.h>

main(void)

{

float x, y, divide;

x = 10;

y = 13;

divide = x / y;

printf(“%f divided by %f is %f”, x, y, divide);

return 0;

}

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;

}

More complicated if statements

if (x > y)

if (x > z)

max = x;

else

max = z;

else

if (y > z)

max = y;

else

max = z;

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)

{

int number, sum;

printf(“Enter a number:”);

scanf(“%d”, &number);

while (number != 0) {

sum = sum + number;

printf(“Enter another number, or 0 to stop:”);

}

printf(“The sum is %d\n”, sum);

return 0;

}

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;

}

2008-10-03T15:06:49