Programmeringsteknik DT143G Lecture 2: Variables and I/O PDF

Document Details

SaintlySphene5524

Uploaded by SaintlySphene5524

Örebro University

2024

null

Pascal Rebreyend

Tags

C programming variables input/output programming lecture

Summary

This document is a lecture on C programming, specifically focusing on variables and I/O functions like printf and scanf. The lecture covers basic concepts of variables, input/output operations, arithmetic computations, type conversions, and operators.

Full Transcript

Introduction Programmeringsteknik DT143G Lecture 2: Variables and I/O Pascal Rebreyend 7-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Variables: Basics Input and output formats...

Introduction Programmeringsteknik DT143G Lecture 2: Variables and I/O Pascal Rebreyend 7-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Variables: Basics Input and output formats Arithmetic computations Pascal Rebreyend Programmeringsteknik DT143G Introduction Output - printf printf is the function to print on the screen A family of functions to print (to a file, fprint,vsprintf,sprintf,... ) to print to different output like file, variables,... arguments between parentheses. First: A string with text to display %d: replace by an integer value %f: for a floating point variables We can have several variables printed Pascal Rebreyend Programmeringsteknik DT143G Introduction printf \n: new line \r: carriage return \b: backspace... Important: \n flush the buffer (important if debugging) Lot of formatting options! And some special characters! See documentation! Pascal Rebreyend Programmeringsteknik DT143G Introduction Type conversion We can mix different types in the same expression If in one operator, one number is a float or double, then, everything is converted into double Thus, to multiply by 1.0 is not like multiplying by 1 Converting may mean rounding c = a + b followed by d = c − a. Is d = b? Pascal Rebreyend Programmeringsteknik DT143G Introduction scanf scanf: a function from the standard library to read from the keyboard #include int main() { int i; scanf("%d", &i); printf("%d", i); } syntax: rather similar than printf (for the format) &d: we need the &! (We will later why, strong particularity of C) Pascal Rebreyend Programmeringsteknik DT143G Introduction Operators +, −, ∗, \: The classical 4 operators Follow the mathematical order division: result as integer! Pascal Rebreyend Programmeringsteknik DT143G Introduction Initialization What’s happen if we read a variable before assigning it to a value? We can do int main() { int i=3; print("%d\n",i); } Example is very simple... but in a huge code? or: The compiler is only finding some errors! Some compilers can issues warning (optional, -W.. options for gcc) Pascal Rebreyend Programmeringsteknik DT143G Introduction math.h A mathematical libray exists. #include provide classical value such as PI, e,... functions such as log, sqrt,sin,... gcc: need top be compiled with the -lm option Pascal Rebreyend Programmeringsteknik DT143G Introduction Conclusion printf and scanf: screen and keyboard Declaration of variables is mandatory before use spaces, tabulations in the source code: useless for the compiler... but useful for us (easier to read) Pascal Rebreyend Programmeringsteknik DT143G

Use Quizgecko on...
Browser
Browser