PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.

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

ARDUINO CLUB Session 1: C & An Introduction to Linux.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Software Development Method & C Language Elements H&K Chapter 1-2
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Guide To UNIX Using Linux Third Edition
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C Tokens Identifiers Keywords Constants Operators Special symbols.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Fundamental Programming: Fundamental Programming Introduction to C++
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Introduction to Programming
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
CSCE 206 Structured Programming in C
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
A bit of C programming Lecture 3 Uli Raich.
Computer Programming Chapter 1: Introduction
Chapter 2, Part I Introduction to C Programming
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Chapter 2 Overview of C.
Lecture2.
Visit for more Learning Resources
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Programming Fundamentals Lecture #3 Overview of Computer Programming
DATA TYPES There are four basic data types associated with variables:
EECE.2160 ECE Application Programming
Course Outcomes of Programming In C (PIC) (17212, C203):
Presentation transcript:

PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need to read the C programming notes (on the PHYS2020 website) as it is not possible to cover everything in lectures.

All C programs must have… main() { } This program doesn’t actually do anything, but it does compile, and so is a valid C program.

Most C programs have at least.. #include /* the include statement includes various header files (libraries) you may need. The libraries are actually bits of code to perform functions such as printing to the keyboard (stdio.h) or using maths functions (math.h) */ int main() { printf(Here is some output.\n”); return(0); }

#include Some Useful Header files: #include #include 1 #include Note, #include statements do NOT end in a semicolon. Note: 1.Including math.h is not enough to get you access to the maths libraries (such as sin, cos). You also need to link in the maths libraries when you compile, with the –lm (link maths) option of gcc. So, if you want to compile a file called myfile.c you would use the command gcc –lm myfile.c

The C programming Language consists of Keywords: 32 words that are reserved as commands etc. by the C language. See page 11 of the C programming notes. (e.g. if, int, char) Functions: Such as main(). Small routines that do specific tasks. There are hundreds available. See gcc.gnu.org to find out all the functions available. Operators: Such as =, +, -. See pp. 8/9 of the C programming notes.

Displaying Output to the Screen printf("Print verbatim text by enclosing it in quotes"); printf("Get a newline like this\n"); printf("To get a quotation mark to print out on screen you need to use the \" escape\" character before the quotation mark.");

Variables in C are of four basic data types:Variables in C are of four basic data types: int: integerint: integer char: single ascii (text) characterchar: single ascii (text) character float: real numberfloat: real number double: double precision whole numberdouble: double precision whole number (there are more than these, but this will get you started)(there are more than these, but this will get you started) You must declare variable that you want to use in your program. e.g.You must declare variable that you want to use in your program. e.g. int my_integerint my_integer char my_charchar my_char Variables

String Variables  There is actually no separate string variable, instead a string is stored as an array of char variables.  To declare a string variable, you need to also declare the number of characters that are to be stored (reserved in memory)  Do this with (e.g.):  char my_string[20] which sets up a variable which can accept up to 20 characters.

There are five basic types of function:There are five basic types of function: void: doesn't return anythingvoid: doesn't return anything int: returns integerint: returns integer char: returns textchar: returns text float: returns real numberfloat: returns real number double: returns double precision whole numberdouble: returns double precision whole number soso int main() {Return(0) } // returns an integer value to the operating system if it runs successfully. } // returns an integer value to the operating system if it runs successfully. Functions

To use printf() to print variables we need to 1) use a placeholder for the variable in the string to be printed out, and 2) Give the name of variable at the end of the printf statement, after a comma. So, to print an integer use printf("print this integer %d \n", val_of_int); where val_of_int is an integer variable. (You could also just use an integer - an "immediate" value, such as 12.) For integers use %d For real numbers use %f For char variables use %c For text strings use %s scanf() reads from the keyboard in a similar way: scanf("%s", &name); reads a string variable typed at the keyboard into the variable name. It is important to use the ampersand in front of the variable. It tells the compiler that the input from the keyboard is to be stored at the address associated with name (&name). Printing out variables

The ampersand (&) and variables  When you declare a variable in C you use, for example:  int my_variable  This statement sets aside a place in memory for this variable to be stored. The variable will have both a value and an address associated with it.  The operator & returns the address of whatever is to the right of it.  So scanf (%s, &my_variable); takes the word you read in at the keyboard and stores it as the value at address &my_variable