C Basics.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Structure of a C program
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Java Syntax Primitive data types Operators Control statements.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Computer Science 210 Computer Organization Introduction to C.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Programming
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
CS 261 – Data Structures Introduction to C Programming.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
C is a high level language (HLL)
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
The Machine Model Memory
Computer Science 210 Computer Organization
Introduction to C Programming
A bit of C programming Lecture 3 Uli Raich.
Chap. 2. Types, Operators, and Expressions
C Programming Tutorial – Part I
CS 61C: Great Ideas in Computer Architecture Introduction to C
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
C programming language
Pointers and References
Computer Science 210 Computer Organization
Starting JavaProgramming
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS 2308 Exam I Review.
Introduction to Abstract Data Types
An Introduction to Java – Part I, language basics
Basics of ‘C’.
Pointers, Dynamic Data, and Reference Types
A History Lesson Adapted from Chapter 1 in C++ for Java Programmers by Weiss and C for Java Programmers: a Primer by McDowell Development of language by.
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
C Programming Getting started Variables Basic C operators Conditionals
Focus of the Course Object-Oriented Software Development
C++ Pointers and Strings
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Java Basics Data Types in Java.
The C Language: Intro.
C Language B. DHIVYA 17PCA140 II MCA.
C++ Pointers and Strings
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

C Basics

Historical Background Created at Bell Labs – to program Unix Professional programmer's language “stays out of programmer's way” Assumption that programmers know what they want to do & how to use language constructs to accomplish it Terse syntax Small language – designed for efficiency Type system & error checking – compile time only Run time is very small (efficiency) Programmer must handle memory management

Development Environment Compiler – gcc Open source compiler OS – unix Need to be able to work at command line IDE Eclipse Codeblocks Any editor (vi, emacs, …..)

Integer Types Integer char – 1 byte short – small integer – at least 16 bits int – default integer – at least 16 bits – hardware implementation long – large integer – at least 32 bits Portability problems Can use typedefs to help

Integer Types char constants '\n' – newline '\t' – tab '\0' – null character '\O12' – character with octal value 12 int constants 0x10 – integer with hex value 10 O12 – integer with octal value 12

Integer Types Automatic “promotion” of integer types 'j' + 4 No overflow checking

Floating Point Types float – single precision floating point number double – double precision floating point number long double – possibly even bigger floating point number Constants – default is double 3.14f 3.14l Do not use equality (==) to compare 2 floating point numbers – round off error

C Syntax Comments – same as Java Variable declarations – same as Java Case sensitive Assignment operator – same as Java Truncation – opposite of promotion – allowed in C int n = 1000; char c = n; Value of c is whatever is in the bottom 8 bits of n double pi = 3.14; int n = pi; n has value 3 No boolean – use int

Mathematical Operators Same as Java Logical operators 0 is false Anything else is true Bitwise operators ~ bitwise negation & bitwise and | bitwise or ^ bitwise exclusive or >> right shift << left shift

Control Structures Same as Java

Complex Data Types Arrays & records (structs) struct fraction { int numerator; int denominator; }; fraction f; f.numerator = 2; f.denominator = 5; Arrays – similar to Java No index checking Constant pointer

Complex Data Types Array of structs struct fraction numbers [100];

Pointers * used to indicate a pointer int* n; char* c struct fraction* f; * to left dereferences pointer *n – integer being pointed to by n (pointee) (*f).numerator or f -> numerator Example struct fraction** f; (*f) -> numerator int* foo[20]

Pointers & Operator Returns address of (pointer to) &n – address of n (pointer to n) NULL Symbolic name for 0 – pointer has no pointee Pitfalls Pointer must be declared & allocated Pointee must be declared & allocated Pointer must point to pointee

C Strings Array of characters – null character terminated string.h library – support for strings defined in this way Developer must manage the string memory & the null character terminator Buffer overflow potential strcpy does not check for size – just copies the characters Better approach is to create the strings dynamically when size is known

C Strings char* Pointer to char – pointer to array of characters – string

Typedef Way of giving a name to a type typedef <type> <name> Examples: typedef NULL UGA typedef struct fraction frac typedef struct treenode* tree struct treenode { int data; tree left, right; }

Functions Syntax – same as Java All parameters – passed by value Pass by reference – semantics can be achieved by passing pointer of argument void swap (int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int n = 2; int m = 3; swap (&n, &m);

Functions const Qualifier indicating that code cannot change variable Ex: void foo (const struct fraction* f); foo cannot change fraction pointed by f

C Programs Main function int main() Return value 0 – program terminates normally Other values – error codes Can have arguments for command line arguments Prototype Name & arguments for function Must appear before implementation of function

C Programs Preprocessor – executes before compiler Directives #define – establishes symbolic names #include – copies text from another file #if If else logic used on symbols created by #define Used at compile time to determine which code to compile #once – prevents problems which can occur if a file is included more than once

C Programs Header files & source files Header files .h extension contain prototypes Source files .c or .cc extension contain implementations assert Macro implementation of assertions