C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Similar documents
C programming basics T3-1 -

File IO and command line input CSE 2451

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Intermediate Programming, Spring 2017*

PRINCIPLES OF OPERATING SYSTEMS

211: Computer Architecture Summer 2016

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

EL2310 Scientific Programming

Standard C Library Functions

C mini reference. 5 Binary numbers 12

Programming in C. Pointers and Arrays

8. Characters, Strings and Files

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Review Topics. Final Exam Review Slides

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

SAMPLE MIDTERM SOLUTION

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

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

CS 261 Fall Mike Lam, Professor. Structs and I/O

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Organization of a file

C-LANGUAGE CURRICULAM

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

C PROGRAMMING. Characters and Strings File Processing Exercise

Dynamic memory allocation

COMP s1 Lecture 1

Arrays, Strings, & Pointers

LSN 3 C Concepts for OS Programming

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Chapter 8: Character & String. In this chapter, you ll learn about;

Computers Programming Course 11. Iulian Năstac

A Fast Review of C Essentials Part I

ENG120. Misc. Topics

Multiple Choice Questions ( 1 mark)

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

High Performance Programming Programming in C part 1

Kurt Schmidt. October 30, 2018

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

EL2310 Scientific Programming

COP 3223 Final Review

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

File I/O. Arash Rafiey. November 7, 2017

The Design of C: A Rational Reconstruction: Part 2

Functions. Arash Rafiey. September 26, 2017

mith College Computer Science CSC231 C Tutorials Fall 2017 Introduction to C Dominique Thiébaut

Continued from previous lecture

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Introduction to string

C Overview Fall 2014 Jinkyu Jeong

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

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

Create a Program in C (Last Class)

Lesson #8. Structures Linked Lists Command Line Arguments

C Basics And Concepts Input And Output

C Review. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

BLM2031 Structured Programming. Zeyneb KURT

n Group of statements that are executed repeatedly while some condition remains true

Chapter 8 - Characters and Strings

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Contents. A Review of C language. Visual C Visual C++ 6.0

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

ESC101N: Fundamentals of Computing End-sem st semester

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

C Characters and Strings

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

BSM540 Basics of C Language

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Chapter 9 Strings. With this array declaration: char s[10];

Chapter 14 - Advanced C Topics

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

File (1A) Young Won Lim 11/25/16

C: How to Program. Week /Mar/05

Fundamentals of Programming & Procedural Programming

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Lecture 02 C FUNDAMENTALS

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Pointers and File Handling

Standard File Pointers

CS 0449 Sample Midterm

CSE 5A Final Fall 2006

The C language. Introductory course #1

C Overview Fall 2015 Jinkyu Jeong

Chapter 2 - Introduction to C Programming

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Transcription:

C Language: Review INFO0939 - High Performance Scientific Computing 26 septembre 2017

Data type int: Basic signed integer type. unsigned int: Basic unsigned (only positive) integer type. float: Real number, usually referred to as a single-precision floating-point type. double: Real number, usually referred to as a double-precision floating-point type. char: Character type.... 2 / 29

Variables type name; type name = value; const type name = value; type: The data type (see previous slide). name: The name of the variable. Only lowercase, uppercase and digits (accents are forbidden); Must start with a letter; Spaces are prohibited; value: Value given to the variable. const: To define a constant number (cannot change after its definition). 3 / 29

Control structure Listing 1: age.c if( age < 12) // If x is smaller than 12 printf (" You are a child.\n"); else if( age >= 12 && age < 18) // If x is greater or // equal to 12 and smaller than 18 printf (" You are a teenager.\n"); else // Else ( if x is greater or equal to 18) printf (" Your are an adult.\n"); 4 / 29

Control structure switch (x) case 0: printf ("x = break ; case 1: printf ("x = Listing 2: number.c 0\n"); 1\n"); break ; case 2: printf ("x = 2\n"); break ; default : printf ("x is different from 0, 1 and 2.\ n"); break ; 5 / 29

Loop structure int factorial = 1; Listing 3: factorial.c for ( int i = 1; i <= 10; i ++) factorial *= i; printf (" Factorial of 10 = %d \n", factorial ); 6 / 29

Loop structure int sum = 0; Listing 4: sum.c while ( sum < 100) sum ++; printf (" sum = %d \n", sum ); 7 / 29

Pointers Memory a 10 3.9... 0xff4aed Address 0xff4aec 0xff4aed 0xff4aee... 0xffa870 int number = 10; int * pointer = & number ; Listing 5: pointers.c printf (" The value of number is: % d \ n", number ); printf (" The address of number is: % p \ n\ n", & number ); printf (" The value of pointer is: % p \ n", pointer ); printf (" The address of pointer is: % p \ n", & pointer ); printf (" The value pointed to by pointer is: % d \ n", * pointer ); 8 / 29

Static array Listing 6: static_array.c char primenumber [6] = 2, 3, 5, 7, 11, 13; printf (" The 6 first prime number are : \n"); for ( int i = 0; i < 6; i ++) printf ("%d \n", primenumber [i ]); 9 / 29

Dynamic array Listing 7: dynamic_array.c int arrayofnumber [7] = 1, 3, 5, 3, 4, 7, 3; int countthree = 0; // We use a loop to count the number of 3 for ( int i = 0; i < 7; i ++) if( arrayofnumber [ i] == 3) countthree ++; // We create a array of size countthree containing data of type in int * threeindex = malloc ( countthree * sizeof ( int )); 10 / 29

Dynamic array // We store indexes in the array threeindex int numthree = 0; for ( int i = 0; i < 27; i ++) if( arrayofnumber [ i] == 3) threeindex [ numthree ] = i; numthree ++; printf (" There are % d number three located at: [", countthree ); for ( int i = 0; i < countthree ; i ++) printf ("%d ", threeindex [i ]); printf ("]\n"); // Don t forget to free the memory!! free ( threeindex ); 11 / 29

String In C language a string: Is seen as a array of characters (char); Must be finished by a special character \0 (backslash and zero). char hello [] = " Hello "; Listing 8: hello.c for ( int i = 0; i < 6; i ++) printf ("%c \n", hello [i ]); 12 / 29

String Listing 9: string.c char name [] = " C. Geuzaine "; char professor [] = " Professor "; // size of a string : function strlen ( str ) printf (" Size of name = %ld \n", strlen ( name )); char bigstring [50]; // copy a string : function strcpy ( str1, str2 ) strcpy ( bigstring, professor ); // concatenates 2 strings : function strcat ( str1, str2 ) strcat ( bigstring, name ); printf ("%s \n", bigstring ); 13 / 29

Conversions You can also convert a string to numerical value (int, float, double,...): char myinteger [] = " 45"; char myreal [] = " 3.14 "; int numi = atoi ( myinteger ); double numd = atof ( myreal ); printf (" numi %d\n", numi ); printf (" numd %lf\n\n", numd ); 14 / 29

Main function All programs must contain a main function. This is the start point of a program. There are two parameters: argc: The number of parameters passed to the program; argv: The value of these parameters; There is always one parameter that contains the name of the program (in argv[0]). Listing 10: parameters.c int main ( int argc, char * argv []) printf (" There are %d parameters : \n", argc ); for ( int i = 0; i < argc ; i ++) printf ("%s \n", argv [i ]); printf ("\n"); return 0; 15 / 29

Main function If you compile and then run the previous code with command:./ myprogram abcd toto 3. 1415 you obtain, There are 4 parameters :./ myprogram abcd toto 3. 1415 16 / 29

Functions A function: Must be declared before it is used; Has a name; Has a return type (or void if there is no return); Has parameters; Example: char myfunction ( int param1, double param2 ); myfunction takes two parameters and returns a char value. The first parameter is named param1 of type int and the second parameter is named param1 of type double. 17 / 29

Functions Listing 11: function.c // The function must be declared before it is used int square ( int number ); int main ( int argc, char ** argv ) int number = 3; // The use of the function printf (" The square of %d is %d\n", number, square ( number )); return 0; // The implementation of the function int square ( int number ) return number * number ; 18 / 29

Functions and pointers Listing 12: functionandpointers.c void myfunction1 ( int number ); void myfunction2 ( int * pointeronnumber ); int main ( int argc, char ** argv ) int number = 3; myfunction1 ( number ); printf (" number = %d\n", number ); myfunction2 (& number ); printf (" number = %d\n", number ); return 0; 19 / 29

Functions and pointers void myfunction1 ( int number ) number = number + 1; void myfunction2 ( int * pointeronnumber ) * pointeronnumber = * pointeronnumber + 1; 20 / 29

Input output To write on the screen: printf ("A text "); printf ("A text with a int %d", num1 ); printf (" A text with a char % c and a double % lf", num2, num3 ); To read from the screen: // read an integer ( int myinteger ) scanf ("%d", & myinteger ); // read an character ( char mychar ) scanf ("%c", & mychar ); // read an float number ( float myfloat ) scanf ("%f", & myfloat ); 21 / 29

Input output int number ; char character ; Listing 13: io.c printf (" Please enter a character : "); scanf ("%c", & character ); printf (" You have enter %c \n", character ); printf (" Please enter an integer number : "); scanf ("%d", & number ); printf (" You have enter %d \n", number ); 22 / 29

File Input output Listing 14: file.c FILE * filew ; // Open the file for writing filew = fopen (" file. txt ","w"); // Check if the file is open if( filew == NULL ) // if not exit return 1; char string [] = " Something the write in the file \ n"; for ( int i = 0; i < strlen ( string ); i ++) fputc ( string [i], filew ); // Close the file fclose ( filew ); 23 / 29

File Input output FILE * filer ; // Open the file for reading filer = fopen (" file. txt ","r"); // Check if the file is open if( filer == NULL ) // if not exit return 1; char character = 0; while ( character!= \ n ) character = fgetc ( filer ); printf ("%c", character ); // Close the file fclose ( filer ); 24 / 29

File Input output Listing 15: hpc.c FILE * file ; // Open the file for reading file = fopen (" HPC. txt ","r"); // Check if the file is open if( file == NULL ) // if not exit return 1; char str [100]; int nume = 0; while ( fgets (str, 100, file )!= NULL ) for ( int i = 0; i < strlen ( str ); i ++) 25 / 29

File Input output if(str [i] == e ) nume ++; printf (" There are %d e in this text. \n", nume ); // Close the file fclose ( file ); 26 / 29

Environmental variables You can defined variables in your computer (outside the program). These variables are called environmental variables. For example PATH is an environmental variable defined on every computer. If you want to define an environmental variable called MY_VARIABLE and set this variable to 3, you can write in command line: export MY_ VARIABLE =3 27 / 29

Environmental variables Then, the following program print the value of this variable (3 in this case): Listing 16: env.c char * numthreads = getenv (" MY_ VARIABLE "); // If the environmental variable don t exist if( numthreads == NULL ) return 1; int num = atoi ( numthreads ); printf (" Environmental variable MY_ VARIABLE = % i \n", num ); 28 / 29

https://www.cprogramming.com/tutorial.html https://en.wikibooks.org/wiki/c_programming https://openclassrooms.com/courses On NIC4 you can copy the previous examples: cp -r /home/ulg/ace/aroyer mydir 29 / 29