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
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
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.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
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.
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
C programming Language and Data Structure For DIT Students.
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.
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.
C Programming Lecture 4 : Variables , Data Types
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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!
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
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.
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.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
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.
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.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
INC 161 , CPE 100 Computer Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
Administrative things
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
INPUT & OUTPUT scanf & printf.
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
Program Breakdown, Variables, Types, Control Flow, and Input/Output
EECE.2160 ECE Application Programming
Introduction to C Programming
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.");

#include int main() { char answer; //variable printf("\nWould you like the computer to tell your fortune?\n\n\n"); printf("Plese enter \"y\" in lowercase if the answer is yes.\n"); printf(" or enter \"n\" if the answer is no.\n"); answer = getchar(); if(answer == 'y') { printf("Tomorrow you will break you leg by tripping over a black cat.\n"); } else if(answer == 'Y') { printf("Tomorrow you will break you leg by tripping over a black cat.\n"); } else if(answer == 'n') { printf("Don't you believe that a computer knows the future?\n"); } else if(answer == 'N') { printf("Don't you believe that a computer knows the future?\n"); } else { printf("Look moron, I asked for \"y\" or \"n\". If you want your fortune told you will need to run the program again!!!\n\n"); } return(0); }

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

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 the actual value to go into the variable 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 up a place in memory with an address and a value, called my_variable.  The & is a unary operator which returns the address of the thing to its right.  So scanf(%c, &my_variable); takes the word you read in at the keyboard and stores it at the address of my_variable.