Introduction to C CSCE 343.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
C Language.
Programming Languages and Paradigms The C Programming Language.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Characters and Strings File Processing Exercise C Programming:Part 3.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Files A collection of related data treated as a unit. Two types Text
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
C++ Lesson 1.
Last week: We talked about: History of C Compiler for C programming
Computer Organization and Design Pointers, Arrays and Strings in C
Lesson #8 Structures Linked Lists Command Line Arguments.
The Machine Model Memory
Computer Science 210 Computer Organization
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
Programming Languages and Paradigms
Review of C… The basics of C scanf/printf if/elseif statements
An Introduction to C Programming
C Short Overview Lembit Jürimägi.
C programming language
Programming Paradigms
C Basics.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Programming in C Input / Output.
Computer Science 210 Computer Organization
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
File I/O Lesson Outline
An Introduction to Java – Part I, language basics
Pointers and Pointer-Based Strings
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Govt. Polytechnic,Dhangar
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Sridhar Narayan Java Basics Sridhar Narayan
File Input and Output.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Programming in C Input / Output.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
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.
Let’s start from the beginning
Introduction to C EECS May 2019.
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
C Programming - Lecture 3
Introduction to java Part I By Shenglan Zhang.
Files Chapter 8.
Introduction to C CS 3410.
Presentation transcript:

Introduction to C CSCE 343

Hello World

Comments Multi-line comments only in standard /* … */ Single line comments allowed in certain compilers: // …

Declarations In C standard, all declarations must appear above regular code. char red; int j; for (int i =0; i < 10 : i++){ } Not required in newer compilers.

Compiling using Cygwin gcc The gcc compiler (GNU Compiler Collection) is on the lab machines through cygwin. gcc –o hello hello.c ./hello On shemp, same process.

Cygwin UNIX environment on Windows Provides many of the UNIX/Linux utilities Differences from regular DOS prompt: Uses forward slash for directory separator Uses ls not dir Must be run from Cygwin icon Available for free download. www.cygwin.com

main Similar to public static void main Return value indicates success/failure to shell. 0: success Non-zero: failure

Arrays No new operator in C: Brackets do not follow type int a[10]; // array of ints char *b[20]; // array of pointers // to char Brackets do not follow type Except in prototypes No array index bounds checking! No length variable sizeof(a) / sizeof(a[0])

Pointers Dereferencing operator: * Address operator: & Except in declaration Address operator: & Declaring pointers: int * xPtr; // Pointer to int char * cPtr; // Pointer to char int y,x =5; xPtr = &x; y = *xPtr + x; Act very much like references in Java, but not quite the same. Can point to primitive types Can do arithmetic on pointers

Arrays and Pointers Array name (without brackets) is an alias to a pointer to the first element in the array. Arrays can be traversed with pointers. Brackets can be used with pointers. int a[10]; int *aPtr; aPtr = a; aPtr[6] // same as a[6] *(aPtr + 6) // same as aPtr[6]

Strings Arrays of char Terminated with the null character: ‘\0’ Array can be longer than the string Must be longer by at least one. Can’t concatenate with + Use strncat strcat(st1,”def”); // st1+”def”

Types Usual suspects: Unusual: No boolean data type Examples int, char, double, float, short, long ... Unusual: long long, unsigned int, … No boolean data type 0 false Non-zero true Any numeric or pointer type works! Examples if (0) if (y=0) if (y==0)

Input stdin: alias for the console standard input Two main choices for reading from stdin: scanf: input formatted data directly into variables reading strings the \n is left in the input stream fgets: read a full string, including \n, parse it yourself, \n is part of the resulting string String to int: atoi String to float (double): atof

Struct Encapsulation construct Encapsulates data, but not methods Everything is public (no access restriction) No inheritance struct employee{ int id; char name[NAME_LEN+1]; double salary; } emp1,emp2; struct employee emp3;

typedef Define new types Commonly used to define a type name for struct typedef struct{ int id; char name[NAME_LEN+1]; double salary; } Employee; Employee emp1,emp2;

File I/O <stdio.h> Basic file access tool: file pointers FILE *filePtr; Open/close: fopen, fclose filePtr = fopen(“fileName”, “r/w/a/r+w/…”) fclose(filePtr) Reading: fgets (stg, 100, filePtr) //line at a time: check for NULL getc(filePtr) //char at a time: check for EOF Writing fprintf (filePtr, stg) fprintf(filePtr, “format string”, variable-list)