{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week2-C-datatypes - The C Programming Language C is a...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The C Programming Language • C is a high-level language — structured • C is a low-level language — machine access • C is a small language, extendable with libraries • C is permissive: assumes you know what you’re doing • Good: efficient, powerful, portable, flexible • Bad: easy to make errors, obfuscation, little support for modularization
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Intro to C #include <stdio.h> int main() { int i; extern int gcd(int x, int y); for (i = 0; i < 20; i++) { printf("gcd of 12 and %d is %d\n", i, gcd(12,i)); } return (0); }
Background image of page 2
3 The rest of the file int gcd(int x, int y) { int t; while (y) { t = x; x = y; y = t % y; } return (x); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 About C • Similar to Java - Java took best of C • #include - use declarations of functions • main() returns int, the exit status • Functions must be – declared - tells compiler how to use function – defined - creates the item • Declarations must appear before code
Background image of page 4
5 Basic Control Structures • Functions - can omit extern declaration • for loop - like Java – body is one statement – braces { } enclose blocks – blocks introduce scope level – can't mix declarations and non-declarations •for (int i - illegal in ANSI C
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 More about C • Uninitialized variables have no default value! • No run-time checking! • No polymorphism ( printf format strings) • No objects Compile: gcc -Wall -g -o gcd gcd.c
Background image of page 6
7 C data types • basic types and literals (King: Ch 7) int i = 38; long el = 38L; int hex = 0x2a; int oct = 033; printf("i = %d, el = %ld, hex = %d, oct = %d\n", i, el, hex, oct); double d1 = 0.3; double d2 = 3.0; double d3 = 6.02e23; printf("d1 = %f, d2 = %f, d3 = %e\n", d1, d2, d3) i = 38, el = 38, hex = 42, oct = 27 d1 = 0.300000, d2 = 3.000000, d3 = 6.020000e+23
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 C literals and types Literal Value Type 38 38 int 38L 38 long int 0x2a (hex) 42 int 033 (octal) 27 int 38.0 38.0 double 38.0f 38.0 float
Background image of page 8
9 C data types • Most things in C are ints: – Boolean values are ints • 0 means false, nonzero means true – characters are ints (ASCII code) • 'a' ==97, '\n' ==10, '\033' ==033==27 – enumerations are really ints • signed vs. unsigned types • char, int, long, … are just different sizes of integers.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Data Type Conversion The expression on the right side is converted to the type of the variable on the left. char c;
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

Week2-C-datatypes - The C Programming Language C is a...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online