Today s material
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

Today’s Material PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

Today’s Material. Why C? History of C Generic C Program Structure Variables Type and Name Assignment Operator Simple I/O scanf/printf. Why C?. Simple and Structural Minimal syntax Availability and Portability C compilers are available for a wide range of platforms

Download Presentation

Today’s Material

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Today’s Material

  • Why C?

  • History of C

  • Generic C Program Structure

  • Variables

    • Type and Name

    • Assignment Operator

  • Simple I/O

    • scanf/printf


Why C?

Simple and Structural

Minimal syntax

Availability and Portability

C compilers are available for a wide range of platforms

Windows, UNIX, embedded systems

Language of choice for system programming

Most OS code are written in C

Widely known

Almost every programmer knows C

There are lots of public code written in C


History of C

C evolvedas follows:

CPL Combined Programming Language (Barron, 1963)

BCPL Basic CPL (Richards, 1969)

B (Thompson, 1970)

C (Kernighan & Ritchie, 1972)

ANSI C American National Standards Institute C (X3J11, 1989)


Generic C Program Structure

#include <stdio.h>

/* main designates the starting place of our program */

main() {

/* Variables hold Data Items Needed in the Program */

Variable Declarations;

/* Steps of our program: I/O, computation (expressions) */

Statement1;

Statement2;

StatementN;

} /* end-of-the program */


Start

Prompt the user for a temperature

Get the fahrenheit temperature

celsius = (fahrenheit-32)/1.8

Print the fahrenheit and

celsius degrees

End

First C Program

PROBLEM

Convert a fahrenheit

temperature to celsius

#include <stdio.h>

/* Convert fahrenheit to celsius */

main()

{

float fahrenheit;

float celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

}


First C Program Dissected

#include <stdio.h>

/* Convert fahrenheit to celsius */

main()

{

float fahrenheit;

float celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

}

Load the standard library to handle I/O

Comments: In between /* …. */

main(): Designates where the execution will start

Curly braces

Determines the beginning and the end of a code block


First C Program Dissected (cont)

#include <stdio.h>

/* Convert fahrenheit to celsius */

main()

{

float fahrenheit;

float celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

}

  • Variables

  • A variable is a memory location whose contents can be filled and changed during program execution

  • float is the variable type

  • fahrenheit & celsius are variable names

  • Statements

  • Steps of your program.

  • Statements end with a semicolon (;)


First C Program Dissected (cont)

#include <stdio.h>

/* Convert fahrenheit to celsius */

main()

{

float fahrenheit;

float celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

}

Output

Use printf function to print something on the screen

Input

Use scanf function to read something from the keyboard

Computation

Use mathematical operators to perform computation


prog.c

COMPILER

prog.s

ASSEMBLER

printf.o

prog.o

scanf.o

LINKER

prog.exe

LOADER + OS

program running

From C Code-2-Machine Code

  • Now that we have our C program, how does it get translated to machine code, i.e., to 0s and 1s

    • Remember a computer understands just 0s and 1s

  • We use several system software components to translate out C program to machine code

    • Compiler, assembler, linker, loader+OS


First C Program Executed

PROGRAM

#include <stdio.h>

/* Convert fahrenheit to celsius */

main() {

float fahrenheit, celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

} /* end-main */

DATA

fahrenheit

75.4

?

?

celsius

24.11

?

Enter a temp in fahrenheit:

75.4 degrees fahrenheit equals 24.11 degrees celsius

75.4


number1

number2

What’s a Variable?

  • A memory location whose contents can be filled and changed during program execution

    • Each variable has a type and name

    • Type defines the type of information that can be stored in that memory location

    • Nameis a label that allows us to refer to that memory location

Memory

name

  • intnumber1; /* stores a natural number (152) */

  • floatnumber2; /* stores a real number (65.324) */


Variable Declaration

  • The basic format for declaring variables is

    datatype varName, varName, ... ;

    where datatype may be

    int/* Stores a natural number, e.g., 34532 */

    float/* Stores a real number, e.g., 15.342 */

    double/* Also stores a real number, but more precision */

    /* e.g., 345.22359573943 */

    char/* Stores an ASCII char, e.g., ‘A’ */


Variable (Identifier) Names

  • C identifiers consist of letters and digits in any order, except that:

    • The first character must be a letter

      • fahrenheit, celsius, sum, a123, i1, i2, i3

    • The identifier can be in lowercase or uppercase

    • The upper and lower cases may be mixed

      • sum, Sum, SUM

    • The underscore (_) can be included and it can also be the first char of the identifier

      • total_sum, num_students, _localVar


Variable (Identifier) Names (cont)

  • C identifiers consist of letters and digits in any order, except that:

    • The identifiers should not contain a blank space, hyphen or quotes

      • “sum” : quotes(“) is illegal

      • total-sum : illegal character ( - )

      • total sum : blank space should not be there

    • Identifier names are case-sensitive

      • sum, Sum and sUm are all different identifiers


Variable Examples & Assignment

int x; /* Define 1 int */

float fahrenheit, celsius; /* Define 2 floats */

double d; /* Define 1 double */

x = -85; /* Assign -85 to x */

d = 3.4545673; /* Assign 3.4545673 to d */

fahrenheit = 75.0; /* Assign 75.0 to fahrenheit */

celsius = 23.4; /* Assign 23.4 to celsius */

  • = is the assignment operator

    • Syntax: identifier = value;

    • Changes the contents of the memory location that the variable refers to

75.0

3.4545673

-85

23.4

x

fahrenheit

d

celsius


Variable Declaration & Initialization

  • Variables can be initialized during declaration

int sum = 32000; /* can be positive */

int x = -23, y = 458; /* and negative */

float f1 = 34.5;

float fahrenheit = 75.3, celsius = 0;

double d1, d2 = -4.567; /* Can be negative */

double d3 = 5e+3; /* Scientific notation: 5x10^3 */

d1 = 675e-4; /* 675x10^-4 = 0.0675 */

32000

-23

458

34.5

75.3

0.0

0.0675

-4.567

5000

sum

x

y

f1

celsius

fahrenheit

d1

d2

d3


Character Variables

  • Represents a single character

    • Characters are letters of the alphabet (both upper and lower case)

    • Ten digits 0 through 9

    • Special symbols such as + $ . , - !

  • Characters need to be enclosed in single quotes

    • e.g. 'A'

char letter;

letter = 'A'; /* Letter A */

letter = ‘9’; /* Digit 9 */

char letter = ‘c’; /* initialization */


Character Variables (more)

  • In fact, char corresponds to 1 byte natural number

    • char type variables occupy 1 byte in memory

  • What’s really stored in a char variable is the ASCII value of the character

    • ASCII value of ‘A’ is 65

    • ASCII value of ‘B’ is 66

    • ASCII value of ‘0’ is 48

    • ASCII value of ‘1’ is 49

    • http://www.asciitable.com/


ASCII Table


Special Characters

  • Characters are enclosed in single quotes

    • How do I denote a single quote?

char letter;

letter = ''';

char letter;

letter = '\'';

  • When a backslash (\) is used in front of a character, the combination is called an Escape Sequence


Escape Sequences

  • Combinations of a backslash (\) and a character that tells the compiler to escape from the ways these character would normally be interpreted

  • Commonly used escape sequences

    • \nmove to next line

    • \tmove to next tab setting

    • \\backslash character

    • \'single quote

    • \"double quote


sizeof Operator

  • You can learn the number of bytes that a certain type occupies in memory using the sizeof operator

int a = sizeof(char); /* Returns 1 */

int b = sizeof(short); /* Returns 2 */

int c = sizeof(int); /* Returns 4 */

int d = sizeof(long); /* Returns 4 or 8 */

int e = sizeof(float); /* Returns 4 */

int f = sizeof(double); /* Returns 8 */

1

2

4

8

4

4/8

a

b

e

f

c

d


Keyboard Input/ Display Output

  • Achieved using scanf/printf functions

    • Definitions of these functions are in <stdio.h>

    • So we must include these definitions at the start

#include <stdio.h>

int main() {

scanf();

printf();

} /* end-main */


printf/scanf Functions

  • Formatted input/output functions in C

    • Definitions in standard I/O library <stdio.h>

    • If we are using these functions in our programs, we must include these definitions by #include <stdio.h> at the beginning of our programs

printf(FormatString, expression1, expression2, …);

scanf(FormatString, &variable1, &variable2, …);

  • FormatString is enclosed in double quotes

    • “abcdxyz”

    • “number is %d\n”

      • Format specification %d is used to print an int


printf Examples

int x = 45;

float y = 56.7;

printf(“What’s up?\n”); /* Print a msg only */

printf(“Number x: %d, 2 times y: %f\n”, x, y);

printf(“Sum of x and y is %f\n”, x+y);

Expression

section

Format section

What’s up?

Number x: 45, 2 times y: 56.70000

Sum of x and y is 101.7000


printf Format Specifiers


printf Examples

printf(“First line.\nSecond line. Percent char %% XYZ\n”);

printf(“3rd line. Slash \\, single quote \`\n”);

printf(“4th line. Double quote \”. End\n”);

printf(“Line 5. char A: %c, ASCII value: %d\n”, ‘A’, ‘A’);

printf(“Line 6. Tab\tTab.\n”);

printf(“Line 7. Carriage return:\rOK\n”);

printf(“Line 8.\n”);

First line.

Second line. Percent char % XYZ

3rd line. Slash \\, single quote `

4th line. Double quote “. End

Line 5. char A: A, ASCII value: 65

Line 6. TabTab.

OKne 7. Carriage return:

Line 8.


printf Examples

char c1 = ‘A’, c2 = ‘c’, c3 = ‘9’, c4 = ‘\\’;

int x = 11, y = 16;

float f = 56.7;

double d = 456.789345;

printf(“c1 is <%c>, c2 is <%c>, c3: <%c>, c4: <%c>\n”,

c1, c2, c3, c4);

printf(“x is <%d>, y is <%d>, avg is <%d>\n”, x, y, (x+y)/2);

printf(“Sum of <%f> and <%lf> is <%lf>\n”, f, d, f+d);

c1 is <A>, c2 is <c>, c3: <9>, c4: <\>

x is <11>, y is <16>, avg is <13>

Sum of <56.70000> and <456.7893450000000> is <513.4893450000000>


printf: Specifying Field Length & Justification

char c1 = ‘A’, c2 = ‘c’;

int x = 11, y = 16;

float f = 56.7;

double d = 456.789345;

printf(“+-------+-------+\n”);

printf(“|%7c|%-7c|\n”, c1, c2);

printf(“|%-7d|%7d|\n”, x, y);

printf(“|%7.2f|%-7.1lf|\n”, f, d);

Printf(“+-------+-------+\n”);

+-------+-------+

| A|c |

|11 | 16|

| 56.70| 456.7|

+-------+-------+


More printf Examples

printf("%.9f\n", 300.00145678901f);

printf("%.19lf\n", 300.0014567890123456789);

300.001464844

300.0014567890123700000

float has 7 digit precision here (3000014)

double has 16 digits (3000014567890123)


scanf Examples

  • “%c” char

  • “%d” int

  • “%f” float

  • “%lf” double

char c;

int x;

float y;

double d;

scanf(“%d”, &x); /* Read 1 int */

scanf(“%c”, &c); /* Read 1 char */

scanf(“%f”, &y); /* Read 1 float */

scanf(“%lf”, &d); /* Read 1 double */

/* Read 1 int, 1 float, 1 double */

scanf(“%d%f%lf”, &x, &y, &d);

Variable

Address

Format

section


getchar and putchar Functions

  • getchar reads a single character from the keyboard

  • putchar writes a single character on the screen

  • Example:

char c;

printf(“What-to-do Menu \n”);

printf(“(a) To write a C program \n”);

printf(“(b) To go swimming \n”);

printf(“(c) To watch TV \n”);

printf(“Select one option: ”);

c = getchar(); /* Read the user choice */

getchar(); /* Skip newline ‘\n’ char */

putchar(‘B’); /* Prints B on the screen */

c = ‘Z’;

putchar(c); /* prints Z on the screen */


  • Login