C Syntax - Summary Programming Fundamentals PDF

Title C Syntax - Summary Programming Fundamentals
Course Programming Fundamentals
Institution Edinburgh Napier University
Pages 15
File Size 68.3 KB
File Type PDF
Total Downloads 291
Total Views 401

Summary

C SYNTAX LINKING header files provide a declaration of the functionality to be provided. It is an interface to the implementation. library files (.lib) provide the definition (or implementation) of the functionality. COMMAND LINE nmake run from makefile cl compiles multiple files into one .exe cl ge...


Description

C SYNTAX

********************** LINKING **********************

header files (.h)- provide a declaration of the functionality to be provided. It is an interface to the implementation.

library files (.lib) - provide the definition (or implementation) of the functionality.

*********************** COMMAND LINE *********************** nmake - run from makefile

cl - compiles multiple files into one .exe

cl /c - generates obj files cl /FE - generate asm files cl /D - defines the specified value to the code

lib - generate library

lib /OUT:array.lib search.obj sort.obj generate.obj - generate lib called array.lib

link ... - The linker can take more than object code files as part of its set of inputs. It can also take library files.

************************** PRE PROCESSOR COMMANDS ************************** #include - Includes (adds) a code from a header file to the code file as part of the code to be compiled

#define - Defines a value which is then replaced in the code file when found, or used for conditional compilation

#undef - Undefines a value. Can overwrite a #define used previously.

#if - Used to check a value of a defined pre-processor value.

#ifdef- Used to check if a value has been defined

#ifndef -Used to check if a value has not been defined

#else -Used with #if and #ifdef

#elif -An else-if statement

#endif -Ends a pre-processor if block

#pragma - Tells the compiler that the rest of the line contains instructions. These are generally compiler specific,

but we will look at one that is fairly cross compiler.

*************************** IMPORTS ***************************

#include #include #include

#include - standard libraries, needed for converstions (atoi, etc) #include - standard i/o #include - if you are using text at all

********************************* PLACEHOLDERS *********************************

\n - new line (ASCI 10) \0 - NULL terminator (ASCI 0)

%s - Take the next argument and print it as a string %d - Take the next argument and print it as an int %f - take the next arg and print as double. %c - take arg and interpret as a character %e - scientific notation format of a value %u %l - long %ll - long long

%lu -

************************************** CASTING **************************************

(type) expression - forces an expression/arg to be treated as a certain type

const float PI = 3.14159; - creates a non-modifiable variable

type casted_value = (type )original_value;

const int x = 500;

static int x = 0;

***************************** DATA TYPES - SIZE ***************************** Int - 4 bytes

unsigned int: 4 bytes, min value 0, max value 4294967295 int: 4 bytes, min value -2147483648, max value 2147483647

(UINT MAX, INT MIN and INT MAX) are all defined in the limits.h header file

(In 32-bit opp system) --------------------------------------------------------------unsigned char is 1 bytes , min value 0 , max value 255 signed char is 1 bytes , min value -128 , max value 127 char is 1 bytes , min value -128 , max value 127 unsigned short is 2 bytes , min value 0, max value 65535 short is 2 bytes , min value -32768 , max value 32767 unsigned int is 4 bytes , min value 0, max value 4294967295 int is 4 bytes , min value -2147483648 , max value 2147483647 unsigned long is 4 bytes , min value 0 , max value 4294967295 long is 4 bytes , min value -2147483648 , max value 2147483647 unsigned long long is 8 bytes , min value 0, max value 18446744073709551615 long long is 8 bytes , min value -9223372036854775808 , max value 9223372036854775807 float is 4 bytes , min value 1.175494 e -038 , max value 3.402823 e +038 double is 8 bytes , min value 2.225074 e -308 , max value 1.797693 e +308 long double is 8 bytes , min value 2.225074 e -308 , max value 1.797693 e +308 char * is 4 bytes short * is 4 bytes int * is 4 bytes

(In 64 bit, pointers are 8 bytes because 2^64 gives more addressable memory.)

***************** LIMITS *****************

UCHAR_MIN, UCHAR_MAX SCHAR_MIN, SCHAR_MAX,

CHAR_MIN, CHAR_MAX,

0,

USHRT_MAX,

SHRT_MIN, SHRT_MAX,

0,

UINT_MAX,

INT_MIN, INT_MAX,

0,

ULONG_MAX

LONG_MIN, LONG_MAX,

0ULL, ULLONG_MAX, LLONG_MIN, LLONG_MAX,

FLT_MIN, FLT_MAX, (all floating points are signed) DBL_MIN, DBL_MAX, LDBL_MIN, LDBL_MAX,

********************************* FUNCTIONS *********************************

strlen(string_var) - gets length of string (excluding null terminator)

strcmp(string_var, "Comparing Strings") - compares two strings for equallity. Returns 0 if true, otherwise returns difference of ASCI intgers. 12

strcat(string_var, "String to add") - adds the second string to the end of the first string. Addition is done at the first null terminator in the first string. All characters in the second string are added.

atoi(charstr_var) - string to int

atof(charstr_var) - string to float (double)

sizeof(type) - returns the size of a data type in bytes. ex: printf("An int is %d bytes. \n", sizeof(int));

switch (response_variable) { case x: //statements break; case y: //statements break; ... }

********************* FILE FUNCTIONS *********************

FILE * file = fopen (" filename ", " mode ") ; - open a file and assign the location to the pointer (here named "file")

(MODES) r opens the file for reading w opens the file for writing. Existing files of the name have their contents discarded a opens the file for appending (writing at the end). Will not discard existing

file contents. File seeking operations are ignored. r+ opens a file for reading and updating w+ opens a file for reading and updating. Discards and existing contents in the file a+ opens a file for reading and updating at the end. Will not discard contents. File seeking operations are ignored. b opens the file as binary rather than text

malloc - memory allocation malloc requires the amount of data (in bytes) we need to allocate. malloc returns a pointer to the memory location it has allocated memory at. This pointer is of type void (so we have a void*). This means that the type of memory is undefined (it is just a block). We cast it to a pointer to int (a int*) to set the data value e.x: malloc(sizeof(int) * size)

free - This releases any allocated memory once we have finished with it. THIS IS VERY IMPORTANT!. If you do not free your allocated memory it cannot be used, leading to memory leaks. e.x. free(data);

fread - This reads in data from a file. It takes the following parameters: 1. the location to read the file into 2. the size of the data type being read in 3. the number of values of the data type to read in 4. the file to read in from

fclose - This closes the file. You should always close your files after you have finished with them!. If you don’t close the

file you can cause system conflicts. If you are writing to a file, you may lose the information sent to the file when the application exits. The application will not automatically push the contents to the hard drive, even on exit. Therefore data can be lost.

fprintf We used fprintf as a command here. This works exactly as printf except that is requires a FILE* to print to. e.x.

fprintf(fPointer, "I love cheese\n"); - writes "I love cheese" to the file pointed to by fPointer.

ALWAYS CLOSE FILES USING fclose(fPointer);

************************************************** CUSTOM FUNCTIONS **************************************************

type func_name (type param a, type param b) { //statements.. }

ex;

double average_grade(int grade_total, int entries_total) { double average = grade_total / entries_total; return average;

}

**************** ENUM FUNC **************** enum CHOICE 7{ 8 EXIT = 0 , 9 HELLO = 1 , 10 GOODBYE = 2 11 };

********************************* STRUCTURES ********************************* DECLARE:

struct student { unsignes int matric; char *name; char *address; };

ACCESS: variable.member_name

DECLARE VARIABLE: struct ;

Byte Data (in memory) 0 matric 1 matric 2 matric 3 matric 4 name 5 name 6 name 7 name 8 address 9 address 10 address 11 address

*********************** RANDOM NUMBERS ************************

//seed random creation (starting num) srand ( time ( NULL ) ) ;

// Generate random numbers for (int i = 0; i < size ; ++ i ) data [ i ] = rand () ;

******************************* NOTES ON LOOPS/CONDITIONALS ******************************

C and C++ support the same basic loops that Java supports. Just as a reminder these are:

while(condition) - the commands in the while block are executed as long as the given condition is true. This means that the loop may never run if condition is false

do ... while(condition) - the commands in the do while loop are executed as long as the given condition is true. The loop will execute at least once.

for(value; condition; command) - for loops allow us to perform some form of initialisation (the value part) at the start of the loop, test for a condition at the start of each loop iteration (the condition part), and perform a command at the end of each iteration (the command part).

continue - ends this iteration of the loop and allows the condition to be checked to see if another iteration should be executed.

break - exits the loop completely

********************************* DECLARINNG STRING ********************************

type name[size]; Where size is a constant, compile time value

// contains helper functions for strings # include < stdio .h > 2 // contains helper functions for strings 3 # include < string .h > 4 5 int main (int argc , char ** argv ) 6{ 7 // Declare a character array for message 8 char msg_1 [5] = {’H’,’e’,’l’,’l’,’o’};

9 // Declare a second character array for message 10 char msg_2 [8] = " World !";

11 // Declare a third message 12 char msg_3 [9] = " Goodbye !\0 ";

13 // Declare a forth message - no size 14 char msg_4 [] = " Compiler worked out my size !";

15 // Declare a fifth message - use a pointer 16 char * msg_5 = " Compiler worked out my size too !"; 17 18 // Print messages . 1st message is not null - terminated 19 printf ("%s\n", msg_1 ) ; 20 printf ("%s\n", msg_2 ) ; 21 printf ("%s\n", msg_3 ) ;

22 printf ("%s\n", msg_4 ) ; 23 printf ("%s\n", msg_5 ) ; 24 25 return 0; 26 }

***************************************** POINTERS *****************************************

Pointers are declared using the * type: type *name;

The use of * can become confusing as we will see it used to also dereference pointers, as well as the obvious use for multiplication. We will introduce these ideas slowly.

****************************************** FGETS / INPUT

******************************************

fgets(string, length, stream ); The parameters are as follows:

string - the array of memory (char array or pointer) to read into

length - the maximum number of characters to read. This must be equal to or less than the size of memory being read into.

stream - the location to read data from. This is a FILE* (pointer to a FILE) type. We will look at file input/output later in the module. However, we can treat the command line as a file. It has the name stdin (STanDard INput).

6 // Declare character arrays to store name 7 char first_name [50]; 8 char last_name [50]; 9 char full_name [50]; 10 11 // Prompt for first name 12 printf (" Please enter your first name : ") ; 13 // Read name into array from stdin ( standard input ) 14 fgets ( first_name , 50 , stdin ) ;...


Similar Free PDFs