1. We have a code sequence that is potentially executed twice.
|
|
- Britton Stokes
- 6 years ago
- Views:
Transcription
1 Long Jump Long Jump Local Jumps In the following example: 1. We have a code sequence that is potentially executed twice. 2. A flag is used to determine whether the sequence is being executed the first or second time. 3. A series of loops establishes the context of the main body of code. 4. If an error occurs a goto is executed which jumps out of the nested loop context and back to a prior point in the sequence. int main( void ) int array[dim1][dim2][dim3]; int second_time = 0; int inx = 0; int jnx = 0; int knx = 0; start: if ( second_time ) puts( "error!" ); exit( EXIT_FAILURE ); (continued) Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/110LongJump.html [3/29/ :12:18 AM]
2 Long Jump Long Jump Local Jumps (continued) for ( inx = 0 ; inx < DIM1 ; ++inx ) for ( jnx = 0 ; jnx < DIM2 ; ++jnx ) for ( knx = 0 ; knx < DIM3 ; ++knx ) char buff[80]; char *err = NULL; int num = 0; printf( "> " ); fflush( stdout ); fgets( buff, 80, stdin ); num = (int)strtol( buff, &err, 0 ); if ( *err == '\n' *err == '\r' ) array[inx][jnx][knx] = num; else second_time = 1; goto start; process( array ); return EXIT_SUCCESS; Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/120LongJump.html [3/29/ :12:18 AM]
3 Long Jump Long Jump Nonlocal Jumps In this example we do the same thing except that we want to jump back to a point in a prior stack frame: 1. We use setjmp to save the context of the stack frame in a global buffer; 2. The value returned by setjmp() tells us which time we are passing through the code; 0 means the first time; 3. If an error occurs longjmp() is executed which restores the saved context; 4. When the context is restored it appears as though we are returning from setjmp() once again; a non-zero return value tells us that it is not the first time. static jmp_buf jump_context_; int main( void ) int array[dim1][dim2][dim3]; if ( setjmp( jump_context_ )!= 0 ) puts( "error!" ); exit( EXIT_FAILURE ); (continued) Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/130LongJump.html [3/29/ :12:18 AM]
4 Long Jump Long Jump Nonlocal Jumps (continued) static void filldim1( int arr[dim1][dim2][dim3] ) int inx = 0; for ( inx = 0 ; inx < DIM1 ; ++inx ) filldim2( arr, inx ); static void filldim2( int arr[dim1][dim2][dim3], int dim1 ) int inx = 0; for ( inx = 0 ; inx < DIM2 ; ++inx ) filldim3( arr, dim1, inx ); static void filldim3( int arr[dim1][dim2][dim3], int dim1, int dim2 ) int inx = 0; for ( inx = 0 ; inx < DIM3 ; ++inx ) char buff[80]; char *err = NULL; int num = 0; printf( "> " ); fflush( stdout ); fgets( buff, 80, stdin ); num = (int)strtol( buff, &err, 0 ); if ( *err == '\n' *err == '\r' ) arr[dim1][dim2][inx] = num; else longjmp( jump_context_, 1 ); Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/140LongJump.html [3/29/ :12:18 AM]
5 Long Jump Long Jump Long Jump Sometimes referred to as non-local goto. Must be primed via setjmp() which saves the application context. Executed via longjmp(), which makes it appear as if setjmp is returning again. The first return from setjmp() is always 0; subsequent returns are always non-0. Previous two examples may be found in the Sample Code pages. Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/150LongJump.html [3/29/ :12:19 AM]
6 Long Jump Long Jump int setjmp( jmp_buf env ); Saves the application context in preparation for a longjmp. env Buffer in which the context is to be saved. Returns: 0 on the initial return to setjmp, non-zero for subsequent returns. void longjmp( jmp_buf env, int status ); Restores the application context previously saved by setjmp(). env Buffer containing saved context. status value to be returned by setjmp; if 0 is specified, the system will force it to a non-zero value. Long Jump file:///c /Documents%20and%20Settings/Jack%20Straub/M...20Work/AdvancedC/Binders/150LongJump/160LongJump.html [3/29/ :12:19 AM]
7 Answers to the Sample Final C Programming: Advanced Answers to the Sample Final 1. typedef struct grade_s char name[71]; double grade; GRADE_t, GRADE_p_t; 2. typedef int FUNK_t( const char*, const char* ); 3. char str[512] = "all: dogs, bark"; int inx = 0; char *token[5]; token[inx] = strtok( str, ":," ); while ( token[inx++]!= NULL && inx < 5 ) token[inx] = strtok( NULL, ":," ); 4. #define MAX_STR_LEN (5) static void next_step( const char *martha ) char george[max_str_len + 1]; strncpy( george, martha, sizeof(george) ); george[max_str_len] = '\000'; static int to_int( const char *str ) char *temp = NULL; long lnum = 0; int num = 0; errno = 0; lnum = strtol( str, &temp, 0 ); if ( errno == 0 && (lnum > INT_MAX) (lnum < INT_MIN) ) errno = ERANGE; if ( errno!= 0 ) file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/100Answers.html (1 of 5) [3/29/ :12:19 AM]
8 Answers to the Sample Final printf( "int conversion error %d: %s\n", errno, strerror( errno ) ); else if ( *temp!= '\000' ) printf( "int conversion error: string contains nonnumeric value\n" ); else if ( *str == '\000' ) printf( "int conversion error: empty string\n" ); else num = (int)lnum; return num; 6. #ifdef MAX #undef MAX #endif #define MAX (32) 7. assert( result >= 7 && result <=15 ); return result; 8. See: 9. See: C source code: samples.c Formatted html: samples.c C source code: coords.c Formatted html: coords.c 10. int main( void ) int rcode = EXIT_SUCCESS; void *temp = NULL; void *mem = malloc( 512 ); if ( mem == NULL ) abort(); temp = realloc( mem, 2048 ); if ( temp == NULL ) abort(); printf( "%s\n", (mem == temp? "extended in place" : "new block allocated") file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/100Answers.html (2 of 5) [3/29/ :12:19 AM]
9 Answers to the Sample Final ); free( temp ); return rcode; 11. See: C source code: funk_array.c Formatted html: funk_array.c 12. See: Answer 13. typedef int FUNKB_PROC_t( char* ); typedef FUNKB_PROC_t *FUNKB_PROC_p_t; typedef FUNKB_PROC_p_t FUNKA_PROC_t( char*, FUNKB_PROC_p_t ); 14. static unsigned adder( unsigned num ) if ( num > 0 ) num = num + adder( num - 1 ); return num; 15. See: C source code: int_search.c Formatted html: int_search.c 16. Solution is left as an exercise for the student. 17. Solution is left as an exercise for the student. 18. Solution is left as an exercise for the student. 19. #define LARGEST( a, b, c ) \ ((a) > (b)? ((a) > (c)? (a) : (c)) : ((b) > (c)? (b) : (c))) 20. #ifndef NDEBUG #define MY_ASSERT( e ) \ ((e)? (void)0 : my_assert( #e, FILE, LINE, DATE, TIME )) #else #define MY_ASSERT( e ) ((void)0) file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/100Answers.html (3 of 5) [3/29/ :12:19 AM]
10 Answers to the Sample Final #endif static void my_assert( const char *expr, const char *file, int line, const char *date, const char *time ) fprintf( stderr, "ASSERTION FAILURE; %s: %s:%d, %s %s\n", expr, file, line, date, time ); abort(); 21. Example 1, building a linked list from scratch: C source code: singlylinked.c Formatted html: singlylinked.c Example 2, building a linked list using the SLIST module: C source code: singlylinked2.c Formatted html: singlylinked2.c 22. a) 25 b) unpredictable c) Unpredictable 24. See: C source code: test_longjmp.c Formatted html: test_longjmp.c 25. int main( void ) int rcode = EXIT_SUCCESS; if ( atexit( atexit_proc )!= 0 ) printf( "eh?\n" ); return rcode; static void atexit_proc( void ) file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/100Answers.html (4 of 5) [3/29/ :12:19 AM]
11 Answers to the Sample Final puts( "atexit proc called" ); Sample Final Answers file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/100Answers.html (5 of 5) [3/29/ :12:19 AM]
12 Answers to the Sample Final C Programming: Advanced Answer mod.h #ifndef MOD_H #define MOD_H extern int mint; void modb( void ); void modc( void ); #endif moda.c #include <stdlib.h> #include <stdio.h> #include <mod.h> int mint = 42; int main( void ) int rcode = EXIT_SUCCESS; modb(); modc(); printf( "mint = %d\n", mint ); return rcode; modb.c file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/150Externs.html (1 of 2) [3/29/ :12:20 AM]
13 Answers to the Sample Final #include <stdio.h> #include <mod.h> void modb( void ) int new_val = 25; printf( "mint = %d; changing to %d\n", mint, new_val ); mint = new_val; modc.c #include <stdio.h> #include <mod.h> void modc( void ) int new_val = -9; printf( "mint = %d; changing to %d\n", mint, new_val ); mint = new_val; Sample Final Answers file:///c /Documents%20and%20Settings/Jack%20Straub/...ork/AdvancedC/Binders/200SampleFinal/150Externs.html (2 of 2) [3/29/ :12:20 AM]
14 type funk_name( type,... ); A variable length argument list must have at least one argument of a fixed type; it may have 0 or more additional arguments of any type; all fixed arguments must precede variable arguments. file:///c /Documents%20and%20Settings/Jack%20Straub/My...ol%20Work/AdvancedC/Binders/250VarArgs/110StdArgs.html [3/29/ :12:20 AM]
15 Writing a Function with a Variable Length Argument List 1. Include stdarg.h. 2. You must have some way to know the type of each argument in the argument list. In the example below it is expected that every argument is type const char*. 3. You must have some way to know when you have processed the last argument in the argument list; in the example below, it is assumed that the last argument is a NULL pointer. vfunk( "str1", "str2", "str3", NULL ); 4. Declare a pointer to the argument list with the macro va_list. va_list arglist; 5. Initialize the pointer to the argument list using the macro va_start; the first va_start argument is the pointer to the arglist, the second is the name of the last fixed argument in the list. va_start( arglist, first ); 6. Access additional, sequential arguments with the va_arg macro; the first va_arg argument is the pointer to the arglist; the second is the type of the next argument in the list. arg = va_arg( arglist, const char * ); 7. When you're finished use the va_end macro to clean up loose ends; the one argument to the macro is the pointer to the argument list. va_end( arglist ); (continued...) file:///c /Documents%20and%20Settings/Jack%20Straub/My...ol%20Work/AdvancedC/Binders/250VarArgs/120StdArgs.html [3/29/ :12:20 AM]
16 Writing a Function with a Variable Length Argument List (continued) #include <stdlib.h> #include <stdio.h> #include <stdarg.h> static void vfunk( const char *,... ); int main( void ) int rcode = EXIT_SUCCESS; vfunk( "str1", "str2", "str3", NULL ); return rcode; static void vfunk( const char *first,... ) va_list arglist; const char *arg = first; va_start( arglist, first ); while ( arg!= NULL ) puts( arg ); arg = va_arg( arglist, const char * ); va_end( arglist ); $./a str1 str2 str3 file:///c /Documents%20and%20Settings/Jack%20Straub/My...ol%20Work/AdvancedC/Binders/250VarArgs/130StdArgs.html [3/29/ :12:20 AM]
17 Another Example int george = 5; double harry = 13.7; fprintf( stdout, "george = %d, harry = %lf", george, harry );... int fprintf( FILE *file, const char *format,... ) In this example fprintf declares a pointer to an argument list and then initializes it. It has two fixed arguments, so it uses the second one in the initialization: va_list args; va_start( args, format ); 2. Next it searches through the format string looking for %. The first one it finds is followed by d so it fetches the next argument on the assumption that it is type int: if ( strcmp( token, "d" ) == 0 int_arg = va_arg( args, int ); 3. The next % is followed by lf so it assumes that the next argument is type double: if ( strcmp( token, "lf" ) == 0 dbl_arg = va_arg( args, double ); 4. Finding no more % signs it assumes there are no more arguments and performs cleanup: va_end( args ); file:///c /Documents%20and%20Settings/Jack%20Straub/My...ol%20Work/AdvancedC/Binders/250VarArgs/140StdArgs.html [3/29/ :12:21 AM]
18 Use variable length argument lists conservatively; they essentially eliminate type checking by the compiler. typedef enum actions_e action0, action1, action2, action3, action4 ACTIONS_e_t; /* This function accepts a list of actions to perform. * Each action consumes 0 or more additional arguments. * The last argument of each action is followed by * another action value until action0 is encountered, * at which point processing terminates. For example: * exec_actions( action1, 10, "geronimo", * action3, 3.14, * action4, 22, 2.2, 32, "department l", * action0 * ); * A description of each action and its respective * arguments follows. * action description * * action1 There will be two additional arguments * of type int and char* respectively... * * action2 There will be no additional arguments... * * action3 There will be one additional argument * of type double... * * action4 There will be additional arguments of * type int, double, int and const char * * * action5 This action takes any number of * additional arguments of type int, the * last one of which must be *... */ void exec_actions( ACTIONS_e_t action,... ); file:///c /Documents%20and%20Settings/Jack%20Straub/My...ol%20Work/AdvancedC/Binders/250VarArgs/150StdArgs.html [3/29/ :12:21 AM]
19 vprintf Family int vprintf( const char *format, val_list args ); int vfprintf( FILE *file, const char *format, va_list args ); int vsprintf( char *str, const char *format, va_list args ); These function work like their printf family counterparts except that they accept a va_list in place of the usual arguments that follow the format string. int main( void ) int rcode = EXIT_SUCCESS; write_args( "sally.txt", "%s was %d years old and %s was %d,\n", "Sally", 10, "Timmy", 8 ); write_args( "sally.txt", "but %s was %lf centimeters taller than %s.\n", "Timmy", 3.2, "Sally" ); write_args( "sally.txt", "\"Oh no!,\" said %s, \"something must be done.\"\n", "Sally" ); write_args( "sally.txt", "%s gazed at %s with a look of pure evil.\n", "Sally", "Timmy" ); return rcode; static void write_args( const char *path, const char *format,... ) va_list arglist; FILE *file = NULL; va_start( arglist, format ); if ( (file = fopen( path, "a" )) == NULL ) file:///c /Documents%20and%20Settings/Jack%20Straub...20Work/AdvancedC/Binders/250VarArgs/160VPrintf.html (1 of 2) [3/29/ :12:22 AM]
20 abort(); vfprintf( file, format, arglist ); fclose( file ); va_end( arglist ); $./a $ cat sally.txt Sally was 10 years old and Timmy was 8, but Timmy was centimeters taller than Sally. "Oh no!," said Sally, "something must be done." Sally gazed at Timmy with a look of pure evil. file:///c /Documents%20and%20Settings/Jack%20Straub...20Work/AdvancedC/Binders/250VarArgs/160VPrintf.html (2 of 2) [3/29/ :12:22 AM]
21 Notes 1. Don't confuse stdargs with varargs. The ISO standard is stdargs. 2. Use variable length argument lists conservatively. 3. The va_arg macro can be picky; the type argument must be composed so that adding an asterisk (*) to the end will be interpreted as "pointer to...." 4. Always use the va_end macro. In some environments this macro is not needed, but to keep your code portable it must be there. file:///c /Documents%20and%20Settings/Jack%20Straub/My...hool%20Work/AdvancedC/Binders/250VarArgs/170Notes.html [3/29/ :12:22 AM]
22 Topics About the C Miscellaneous Directives Macro Expansion file:///c /Documents%20and%20Settings/Jack%20Straub/My%2...k/AdvancedC/Binders/260/110.html [3/29/ :12:22 AM]
23 About the C Traditionally a process that runs separately from the compiler. Output from the preprocessor usually feeds directly into the compiler. Sometimes it is helpful to examine the output of the preprocessor (consult your compiler documentation for how to do this): adhoc.c /* This is a bad macro used for illustration */ #define SQUARE( x ) (x * x) int funk( void ) int inx = 5; int jnx = SQUARE( inx + 1 ); int funk( void ) int inx = 5; int jnx = (inx + 1 * inx + 1); file:///c /Documents%20and%20Settings/Jack%20Straub/My...20Work/AdvancedC/Binders/260/120About.html [3/29/ :12:22 AM]
24 About the C It may be convenient to use the preprocessor as a utility apart from compiling code. adhoc.c #define SITE " #define LINKTO( name, addr ) \ <a href = addr> \ name \ </a> <html> <head> </head> <body> <p> For more information see LINKTO( this, SITE ). </body> </html> $ gcc -E -P adhoc.c <html> <head> </head> <body> <p> For more information see <a href = " this </a>. </body> </html> file:///c /Documents%20and%20Settings/Jack%20Straub/My...20Work/AdvancedC/Binders/260/130About.html [3/29/ :12:23 AM]
25 Miscellaneous Directives #line Sets the current line number in the source file, and, optionally, the name of the current source file. This is mainly useful if you are writing a C code generator. int main( void ) printf( "%d, %s\n", LINE, FILE ); #line 99 printf( "%d, %s\n", LINE, FILE ); #line 200 "gsharp.c" printf( "%d, %s\n", LINE, FILE ); return 0; $./a 5, adhoc.c 99, adhoc.c 200, gsharp.c file:///c /Documents%20and%20Settings/Jack%20Straub/My%...l%20Work/AdvancedC/Binders/260/140Line.html [3/29/ :12:23 AM]
26 Miscellaneous Directives #error Prints a user-define diagnostic and prevents completion of compilation. #define INSTALLATION (39) #if defined( INSTALLATION ) &&!defined( POWER ) #error "Power requirements must be specified." #endif int main( void ) return 0; $ gcc adhoc.c adhoc.c:4:2: #error "Power requirements must be specified." file:///c /Documents%20and%20Settings/Jack%20Straub/My%...%20Work/AdvancedC/Binders/260/150Error.html [3/29/ :12:23 AM]
27 Miscellaneous Directives #warning Prints a user-define diagnostic; does not prevents completion of compilation. #ifdef BIT64_ENABLED #warning "64-bit pointer issues may arise." #endif int main( void ) return 0; $ gcc adhoc.c adhoc.c:4:2: warning: "64-bit pointer issues may arise." file:///c /Documents%20and%20Settings/Jack%20Straub/My%...0Work/AdvancedC/Binders/260/160Warning.html [3/29/ :12:23 AM]
28 Miscellaneous Directives #pragma Used for compiler-dependent features. #ifdef DIGITAL_UNIX #pragma builtins #endif #pragma warning( disable : 4996 ) file:///c /Documents%20and%20Settings/Jack%20Straub/My%...20Work/AdvancedC/Binders/260/170Pragma.html [3/29/ :12:24 AM]
29 Miscellaneous Directives # The null directive; treated as a blank line (may be helpful in porting old code). file:///c /Documents%20and%20Settings/Jack%20Straub/My%...l%20Work/AdvancedC/Binders/260/180Null.html [3/29/ :12:24 AM]
CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output
CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output Instructor: Dr. Kun Sun 1 This lecture: [Seacord]: Chapter 6 Readings 2 Secure Coding String management Pointer Subterfuge
More information2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge
CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output Instructor: Dr. Kun Sun 1 This lecture: [Seacord]: Chapter 6 Readings 2 String management Pointer Subterfuge Secure
More informationC: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this
More informationAdvanced Pointer & Data Storage
18, 19: storage classes 14: Preprocessor & Polymorphism in C) 15 : command line building 26 : stdarg Advanced Pointer & Data Storage (for ch. 14, 15 18, 19, 26) Contents Preprocessor & Polymorphism in
More informationThis code has a bug that allows a hacker to take control of its execution and run evilfunc().
Malicious Code Insertion Example This code has a bug that allows a hacker to take control of its execution and run evilfunc(). #include // obviously it is compiler dependent // but on my system
More informationCS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco
CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level
More informationChapter 14 - Advanced C Topics
Chapter 14 - Advanced C Topics Outline 14.1 Introduction 14.2 Redirecting Input/Output on UNIX and DOS Systems 14.3 Variable-Length Argument Lists 14.4 Using Command-Line Arguments 14.5 Notes on Compiling
More informationTopic 6: A Quick Intro To C
Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationCSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads
CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads Overview This homework is due by 11:59:59 PM on Tuesday, April 10,
More informationTopic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History
Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program
More informationHave examined process Creating program Have developed program Written in C Source code
Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationMemory. Memory Topics. Passing by Value. Passing by Reference. Dynamic Memory Allocation. Passing a Pointer to a Pointer. Related Memory Functions
Memory Memory Memory Topics Passing by Value Passing by Reference Dynamic Memory Allocation Passing a Pointer to a Pointer Related Memory Functions Memory file:///c /Documents%20and%20Settings/Jack%20Straub/My...hool%20Work/AdvancedC/Binders/040Memory/110Memory.html
More informationC: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36
Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar
More informationStandard File Pointers
1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf
More informationCSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include
More informationSignature: ECE 551 Midterm Exam
Name: ECE 551 Midterm Exam NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual work.
More informationCSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C
CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C Overview This homework is due by 11:59:59 PM on Tuesday, September 19,
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationC mini reference. 5 Binary numbers 12
C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char
More informationModel Viva Questions for Programming in C lab
Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous
More information1 The CTIE processor INTRODUCTION 1
1 The CTIE processor INTRODUCTION 1 1. Introduction. Whenever a programmer wants to change a given WEB or CWEB program (referred to as a WEB program throughout this program) because of system dependencies,
More informationTopic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)
Topic 8: I/O Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional) No C language primitives for I/O; all done via function
More informationThis lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988).
Errata for The C Programming Language, Second Edition This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988). The pagination
More informationFile Access. FILE * fopen(const char *name, const char * mode);
File Access, K&R 7.5 Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ header
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationECE264 Spring 2014 Exam 2, March 11, 2014
ECE264 Spring 2014 Exam 2, March 11, 2014 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationCS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files
CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as
More informationECE264 Spring 2013 Exam 1, February 14, 2013
ECE264 Spring 2013 Exam 1, February 14, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationCS240: Programming in C. Lecture 14: Errors
CS240: Programming in C Lecture 14: Errors Errors We ve already seen a number of instances where unexpected (and uncaught) errors can take place: Memory buffer overflow/underflow unintended casts misuse
More informationC Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS
C Programming C SYLLABUS COVERAGE Introduction to Programming Fundamentals in C Operators and Expressions Data types Input-Output Library Functions Control statements Function Storage class Pointer Pointer
More informationM1-R4: Programing and Problem Solving using C (JAN 2019)
M1-R4: Programing and Problem Solving using C (JAN 2019) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter
More informationCS 261 Fall Mike Lam, Professor. Structs and I/O
CS 261 Fall 2018 Mike Lam, Professor Structs and I/O Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful
More informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationCSC209H Lecture 3. Dan Zingaro. January 21, 2015
CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without
More informationNEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.
NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Programming in C! Advanced Pointers! Reminder! You can t use a pointer until it points
More informationIntroduction to N1031. Components of N1031. Guiding Principles. Walk through, issues, and rationale
Introduction to N1031 Walk through, issues, and rationale Components of N1031 New functions that protect against buffer overflow and always produce null terminated strings New reentrant versions of old
More informationCodeWarrior Development Studio for Microcontrollers V10.0 MISRA-C:2004 Compliance Exceptions for the HC(S)08, RS08 and ColdFire Libraries Reference
CodeWarrior Development Studio for Microcontrollers V10.0 MISRA-C:2004 Compliance Exceptions for the HC(S)08, RS08 and ColdFire Libraries Reference Manual Revised: May 12, 2010 Freescale, the Freescale
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationWeek 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430
Week 2 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430 1 Why is the Shell Important? Shells provide us with a way to interact with the core system Executes programs on
More informationEverything Else C Programming and Software Tools. N.C. State Department of Computer Science
Everything Else C Programming and Software Tools N.C. State Department of Computer Science BOOLEANS CSC230: C and Software Tools NC State University Computer Science Faculty 2 Booleans In C99, bools are
More informationA Crash Course in C. Steven Reeves
A Crash Course in C Steven Reeves This class will rely heavily on C and C++. As a result this section will help students who are not familiar with C or who need a refresher. By the end of this section
More informationCERT C Rules implemented in the LDRA tool suite
CERT C Rules implemented in the LDRA tool suite This section lists a snapshot of the CERT C Coding Standard guidelines in 2014 that are automatically checked by version 9.5.1 of the LDRA tool suite. Guidelines
More informationAbout Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals
More informationECE551 Midterm Version 2
Name: ECE551 Midterm Version 2 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual
More informationJTSK Programming in C II C-Lab II. Lecture 3 & 4
JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically
More informationCS1003: Intro to CS, Summer 2008
CS1003: Intro to CS, Summer 2008 Lab #07 Instructor: Arezu Moghadam arezu@cs.columbia.edu 6/25/2008 Recap Pointers Structures 1 Pointer Arithmetic (exercise) What do the following return? given > char
More informationLecture 8: Structs & File I/O
....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \
More information211: Computer Architecture Summer 2016
211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Structure: - header files - global / local variables - main() - macro Basic Units: - basic data types - arithmetic / logical / bit operators
More informationCSI 402 Lecture 2 Working with Files (Text and Binary)
CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value
More informationCSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks. Professor Lisa Luo Spring 2018
CSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks Professor Lisa Luo Spring 2018 Previous Class Buffer overflows can be devastating It occurs when the access
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationP.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above
P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above
More informationFile IO and command line input CSE 2451
File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to
More informationECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.
Name: ECE551 Midterm NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual work. You
More informationThis homework is due by 11:59:59 PM on Thursday, October 12, 2017.
CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 2 (document version 1.3) Process Creation and Inter-Process Communication (IPC) in C Overview This homework is due by 11:59:59
More informationDecision Making -Branching. Class Incharge: S. Sasirekha
Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the
More informationCSE 333 Midterm Exam July 24, Name UW ID#
Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,
More informationStandard C Library Functions
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationAdvanced C Programming Topics
Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8
More information8. Characters, Strings and Files
REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence
More informationTHE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming
THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY ISA 563: Fundamentals of Systems Programming Announcements Homework 2 posted Homework 1 due in two weeks Typo on HW1 (definition of Fib. Sequence incorrect)
More informationHere's how you declare a function that returns a pointer to a character:
23 of 40 3/28/2013 10:35 PM Violets are blue Roses are red C has been around, But it is new to you! ANALYSIS: Lines 32 and 33 in main() prompt the user for the desired sort order. The value entered is
More informationContents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44
Contents Lecture 3 C Preprocessor, Chapter 11 Declarations, Chapter 8 Jonas Skeppstedt (js@cs.lth.se) Lecture 3 2014 1 / 44 C Preprocessor Predefined macros Macro replacement Conditional inclusion Source
More informationECE551 Midterm Version 1
Name: ECE551 Midterm Version 1 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationC Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:
C Programming Code: MBD101 Duration: 10 Hours Prerequisites: You are a computer science Professional/ graduate student You can execute Linux/UNIX commands You know how to use a text-editing tool You should
More informationCSci 4061 Introduction to Operating Systems. Input/Output: High-level
CSci 4061 Introduction to Operating Systems Input/Output: High-level I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language! High-level I/O Hide device
More informationFile I/O. Preprocessor Macros
Computer Programming File I/O. Preprocessor Macros Marius Minea marius@cs.upt.ro 4 December 2017 Files and streams A file is a data resource on persistent storage (e.g. disk). File contents are typically
More information#ifndef DOUBLE_LIST /* this string SHOULD NOT previously exist */ #define DOUBLE_LIST
/* This is a personal header file. Call it double_list.h * Its name should be reflected into the macro definition (#define). * For instance, here I should say something like: * #define DOUBLE_LIST #ifndef
More informationCMPS 105 Systems Programming. Prof. Darrell Long E2.371
+ CMPS 105 Systems Programming Prof. Darrell Long E2.371 darrell@ucsc.edu + Chapter 7: The Environment of a UNIX process + Introduction + The main() fuction n int main(int argc, char* argv[]); n argc =
More informationC for C++ Programmers
C for C++ Programmers CS230/330 - Operating Systems (Winter 2001). The good news is that C syntax is almost identical to that of C++. However, there are many things you're used to that aren't available
More informationECE551 Midterm Version 1
Name: ECE551 Midterm Version 1 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual
More informationCSci 4061 Introduction to Operating Systems. Programs in C/Unix
CSci 4061 Introduction to Operating Systems Programs in C/Unix Today Basic C programming Follow on to recitation Structure of a C program A C program consists of a collection of C functions, structs, arrays,
More informationCSE 333 Autumn 2013 Midterm
CSE 333 Autumn 2013 Midterm Please do not read beyond this cover page until told to start. A question involving what could be either C or C++ is about C, unless it explicitly states that it is about C++.
More informationC Program Development and Debugging under Unix SEEM 3460
C Program Development and Debugging under Unix SEEM 3460 1 C Basic Elements SEEM 3460 2 C - Basic Types Type (32 bit) Smallest Value Largest Value short int -32,768(-2 15 ) 32,767(2 15-1) unsigned short
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationMid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:
Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering Mid-term Exam Name: This exam is closed book and notes. Read the questions carefully and focus your answers on what has
More informationCODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report
CODE TIME TECHNOLOGIES Abassi RTOS MISRA-C:2004 Compliance Report Copyright Information This document is copyright Code Time Technologies Inc. 2012. All rights reserved. No part of this document may be
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationProgramming in C - Part 2
Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb
More informationtoday cs3157-fall2002-sklar-lect05 1
today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical
More informationC Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach
C Fundamentals & Formatted Input/Output adopted from KNK C Programming : A Modern Approach C Fundamentals 2 Program: Printing a Pun The file name doesn t matter, but the.c extension is often required.
More informationedunepal_info
facebook.com/edunepal.info @ edunepal_info C interview questions (1 125) C interview questions are given with the answers in this website. We have given C interview questions faced by freshers and experienced
More informationMotor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks
Goanna 3.3.2 Standards Data Sheet for MISRA C:2012 misrac2012-datasheet.pdf Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks The following
More informationCS246 Spring14 Programming Paradigm Files, Pipes and Redirection
1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows
More informationLecture 03 Bits, Bytes and Data Types
Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics
More informationC introduction: part 1
What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation
More informationBasic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types
More informationFormat String Dangers. Shachar Shemesh Security Consultant
Format String Dangers Shachar Shemesh Security Consultant http://www.shemesh.biz Schedule Reintroduction to printf (as if you don t already know ). Some reflection about common uses of printf. Dangers
More informationWriting to and reading from files
Writing to and reading from files printf() and scanf() are actually short-hand versions of more comprehensive functions, fprintf() and fscanf(). The difference is that fprintf() includes a file pointer
More informationC Legacy Code Topics. Objectives. In this appendix you ll:
cppfp2_appf_legacycode.fm Page 1 Monday, March 25, 2013 3:44 PM F C Legacy Code Topics Objectives In this appendix you ll: Redirect keyboard input to come from a file and redirect screen output to a file.
More informationCSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.
CSE 303, Winter 2007, Midterm Examination 9 February 2007 Please do not turn the page until everyone is ready. Rules: The exam is closed-book, closed-note, except for one 8.5x11in piece of paper (both
More informationProgramming languages, their environments and system software interfaces
ISO/IEC JTC1 SC22 WG14 N1146 Date: 2005-10-25 Reference number of document: ISO/IEC TR 24731 Committee identification: ISO/IEC JTC1 SC22 WG14 SC22 Secretariat: ANSI Information Technology Programming languages,
More informationMacros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang
Macros and Preprocessor CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Previously Operations on Linked list (Create and Insert) Agenda Linked List (More insert, lookup and delete) Preprocessor Linked List
More informationSystems Programming. 08. Standard I/O Library. Alexander Holupirek
Systems Programming 08. Standard I/O Library Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Last lecture:
More informationEach line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.
Decision-making in C Squeezing Digits out of an Integer Assignment For part of this assignment, you will use very basic C techniques to implement a C function to remove from a given nonnegative integer
More informationMid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:
Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering Mid-term Exam Name: This exam is closed book and notes. Read the questions carefully and focus your answers on what has
More information