CS156: Intro to C, Part I

Spring 2018

Variables

See this page as a slide show

CS156 Variables: C Variables & Expressions

Basic C Program Structure

/*
 * Comment section
 */
… Preprocessor directives …
… Evil Global Variable Declarations …
int main( ) 
{ 
        … Local Variable Declarations …
        // Comment
        … Executable Statements …
        … Local Variable Declarations …
        // Comment
        … Executable Statements …
        return 0; 
} 

C Elements: Comments

// CS156 HW3
// Author: Jack Applin
// Purpose: Demo of first program
// Input: none
// Output: prints Hello World
// This is an in-line comment.
//
/* This is a multi-line comment. */
/*
 * so is this
 */
/* and
this
as well */
// But, of course,
// so is this, sort of.

Careful!!

// This // will // work.
// This won’t work:
/*
int a=1;
int b=2;       /* hello */
int c=3;
*/

C Elements: #include

C Elements: Variables

Data types

Standard Data Types
intchardouble & floatvoid
-20'x'-30.4 
42'3'3.0 
123456789'\n'6.022e23 

Note that a single char literal is 'x' with just a single character between the single quotes. A string literal "xyz abc" may contain 0 or more characters between the double quotes and is automatically null terminated.

C Elements: Variable Declarations

Basic Variable Types

The word “decimal”

C Elements: Simple Expressions

Mathematicians Beware

Beware, mathematicians: some things are different:

ExpressionResult
axbthe variable axb
a x ba syntax error
3da syntax error
a⋅ba syntax error
d3the variable d3

C Elements: Assignment Statements

Example

#include <stdio.h>
int zip_code;       // an evil global variable
int main() { 
    double friction; // local variable for amt of friction 
    double number;   // local variable for calculation

    // assign some values
    zip_code = 44455;
    friction = 0.04;
    number = (zip_code * friction) - 3.2;

    // print number
    printf("The final number was %f\n", number);
    return 0; 
} 
The final number was 1775.000000

stdio.h: printf

Example 2

#include <stdio.h>

int main() { 
    double friction, number;
    int numPeople = 3;

    // assign some values
    friction = 0.04;
    number = friction + 3.2 * 2;

    printf("The final number was %f\n", number);
    printf("The value of \"friction\" was %f.\n",friction );
    printf("There are %d people here.\n", numPeople );
    return 0; 
} 
The final number was 6.440000
The value of "friction" was 0.040000.
There are 3 people here.

Your Dear Aunt Sally can take a hike

In elementary school, students often learn acronyms such as PEMDAS. These are useful, to a point.

Consider 1 − 2 + 3.

Expressions and Operator Precedence

Highest precedence, happens first
( )
* / %
+ -
=
Lowest precedence, happens last

Example 3

printf("1+2*3 = %d\n",     1+2*3);
printf("(1+2)*3 = %d\n",   (1+2)*3);
printf("2*3+4*5 = %d\n",   2*3+4*5);
printf("2*(3+4)*5 = %d\n", 2*(3+4)*5);
1+2*3 = 7
(1+2)*3 = 9
2*3+4*5 = 26
2*(3+4)*5 = 70

The format HAS to match the type

printf("%d\n", 123.456);
printf("%f\n", 789);
c.c: In function 'main':
c.c:1: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double'
c.c:2: warning: format '%f' expects argument of type 'double', but argument 2 has type 'int'
-232794680
123.456000

Type Conversion and Division

Type Conversion Examples

printf("%f\n", 42 + 0.0);
printf("%f\n", 3.0 / 1.5);
printf("%f\n", 3 / 1.5);
printf("%d\n", 3 / 2);
printf("%d\n", 21 / 8);
42.000000
2.000000
2.000000
1
2

A brief note about floating-point constants

How to confuse your readers:

    x = 3. + .4;

Was that .4 or 4? Don’t make me squint to see a leading or trailing decimal point—help me out with a zero:

    x = 3.0 + 0.4;

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-21T14:18

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