Ecse 202 midterm prep
Warning with low language
-no range checking (checking if the value is within th bounds of the array) -limited type safety at compile time (type checking checks if the type used can be used ex. using modulo % will only work if theres two integers) -no type checking at run time
Write a program to calculate the sum of digits from 0 to 5
#include <stdio.h> int main(){ int i,sum=0; for (i=0; i=<5; i++) sum=sum+i ;// i++= i+1 print("The sum of digits from 0 to 5 is %d.\n",sum); }
Tips on how to handle it with care to avoid problems :
- Always run a debugger like gdb -never run as root (when youre doing it as admin) -never test code on Athena servers (MIT academic computing environment)
IDE : all in one solution popular ones? what are they? very convenient fro what ?
- Eclipse -Microsoft Visual C++ -KDevelo, XCode -Integrated editor with compiler, debugger - very convenientfor larger programs
CV related languages
- Most recent derivative : C++, Objective C, C# Influenced : Javal Perl Python
what is gcc what is gc extension (hint u use it in cmd)
- compiler his extension is -o
Conditional preprocessor macros #if #ifdef #ifndef #else #elif #endif can control which lines are compiled
-evaluated before code itself is compiled so conditions must be preprocessor defines or literals -used in header iles to ensure declarations happen only once
Strings : strings stored as character array - null terminated in C (write what it means )
-last character in array is '\0' , not written explicitely in string literals (output to the printer as written)
there exists more complicated form of gcc
-multiple source files -auxiliary directories -optimization linking
family of data types in C
-numeric (int, float, double) -character (char) -user defined (struct, union )
types of operators : 1.unary operators 2. binary operators 3.ternary operators
1. operate on a single operand ex. -, ++, -- , ! (not ) 2.operate on 2 operands (+,-,*,/) 3.operates on 3 operands (?, : ex.a ? b : c)
Examples 1. #pragma 2. #error, #warning 3. #undef msg
1. preprocessor directive it's basically compiler directives that are not default 2.trigger a costum compiler error/warning 3. remove the definition of msg at compile time
suppose x and v are variables Binary arithmetic: x+y, x-y, x*y, x/y, x%y Simple statement : y= x+3*x/ (y-4); (; ends statement not new line) What do these mean : 1. x+=y 2. x-=y 3.x*= y 4. x/=y 5.x%=y
1. x+y=x 2. x-y=x 3. x*y=x 4.x/y=x 5.x%y=x
Other important header files
<stlib.h>, <ctype.h>, <math.h> , <stdlib.h> , <string.h> , <time.h>
Def: compiler
A program that translates high level code (e.g. C) into that native instructions of the machine on which the code is executed.The output is usually in the form of assembly language, a symbolic representation of the machine's instruction set.
what is a return value?
A single value returned to caller when function exists
Assembly language if branch=0 base on last ALU instruction performed
BREQ
Def: linking
Combines object code with library and system code to produced a program that can be directly executed by hardware (the case with C) or interpreted by software (ex java virtual machine)
Assembly language : ADD instruction has 2 forms. Form 1: ADD destination, source 1, source 2
Computes sum of values in location source 1 and 2 result to destination
Assembly language: LD (load) type 1/ LD : destination, source give definition
Copies the data at memory location source to memory location destination
Assembly language: LD (load) type 2 : LDI : destination value
Copies values a numeric argument embedded directly in the instruction to destination
In a basic computer what is the data memory linked to? (2) for two headed
Counter program(2), controller(2),input\output(2),to ALU
In a basic computer , what is the input\output linked to?
Data memory(2)
In a basic computer, What is the program counter linked to? Put (2) if it's a 2-headed arrow
Data memory(2), controller(2),program memory(2), ALU(2)
In a basic computer, what is the controller linked to?(2) for 2-headed arrows
Data memory(2),from program memory, counter program(2),ALU(2)
C History :who invented it , where did he work (electrical eyyy) ,when and where did he do it? on what computer?
Dennis Ritchie - AT&T Bell , Laboratories 1972, 16-bit DEC PDP-11
How does source code get translated into executable code? (Schema)
Source code-->compiler+Assembly-->object code(+other object code & libraries)-->linker-->executable
what is a datatype?
The datatype of an object in memory determines the set of values it can have and what operations that can be performed on it C is weakly typed language it allows implicit conversions (type conversions by the compiler) as well as forced (potentially dangerous) casting
what is an expression
a combination of values , operators, variables and functions
const char msg[]
a constant array of characters
QUESTION : When it's written : the gcc option Dname=value sets a preprocessor define that can be used
does this mean write that on the cmd when looking for our file
return 0 ;
exits the function, returnin value 0 to caller we put this because the main function signature requires it in C , it's the exit code for the program
Why is C widely used today
extends to newer system architecture low level access (works closely with the processor's instruction) efficiency/performance
What does C feature?
fils comme pute mange ses endives (Few keyworks, Compiles to native code, Pointers- memory,arrays, Macro preprocessor, Structure, unions, compound data type , External Standard Library - I/O , other facilities
How do i insert parentheses to get z= 4.0
float z = (x+3*x)/(y-4)
What are declaration of functions called
function prototype
to enable compiler warning use wall where ?
gcc -wall filename.c -o filename.o
How would you write a function type factorial which returns an integer and has one argument
int factorial (int); int factorial (int n)
Two argument form of main() : access command-line arguments
int main (int argc, char** argv);
write the code to store the string in a variable before printing
int main (void ) { const char msg[] = "hello 6087 students" ; puts (msg); return 0; }
Declaring variables : we must declare variables before use ; write the way we should do it for an integer number and a fractional number with the variable x
int x; float x;
const keyword
qualifies variable as constant
#define can also take arguments and be treated like a function ex. #define add3(x,y,z) ((x)+(y)+(z)) compiler performs inline remplacements which means that it's not suitable for
recursion
What's the general form of declaring a function?
return type function name (arg1, arg2...)
naming rules for variables
-they can contain letters, digits and_ -name should start with letter -keywords cannot be used as variables (ex. for while etc) -variable names are case sensitive (theres a pop quix slide 9 ppt 2 if u wanna practice)
int x=0 , y=0 y =x+2 what are the variables, the expression and the operator
-x,y are variables -y=x+2 is the expression - + is the operator
Structure of a C file
/*Begin with comments about filecontents*/ Insert#includestatementsandpreprocessordefinitions Functionprototypesandvariabledeclarations Definemain()function{Functionbody} Defineotherfunction{Functionbody}...
Special characters specified in C using \ (escape character) what do these mean ? 1. \\ 2. \' 3.\" 4. \b 5. \t 6. \r 7. \n 8.\ooo 9. \xhh
1. backlash 2.apostrophe 3. quotation mark 4.backspace 5.tab 6.carriage return (cursor at the beginning of the line) 7.line feed (start a new line) 8. Ascii character translates letters into binary code theres 127 of them
Initializing values : uninitialized variable assumes a default value 1. variables initialized via assignment operator 2.can also initialize a declaration 3.can declare/initialize multiple variables at once Give examples for each
1. int n n=3; 2. float phi=1.4793749; 3. int a, b ,c=2, d=4;
Console input/ output: stdout, stdin: console output and input streams What do these mean? 1.puts(string) 2.putchar(char) 3.char = getchar() 4.string = (get(string))
1.print string to stdout 2. print character to stdout 3. return the character from stdin 4. read the line from stdin into string
Order of operations : say the evaluation direction for each of these signs 1. +, - (sign) 2. *, /, % 3. + , - 4. =, +=, -=, *= , /= , %= Use parentheses to override order of evaluation
1.right-left 2. left to right 3. left- right 4. right to left
Numeric data type write the different data types in their signed and unsigned counterparts (the unsigned has double the range , they only differ in arithmetic expressions) 1. short 2. default 2. long 4. float 5. double 6. char
1.short int x; short y; - unsigned short int x, unsigned short y; 2. int x ; - unsigned int x 3. long x - unsigned long x 4. float x - N/A 5. double x - N/A 6. char x,signed char x - unsigned char x;
1972 :? 1978:? 1989:? 1990: ? 1999: ? 2007: ?
1972 : Cinvented 1978 : C programming languge 1989: C89 standard (known as ANSI C or standard C) 1990: C99 Standard -mostly backward compatible -not completely implemented in many computers -2007 : work on new C standard C1 X announce Inn this course : ANSI/ISO C 9C89-C90)
Def:input/output
Allows external access to data memory
Assembly language : BR (branch) #instructions to skip instruction : which one is unconditional vranch
BR
Assembly language which one is branch if >0 based on the last ALU instruction performed Branch is an instruction that makes the program execute a different instruction sequence and deviate from its default path
BRGT
Assembly language which on is branch if <0 base on last ALU instruction performed
BRLT
What is C used for
C is used for MODE Microcontrollers : airplanes and automobiles Osed , like Linux DSP processors : digital audio and TV systems E : embedded processors : phones, portable, electronics etc.
Advantage of a low level language?
Faster code
Assembly language: ADD instruction type 2 : ADI def
Like add but second argument is value embedded in instruction
C lacks
PORGE (Like the purge ) Polymorphism , Object-oriented programming, Range-checking, Garbage collection, Exceptions
Def: ALU, Arithmetic and logical unit *has a minimal instruction set{ADD,COMP(1's complement),AND,OR,SHL(shift left),SHR(shift right),ASR(arithmetic shift right),ROL (rotate left) and ROR (rotate righr)
Performs arithmetic and logical operations on entities stored in data memory
In a basic computer, what is the ALU linked to?2 for two headed
Program counter(2),controller(2),from data memory,to program counter
What is source code (written in a text editor) also referred to as?
Raw TEXT or ASCII
Def: Program counter
Register containing the adress in program memory of the next instruction to execute.The new Program counter value is calculated as a by product of the current instruction.
Controller
Sequences data with ALU to perform machine instructions.Sequences groups of instructions to compute programs.
Comments
Simple comment /*this is a simple comment*/ can span multiple lines -can appear almost anywhere
In a basic computer, what is the program memory linked to?(2) for 2-headed arrows
To controller,from program counter
Def : compilation
Translates code from high level code into an intermediate representation (assembly language) which ultimately is stored as object code and stored as files of binary data
QUESTION : included files must be on include path -Idirectory with gcc : specify additional include directories -standard include directories assumed by default
What are thses standard directories assumed by default?
Def : data memory
Where data associated with a program is stored. Most machines can adress memory in chunks of 8-bits(byte),16-bits(short word),32-bits(long word),64 bits(long long word)
Def: Program memory
Where the program is stored in machine code.A compiler translates high level code into a set of simple instructions that are interpreted by the hardware.
you need to be aware of how the code you write impacts performance (write example in a) ) you need to be aware of tradeoffs that can be made (ex. b)
a) size , speed power b)hardware vs software, speed vs battery life, cost of design vs security of code
what is a variable
as named link/reference to a value stored in the system's memory or an expression that can be evaluated
Useful commands in gdb (debugger)
break (line number) : create breakpoint at specified line break (file: line number) : create breakpointat specified line in file) c : continue execution run : run program next : execute next line step : execute next line or step into function quit : quit gdb print (expression) : print current value of the specified expression help (command) : in program help
performance depends on
compiler ( efficiency translation of the code), the characteristic of the hardware, characteristic of the algorithm (computational complexity)
what is the char data type in C
corresponds to a signed (can be positive or negative ) 8 bit integer [-128, 127], it happens that this is a convenient representation for the ASCII character code
char
data type representing a single character written in quotes 'a', '3' , 'n'
what is embedded in gcc?
debugging info and disable optimization
Preprocessor macros begin with # character #include <stdio.h> #define mds "hello, 6087 students" what does it do
defines msg as "hello 6087 students" throughout source file
main () entry point for C program what is the simplest version of main : no inputs, outputs0 when succesful and nonzero to signal some error
int main(void)
write code that puts "hello 6087 students" on the console
int main(void) { puts ("Hello 6087 students"); } return 0 ;
What does the #include in C mean
it calls a header file which contains functions, constants and other declarations
What are arguments?
local variables , values passed from caller caller is basically the main function at the beginning ex. void (calculate) int (counter= 0) add (counter, 5) calculate is the caller
what does void signify?
no return value/arguments
what is puts() :
output text to console window (stdout) and end the line (stdout is a pointer to a file which represents the default output device for the application
string literal
output to the screen or printer as written written surrounded by double quotes
<stdio.h> what is it
standard I/O (input/output) functions for consoles, files
what does int factorial (int n ) mean?
the function takes int and returns int
operators
they specify how an object can be manipulated (ex. numeric vs string operations
Evaluate the statement : float z = x+3*x/(y-4) x= 2.0 y=6.0
z=5.0