C Intro.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 CSSE 332 Course Introduction. Roll Call and Introductions Name (nickname) Name (nickname) Hometown Hometown Local Residence Local Residence Major Major.
Structure of a C program
Introduction to Computers and Programming Lecture 7:
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
CPSC 441 Tutorial TA: Fang Wang Introduction to C.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Data Structures Systems Programming Concepts.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Introduction to C Programming I Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Chapter 1.2 Introduction to C++ Programming
Introduction to the C Language
Decisions Chapter 4.
7. BASIC TYPES.
C Programming Tutorial – Part I
Day 01 Introduction to Linux and C
C programming language
C Basics.
Introduction to C Programming Language
Introduction to the C Language
Introduction to the C Language
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to CS Your First C Programs
פרטים נוספים בסילבוס של הקורס
פרטים נוספים בסילבוס של הקורס
MSIS 655 Advanced Business Applications Programming
Govt. Polytechnic,Dhangar
Variables in C Declaring , Naming, and Using Variables.
Programming Language C Language.
Programming Languages and Paradigms
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

C Intro

Why learn C (after Java)? Both high-level and low-level language Better control of low-level mechanisms Much better performance Java hides too many details needed for writing OS code Memory management responsibility Explicit initialization and error detection But, more room for mistakes

The “look” of C #include <stdio.h> struct list{int data; struct list *next}; struct list *start, *end; void add(struct list *head, struct list *list, int data}; int delete(struct list *head, struct list *tail); void main(void){ start=end=NULL; add(start, end, 2); add(start, end, 3); printf(“First element: %d”, delete(start, end)); } void add(struct list *head, struct list *tail, int data}{ if(tail==NULL){ head=tail=malloc(sizeof(struct list)); head->data=data; head->next=NULL; else{ tail->next= malloc(sizeof(struct list)); tail=tail->next; tail->data=data; tail->next=NULL; void delete (struct list *head, struct list *tail){ struct list *temp; if(head==tail){ free(head); head=tail=NULL; temp=head->next; free(head); head=temp;

Goals To introduce some basic C concepts to you To warn you about common mistakes made So that you get your homework done quickly You will be able to understand and write complicated code

Simple Example #include <stdio.h> void main(void) { printf(“Hello World. \n \t and you ! \n ”); /* print out a message */ return; } $Hello World. and you ! $

Summarizing the Example #include <stdio.h> = include header file stdio.h No semicolon at end Small letters only – C is case-sensitive void main(void){ … } is the only code executed printf(“ /* message you want printed */ ”); \n = newline \t = tab Dessert: \ in front of other special characters within printf. printf(“Have you heard of \”The Rock\” ? \n”);

Simple Data Types data-type # bytes(typical) values short-hand int               8       -2,147,483,648 to 2,147,483,647 %d char            1       -128 to 127 %c float            16       3.4E+/-38 (7 digits) %f double        32       1.7E+/-308 (15 digits long) %lf long            8       -2,147,483,648 to 2,147,483,647 %l short           2       -32,768 to 32,767 Lookup: signed / unsigned - int, char, long, short long double ex: int num=28458; printf(“WVU has about %d students.\n”, num);

Example ! #include <stdio.h> void main(void) { int nstudents = 0; /* Initialization, required */ printf(“How many students does WVU have ?:”); scanf (“%d”, &nstudents); /* Read input */ printf(“WVU has %d students.\n”, nstudents); return ; } $How many students does WVU have ?: 20000 (enter) WVU has 20000 students. $

Type conversion #include <stdio.h> void main(void) { int i,j = 12; /* i not initialized, only j */ float f1,f2 = 1.2; i = (int) f2; /* explicit: i <- 1, 0.2 lost */ f1 = i; /* implicit: f1 <- 1.0 */ f1 = f2 + (int) j; /* explicit: f1 <- 1.2 + 12.0 */ f1 = f2 + j; /* implicit: f1 <- 1.2 + 12.0 */ } Explicit conversion rules for arithmetic operation x=y+z; convert y or z as double <- float <- int <- char, short then type cast it to x ’s type Moral: stick with explicit conversions - no confusion !

Like Java, like C Operators same as Java: Arithmetic int i = i+1; i++; i--; i *= 2; +, -, *, /, %, Relational and Logical <, >, <=, >=, ==, != &&, ||, &, |, ! Syntax same as in Java: if ( ) { } else { } while ( ) { } do { } while ( ); for(i=1; i <= 100; i++) { } switch ( ) {case 1: … } continue; break;

Example #include <stdio.h> #define DANGERLEVEL 5 /* C Preprocessor - - substitution on appearance */ /* like Java ‘final’ */ void main(void) { float level=1; /* if-then-else as in Java */ if (level <= DANGERLEVEL){ /*replaced by 5*/ printf(“Low on gas!\n”); } else printf(“Good driver !\n”); return;

One-Dimensional Arrays #include <stdio.h> void main(void) { int number[12]; /* 12 cells, one cell per student */ int index, sum = 0; /* Always initialize array before use */ for (index = 0; index < 12; index++) { number[index] = index; } /* now, number[index]=index; will cause error:why ?*/ for (index = 0; index < 12; index = index + 1) { sum += number[index]; /* sum array elements */ return;

More arrays Strings Multi-dimensional arrays char name[6]; name = {‘C’,’S’,’4’,’1’,’4’,’\0’}; /* ’\0’= end of string */ printf(“%s”, name); /* print until ‘\0’ */ Functions to operate on strings strcpy, strncpy, strcmp, strncmp, strcat, strncat, strstr,strchr #include <strings.h> at program start Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4] */ printf(“%d”, points[1][3]);

Like Java, somewhat like C Type conversions but you can typecast from any type to any type c = (char) some_int; So be careful ! Arrays Always initialize before use int number[12]; printf(“%d”, number[20]); produces undefined output, may terminate, may not even be detected. Strings are terminated by ’\0’ character char name[6] = {‘C’,’S’,’4’,’1’,’4’,’\0’}; /* ’\0’= end of string */ printf(“%s”, name); /* print until ‘\0’ */

Memory layout and addresses int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘c’, d = ‘d’; 5 10 12.5 9. 8 c d 4300 4304 4308 4312 4316 4317