12
Summary

Summary. Data Types int A; I am declaring a variable A, which is an integer

Embed Size (px)

Citation preview

Page 1: Summary. Data Types int A; I am declaring a variable A, which is an integer

Summary

Page 2: Summary. Data Types int A; I am declaring a variable A, which is an integer

Data Types

int A;

I am declaring a variable A, which is an integer.

Page 3: Summary. Data Types int A; I am declaring a variable A, which is an integer

Arrays

int A[50];

I am declaring an array A. This array has 50 elements, and each element is an integer.

A[0] is the first element of the arrayA[49] is the last element of the array

Page 4: Summary. Data Types int A; I am declaring a variable A, which is an integer

if, else if, elseif ( statement){

your code here}else if ( statement){

your code here}else if (statement){

your code here}…else{

your code here}

Page 5: Summary. Data Types int A; I am declaring a variable A, which is an integer

while

while(statement){

your code here}

Page 6: Summary. Data Types int A; I am declaring a variable A, which is an integer

for

for(initial condition ; statement ; final operation){

your code here}

Page 7: Summary. Data Types int A; I am declaring a variable A, which is an integer

function

int foo(int A, int B){

int C;your code herereturn C;

}

Page 8: Summary. Data Types int A; I am declaring a variable A, which is an integer

How to call your function

void main(){

int X;int Y;int Z;Z = foo (X, Y);printf(foo(%d, %d) = %d\n, X, Y, Z);

}

Page 9: Summary. Data Types int A; I am declaring a variable A, which is an integer

Struct

typedef struct tag_name{

type element1; type element2;

} tag_name;

Page 10: Summary. Data Types int A; I am declaring a variable A, which is an integer

Struct Example#include <stdio.h>

typedef struct Student{

int midterm;//%40int hw;//%20int final;//%40int average;

} Student;

void main(){

Student Veli;Veli.hw = 80;Veli.midterm = 90;Veli.final = 100;Veli.average = (Veli.hw*20 + Veli.midterm*40 + Veli.final*40)/100;printf("Veli has an average grade of %d \n", Veli.average);

}

Page 11: Summary. Data Types int A; I am declaring a variable A, which is an integer

Base Arithmetic

• Binary numbers (base 2): Use only digits 0 and 1

• Decimal numbers (base 10): Use digits 0,1,2,3,4,5,6,7,8,9

• Hexadecimal numbers (bas 16): Use digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F– A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Page 12: Summary. Data Types int A; I am declaring a variable A, which is an integer

One’s and Two’s complement

• 1’s complemenf of an N-bit number: Invert all the bits in binary representation.

• 2’s complement of an N-bit number: subtract the number from 2N.