C Programming Review CSC 4320/6320
Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile & Run C program on Ubuntu Linux
Facts About C Structured Programming language Compiled on various platforms System programming language Examples of the use of C: OS- Unix Language Compilers Assemblers Network Drivers Databases Most State-of-the-art software Language interpreters Case-sensitive language. C- Basically collection of functions.
C Program Structure
/* Document section or comments */ C Program Structure contd.. #include <stdio.h> /* Link section (Preprocessor) */ int total = 0; /* Global declaration, definition section */ int sum (int, int); /* Function declaration section */ int main () /* Main function means start here */ { } printf ("This is a C basic program \n"); total = sum (1, 1); printf ("Sum of two numbers : %d \n", total); /* Library command */ return 0; int sum (int a, int b) /* User defined function */ { return a + b; /* definition section */ } Brackets define code blocks
Keywords & C Types Flow control if, else, return, switch, case, default Loops for, do, while, break, continue Common types int, float, double, char, void structures struct, typedef, union Counting and sizing things enum, sizeof Rare but still useful types extern, signed, unsigned, long, short, static, const
C Input & Output Read Input :scanf function E.g. scanf( %d, &x); Print output: printf function E.g. printf( %d, x); Both printf function and scanf function must be supplied with a format string as 1 st argument.
Scanf Example int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y); Sample input: 1-20.3-4.0e3 scanf will assign 1, 20, 0.3, and 4000.0 to i, j, x, and y, respectively.
printf Example: int i, j; float x, y; i = 10; j = 20; x = 43.2892f; y = 5527.0f; printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y+1); Output: i = 10, j = 20, x = 43.289200, y = 5528.000000
Declare Array: type arrayname [ arraysize ]; type array_name[size] = {a_list_of_value}; Arrays Example 1: Example 2 : double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; Example 3 : float matrixnum[20][25]; // 20 rows 25 columns Example 4 : char chvowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
Arrays Note: C doesn t require subscript bounds be checked. No compile time error if subscript goes out of range.
Functions Types: 1.Standard library Function 2.User-defined function
Standard library Function String Standard functions
User-defined Functions
A pointer is a variable that holds memory address of another variable, where, the actual data is stored. type_of_stored_data * pointer_variable_name; Pointers char* chname; int * ntypeofcar; float *fvalue; 1. Indirection operator (*) : gain access of object that pointer points to 2. Address-of-operator (&): gets address of variable
Pointers As Arguments
Compare to Value as Arguments
Pointers as Return Values Returns location instead of value
Pointers pointing to arrays
Pointers to access Strings
A structure is a collection of related data items(members) stored in one place and can be referenced by more than one names. Structure variables can be initialized. The initialization for each variable must be enclosed in braces. Both structure types and variables follow the same scope as normal Structures variables, as do all identifiers. If you define a structure within a function, then you can only use it within that function. specifies a type members
Structures
Structures
LinkedList A linked list is a basic data structure. Linked list parts. Nodes make up linked lists. Nodes are structures made up of data and a pointer to another node. Usually the pointer is called next.
Creating a Node: Allocate space for a new node. Set the next pointer to the value of NULL Set the data value for the node. LinkedList Adding Node to list: If the start node is null then the start node becomes the new node. If start is not null then start becomes the new node s next ] and the start becomes the new node.
LinkedList- Deleting a Node Q
Dynamic Memory Allocation
Dynamic Memory Allocation
Macro: fragment of code given a name. When name is used, it is replaced by contents. MACRO Types: Object-like macros resemble data objects when used function-like macros resemble function calls.
Function Macro Compiled Preprocessed Differentiate Macro & Function translates into ASM call to pass parameters and return values Implementation not hidden Easy to debug C preprocessor translates strings using macro and compiles. Hides implementation Hard to debug large code.
An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. Object-like macro (https://gcc.gnu.org/onlinedoc s/cpp/macros.html) You create macros with the #define directive. #define is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro's body, expansion or replacement list. For example, #define BUFFER_SIZE 1024 defines a macro named BUFFER_SIZE as an abbreviation for the token 1024. If somewhere after this #define directive there comes a C statement of the form foo = (char *) malloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro BUFFER_SIZE. The C compiler will see the same tokens as it would if you had written foo = (char *) malloc (1024);
Functionlike macro (https://gcc.gnu.org/onlin edocs/cpp/macros.html) You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same #define directive, but you put a pair of parentheses immediately after the macro name. For example, #define lang_init() c_init() lang_init() ==> c_init() Function-like macros can take arguments, just like true functions. As an example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs, and some uses. #define min(x, Y) ((X) < (Y)? (X) : (Y)) x = min(a, b); ==> x = ((a) < (b)? (a) : (b)); y = min(1, 2); ==> y = ((1) < (2)? (1) : (2)); z = min(a + 28, *p); ==> z = ((a + 28) < (*p)? (a + 28) : (*p));
Compile & Run C program on Ubuntu Linux Compiler Command: gcc -o [targetname] [.c file Name]
Useful Links Pass by Value or Pass by Pointer: http://denniskubes.com/2012/08/20/is-c-pass-by-value-or-reference/ Explain list_for_each_entry and list_for_each_entry_safe http://stackoverflow.com/questions/16230524/explain-list-for-each-entry-and-list-for-each-entry-safe Swap function in java http://stackoverflow.com/questions/2393906/how-do-i-make-my-swap-function-in-java Macro https://gcc.gnu.org/onlinedocs/cpp/macros.html