C Programming Basics – Part 1 Ritu Arora Texas Advanced Computing Center June 18, 2013 Email: [email protected] Overview of Content • Writing a Basic C Program • Understanding Errors • Comments, Keywords, Identifiers, Variables • Standard Input and Output • Operators • Control Structures • Part 1 • Functions in C • Part 2 • Arrays, Structures • Pointers • Working with Files All the concepts are accompanied by examples. 2 Creating a C Program • Have an idea about what to program • Write the source code using an editor or an Integrated Development Environment (IDE) • Compile the source code and link the program using a C compiler • Fix errors, if any • Run the program and test it • Fix bugs, if any 3 Write the Source Code: firstCode.c #include <stdio.h> int main(){ printf("Introduction to C!\n"); return 0; Output: } Introduction to C! 4 Understanding firstCode.c Preprocessor directive Name of the standard header #include <stdio.h> file to be included is specified within angular brackets Func tion’s return type Function name Function name is followed by parentheses – when int main(){ empty no arguments are being passed printf("Introduction to C!\n"); C language function for displaying information on the screen return 0; Keyword, command for returning function value } The contents of the functions are placed inside the curly braces Text strings are specified within "" and every statement is terminated by ; Newline character is specified by \n 5 Save-Compile-Link-Run • Save your program (source code) in a file having a “c” extension. Example, firstCode.c • Compile and Link your code (by default, GCC automatically does the linking) gcc -o firstCode firstCode.c • Run the program ./firstCode Repeat the steps above every time you fix an error! 6 Different Compilers • Different commands for different compilers (e.g., icc for intel compiler and pgcc for pgi compiler) – GNU C program gcc -o firstCode firstCode.c – Intel C program icc -o firstCode firstCode.c – PGI C program pgcc -o firstCode firstCode.c • To see a list of compiler options, their syntax, and a terse explanation, execute the compiler command with the -help or --help option 7 Summary of C Language Components • Keywords and rules to use the keywords • Standard header files containing functions like printf • Preprocessor directives for including the (standard) header files • Function main • Parentheses and braces for grouping together statements and parts of programs • Punctuation like ; • Operators like + • All the above and more to come make up the syntax of C 8 Pop Quiz (add the missing components) ________ <stdio.h> int main()__ printf("Introduction to C!\n") __ printf("This is a great class!\n"); return 0; __ 9 Overview of Content • Writing a Basic C Program • Understanding Errors • Comments, Keywords, Identifiers, Variables • Standard Input and Output • Operators • Control Structures • Functions in C • Arrays, Structures • Pointers • Working with Files All the concepts are accompanied by examples. 10
Description: