CS156: Intro to C, Part I

Spring 2018

How To Program

See this page as a slide show

CS156 HowToProgram: How to write a program

Hello World #1

/*
 * Purpose: Say hello to the world.
 */
#include <stdio.h>
int main() {
    // Say hello
    printf("Hello, world.\n");
    return 0;
}

Hello World #2

#include <stdio.h> 
int main(){ 
  int x=0,y[14],*z=&y;*(z++)=0x48;*(z++)=y[x++]+0x1D;    
  *(z++)=y[x++]+0x07;*(z++)=y[x++]+0x00;*(z++)=y[x++]+0x03; 
  *(z++)=y[x++]-0x43;*(z++)=y[x++]-0x0C;*(z++)=y[x++]+0x57; 
  *(z++)=y[x++]-0x08;*(z++)=y[x++]+0x03;*(z++)=y[x++]-0x06;   
  *(z++)=y[x++]-0x08;*(z++)=y[x++]-0x43;*(z++)=y[x]-0x21; 
  x=*(--z);while(y[x]!=NULL)putchar(y[x++]); 
} 

Signs of a Good Program

Signs of a Bad Program

Why does good programming matter?

A Few Style Tips

Practical Program Writing: What to do.

Practical Program Writing: What not to do.

Bugs and Errors

Correcting Syntax Errors

Example

    // a bad example
    #include <stdio.h>
    int main() {
        float friction;
        foat number;

        // assign some values
        friction = .04;
        number = friction 3.2 * 2;

        // print number
        printf("The final number was %f\n" number);
        printf("The value of \"friction\" was %f.\n",friction);
        return 0;
    }

Example

    % c11 c.c
    c.c: In function ‘main’:
    c.c:5: error: ‘foat’ undeclared (first use in this function)
    c.c:5: error: (Each undeclared identifier is reported only once
    c.c:5: error: for each function it appears in.)
    c.c:5: error: expected ‘;’ before ‘number’
    c.c:9: error: ‘number’ undeclared (first use in this function)
    c.c:9: error: expected ‘;’ before numeric constant
    c.c:12: error: expected ‘)’ before ‘number’
    c.c:12: warning: too few arguments for format

Finding and Correcting Logical Errors

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2017-03-05T17:12

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building