int main() { return 0; }

Similar documents
CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Lab 1: Introduction to C Programming

Pointers (2) Applications

CS 241 Data Organization Binary

Midterm Exam Review Slides

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Fundamental of Programming (C)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CS61, Fall 2012 Section 2 Notes

CS C Primer. Tyler Szepesi. January 16, 2013

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

Representing Integers

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

A Short Course for REU Students Summer Instructor: Ben Ransford

Systems Programming and Computer Architecture ( )

CS 0449 Sample Midterm

int main(void) { int a, b, c; /* declaration */

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers (part 2) Be extra careful with pointers!

CSC 373 Sections 501, 510 Winter 2015 C Examples

Language comparison. C has pointers. Java has references. C++ has pointers and references

ECET 264 C Programming Language with Applications

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Internal representation. Bitwise operators

Arrays and Pointers (part 2) Be extra careful with pointers!

C programming for beginners

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

ECET 264 C Programming Language with Applications

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Chapter 3. Fundamental Data Types

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Internal representation. Bitwise operators

Chapter 11 Introduction to Programming in C

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Internal representation. Bitwise operators


Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Lecture 05 Integer overflow. Stephen Checkoway University of Illinois at Chicago

cs50.c /**************************************************************************** * CS50 Library 6 *

Read: Kernighan & Ritchie

THE FUNDAMENTAL DATA TYPES

Programming Language A

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Computer Systems Programming. Practice Midterm. Name:

ECE264 Fall 2013 Exam 3, November 20, 2013

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

COMP 2355 Introduction to Systems Programming

Chapter 11 Introduction to Programming in C

Programming with Indexed Variables

Chapter 11 Introduction to Programming in C

High Performance Programming Programming in C part 1

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Detecting Errors in the Dual Port TPU RAM (DPTRAM) Module

CS 261 Data Structures. Introduction to C Programming

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Principles of C and Memory Management

Introduction to C. Systems Programming Concepts

Memory Allocation. General Questions

Understanding Pointers

CS270 Homework 5 (HW5)

While single variables have many uses, there are times when we wish to store multiple related values. For these we may wish to use an array.

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Functions & Memory Maps Review C Programming Language

COMP1917 Computing 1 Written Exam Sample Questions

Chapter 11 Introduction to Programming in C

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Fundamentals of Programming

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Fundamental of Programming (C)

Basic and Practice in Programming Lab7

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

C LANGUAGE A Short Course

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Computer Programming Unit 3

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department

coe628 Final Study Guide (2017)

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

ECE264 Summer 2013 Exam 1, June 20, 2013

Computer Systems C S Cynthia Lee

Chapter 11 Introduction to Programming in C

PRINCIPLES OF OPERATING SYSTEMS

Transcription:

int main() { return 0;

% gcc x.c %./a.out % gcc -o x x.c %./x

int main() { return 1; 0

% gcc -Wall -o x x.c %./x

int main() { printf("hi\n"); return 0; \n

#include <stdio.h> int main() { printf("hi\n"); return 0; #include import

#include <stdio.h> int main() { printf("ten and ten make %d\n", 10+10); return 0; printf %d int %f double %s %p %c

#include <stdio.h> int main() { printf("hex 10 and hex 10 make %d\n", 0x10 + 0x10); return 0; 0x

#include <stdio.h> int main() { printf("%p %p\n", main, printf); return 0;

#include <stdio.h> int main() { int v = 5; printf("at %p is %d\n", &v, v); return 0; &

&v 0x10756 v0x10756 5

&v v 5

#include <stdio.h> int main() { int v = 5; int* p = &v; v = 6; printf("at %p is %d\n", p, *p); return 0; * *

p v 5

#include <stdio.h> int main() { int v = 5; int* p = &v; *p = 7; printf("v at end: %d\n", v); return 0;

#include <stdio.h> int main() { int v = 5; int* p = &v; printf("at %p is %d\n", p, p[0]); return 0;

#include <stdio.h> int main() { int v = 5; int* p = &v; printf("at %p is %d\n", p+1, p[1]); return 0;

p v 5?

#include <stdio.h> int main() { int a[3] = { 1, 2, 3 ; int* p = a; printf("%d, %d, %d\n", a[0], p[1], *(p + 2)); return 0;

a[_] 1 2 3 p

#include <stdio.h> int main() { int a[3] = { 1, 2, 3 ; int* p = a; int* q = &a; printf("%p = %p, but not %p\n", p, q, &p); return 0; []

a[_] 1 2 3 p q

#include <stdio.h> int main() { char* s = "apple"; printf("%s: %c, %c, %c\n", s, s[0], s[1], *(s + 3)); return 0;

'a' 'p' 'p' 'l' 'e' 0 s

97 112 112 108 101 0 s

#include <stdio.h> int main() { char* s = "apple"; printf("%s: %d, %d, %d\n", s, s[0], s[1], *(s + 3)); return 0;

#include <stdio.h> int main() { char* s[3] = { "apple", "banana", "coconut" ; char** ss = s; printf("%s (%c...), %s, %s\n", ss[0], ss[0][0], ss[1], s[2]); return 0;

ss 'a' 'p' s 'b' 'a' 'c' 'o'

#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int a, b; a = atoi(argv[1]); b = atoi(argv[2]); printf("%d\n", a + b); return 0; atoi()

char short int long char*int*char**

#include <stdio.h> int main() { char cs[2] = {0, 1; int is[2] = {0, 1; printf("goes up by 1: %p, %p\n", cs, cs+1); printf("goes up by 4: %p, %p\n", is, is+1); return 0;

#include <stdio.h> int main() { char cs[2] = {0, 1; printf("char size is %ld\n", sizeof(char)); printf("char size is %ld\n", sizeof(cs[0])); printf("address size is %ld\n", sizeof(&cs)); return 0; sizeof

#include <stdio.h> #include <stdlib.h> int main() { int* a; a = malloc(100 * sizeof(int)); a[99] = 5; printf("array at %p ends in %d\n", a, a[99]); return 0;

#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char** argv) { char *a; int len1, len2; len1 = strlen(argv[1]); len2 = strlen(argv[2]); a = malloc(len1 + len2 + 1); memcpy(a, argv[1], len1); memcpy(a + len1, argv[2], len2 + 1); // include terminator printf("%s\n", a); return 0;

#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int i; int sum = 0; for (i = 1; i < argc; i++) { sum += atoi(argv[i]); printf("%d\n", sum); return 0;

#include <stdio.h> #include <stdlib.h> int twice(int n) { return n + n; int main(int argc, char** argv) { printf("%d\n", twice(atoi(argv[1]))); return 0;

byte bits 0000 0000 0000 0001 0000 0100 0010 0100 1000 0000 1111 1111 unsigned

byte bits 0000 0000 0000 0001 0000 0100 0010 0100 1000 0000 1111 1111 signed two's complement

0x171 0x172 1111 1111 0000 0000

0x171 0x172 1111 1111 0000 0000 char unsigned char short int unsigned int long

1111 0001 0100 0001 0100 0001 0100 0001

1111 0001 0100 0001 0100 0001 0100 0001

0 01 &&! 5 && 7 1 5 && 0 0!(5 && 0) 1

#include <stdio.h> int main(int argc, char* argv[]) { int a = 0x0; int b = 0x93; printf("%x %x\n",!a,!b); printf("%x %x %x %x\n", a&&b, a b,!a!b,!a&&!b); return 0;

& ^ ~ 5 101 & 7 5 5 111 101 101 6 7 110 5 101 111 ^ 6 3 110 011 ~(5 0..101 & 0) -1 0..000 1..111 &&

#include <stdio.h> int main(int argc, char* argv[]) { int a = 0x13; // 0001 0011 int b = 0x55; // 0101 0101 printf("%x %x %x\n", a 0, a&1, ~~a); printf("%x %x\n", a a&b, a&(a b)); printf("%x %x\n", ~(a&b), ~a ~b); return 0;

<< >> 5 101 << 1 10 1010 5 101 >> 1 2 10 5 >> 3 0 00101 00

#include <stdio.h> int main(void) { int a = 0x1; int b = 0x11; int c = 0x80000000; printf("%i %i %i\n", a<<1, a<<2, a<<3); printf("%i %i\n", b<<5, b*32); printf("%x %x %x %x\n", b, b>>1, b>>2, b>>3); printf("%x %x %x %x\n", c, c>>1, c>>2, c>>3); return 0;

#include <stdio.h> int main() { int x = 1000000000; // almost 2^30 int y = 2000000000; // almost 2^31 int z = x + y; printf("%d\n", z); // maybe -1294967296 return 0; Beware:

#include <stdio.h> int main() { unsigned int x = 1000000000; // almost 2^30 unsigned int y = 2000000000; // almost 2^31 unsigned int z = x + y; // less than 2^32 printf("%u\n", z); // definitely 3000000000 return 0; unsigned int

#include <stdio.h> int main() { unsigned int x = 1000000000; // almost 2^30 unsigned int y = 2000000000; // almost 2^31 unsigned int z = x + y; // less than 2^32 printf("%d\n", z); // definitely -1294967296 return 0; unsigned int unsigned int int

#include <stdio.h> int main() { unsigned int x = 1000000000; // almost 2^30 unsigned int y = 2000000000; // almost 2^31 int z = x + y; printf("%d\n", z); // definitely -1294967296 return 0;

int unsigned unsigned char unsigned char int unsigned long unsigned long int

#include <stdio.h> int main() { char c = -5; int i = c; unsigned u = i; unsigned u2 = c; printf("%d %u %u\n", i, u, u2); return 0;

float double floating-point numbers float double

Normalized: Denormalized: Infinity: Not-a-Number:

00100100010000000000000000000000 0 01001000 normalized 01001000 10000000000000000000000

00100100010000000000000000000000 0x2 0x4 0x4 0x0 0x0 0x0 0x0 0x0 #include <stdio.h> #include <string.h> int main() { int i = 0x24400000; float f; memcpy(&f, &i, sizeof(float)); printf("%g\n", f); return 0;

10000000010000000000000000000000 1 00000000 denormalized 00000000 10000000000000000000000

10000000010000000000000000000000 0x8 0x0 0x4 0x0 0x0 0x0 0x0 0x0 #include <stdio.h> #include <string.h> int main() { int i = 0x80400000; float f; memcpy(&f, &i, sizeof(float)); printf("%g\n", f); return 0;

01111111100000000000000000000000 0 11111111 special Infinity: Not-a-Number: 00000000000000000000000

01111111100000000011000000000000 0 11111111 special Infinity: Not-a-Number: 00000000011000000000000

1111 0001 0100 0001 0100 0001 0100 0001

1111 0001 0100 0001 0100 0001 0100 0001

1111 0001 0100 0001 0100 0001 0100 0001

0x171 0x172 1111 1111 0000 0000 Big endian 1111 1111 0000 0000 Little endian 0000 0000 1111 1111

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

char ASCII 'a' 97 'b' 98... 'A' 65 'B' 66 '0' 48 '1' 49

char 0xf01 97 'a' 0xf02 112 'p' 0xf03 112 'p' 0xf04 108 'l' 0xf05 101 'e' 0xf06 0 char*

#include <stdio.h> int main() { char* s = "apple"; printf("%s: %d, %d, %d\n", s, s[0], s[1], s[2]); return 0;

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

0xa051 0xa052 0xa053 0xa054 0100 0001 0100 0001 0100 0001 1111 0001

#include <stdio.h> int main() { char* s = "apple"; short* p = (short *)s; printf("%s: %d %d\n", s, *p, s[0] + (s[1] * 256)); return 0;

#include <stdio.h> int main() { float f = 2.5; int i = *(int *)&f; printf("%f %d\n", f, i); return 0;

memcpy #include <stdio.h> #include <string.h> int main() { float f = 2.5; int i; memcpy(&i, &f, sizeof(int)); printf("%f %d\n", f, i); return 0;

sizeof(int*) int intshortlong int long intptr_t #include <inttypes.h>

#include <stdio.h> int main(void) { int i; float f = 0.0; for (i = 0; i < 10; i++) { f = f + 0.1; printf("%f\n", f); return 0; f1.0

#include <stdio.h> #include <limits.h> int check_grow (int x) { return (x+1) > x; int main (void) { printf ("%d\n", (INT_MAX+1) > INT_MAX); printf ("%d\n", check_grow(int_max)); return 0; -O2

#include <stdio.h> #include <limits.h> int check_grow (unsigned x) { return (x+1) > x; int main (void) { printf ("%d\n", (UINT_MAX+1) > UINT_MAX); printf ("%d\n", check_grow(uint_max)); return 0;

#include <stdio.h> void set(int *i, float *f, int *j) { printf("f at %p, j at %p\n", f, j); *j = 1; *f = 0.0; /* what if `j` and `f` at same address? */ *i = *j; int main (void) { int i, j; set(&i, (float *)&j, &j); printf ("%d %d\n", i, j); return 0;