Download presentation
Presentation is loading. Please wait.
Published byCory Hall Modified over 9 years ago
1
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers
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
Recap ● Numbers - int, float ● Output – printf ● Input - scanf ● If-else statements ● While loops ● Characters - char
5
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
6
Basic char program #include main(void) { char letter; printf(“Enter a character:”); scanf(“%c”,&letter); printf(“You entered %c”, letter); return 0; }
7
Fun with char #include 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; }
8
Char tricks #include 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; }
9
A shortcut ● The c command “getchar()” reads the next character from the input ● So letter = getchar(); is equivalent to scanf(“%c”, &letter);
10
Count the length of a message #include 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; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.