非常简练的对c语言的编译,操作符,运算,函数,变量等基本概念进行了梳理。

献良发布于2019/01/19

注脚

展开查看详情

1.C Basics EE 312 Adapted from Dr. Mary Eberlein, UT Austin

2.C: A low-level language C does not have: exceptions for error-handling range-checking garbage collection OO programming String type So... be careful: Recommendations Always use a debugger like gdb (more about this later) Use libraries of existing code (both to save time and increase reliability). Adopt a sensible set of coding conventions. Avoid programming “ tricks ” and complicated code. low level  faster (usually)

3.C Standards Three C Standards: ANSI C: from late 80s (aka C89) C99: standard from 1999 New built-in types (e.g., _ Bool ) stdbool.h provides bool Added variable-length arrays, single line comments, declarations and code mixing C11: current standard, released in 2011 Improved Unicode support (char16_t, char32_t types for storing UTF-16, UTF-32 encoded data)

4.High Level Languages Assembly language : better than sequences of bits lots of commands to accomplish things High Level Computer Languages: accomplish a lot with fewer commands compared to machine or assembly language easier to understand instructions translated/compiled into machine instructions int sum = 0; int count = 0; while( list[count] != -1 ) { sum += list[count]; count = count + 1; } 4

5.Structure of a C Program #include< something.h > int main(void) { <statement>; <statement>; ... <statement>; } Stored in .c file Every executable C program contains main function containing statements to be executed function: a named group of statements statement: a command to be executed

6.Build Process with gcc Source Code to Executable Two types of source code files: regular code (files end in .c ) header files (files end in .h ) Compiler turns source code into object code files end in .o Linker turns object code files into executable a.out by default

7.Source Code to Executable gcc program is compiler and linker % gcc hello.c –o hello What happens? gcc does this: compiles hello.c to hello.o links hello.o with system libraries produces binary executable hello -Wall enables compiler warnings gcc –Wall –o hello hello.c -g embeds debugging info gcc –g hello.c

8.Compilation with gcc GNU C Compiler gcc : included on unix Available via MinGW or Cygwin on Windows Specify output file with –o option (default is a.ou gcc compilation process % gcc –o hello hello.c % ./hello Hello World % gcc hello.c % ./ a.out Hello World Preprocessing Compilation Linking Assembler source code (.c, .h) Assembly code (.S) Machine code (.o) Include header, expand macro (. i ) executable Richard Stallman, founder of the GNU project

9.Compilation with gcc gcc options can be used to determine how far to go in build process gcc –c hello.c –o hello.o machine code in file hello.o gcc –E hello.c –o hello.i preprocessed file in hello.i Preprocessing Compilation Linking Assembler source code (.c, .h) Assembly code (.S) Machine code (.o) Include header, expand macro (. i ) executable -E -S -c

10.Build Summary Preprocessing : 1 st pass of C compilation processes include files & macros Compilation : takes preprocessed code and generates assembly code (.S) Assembly : produces object file (.o) Linking : takes all object files, libraries and combines to get executable file gcc flags : indicate which steps to carry out

11.gdb : command line debugger Useful commands break linenumber  create breakpoint at specified line run  run program c  continue execution step  execute next line or step into function quit  quit gdb print expression  print current value of expression l  list program

12.CLion : Integrated Development Environment Free for students Install on your machine You can choose to use a different IDE You are strongly encouraged to use CLion

13.Text Editor & Command Line Compilation Pick one... emacs vi Notepad++ (Windows) Compiler: gcc Make sure that your projects run correctly when compiled with gcc on the 64 bit ECE linux machines

14.General layout An example C Programs:

15.A C Program A C program contains: comments – describe program in English include section C is small Need external libraries to do much Include header files to indicate what external code to use stdio library: contains code for reading and writing data from/to terminal, e.g., printf , puts for writing data to terminal scanf for reading data from terminal function prototypes main + other functions

16.#include macro Header files: constants, functions, other declarations #include< stdio.h > - read contents of header file stdio.h stdio.h : standard I/O functions for console, files Other important header files: math.h , stdlib.h , string.h , time.h

17.Symbolic Constants: const Variable (Better than macro) const variable is type checked by compiler. For compiled code with any modern compiler, zero performance difference between macros and constants. Examples : const int EOF = -1; const float TWO_PI = 2*3.14159; Naming convention for constants: All cap words separated by underscores TWO_PI CONVERSION_FACTOR

18.main and other functions C code contained in functions function: named chunk of code that carries out some task Use functions to break large program into small, self-contained & cohesive units Reduces program complexity Increases reusability of code main most important function where execution of your program begins

19.Functions Function should carry out specific, well-defined task C program = 1 or more functions, including main return-type name(type1 parm1, type2 parm2, ... ) { // function body }

20.main function main() : where execution begins Simple version: no inputs, returns 0 when successful and non-zero to signal error int main() Two argument form of main(): provides access to command-line arguments int main( int argc , char * argv []) More on this later...

21.Function Prototypes Declare functions before using Declaration called function prototype Examples of function prototypes: int sumEm ( int , int ); or int sumEm ( int n, int m); Prototypes for common functions in header files in C Standard Library General prototype form: return_type function_name (type1, type2, ....); typei is type of argument i arguments: local variables, values passed from caller return value: value returned to calling function when function exits

22.We are EE 312 /* Print message "We are EE 312" */ #include< stdio.h > int main() { // print text to console puts("We are EE 312!"); return 0; // 0 means success } puts() : output text to console window ( stdout ) and end the line String literal: enclosed in double quotes

23.We are EE 312, revised /* Print message "We are EE 312" */ #include< stdio.h > int main() { const char msg [] = "We are EE 312!"; puts ( msg ) ; // print text to console return 0; // 0 means success } const : variable is a constant char : data type representing a single character char literals enclosed in single quotes: a, t, $ const char msg [] : a constant array of characters int return value is error code: normal exit if 0, problem if non-zero C standard: implied return of 0 at end of main

24.Example #include< stdio.h > int sumEm ( int , int ); int main() { int x = 3; int y = 5; int sum = sumEm (x, y); printf ("The sum is % i

25.Console I/O stdout , stdin : console output & input streams char = getchar () : return character from stdin Later: scanf () puts(string) : print string to stdout putchar (char) : print character to stdout printf ("format-string", list-of-values);

26.Console I/O printf format string contains conversion specifications that correspond to type of values to print: % i : print integer value %g: print floating point number in general format %s: print a string %c: print a char printf ("Name: %s, favorite integer: %i

27.And some other C Basics... Variables, Operators, and Types

28.28 Escape sequences escape sequence : A special sequence of characters used to represent certain special characters in a string. tab character

29.29 Question How many visible characters does the following printf statement produce when run? printf (" \ nn \ \"\ tt "); 0 1 2 3 4