Docsity

CMSC 106 Lecture Set #2: C Language Syntax and Semantics - Prof. Jandelyn Dawn Plane, Study notes of Computer Science

Toggle sidebar
bg1
CMSC 106
Lecture Set #2
Set Started:
Tuesday, September 4, 2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download CMSC 106 Lecture Set #2: C Language Syntax and Semantics - Prof. Jandelyn Dawn Plane and more Study notes Computer Science in PDF only on Docsity!

CMSC 106Lecture Set

Set Started:

Tuesday, September 4, 2007

C language – syntax^ ^ Syntax^ 

rules of the grammar  vocabulary recognized by the language  ANSI standard^ ^ American National Standards Institute

^ Semantics^ 

the meaning of what is being said

Program Errors

^ Incorrect Syntax^ ^

The compiler gives error message at that spot andrefuses to compile it.  The compiler gives warning message at that spot but stillcompiles it.  The compiler gives error or warning message at a spotlater in the file.

^ Incorrect Semantics^ ^

Program does nothing when run  Program does nothing useful when run  Program does the “wrong” thing when run  Program “crashes” or “hangs” when running

Basic Program Structure

^ a program must be comprised of 1 or morefunctions^ ^

function = named program part for performinga specific task

^ must be 1 and only 1 function named

main

^ controls everything else ^ starts there and determines who gets to gowhen ^ for now this is the only function you’ll design

Functions: Identified by Name

^ Identifiers^ ^

Used to name functions, variables, etc.  String of alphabetic characters, numeric digitsand the underscore  Case sensitive  Can not start with a numeric digit  Must be a unique name

  • Identifier examples  cmsc106  CMSC106  cmsc_106  cmsc.106  _cmsc_106  106cmsc  106_cmsc  cmsc 106 

Function Return #include <stdio.h>int main(){printf("this is a complete program\n");return 0;}  every function should end with a returnstatement that returns its “return value” to thecaller  main returns to the operating system  0 as a return value from main means “all is well”

Readability Issues

^ Comments^ ^

/*^ comment

^ ignored by compiler ^ for human reader ^ multiple lines is fine ^ used to explain what it is doing and/or how ^ can not

be nested

^ every function needs a comment to tell its use and purpose ^ every place it would help the readability comments should be included  Spacing ^ vertical spacing ^ horizontal spacing ^ also ignored by the compiler and for the human reader ^ should acurately reflect the meaning and flow

Data types

^ first two basic data types:^ ^

int  float^ |^

| +------+|^?

|^

amount +------+ memory

|^

?^ |^

grand_total +------+|^?

|^

tax +------+|^

+------+|^

values random when first declared

#include <stdio.h>main(){int amount,

grand_total; float tax;... }

#include <stdio.h>main(){ declarationsstatementsreturn 0;}

Assignment Statement

^ variable name = value; ^ Semantics^ ^

when executed, the right side iscalculated and  the result is stored in variable on left

^

a= b;  previous value

of left-side variable is

lost  left sit must be a variable  right side can be:^ ^ a number (

literal)

^ a^ variable ^ an^ expression  right side's value unchanged (

copy)

^ e.g.:

grand_total = 125; |^

+------+|^?^

|^ amount

+------+|^ 125 |

grand_total

+------+|^?^

|^ tax

+------+|^

Example

^ Trace this program's variables in memory #include <stdio.h>int main() {int num = 3, num2;num2 = 17;num2 = num;return 0;} ==================================== ^ Problem Solving: Exchanging variables' values^ ^

any variable can only hold one value at a time  assigning a value to a variable causes its previous value to be lost

^ Must use a "temporary" variable to exchange.

Printf – The rest of the story

^ Printing Variables^ ^

Syntax of printf:

printf(“literal string");

or

printf("format control string", list of variables);

^ format control string must hav a

format specifier

for^ each variable

in list

^ format specifiers:

%letter

^ %d

  • print as an integer

^ %f

  • print as a real number

^ Example of printf for values:

printf("num is %d\n and num2 is %d\n", num, num2);

(ex: printf.with.values.c)

Symbolic constants^ #define NAME value^ ^

gives a name to a

constant

value

^ #define

BOILING

^ no semicolon because it is not a C statement isit handled by the preprocessor ^ convention: to distinguish constant names they arewritten in ^

all uppercase letters

^ (ex: constants.c)

Data Types

^ Integer Family^ ^

char^

typically 8 bits ^ short

typically 16 bits ^ int^

typically 32 or 64 bits ^ long

typically 32 or 64 bits ^ long long

typically 64 bits ^ All are signed by default, but can be made unsigned^ ^

unsigned int

(typically, 0 to 4,294,967,295)

^ Literals^ ^

Decimal (255), Hex (0x255), signed (-255)  Character (‘a’, ‘\n’)

^ Don’t be stingy with size, when in doubt use a larger size  Floating Point ^ float, double, long double ^ Literals (3.14159, 1E10, 25., 6.023e23)  Character and String Literals ^ Character Literals: ‘a’, ‘9’, ‘\n’ ^ String Literals: “a long dull string”, “\n”, “”