Play with C

Information about Play with C

Published on April 8, 2009

Author: ankush85

Source: authorstream.com

Content

Play with C : Play with C For Programmers Play with C : Play with C Why C now? Try programming in a different language Everything you do in Java (or other O-O language) can also implemented with C See the real world - learn to deal with pointers. Direct memory operation Abundant Internet resources on C “Learning C from Java” “C language tutorial” A good practice – part I, project 3 A Simple c program – “hello.c” : A Simple c program – “hello.c” /*---- Hello World C Example ("hello.c") -------------------------------------*/ /* ANSI C Headers */ #include <stdio.h> #include <stdlib.h> /* Main Program starts here */ void main( ) { int i = 0; /* End of declarations ... */ for ( i = 0; i < 10; i++ ) { printf( "%d Hello World ! \n", i ); /* print to standard output */ } } Include headers if you need to use functions in these files Declare all your variables first Comment: /* .. */ NOT // Compile C program : Compile C program Obtain a C compiler Linux/Unix (suggested! use your CS account) Usually c compiler is already installed Windows 2000/XP Microsoft Visual Studio has c compiler Downloadable Free GNU c compilers e. g. http://csjava.occ.cccd.edu/~gilberts/mingw/ Compiling a single-source C program in Unix (use cc, gcc, or g++ ) e.g. cc –o hello hello.c - An executable file named “hello” will be generated. - “ cc hello.c “ is also okay. The output file will be named as “a.out”. cc –o <output file> <source_file> Run C program : Run C program Running the resulting program Just type the name of the executable file. e.g. hello If you local directory is not included in “PATH”, ./hello More details on C compilation and running http://users.actcom.co.il/~choo/lupg/tutorials/c-on-unix/c-on-unix.html Arrays in C : Arrays in C Array can be created with any type int a[5]; /* use a[0], a[1], .., a[4] */ char b[3] ; /* use b[0], b[1], b[2] */ float c[2] = { .1, .2}; /*initialization at declaration. c[0] = 0.1 , c[1]=0.2 */ double c[3][5]; /* two dimensional array (column major as Java) */ Array starts at 0. There is no boundary checking. Value of an array variable is the memory address of the first element. Character arrays can be used as strings char str[5]; strcpy(str, “hello”); /* str[0]=‘h’, str[1]=‘e’, …str[4]=‘o’*/ How to declare an array of strings ? 2D character array - char strs[20][5]; /* 20 strings with max-length=5 */ Example - Arrays in C : Example - Arrays in C #include <stdio.h> #include <stdlib.h> /* Print 26 letters in lower case and upper case */ void main() { char a[2] = {'a', 'A'}; /* One dimensional array */ int b[2][26]; /* Two dimensional array */ int i; for(i = 0; i < 26; i++) { b[0][i] = a[0] + i; /*Use the ASCII value of a[0] */ printf("%c ", b[0][i]); /*print the letter with ascii b[0][i] */ } printf("\n"); for(i = 0; i < 26; i++) { b[1][i] = a[1] + i; /*Use the ASCII value of a[1] */ printf("%c ", b[1][i]); /*print the letter with ascii b[1][i] */ } } Pointers in C : Pointers in C Declare pointers to any data type using dereference operator * int* int_ptr; char* ch_ptr; void* void_ptr; A pointer takes its value as the address in memory of another variable int a = 5 int* a_ptr; a_ptr = &a; /* ‘&’ : address of operator */ (*a_ptr) ++ ; /* ‘*’ : dereference operator */ a a_ptr Pointer example : Pointer example malloc, free – Allocate/Free Memory Blocks : malloc, free – Allocate/Free Memory Blocks Allocates memory blocks Returns a void pointer to the allocated space Return NULL if there is insufficient memory available To return a pointer to a type other than void, use a type cast on the return value. Example: int* i_ptr = (int*) malloc (10 * sizeof(int) ); /* Allocate a memory block with size of 10 int variables. */ Deallocates or frees a memory block. Example: free( i_ptr ); /* ignore if i_ptr is null */ void *malloc( size_t size ); void free( void *memblock ); Example of pointers, malloc, free : Example of pointers, malloc, free #include <stdio.h> #include <stdlib.h> void main() { int cost = 5, i; int* cost_ptr; cost_ptr = &cost; /*Let cost_ptr point to "cost“ */ printf("Pointer = %d. Dereference = %d\n", cost_ptr, *cost_ptr); cost_ptr = (int*) malloc( 10 * sizeof(int)); for (i = 0; i < 10; i++) { cost_ptr [i] = i*2; /* cost_ptr can be used as an array variable*/ printf("value %d: %d\t", i, cost_ptr[i]); /* You can also explicitly use the memory location of ith element (can be dangerous).*/ *(cost_ptr + sizeof(int) * i) = 2*i ; printf("value %d: %d\n", i, *(cost_ptr + sizeof(int) * i)); } /* Free the allocated mormory if you don't use it in the future */ free( cost_ptr); } Implement singly linked list in C : Implement singly linked list in C head Define structure with necessary data fields. struct StrListElement { int value; /* value of current list element */ struct StrListElement* next; /* pointing to next element in the list */ }; ... StrListElement temp; StrListElement* temp_ptr = &temp ; User can explicitly access structure data fields use ‘.’ or ‘->’ “->” is used after a pointer to structure variable: temp_ptr->value = 5; temp_ptr->next = null; “.” is used after a structure variable: temp.value = 5; temp.next = null; Add elements to the singly linked list : Add elements to the singly linked list struct StrListElement* head = NULL; //Head of the singly linked list struct StrListElement* str_elem_ptr; int i; for( i = 0; i < 5; i++) { /* Create an element which is going to be added into the list */ str_elem_ptr = (struct StrListElement* ) malloc ( sizeof(struct StrListElement)); str_elem_ptr -> value = i; /* /* Add new element to the head of the list */ str_elem_ptr -> next = head; head = str_elem_ptr; } head same function as addFirst method Other operations on singly linked list : Other operations on singly linked list Traverse elements in the list str_elem_ptr = head; while(str_elem_ptr != NULL) { printf("%d\n", str_elem_ptr->value); str_elem_ptr = str_elem_ptr -> next; } Delete elements from the head of list. Free the memory of deleted elements while(head != NULL) { str_elem_ptr = head; head = str_elem_ptr -> next; free(str_elem_ptr); } C functions : C functions A function has the following layout: return-type function-name ( argument-list-if-necessary ) e.g. int foo( int a, int b ) { ..} void f2(int* pa) {..} Arguments are always pass by values in C i.e. “Local copies'' of the values of the arguments are passed to the functions What if we want to pass values out of a function ? Solution 1: return value (but at most 1 value can returned) Solution 2: pointers Pass arguments in/out of C functions : Pass arguments in/out of C functions What are the values of v1 and v2 after swap1 and swap2 ? char v1=‘a’, v2 = ‘b’, char* p1 = &v1; char* p2 = &v2; swap1 ( v1, v2 ); swap2 ( p1, p2 ); void swap1 (char v1, char v2) { char temp; temp = v1; v1 = v2; v2 = temp; } void swap2 (char* v1, char* v2) { char temp; temp = *v1; *v1 = *v2; *v2 = temp; } /* v1 = ‘a’, v2 = ‘b’. */ /* v1 = ‘b’, v2 = ‘a’. */

Related presentations


Other presentations created by ankush85

Nanotechnology
03. 04. 2009
0 views

Nanotechnology

Human Resource Management System
06. 03. 2009
0 views

Human Resource Management System

Nanotechnology
27. 03. 2009
0 views

Nanotechnology

INTRODUCTION TO NANOTECHNOLOGY
20. 11. 2009
0 views

INTRODUCTION TO NANOTECHNOLOGY

Nanotechnology in Sports
22. 11. 2009
0 views

Nanotechnology in Sports

Computer Architecture
14. 07. 2009
0 views

Computer Architecture

HR
18. 03. 2009
0 views

HR

Hospital Management System
08. 04. 2009
0 views

Hospital Management System

Welcome to Visual Basic
06. 03. 2009
0 views

Welcome to Visual Basic

HTML
20. 04. 2009
0 views

HTML

Computer Languages
03. 06. 2013
0 views

Computer Languages

Taking Care of Computer
03. 06. 2013
0 views

Taking Care of Computer

Understanding Camera
16. 10. 2012
0 views

Understanding Camera

Photography Technical Terms
25. 09. 2012
0 views

Photography Technical Terms

Basics of Photography
25. 09. 2012
0 views

Basics of Photography

AIR MUSCLES
03. 01. 2012
0 views

AIR MUSCLES

Molecular Nanotechnology
24. 11. 2009
0 views

Molecular Nanotechnology

Nanotech Innovation
22. 11. 2009
0 views

Nanotech Innovation

FINANCIAL  MARKET
16. 05. 2009
0 views

FINANCIAL MARKET

Operating System
19. 04. 2009
0 views

Operating System

Management Control
10. 07. 2009
0 views

Management Control

Accounting Principles
01. 07. 2009
0 views

Accounting Principles

Balance Sheet
21. 07. 2009
0 views

Balance Sheet

Balance Sheet Auditing
01. 07. 2009
0 views

Balance Sheet Auditing

BRANCH AND BOUND
05. 03. 2009
0 views

BRANCH AND BOUND

THE  THREE  BRANCHES  OF GOVERNMENT
13. 06. 2009
0 views

THE THREE BRANCHES OF GOVERNMENT

Structure of Atom
11. 05. 2009
0 views

Structure of Atom

Compression Techniques
05. 03. 2009
0 views

Compression Techniques

DYNAMIC PROGRAMMING
06. 03. 2009
0 views

DYNAMIC PROGRAMMING

Quantum Mechanics
25. 08. 2009
0 views

Quantum Mechanics

Marketing Plan
19. 06. 2009
0 views

Marketing Plan

Book Keeping
24. 06. 2009
0 views

Book Keeping

DFDS
02. 10. 2008
0 views

DFDS

Semiconductors
25. 04. 2009
0 views

Semiconductors

Organic  Chemistry
28. 04. 2009
0 views

Organic Chemistry

Atoms Molecules and Ions
17. 06. 2009
0 views

Atoms Molecules and Ions

Covalent Bond
25. 04. 2009
0 views

Covalent Bond

Cost Accounting Standards
25. 06. 2009
0 views

Cost Accounting Standards

Crisis Management
19. 06. 2009
0 views

Crisis Management

Marketing
18. 03. 2009
0 views

Marketing

Business Strategy
27. 04. 2009
0 views

Business Strategy

Time Management
23. 03. 2009
0 views

Time Management

Networking Protocols
23. 05. 2009
0 views

Networking Protocols

Network Layer
23. 05. 2009
0 views

Network Layer

Final Accounts
24. 06. 2009
0 views

Final Accounts

ARTIFICIAL  INTELLIGENCE
03. 04. 2009
0 views

ARTIFICIAL INTELLIGENCE

Software Development Cycle
23. 03. 2009
0 views

Software Development Cycle

THE GREEDY METHOD
06. 03. 2009
0 views

THE GREEDY METHOD

JOB SEQUENCING
06. 03. 2009
0 views

JOB SEQUENCING

Electricity and Magnetism
29. 04. 2009
0 views

Electricity and Magnetism

Optical Fiber
24. 05. 2009
0 views

Optical Fiber

Quality Assurance
06. 03. 2009
0 views

Quality Assurance

Object-oriented Design
11. 04. 2009
0 views

Object-oriented Design

A.R. Rahman
08. 03. 2009
0 views

A.R. Rahman

Hollywood Female Celebrities
13. 03. 2009
0 views

Hollywood Female Celebrities

Flow nets
04. 09. 2009
0 views

Flow nets

Energy and Nanotechnology
22. 11. 2009
0 views

Energy and Nanotechnology

LOGIC  DESIGN
06. 03. 2009
0 views

LOGIC DESIGN

VB-IDE
06. 03. 2009
0 views

VB-IDE

DLF IPL FINAL
25. 05. 2009
0 views

DLF IPL FINAL

MINIMUM SPANNING TREES
06. 03. 2009
0 views

MINIMUM SPANNING TREES

CPU Scheduling
05. 03. 2009
0 views

CPU Scheduling

Virtual Memory
05. 03. 2009
0 views

Virtual Memory

POK
09. 05. 2009
0 views

POK

2D Transformations
05. 03. 2009
0 views

2D Transformations

Greedy Algorithms
05. 03. 2009
0 views

Greedy Algorithms

BACKTRACKING
05. 03. 2009
0 views

BACKTRACKING

DIVIDE And CONQUER
06. 03. 2009
0 views

DIVIDE And CONQUER

ELEMENTARY DATA STRUCTURES
06. 03. 2009
0 views

ELEMENTARY DATA STRUCTURES

JPEG Compression
06. 03. 2009
0 views

JPEG Compression

Mpeg-compression
06. 03. 2009
0 views

Mpeg-compression

NP - HARD
06. 03. 2009
0 views

NP - HARD

TRAVELLING SALESPERSON PROBLEM
06. 03. 2009
0 views

TRAVELLING SALESPERSON PROBLEM

VBA Introduction
06. 03. 2009
0 views

VBA Introduction

Introduction to Java
09. 03. 2009
0 views

Introduction to Java

Java  Basics
09. 03. 2009
0 views

Java Basics

Heuristic Search
13. 03. 2009
0 views

Heuristic Search

HSM
19. 03. 2009
0 views

HSM

Tata's  Nano  Car
25. 03. 2009
0 views

Tata's Nano Car

Air Cranes
08. 04. 2009
0 views

Air Cranes

Newborn Care
10. 04. 2009
0 views

Newborn Care

Photosynthesis Process
10. 04. 2009
0 views

Photosynthesis Process

ActionScript
13. 04. 2009
0 views

ActionScript

Xml
15. 04. 2009
0 views

Xml

Snakes mis use
17. 04. 2009
0 views

Snakes mis use

Php Web Development
21. 04. 2009
0 views

Php Web Development

Cricket Intro
21. 04. 2009
0 views

Cricket Intro

Cricket Umpiring and Rules
21. 04. 2009
0 views

Cricket Umpiring and Rules

Arm and Forearm
23. 04. 2009
0 views

Arm and Forearm

Elements Ions Isotopes
25. 04. 2009
0 views

Elements Ions Isotopes

Chemical Bond
25. 04. 2009
0 views

Chemical Bond

Discovering Newtons Laws
29. 04. 2009
0 views

Discovering Newtons Laws

FreeFall
29. 04. 2009
0 views

FreeFall

Digital photography
26. 04. 2009
0 views

Digital photography

Health Effects of Alcohol
26. 04. 2009
0 views

Health Effects of Alcohol

Poetry Terminology
27. 04. 2009
0 views

Poetry Terminology

Indian Force
05. 05. 2009
0 views

Indian Force

Machine Intelligence
14. 05. 2009
0 views

Machine Intelligence

Data Link Layer
16. 05. 2009
0 views

Data Link Layer

Database Development Cycle
15. 05. 2009
0 views

Database Development Cycle

Queue
15. 05. 2009
0 views

Queue

Presentaion Skills
23. 04. 2009
0 views

Presentaion Skills

Network Layers
23. 05. 2009
0 views

Network Layers

Narmada Dam, India
23. 05. 2009
0 views

Narmada Dam, India

User Datagram Protocol
24. 05. 2009
0 views

User Datagram Protocol

Linear Momentum
28. 05. 2009
0 views

Linear Momentum

Stack and Queue
04. 06. 2009
0 views

Stack and Queue

Information Management
19. 06. 2009
0 views

Information Management

Role of Senior Management
19. 06. 2009
0 views

Role of Senior Management

Wake Up India
07. 06. 2009
0 views

Wake Up India

Adobe Flex 3.0
13. 06. 2009
0 views

Adobe Flex 3.0

Adobe Flex Presentation
13. 06. 2009
0 views

Adobe Flex Presentation

Introduction to Adobe Flex
13. 06. 2009
0 views

Introduction to Adobe Flex

Adobe Flash Media Server
13. 06. 2009
0 views

Adobe Flash Media Server

Dreamweaver
13. 06. 2009
0 views

Dreamweaver

Adobe Flash
13. 06. 2009
0 views

Adobe Flash

Flash CS4 Professional
13. 06. 2009
0 views

Flash CS4 Professional

Adobe Flash Lite
13. 06. 2009
0 views

Adobe Flash Lite

Digital Camera
13. 06. 2009
0 views

Digital Camera

Mozilla_Firefox
15. 06. 2009
0 views

Mozilla_Firefox

Managerial Accounting
27. 06. 2009
0 views

Managerial Accounting

Accounting  Information  System
01. 07. 2009
0 views

Accounting Information System

RETAILING AND MARKETING
06. 07. 2009
0 views

RETAILING AND MARKETING

ACCOUNTING IN BUSINESS
05. 07. 2009
0 views

ACCOUNTING IN BUSINESS

STRATEGIC RETAIL MANAGEMENT
05. 07. 2009
0 views

STRATEGIC RETAIL MANAGEMENT

Reiki
01. 08. 2009
0 views

Reiki

Using Buttons in PowerPoint
26. 04. 2009
0 views

Using Buttons in PowerPoint

Nanotechnology  for  Students
21. 08. 2009
0 views

Nanotechnology for Students

NANOSCIENCE
25. 08. 2009
0 views

NANOSCIENCE

Fundamentals of Nanoscience
27. 08. 2009
0 views

Fundamentals of Nanoscience

Nanoscience in Nature
27. 08. 2009
0 views

Nanoscience in Nature

Applied Mechanics
04. 09. 2009
0 views

Applied Mechanics

Accounts Payable Training
27. 06. 2009
0 views

Accounts Payable Training