A bit of C programming Lecture 3 Uli Raich.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Kernighan/Ritchie: Kelley/Pohl:
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
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.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Introduction to Programming
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
C++ Lesson 1.
LINKED LISTS.
CSE 374 Programming Concepts & Tools
Lesson #8 Structures Linked Lists Command Line Arguments.
CSE 374 Programming Concepts & Tools
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
Programming Languages and Paradigms
Chapter 18 I/O in C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
C programming language
Programming Paradigms
C Basics.
Introduction to C Programming Language
Computer Systems and Networks
CSCI206 - Computer Organization & Programming
Arrays in C.
Programming in C Input / Output.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Programming in C Input / Output.
C Stuff CS 2308.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
פרטים נוספים בסילבוס של הקורס
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
C Programming Getting started Variables Basic C operators Conditionals
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Programming in C Input / Output.
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Programming in C Pointers and Arrays.
C Programming - Lecture 5
Programming Languages and Paradigms
Variables in C Topics Naming Variables Declaring Variables
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming - Lecture 3
Presentation transcript:

A bit of C programming Lecture 3 Uli Raich

Programming Languages To make a language a programming language it needs to implement: Assignments Conditional statements loops

C libraries C uses a large number of code libraries and you can create C libraries yourself. These libraries may use special data types, which are defined in include files Before using the library functions #include <stdio.h> or #include “myOwnIncludeFile.h”

The C main program As a first example people usually write the “Hello World” program. #include <stdio.h> void main() { printf(“Hello World!\n”); } Let us try to compile and execute this program First we start the editor, we type and save the program, then we compile it using the gcc compiler and finally we execute it

C data types C has a number of data types: char, short, int, long, float, double unsigned char, unsigned short, unsigned int can be extended to boolean (in C99 you can #include <stdbool.h> No real strings! But a pointer to a zero terminated chain of characters struct union enum And you can define your own data types with typedef

Assignments We modify the program to do some calculation: I call the program assignments.c and I compile it with gcc -o assignments assigments.c

Type casting Why do we get this strange result for the division? and how can we correct this? Yes, the reason is that div has got the wrong (namely integer) type To correct we must first convert a and b to doubles (or floats) before doing the calculation and div must be a double as well.

Printing formatted output You have seen that I use the function printf to output the results of the calculation. The format string “%d” tells the system to output the result as a decimal number. In an exercise this afternoon, where you will implement a simple calculate we will use the format %10.4f There are many additional number formats to output strings, decimal, octal and hex numbers … Look up the man page for printf for an explanation. Instead of printing on the screen you can also convert the number to a string using sprintf. The equivalent call for input also exists and is called scanf.

Conditions: if statement Conditions can be tested with if if (a < b) printf(“a is bigger than b\n”); else printf(“b is bigger than a\n”);

Calculating the Fibonacci numbers 0 1 1 2 3 5 8 13 21 34 … or xn = xn-1+xn-2 How can we write a program to calculate up to 12 such numbers? This can easily be done in a for loop

The for loop

The while loop Can we also do calculations as long as the Fibonacci number does not exceed a certain value?

Pointers We can define variables which do not contain the value but the address of where the value is stored in memory: char a=5; is the value char *myText=”Hello World!”; myText points to the place in memory where myText is stored.

Pointer example

Printing to a file Instead of printing to stdout with printf you can also print to a file with fprintf:

Command line arguments The main routine has 2 parameters, which we did not use yet as well as a return code. int main(int argc, char ** argv) or int main(int argv, char *argv[ ]); int argc is the number of arguments passed char **argv is a pointer to a list of null terminated C strings.

Command line arguments example Please note that the argv values are zero terminated strings even if you give a number! If you expect a number then you have to convert the string into a number with atoi or atof

Conditional statements if, else if, else switch (tag) Combination of assignment and condition: the ? operator:

Numeric command line arguments and if conditions

The switch statement Imagine a simple calculator taking 3 parameters: 2 numbers An mathematical operator: +,-,*,/ We do not want to have too many if statements to find out which operation must be carried out Use: switch (operator) { case ‘+’: do addition; break; case ‘-’: do subtraction; default: error; }

Switch example

Switch example (2)

Composite variables We can have a series of values of the same type in an array: int myArray[10]; the size of the array is fixed! An array is in reality a pointer to a series of values of the same type. Or we can define structures where values of different types can be combined In addition we have enumerations with a certain fixed range of values.

Composite data types example

The ? operator If (a < b) c = a else c = b; Can be written in a single statement: c = (a<b) ? a : b;

? operator example

functions Up to now the size of our programs did not exceed one page. Bigger problems must be broken down into smaller, manageable pieces using functions Example: the operations of our calculator: double add(double num1, double num2) { return (num1+num2); } The other operations look similar. These functions may go into the same file as main but also into separate files. Then they are compiled separately and linked to the main routine to form an executable program.

Include files How does the main program know the name of the function and its parameters? Define these in an include file to be added to the calling program: In the calling routine and the call itself: result = add( 5.2, 4.8);

libraries A big number of libraries are available for use with C and you can write you own libraries to extend the set. There are 2 types of libraries: static libraries and dynamic ones. The static libraries are named lib name of the library .a Example libm.a for the mathematics library containing a great number of mathematical functions Its dynamic brother is called libm.so When linking statically the library is added to the executable making it substantially bigger but independent of library versions The dynamic library is loaded once into memory and can be used by several executable. The executable is therefore much smaller but there is a risk it will not work if it was linked to a wrong library version.

Linking libraries Let’s say your program sine.c uses the sine function. The you must link it to libm In the static case: gcc -static -o sine sine.o -lm In the dynamic case you skip the -static option Gcc -o sine sine.c -lm