Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Introduction To C

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

An Introduction To C

A Presentation for CI Computer Girls

Avatar for Winter Smith

Winter Smith

April 14, 2017

Other Decks in Programming

Transcript

  1. A Basic Introduction to C CI Computer Girls Coding Bootcamp

    2017 Winter Smith and Nicholas Dolan-Stern Code written by Anthony Brice and Kelsey Geiger
  2. What is C? C is an imperative programming language. Chances

    are you've worked with one of the following imperative languages before: Java Assembly Python Ruby After we get past the syntax differences, the flow of C should feel natural.
  3. Overview In this presentation we'll cover: Getting started with the

    shell Hello World! Arguments Functions Pointers Structs Headers After this workshop you'll recieve a copy of this presentation and the finished code for further reference
  4. Getting Started We're going to be using the following programs

    Sublime Text Editor This is where you'll be writing and editing your code Terminal This is the OSX command-line interface (CLI) where you'll be compiling and running your code. To find this open up Spotlight search (command-space) and enter in 'Terminal'
  5. Getting Started Finding your way around Terminal and learning basic

    commands 1. Open up a new Terminal window 2. Type in c d ~ / D e s k t o p cd stands for change directory 3. Type in m k d i r b o o t c a m p mkdir stands for make directory and "bootcamp" is the name of our new directory 4. Type in p w d pwd stands for print working directory. 5. Place all the files from the C Presentation folder on your usb in here
  6. Hello World Let's write your first C program Pop open

    Sublime and let's start coding! # i n c l u d e < s t d i o . h > m a i n ( ) { p r i n t f ( " H e l l o , w o r l d ! \ n " ) ; } Because we're using printf, we have to include where it's defined. That's where #include <stdio.h> comes from. printf takes in a string as it's argument. Like System.out.println() in java or print() in python Save this file as hello.c and save it in your bootcamp directory
  7. Hello World Let's compile and run your first C program

    Pop back into Terminal and type the following commands 1. p w d to make sure you're in the right place. 2. l s and you should see hello.c in your directory. 3. g c c h e l l o . c gcc is a tool that compiles your code. A compiler can be built into your text editor or not. 4. . / a . o u t
  8. Prompts In your bootcamp folder, you should have a file

    called prompt.c open that up in Sublime It should contain the following code # i n c l u d e < s t d i o . h > m a i n ( ) { c h a r n a m e [ 4 0 ] ; / / H o w s t r i n g s w o r k i n C p r i n t f ( " E n t e r y o u r n a m e : \ n " ) ; / / Y o u r T a s k : u s e t h e u s e r i n p u t t o s a y h e l l o t o t h e m ! } On the next slide we'll go over the functions you'll need to complete this task!
  9. Prompts Prompting users for input is fun, but it's useless

    if we don't know how to save the input right? To save user input: s c a n f ( " % s " , d e s t i n a t i o n ) ; s c a n f takes in two arguments a data type and a destination variable. Here we assume that the input is a string and we write %s and we're sending it to a variable named destination. So, we learned how to use p r i n t f ( ) to say "Hello World!" but what if we want to print out a variable AND a string? To print out a variable: p r i n t f ( " G o o d b y e % s " , v a r i a b l e " ) ; like s c a n f , p r i n t f can also be given two arguments.
  10. Prompts Revisit the code in prompt.c Using the functions that

    you learned on the last slide, you should now be able to fill in the missing code and run the program like we did before
  11. Arguments Pop open your file titled arguments.c it should contain

    something like this: # i n c l u d e < s t d i o . h > # i n c l u d e < s t d l i b . h > i n t m a i n ( i n t a r g c , c h a r * a r g v [ ] ) { i f ( a r g c < 3 ) { p r i n t f ( " U s a g e : % s < n a m e > < i n t e g e r > \ n " , a r g v [ 0 ] ; r e t u r n - 1 ; } / / Y o u r T a s k : R e a d t h e u s e r ' s n a m e / / a n d a n i n t e g e r f r o m c o m m a n d l i n e a r g u m e n t s , / / t h e n s a y h e l l o t o t h e u s e r a s m a n y t i m e s a s / / g i v e n b y t h e i n t e g e r . }