Introduction to C An overview of the programming language C, syntax, data types and input/output

Similar documents
Introduction to C Recursion, sorting algorithms, files

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS16 Exam #1 7/17/ Minutes 100 Points total

C Language, Token, Keywords, Constant, variable

The C language. Introductory course #1

Lecture 03 Bits, Bytes and Data Types

SWEN-250 Personal SE. Introduction to C

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Lecture 3. More About C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Course Information and Introduction

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Topic 6: A Quick Intro To C

Computers Programming Course 5. Iulian Năstac

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

EL2310 Scientific Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Computer Components. Software{ User Programs. Operating System. Hardware

C introduction: part 1

Lesson 7. Reading and Writing a.k.a. Input and Output

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Programming refresher and intro to C programming

CSCI 171 Chapter Outlines

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

C: How to Program. Week /Mar/05

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Functions. Arash Rafiey. September 26, 2017

A Fast Review of C Essentials Part I

COMP s1 Lecture 1

EC 413 Computer Organization

C programming basics T3-1 -

Introduction to C. Systems Programming Concepts

6.096 Introduction to C++ January (IAP) 2009

Dynamic Memory Allocation and Command-line Arguments

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 2 - Introduction to C Programming

Should you know scanf and printf?

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

High Performance Computing

2. Numbers In, Numbers Out

CS 61C: Great Ideas in Computer Architecture Introduction to C

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

211: Computer Architecture Summer 2016

CpSc 1111 Lab 4 Formatting and Flow Control

PROGRAMMING FUNDAMENTALS

The Compilation Process

2. Numbers In, Numbers Out

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Final CSE 131B Spring 2004

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

Decision Making -Branching. Class Incharge: S. Sasirekha

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSE 124 Discussion (10/3) C/C++ Basics

Software and Programming 1

Programming in C Quick Start! Biostatistics 615 Lecture 4

EL2310 Scientific Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Intermediate Programming, Spring 2017*

today cs3157-fall2002-sklar-lect05 1

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Kurt Schmidt. October 30, 2018

CMSC 246 Systems Programming

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Procedures, Parameters, Values and Variables. Steven R. Bagley

Exercise: Inventing Language

Introduction to Programming Using Java (98-388)

Introduction to C Language

Chapter 1 & 2 Introduction to C Language

3/13/2012. ESc101: Introduction to Computers and Programming Languages

Programming in C. What is C?... What is C?

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Creating, Compiling and Executing

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

C and Programming Basics

Motivation was to facilitate development of systems software, especially OS development.

Unit 4. Input/Output Functions

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

PRINCIPLES OF OPERATING SYSTEMS

Arrays and Strings. Arash Rafiey. September 12, 2017

C, C++, Fortran: Basics

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Transcription:

Introduction to C An overview of the programming language C, syntax, data types and input/output

Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1

PROGRAMMING LANGUAGE C is a general-purpose and imperative programming language. Developed in the beginning of the 1970s by Dennis Ritchie. It is old. It is simple. It is fast. It is not Fortran. It is not as userfriendly as newer programming languages. It is well suited to provide an appropriate understanding of scientific programming. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1

HELLO WORLD! The simplest program in any language is the output of a Hello World! string onto the screen. / h e l l o w o r l d program / #include<s t d i o. h> i n t main ( ) { p r i n t f ( " Hello World \n " ) ; return 0; } Task 7 Transfer the example code to the main.c file you created in the last section Linux Command Line! TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 2

COMPILATION To create an executable file out of our text file we need a compiler, that translates our programming language code to machine code and links it into an executable file. For C we use the gcc compiler, which is short for GNU C Compiler. To compile our Hello World! program we use the Linux command line in the same directory where the main.c file is located: gcc main. c to create an executable named a.out, which we execute via:. / a. out to get the wanted output of the program. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 3

COMPILATION To be able to name the executable file, except from renaming it after the compilation with mv, we compile: gcc o h e l l o w o r l d main. c where helloworld is the name of the executable that is created and can be choosen at will. Task 8 Compile the example code from the main.c file and check the output of the executable file a.out! TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 4

Teil II. C syntax TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 5

KEYWORDS Keywords are words with special meaning and can not be chosen as variable names. In most text editors, with the correct setting of the used programming language, keywords are highlighted as in the example code in these slides. Some keywords in C: int, float, double, char, void,... - specifying data types, for, while, do, break, continue - regarding loops, if, else, switch, case - managing branches. Overall there are 32 keywords in C. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 5

FUNCTIONS Functions in C are methods that are called by a unique name. They can be viewed as a block of code, which is executed in the same, programmed manner every time. The output for our first C program is created with p r i n t f ( " Hello World \n " ) ; which is a function with a unique identifier printf, like a variable has also. Distinctive though are the brackets () after the name. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 6

FUNCTIONS They can have specific input data and generate different output data in every call, but the method itself does not change. i n t main ( ) {... return 0; } is a function too, but different from the printf() function in our example, because main() is created, while printf() is called, after being created somewhere else! Later in this course we will return to the topic of functions. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 7

LIBRARIES Although the function printf() is visible nowhere in the main.c created, we can still use it. That is because it is part of a global library saved somewhere on the computer and we can access every function in that library by including it: #include<s t d i o. h> We will be able to create our own local libraries and include them with: #include " mylibrary. h " Notice the different signs encasing the.h files signalizing, that the first one is a global library, while the second one is local. Later in this course we will briefly return to this topic. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 8

VARIABLES The other big part in programming, opposite of functions, take variables. Like functions they need to have a unique identifier, who can not be a keyword of the language. Variables are stored at runtime of our program on the random access memory (RAM) of the computer and assigned values throughout their lifetime in the program. Depending on its data type a variable needs a different amount of space on the RAM and its binary representation will be interpreted differently from the compiler. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 9

VARIABLES An examples for a variable is: i n t i ; an integer with the name i. It has not been assigned a values yet, so it is not yet defined, but the line above is the declaration of a variable, meaning the reservation of the itendifier and space on the RAM. To define a variable, we have to assign a value to it: i = 5; Now the integer variable i is defined and the value 5 has been stored on the RAM. When introducing pointers, we will return to the concept of saving variables. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 10

DATA TYPES Some selected data types available in C: data type memory range format boolean true or false char 1 byte 2 7 to 2 7 1 %c int 2 byte 2 31 to 2 31 1 %d float 4 byte 7 decimal digits %f long long int 8 byte 2 63 to 2 63 1 %lld double 8 byte 16 decimal digits %lf The format column will be needed for later when we add variables to the output of the program. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 11

Teil III. first steps towards an own C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 12

OUTPUT The standard function to communicate output to the command line is printf(). There is at least another function fprintf(), but it is regarding files and we will attend it much later. As mentioned before, a function can use arguments. In the case of: p r i n t f ( arg1, [ arg2, arg3, arg4,... ] ) ; these arguments are seperated into two types: The first on arg1 is a string, where the other ones are variables whose values are put in the string arg1. Hint The printf() function is in the stdio.h library. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 12

OUTPUT A simple example for only arg1 was given in the Hello World! program p r i n t f ( " Hello World! \ n " ) ; where the \n causes a line break in the output text. When we want to print values of variables we have to insert place holders into the string. This is where the format column of the data type table is needed: p r i n t f ( " Value of the i n t e g e r i = %d\n ", i ) ; p r i n t f ( " Value of the double d i = % l f \n ", d i ) ; The \n is not necessary but for readability. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 13

INPUT The standard function to get input from the user during runtime is scanf(). The function fscanf() is for files and subject of later lessons. The function has two type of arguments: scanf ( [ fspec1 fspec2... ], [ vadd1 vadd2... ] ) ; fspec is short for format specifier and has to be put into a string and seperated by spaces. The vadd are the addresses of the variables where we want to store the entered values. In the chapter about pointer we will attend more closely the address of a variable. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 14

INPUT To demonstrate the usage of scanf() some examples: scanf ( "%d ",& i ) ; for reading an integer value into an integer variable i. scanf ( "% l f % l f % l f ", &a1, &a2, &a3 ) ; for collecting three values of the data type double at once. The input needed from the user will have the form: 1.2 2.4 3.6 three numbers, seperated by spaces. Hint The scanf() function is in the stdio.h library. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 15

FORMAT SPECIFIERS Task 9 Write a program that declares and defines variables of type int, char, float and double and prints their values in the command line! Hint The format specifiers for the data types above where d, c, f and lf. You can furthermore set the amount of pre-decimal and decimal digit with: p r i n t f ( " %3.4 l f ", d i ) ; This will cut the double variable di off, after the fourth decimal digit. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 16

CALCULATOR Task 10 Write a program that resembles a basic calculator. It should introduce itself by explaining its purpose, then ask the user for two numbers as input for the following calculations: addition, substraction, multiplication and division. After calculations the four results need to be returned to the user! Hint Remember that \n cause a line break for better readability and scanf() can process two variables at the same time. TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 17