Cs288 Intensive Programming in Linux

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Minimal standard C program int main(void) { return 0 ; }
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ Lesson 1.
Cs288 Intensive Programming in Linux
The Machine Model Memory
C/C++ Tutorial.
Cs288 Intensive Programming in Linux
A bit of C programming Lecture 3 Uli Raich.
Announcements Midterm2 Grades to be announced THIS WEEK
Programming Languages and Paradigms
Cs288 Intensive Programming in Linux
Learning C Language.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Programming Paradigms
Command-Line Arguments
CSCI206 - Computer Organization & Programming
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
Introduction to C Programming
CMSC 104, Section 4 Richard Chang
C++ Basics.
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Lexical Elements, Operators, and the C Cystem
Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Announcements Final will be NEXT WEEK on August 17
C programming Language
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Functions continued.
Chapter 11 Programming in C
Module 2 Variables, Data Types and Arithmetic
Programming Languages and Paradigms
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
INTRODUCTION TO C.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkoski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 6 6-9-15

Topics today Homework revu More C Time for questions

text 6-9-15 cfy

Submit only .c files (or .h if I ask for header files too.) Insure that your code compliles on the afs Linux using the default options (no –gcc) Write a hello world program – done in class last week Do problem 1-4 from text Do problem 1-8 from text

First program /* This is a comment */ #include <stdio.h> /* preproc d*/ main() { printf("hello world\n"); } 6-9-15

Problem 1-4 Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table. https://web.njit.edu/~yurkowsk/x/problem1-4.c

Problem 1-8: ite a program to count blanks, tabs, and newlines. ./public_html/x/exer1-8A.c

Some more Basics of C 6-9-15

Conditional Operator ? : exp1 ? exp2 : exp3 If exp1 is non-zero value of expression is exp2 otherwise it is exp3 1 ? 2 : 3 6-9-15 cfy

Operator Precedence left to right * / % + - 6-9-15 cfy

Some Basics /* Comments */ { blocks } Parameters ( ) # preprocessor directive Statements end with ; Loops: for while Conditionals if then 6-9-15

$ cat header. h #define ROOT 1 $ cat main1. c /. This is a multi-line $ cat header.h #define ROOT 1 $ cat main1.c /* * This is a multi-line * comment block. */ #include <srdio.h> #include "header.h" /* This a one line comment */ main(){ int x=ROOT; /* this is an embeeded comment */ #ifdef ROOT printf("%d",x); #else printf("not root\n"); #endif }

$ cc main1.c $ ./a.out 1$

Basic printf formats %i or %d int %c char %f float %s string 6-9-15 cfy

Width and precision %d print as decimal integer at least 6 characters wide %f print as floating point %6f print as floating point, at least 6 characters wide %.2f print as floating point, 2 characters after decimal point 6-9-15 cfy

I/O C=getchar(); putchar(C); 6-9-15 cfy

for statement for(init;condition;increment) { statement(s); } for(i=0;i<5;i++) printf(“%d\n”,i); 6-9-15 cfy

Symbolic Contants #define name replacement text #define LIMIT 30 6-9-15 cfy

Escape sequences \a 07 Alarm (Beep, Bell) \b 08 Backspace \f 0C Formfeed \n 0A Newline (Line Feed) \r 0D Carriage Return \t 09 Horizontal Tab \v 0B Vertical Tab \\ 5C Backslash \‘ 27 Single quotation mark \“ 22 Double quotation mark \? 3F Question mark \nnn any The character whose numerical value is given by nnn interpreted as an octal number \xhh any The character whose numerical value is given by hh interpreted as a hexadecimal number 6-9-15

if statement if ( expression) statement else if ( expression ) else if ( expression ) 6-9-15 cfy

arrays int myarray[10]; 6-9-15 cfy

functions int function ( int param1, char param2); … int function ( int param1, char param2) { int local1, local2; /* call by value */ retval=function2 (local1); return(10); } 6-9-15 cfy

argv, argc #include <stdio.h> int main (int argc, char *argv[]) { printf(“%s\n”,argv[0]); return 0; } 6-9-15 cfy

compiler cc file.c –o file 6-9-15 cfy

More basic C More on arrays: Array initialization during declaration: int num[5]; num[0]=1; int num[5]={1,2,3,4,5}; 6-9-15 cfy

Multi-dimensional arrays int a[2][3]; int a[2][3]={{1,2,3},{4,5,6}}; 6-9-15 cfy

Multi-dimensional arrays cont #include <stdio.h> main() { int a[2][3]={{1,2,3},{4,5,6}}; int i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) printf("a[%d][%d]=%d\n",i,j,a[i][j]); } 6-9-15 cfy

Multi-dimensional arrays cont afsconnect1-58 >: ./a.out a[0][0]=1 a[0][1]=2 a[0][2]=3 a[1][0]=4 a[1][1]=5 a[1][2]=6 6-9-15 cfy

Strings are just arrays of characters. #include <stdio.h> main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; printf("name1=%s\n",name1); printf("name2=%s\n",name2); } >: ./a.out name1=NJIT name2=NJIT 6-9-15 cfy

Strings (cont). main() { char name1[25]="NJIT"; char name2[25]={'N','J','I','T','\0'}; char *name3="NJIT"; printf("name1=%s\n",name1); printf("name2=%s\n",name2); printf("name3=%s\n",name3); } afsconnect1-67 public_html >: ./a.out name1=NJIT name2=NJIT name3=NJIT 6-9-15 cfy

Strings (cont). Header file string.h Some examples: strcmp String Compare strcat String Concatenation memcpy Copy Memory Block strlen String Length 6-9-15 cfy

General form: Strcat(string1,string2) Appends string2 to string1; Strings (cont). General form: Strcat(string1,string2) Appends string2 to string1; 6-9-15 cfy

Addresses! & used to get the address of a variable main() { int i=666; printf("i= %d\n",i); printf("&i= %d\n",&i); } afsconnect1-72 public_html >: ./a.out i= 666 &i= 1814055836 6-9-15 cfy

More C keywords switch break, continue, goto sizeof Storage classes: auto register extern static 6-9-15 cfy

switch( Grade ) { case 'A' : printf( "Excellent\n" ); case 'B' : printf( "Good\n" ); case 'C' : printf( "OK\n" ); case 'D' : printf( "Mmmmm....\n" ); case 'F' : printf( "You must do better than this\n" ); default : printf( "What is your grade anyway?\n" ); } 6-9-15 cfy

label: while (true) { if(a) break; if(b) continue; if(c) goto label; } 6-9-15 cfy

printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n", main() { int i=666; char array[15]; double n; printf("sizeof i= %d\n",sizeof(i)); printf("sizeof array= %d\n", sizeof(array)); printf("sizeof n= %d\n", sizeof(n)); } >: ./a.out sizeof i= 4 sizeof array= 15 sizeof n= 8 6-9-15 cfy

static Has number of implications: Local static variables initialized only once and retain their value. Guaranteed to be automatically initialized to zero if not otherwise initialized. Global static variable inaccessible outside the file. Can be applied to functions. 6-9-15 cfy

Structures! struct <name> { element1; element2; … } 6-9-15 cfy

Structures example struct class { char name[30]; int number; char instructor[25]; int students[35]; }; 6-9-15 cfy

Structures element dereferencing structure-variable.stucture-member; e.g.: x=class.number; 6-9-15 cfy

Arrays of Structures struct class semester[30]; struct grades { char name[30]; int id; int grades[10]; }g[35]; 6-9-15 cfy

Unions struct class semester[30]; union students { long x; char grades[8]; }g[35]; 6-9-15 cfy

sizeof can be applied to unions and structures Structures can be nested. 6-9-15 cfy

Pointers General form: datatype *ptrname; int x=666, y=555; Int *p; p = &x; y = *p; 6-9-15 cfy

Some cc command line options Compiling multiple C files. Linking multiple objects.

$ cat main. c main(){ func(); } $ cat sub. c func(){} $ cc main $ cat main.c main(){ func(); } $ cat sub.c func(){} $ cc main.c /tmp/ccRFdyU8.o: In function `main': main.c:(.text+0xa): undefined reference to `func' collect2: error: ld returned 1 exit status $ cc sub.c /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' $ cc main.c sub.c $ cc -c main.c $ cc -c sub.c $ cc main.o sub.o $ cc main.c sub.o $

$ cat main. c #include "tmp. h" main(){ func(); } $ cc -c main. c main $ cat main.c #include "tmp.h" main(){ func(); } $ cc -c main.c main.c:1:17: fatal error: tmp.h: No such file or directory ^ compilation terminated. $ ls -l /tmp/tmp.h -rw-rw-r--. 1 integration integration 0 Mar 3 09:23 /tmp/tmp.h $ cc -c -I /tmp main.c $

Homework Do exercise 1-19 from the text. Write a program which reads stdin and converts any local case characters to upper case. Redo problem 4 from class 2 as a C program. 6-9-15 cfy

problem 4 Write a C program, sort.c, which sorts a list of command line parameters in ascending order. For example, your command will look something like:  $ sort 7 2 3 9 -1 and type enter. Your program will return: -1 2 3 7 9 6-9-15 cfy