0% found this document useful (0 votes)
7 views8 pages

C Programming Basics and Concepts

The document provides an introduction to programming with C and C++, emphasizing their importance in the industry compared to Matlab. It covers key concepts such as compilers, data types, variables, operators, control structures, and functions, along with best practices for coding and debugging. The document also highlights the differences between local and global variables, as well as the significance of meaningful variable names and proper code formatting.

Uploaded by

1311720215ljy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

C Programming Basics and Concepts

The document provides an introduction to programming with C and C++, emphasizing their importance in the industry compared to Matlab. It covers key concepts such as compilers, data types, variables, operators, control structures, and functions, along with best practices for coding and debugging. The document also highlights the differences between local and global variables, as well as the significance of meaningful variable names and proper code formatting.

Uploaded by

1311720215ljy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ENGR1010J 25FA RC2

Author: Cao Siqi

Intro
Learning Matlab is for dealing with exams and homework, because you may never need
to use Matlab in the future.
However, learning C and C++ is not only for dealing with exams and homework. They
are actually important programming languages that are widely used in the industry.
If you want to be a programmer in the future, strong C and C++ (maybe python as well)
programming skills can help you get offer and get paid.
No wonder C/C++ is more difficult. Patience and hardwork is needed.

Some Concepts
Compilers
A compiler translates the program written in a high-level language so that the computer can
run it.

Eg: GCC, Clang, Visual C++ compiler, ...

Editors
An editor is something where you can edit text. Specifically, we need a code editor which
provides more help for coding.

Eg: Visual Studio Code, Vim, Sublime Text, Notepad++, ...

IDE: Integrated Development Environment


= editor + compilers + debuggers + ...

Eg: Visual Studio, Qt, CLion, Dev-C++, ...

GCC, GNU and MinGW


GCC is the GNU Compiler Collection, an optimizing compiler produced by the GNU Project,
supporting various programming languages, hardware architectures and operating systems.

MinGW is short for Minimalist GNU for Windows.


Basic Information about C
#include <stdio.h>
int main(){
printf("hello world");
return 0;
}

Library: stdio.h, string.h, math.h


main function: there should be one and only one main function Reference for C or C++

Data Type
int: integer, 4 bytes
double/float: floating point number, 4/8 bytes
char: character, 1 byte
ASCII code
some special kinds of char: \n , \0
switch between char and int

// for char a and int b


int i = a + '0'; // convert char into int
char c = b + '0'; // convert int into char
Variables
Declare before use!

int a;
int a = 1;

Variables can be divided into global variable and local variable:

global variable: declare outside of any functions (can be accessed in the whole file), but
avoid using it!
local variable: declare inside a function or a block (can only be accessed in where it is
defined).

#include <stdio.h>

int global_var = 5; // <- global variable (not recommended)


int fn1(int, int);
float expn(float);

int main() {
char reply; // <- local variables -- these two variables are
int num; // <- only known inside main()
// ...
}
int fn1(int x, int y) { // <- local x, y -- formal parameters only known
inside this function
float fnum; // <- these two variables are known
int temp; // <- in this function only
global_var += 10;
// ...
}

float expn(float n) {
float temp; // <- local -- this variable is known in expn() only
// ...
}

To deliver variables outside the function into the function, we need other tools to achieve
that.

Therefore, we are going to introduce pointers, so that we can directly manipulate the
address of the variable.

Printf and Data Types


When using printf , the datatype is very important!
%d , %f , %c , %s

Operators
Different from MATLAB, they cannot be used in arrays.

+, -, *, /
++ (increment), -- (decrement)

// have difference in some aspects


int i = 7;
printf("%d\n", i++);
printf("%d\n", ++i);
// the same for '--'

+= , -= , *= , /=
&& , ||
> , >= , < , <= , == , !=
If

if (logical){
statements
}
else if (logical){
statements
}
else {
statements
}

Ternary Operator

int x, y, max;
max = (x > y) ? x : y;
// this is the same as:
if (x > y){
max = x;
}
else {
max = y;
} // recommend to use `if`

Switch

switch (expression) {
case constant1:
statements
break; // break is necessary!(in most cases)
case constant2:
statements
break;
default:
statements
}

See problem 1
For and While Loop

while (expression){
statements
}

for (init; test; step){


statements
}

break:
while: jump out of the whole loop
for: jump out of this loop and directly start the next one
continue: jump out of this loop and directly start the next one

Better coding style


Variables are your children, and you should give them meaningful names.
Using space properly will make your code more readable.
Adding commments to the code to briefly describe the functionality of this piece of code.

Bad examples:

int a,b,c,d,e,f,g;
int i,ii,iii,iiii,zhengshu,jieguo;
char h[1000];
float a=0;

Good examples:

#define MAX_LEN 255


int DistributeGroup[MAX_LEN]; // Array called "Distribute Group"
char UserName[MAX_LEN]; // String called "User Name"
int RandomSeed = 233; // Integer called "Random Seed"

Debugging / Testing
PS: In ECE2800J, debugging is not equivalent to testing, but for now it may not matter that
much.
Understand the specification
Identify the required behaviors
Write specific tests
simple inputs -- normal for problem
boundary conditions -- at the edges of what is expected, or formed to exploit some
detail of implementation
nonsense -- cases that are clearly unexpected
Know the answers in advance
Include stress tests -- large and long test cases

See problem 2

Functions
Packing a bunch of instructions together.

Declare before use! (similar to variable)

To declare a function:

prototype and implementation: returntype function_name(data_type


input_argument){<content of your function>}
Forward Declaration: Prototype first, then do then implementation.

There are many library functions in C, like printf and scanf in stdio.h.

void function: without return value. So directly print or modify the input value.

void parameter: usually just ignore it.


int main(void){ } = int main(){ }

Method Copy Created Modify Original Variable Syntax


Call by value Yes No modify(a)
Call by reference No Yes modify(&a)
Call by pointer No Yes modify(&a)

void swap(int a, int b);

int main(){
int a = 2;
int b = 3;
swap(a, b);
printf("%d ", a);
printf("%d", b);
}

void swap(int a, int b){


int temp = a;
a = b;
b = temp;
}
//However, nothing happens to a and b.

Function has its own "territory"


void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}

Variables a and b here are local variables


When passing parameters to swap() , the function will copy the value of the
parameters to the local variables a and b
Therefore, the value of variables outside the function will not be changed

Reference
101FA24 RC2
101SU24 HW4
[Link]

You might also like