1Click to edit Master text stylesSecond levelThird levelFourth levelFifth levelEECS 2031Software ToolsModule 5 – Introduction to C
2TextbookThe C Programming Language (2nd edition)by Brian Kernighan and Dennis RitchiePrentice Hall Software Series
3C vs. Java•Java-like (actually Java has a C-like syntax), some differences•No garbage collection•No classes•No exceptions (try ... catch)•No String type•Pointers
4First C Program (first.c)#include <stdio.h>main() {printf(“hello, world \n”);}Note: #include <filename.h> replaces the line by the actual file before compilation starts.
5Basic I/O•Every program has a standard input and a standard output.•By default, keyboard and monitor, respectivelyInput functionsOutput functionsscanf()printf()getchar()putchar()fgets()fputs()
6Output is easy... (celsius.c)•Most of the time, use printf()•Very similar to Java• See Chapter 7 in the textbook•Returns the number of characters printed•Can also use putchar() for a single character
7Input is more complicated•Several functions for input should neverbe used because they are unsafe•They are still in the standard library because a lot of code out there uses them•Avoid using gets()as well as scanf()for strings•Recommended way to read input: getchar()or fgets()+ sscanf()
8getchar()•To read one character at a time from the standard input (the keyboard by default):int getchar(void)•Returns the next input character each time it is called•Returns EOF when it encounters end of file.•EOF input: Ctrl-D (Unix) or Ctrl-Z (Windows).
End of preview
Want to access all the pages? Upload your documents or become a member.
Related Documents
C program code:.lg...
|3
|530
|57
Hostel Management Systemlg...
|25
|1266
|447
Common Programming Errors and Security Measureslg...