You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>// For standard input and output.#include<math.h>// For basic mathematic operations like sqrt, sin, pow.#include<stdlib.h>// For random number generation and dynamic memory allocation.#include<string.h>// For efficient string manipulation.#include<time.h>// For recording program running time.
Difference between <> and ""
main function
main function is where a program starts executing.
A program only contains onemain function.
main function is not callable for you. (No recursion for main function)
Return type and return value for main should be int and 0 (non-zero return value is to indicate some errors)
intmain() // Return type: a integer
{
// Write your code herereturn0; // Return value: 0
}
Variable
Variable name
First character must be letter or _
Followed by letter, number, or _
Case sensitive. a and A are different variable names.
Variable type
Basic variable types int, long long int, double, float, char
Pointer types int *, double *, void *
Constant const int, const int *, int const *
Array int a[10]
Structure type
Question
// What is the variable type of a, b?int*a, b;
Variable initialization
Give value when declaration int a = 1, int * b = NULL
Different variables split by ,
Wrong: int a = b = 1 if b undeclared before.
Variable scope
Start from declaration, end with the scope (block)
Local variable, global variable, static local variable
// What's the value of each variableinta=1+1.5;
doubleb=3 / 2;
doublec=3 / 2.0;
intd=a+b+c;
chare='a';
e=e+1;
// What's the value of integerPi?doublepi=3.1415926;
intintegerPi= (int)pi;
Control statements
if, switch, while, for
continue, break
Mind the place of ;
Block {}: enclose multiple statesments
Block {} can be omitted if it only enclose one statement.
Again, Indent is important and useful!
In midterm2, TA hand grade your code. No indent -> messy -> grading mistakes.
Function
Syntax:
intadd(inta, intb) // returnType functionName (parameterType parameter, ...)
{ // block enclose function contentreturn (a+b); // return value (must consistent with returnType)
}
Parameter passed by value
Same variable name can be used in different function (they do not share value)
If the inside variable share the same name with the outside variable, it will block the outside one
Use void function if function return nothing.
Use void parameter if function needs no input parameters. (can be omitted)
voidhelloWorld(void) // No input parameter, no return value. void helloWorld() is also okay
{
printf("Hello world\n"); // Only do something.
}
I/O
Include <stdio.h>
Output function: printf
Input function: scanf
File output function: fprintf
File input function: fscanf
File pointer FILE *
Arrays
An ordered collection of data values of the same type.
Size must be a constant number.
Declaration and initialization
Declare an array: type, name, size.
Macro define: #define MAX_SIZE 100
Ways of initialization
intzeros[20] = {0}; // All elements initialized as 0intnotAllZero[20] = {1}; // Only the first one will be 1, other will be 0intnumbers[5] = {1, 2, 3, 4, 5};
intnumbers[] = {1, 2, 3, 4, 5}; // Equivalent to previouscharstr[4] = {'a', 'b', 'c', '\0'}; // Define a string, '\0' is ASCII 0intnumbers[5];
for (inti=0; i<5; i++)
numbers[i] =i+1;
Element accessing
Index starts with 0.
Can use integer variable/expression to as array index
Be careful: array out of bounds!
Two-dimensional Array
An array whose elements are arrays.
Use A[i][j] to access elements.
Row first, column second.
Stored as one dimensional array in memory.
Address issue is more complicated.
Higher-dimensioanl array has same property.
C-style string
An character array end with '\0' (0 in ASCII)
Always remember to keep a place for \0 (it is also a char)
Difference between char array and C-style string
Common string function (<string.h>)
fgets
strlen
stpcpy, strcat, strcmp
strchr, strstr
atoi, atof, atol
Pointer
Declaration and Assignment
Pointer declaration int * px
Pointer assignment px = &x
NULL pointer
void * pointer
Array with Pointer
Array name is a pointer!
array[3] is equal to *(array + 3)
A two dimension array is essentially an array of pointer.
The type of a constant C-string "Hello World!" is essentially a const char*
Question
// Which element will be printed?inta[5][5];
printf("%d\n", *(a+10));
Advanced pointer structures
pointer to pointer int ** pt
array of pointers int* pt[10]
Dynamic Memory Allocation
#include <stdlib.h>
malloc() and free()
Use sizeof() to calculate size.
int*arr= (int*) malloc(10*sizeof(int)); // allocate an piece of memory // with the size of 10 intsfree(arr); // free the memory
Life cycles of allocated memory: start with malloc(), end with free()
You must free all the memory you allocate.
Structure
Assemble a package of variables into a structure.
Two ways to define strctures (they are equivalent):