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

Preview:

Citation preview

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

if, else if, elseif ( statement){

your code here}else if ( statement){

your code here}else if (statement){

your code here}…else{

your code here}

while

while(statement){

your code here}

for

for(initial condition ; statement ; final operation){

your code here}

function

int foo(int A, int B){

int C;your code herereturn C;

}

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);

}

Struct

typedef struct tag_name{

type element1; type element2;

} tag_name;

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);

}

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

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.

Recommended