C Tutorial CIS 5027 Prof Dr ShuChing Chen













![Arrays in C �syntax for declaring an array: int examplearray[100]; /* This declares an Arrays in C �syntax for declaring an array: int examplearray[100]; /* This declares an](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-14.jpg)
![Arrays in C �Accessing array elements: Double salary = balance[9]; �Example for arrays: #include Arrays in C �Accessing array elements: Double salary = balance[9]; �Example for arrays: #include](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-15.jpg)













![I/O in C �gets() and puts() #include <stdio. h> int main() { char str[100]; I/O in C �gets() and puts() #include <stdio. h> int main() { char str[100];](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-29.jpg)
![I/O in C �scanf() and printf() #include <stdio. h> int main() { char str[100]; I/O in C �scanf() and printf() #include <stdio. h> int main() { char str[100];](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-30.jpg)




![Command Line Argument in C #include <stdio. h> int main( int argc, char *argv[] Command Line Argument in C #include <stdio. h> int main( int argc, char *argv[]](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-35.jpg)













- Slides: 48

C Tutorial CIS 5027 Prof: Dr. Shu-Ching Chen TAs: Samira Pouyanfar (spouy 001@cs. fiu. edu) Hector Cen (hcen 001@cs. fiu. edu) Tianyi Wang (wtian 002@cs. fiu. edu) Spring 2018

Agenda �What is C? �Basic C �Advanced C �References

What is C? �C is a structured, procedural programming language �C has been widely used both for operating systems and applications �It has become one of the most widely used programming languages of all time �Many later languages have borrowed directly or indirectly from C, including: C++, Java, Java. Script, C#, Perl, PHP, Python, etc…

Basic Syntax in C �Semicolons – each statement ended with semicolon printf(“Hello, World! n”); Return 0; �Comments – use pair of /* and */ /* my first program in C */ �C is case sensitive

Header Files in C �A header file is a file with extension. h �Two types of header files: �User generated header files �System header files � preprocessing directive: #include � user defined header file: #include “file” � System header files: #include <file>

Header Files in C �Header File can’t be included twice �Use conditional to prevent the conflict: #ifndef HDADER_FILE #define HDADER_FILE The entire header file #endif

Main function in C �Every full C program begins inside a function called main �A function is simply a collection of commands that do "something" �The main function is always called when the program first executes �From main, we can call other functions �Usually, the main function will “return 0” at the end

Main function in C � To access the standard functions that comes with your compiler, you need to include a header with the #include directive

Print to Terminal �Use printf to print a string int fprintf(FILE *stream, const char *format, …) �Inside string use % escapes to add parameters �int: %d �float: %f �characters: %c �strings: %s int x = 37; double y = 56. 56; printf( “The int x is %d, The double y is %f”, x, y ); The above code will print: The int x is 37, The double y is 56. 56

Variables in C There are several types of variables: �char �int �float �Void – represents the absence of type <variable type> <name of variable> int x; int a, b, c, d; int n = 10; extern int m = 10;

Scope Rules in C �Local variables #include <stdio. h> int main () { /*local variable declaration */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf(“value of a = %d, b = %d and c = %dn”, a, b, c); return 0 }

Scope Rules in C �Global Variables #include <stdio. h> /* global variable declaration */ int g; int main () { /*local variable declaration */ int a, b; /* actual initialization */ a = 10; b = 20; g = a + b; printf(“value of a = %d, b = %d and g = %dn”, a, b, g); return 0 }

Operators in C �Assignment operators �=, +=, -=, *=, /=, %= �Logical Operators �&&, ||, ! �Relational operators �==, !=, >, < >=, <= �Arithmetic operators �+, -, *, /, %, ++, --
![Arrays in C syntax for declaring an array int examplearray100 This declares an Arrays in C �syntax for declaring an array: int examplearray[100]; /* This declares an](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-14.jpg)
Arrays in C �syntax for declaring an array: int examplearray[100]; /* This declares an array */ � For example: Char astring[100]; � Initializing arrays: Double balance[] = {1000. 0, 2. 0, 3. 4, 7. 0, 50. 0};
![Arrays in C Accessing array elements Double salary balance9 Example for arrays include Arrays in C �Accessing array elements: Double salary = balance[9]; �Example for arrays: #include](https://slidetodoc.com/presentation_image_h/75897af4760db385023087f8359b52e1/image-15.jpg)
Arrays in C �Accessing array elements: Double salary = balance[9]; �Example for arrays: #include <stdio. h> int main () { int n[10]; /* n is an array of 10 integers */ int i, j: /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ){ n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element’s value */ for ( j = 0; j < 10; j++ ) { printf( “Element[%d] = %dn”, j, n[j] ); } return 0; }

Strings in C �String is character array. Two ways to initialize a string: char greeting[6] = {'H', 'e', 'l', 'o', '