C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.

Slides:



Advertisements
Similar presentations
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
ARDUINO CLUB Session 1: C & An Introduction to Linux.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointers in C Rohit Khokher
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Kernighan/Ritchie: Kelley/Pohl:
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
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.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Pointers and Arrays C and Data Structures Baojian Hua
C Slides and captured lecture (video and sound) are available at:
C Slides and captured lecture (video and sound) are available at:
1 Intro to C/C++ #include using namespace std; int main() { cout
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
ITEC 320 C++ Examples.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Characters and Strings File Processing Exercise C Programming:Part 3.
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)
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
(language, compilation and debugging) David 09/16/2011.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Functions & Pointers in C Jordan Erenrich
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
Arrays and Pointers.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
C is a high level language (HLL)
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Command line arguments
Quiz 11/15/16 – C functions, arrays and strings
CS 537 Section 1 Programming in Unix and C
C Basics.
Programmazione I a.a. 2017/2018.
C Stuff CS 2308.
Miscellaneous functions
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Programming in C Pointers and Arrays.
Characters and Strings
Presentation transcript:

C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by value/reference 4. arrays & structures 5. pointers 6. library functions 7. Compiling and Debugging – gcc and gdb 8. Shell Commands C By Example

2 1. “Hello World” – The Simplest Program #include intmain( int argc, char *argv[] ) { printf( "Hello, world\n" ); } This is an include file. It contains definitions of constants, structures, and functions Printf is a library subroutine that you call to output data to a screen or file. Every C program must have a main. It’s where the program starts execution. This is the form used to declare and pass parameters into a program or subroutine. This says argc is an int and argv is a char string. If I run this program by saying “prog1 - C is fun” Then argc is 5 and argv[0] is “-”, argv[1] is “C”, etc.

C By Example 3 Declarations #include intmain( int argc, char *argv[] ) { int var_A; int *ptr_var_A; double pi = ; char string[32]; var_A = 17; if ( argc > 1 ) var_A = atoi( argv[0] ); ptr_var_A = &var_A; strcpy( string, "Hello, world"); printf( "String->%s var_A->%d ptr_var_A-> %X pi-> %f\n", string, var_A, ptr_var_A, pi ); } Var_A is declared to be an integer. ptr_var_A is declared to be a pointer (or address) of an integer. If there is an input argument, then use it for the value of var_A. The “&” says to take the address of the variable. Strcpy is a library routine to copy strings.

C By Example 4 Pass by Value and Pass by Reference #include voidsubroutineA( int, int * ); shortfunctionB( int, int ); intmain( int argc, char *argv[] ) { intvar_A; int var_B; var_A = 17; var_B = 33; subroutineA( var_A, &var_B ); printf( "Return from SubA: %d %d\n", var_A, var_B ); printf( "Return from FunB: %d\n", functionB( var_A, var_B ) ); }// End of main voidsubroutineA( int A, int *B ) { *B = A; // B is a pointer to an int }// End of subroutineA shortfunctionB( int A, int B ) { return( A + B ); }// End of functionB These are called “prototypes”. Var_A is passed by value. Note the & on var_B which means that its address is passed in. Note how int and ptr to int are declared.

C By Example 5 Arrays and Structures (1) #defineARRAY_LEN32 #include typedefstruct { intarr1[ARRAY_LEN]; charstring[ARRAY_LEN]; } MY_STRUCT; voidsubroutineA( MY_STRUCT * ); This is called a “define”. Used to parameterize constants. Here we define the structure MY_STRUCT. It’s laid out in memory as 32 ints (4 bytes each) plus 32 bytes arranged as a string. We’ll be passing the address of this structure to the subroutine.

C By Example 6 Arrays and Structures (2) intmain( int argc, char *argv[] ) { intindex; MY_STRUCT my_struct; for ( index = 0; index < ARRAY_LEN; index++ ) { my_struct.arr1[index] = index; my_struct.string[index] = (char)index; } subroutineA( &my_struct ); printf( "Return of SubA: %d %c\n", my_struct.arr1[0], my_struct.string[0] ); }// End of main voidsubroutineA( MY_STRUCT *xx ) { xx->arr1[0] = 17; xx->string[0] = (char)33; }// End of subroutineA Declare an instance of the structure. Here we’re fillling in the structure Pass the structure by reference. Note the use of “->” to reference the structure. What is printed out by this program?

C By Example 7 Pointers #include intmain( int argc, char *argv[] ) { inta, b; int*p; a = b = 7; p = &a; // p points to a printf( "p = %d\n", *p ); // 7 is printed *p = 3; printf( "a = %d\n", a ); // 3 is printed p = &b; *p = 2 * *p - a; printf( "b = %d\n", b ); // 11 is printed p = &a; printf( "Input an integer "); scanf( "%d", p ); } // End of Main a and b are ints. p is a pointer to an int.

C By Example 8 Library Functions (1) #include int isalnum(int c);// returns TRUE if char is alpha or numeric int isspace(int c);// returns TRUE if char is white space int tolower(int c); // returns the conversion of c to lower case #include double cos(double x); double exp(double x); #include #typedef unsigned size_t; #define NULL ((void *) 0) #define offsetof(s_type, m) \ ((size_t) &(((s_type *) 0) ->m )) #include #defineEOF(-1)// End of File #defineNULL0 These pages show include files and the library function prototypes that are contained in them.

C By Example 9 Library Functions (2) #include int atoi( const char *s ); int rand(void); void *malloc( size_t size );// Allocates "size" bytes of memory #include void *memcpy(void *to, void *from, size_t n); char *strcat( char *s1, char *s2); // Concatenates size_t strlen( char *s);

C By Example 10 Compilation And Debugging Babbage gcc –g prog4.c –o prog4 babbage% gdb prog4 (gdb) l (gdb) b 24 (gdb) r (gdb) p index (gdb) s (gdb) c (gdb) p my_struct $4 = {arr1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, string = "\000\001\002\003\004\005\006\a\b\t\n \013\f\r\016\017\020\021\022\023\024 \025\026\027\030\031\032\e\034 \035\036\037"} (gdb) q babbage % -g says prepare for debugging. – o is the name of the executable. l says list lines of the program b says set a breakpoint (at line 24 in this case) r means run the program. p says to print a variable. s == single step c == continue to next breakpoint. q == quit

C By Example 11 Shell Commands babbage% vi make_all babbage% chmod 700 make_all babbage% ls -l make_all -rwx jbreeche users 144 Jul 21 04:19 make_all babbage% more make_all gcc -g prog1.c -o prog1 gcc -g prog2.c -o prog2 gcc -g prog3.c -o prog3 gcc -g prog4.c -o prog4 gcc -g prog5.c -o prog5 gcc -g prog6.c -o prog6 babbage% mkdir foo babbage% cd foo babbage% echo "This is a line" > bar babbage% cat bar This is a line babbage% rm bar You need an editor – either emacs or vi. Change privileges ls –l == list directory contents more == print out file, 1 screen at a time. mkdir == create a directory cd == change directory echo == print a line. In this case “>” puts that line in a file. rm == delete a file (rmdir == delete a directory.) “man” and “apropos” are my favorites.