0% found this document useful (0 votes)
69 views53 pages

Everything You Need To Know To Start With C

C is an imperative programming language where all code must be inside functions. Source code files are compiled into executable files. Comments begin with /* and end with */. Variables have data types like int, char, float. Arrays store multiple values of the same type sequentially in memory. Structures group related variables under one name. Functions perform tasks, take parameters, and return values. The main function is the program entry point. Code blocks are sections of code grouped by curly braces. Expressions include arithmetic, assignments, comparisons, and logical operators. Control structures like if/else and loops alter program flow. Functions end with return statements. Source code is executed sequentially, with functions calling each other.
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)
69 views53 pages

Everything You Need To Know To Start With C

C is an imperative programming language where all code must be inside functions. Source code files are compiled into executable files. Comments begin with /* and end with */. Variables have data types like int, char, float. Arrays store multiple values of the same type sequentially in memory. Structures group related variables under one name. Functions perform tasks, take parameters, and return values. The main function is the program entry point. Code blocks are sections of code grouped by curly braces. Expressions include arithmetic, assignments, comparisons, and logical operators. Control structures like if/else and loops alter program flow. Functions end with return statements. Source code is executed sequentially, with functions calling each other.
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
You are on page 1/ 53

Everything you need to

know to start with C


while(98) printf(“#cisfun\n”);
What is C?
C is an imperative (procedural) language.

All the code has to be inside a function.

C files are source code of your program.

You have to compile C files with a compiler (for instance gcc) to create an
executable file.
Comments
#cisfun
Comments
Begins with /* and ends with */

Can be inserted anywhere a white-space character


is allowed

Comments don’t nest

Use comments to document your code


Variables
#cisfun
Data types | Integer types (on most 64bits computers)
Type Storage size Value range

char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

short 2 bytes -32,768 to 32,767

unsigned short 2 bytes 0 to 65,535

int 4 bytes -2,147,483,648 to 2,147,483,647

unsigned int 4 bytes 0 to 4,294,967,295

long 8 bytes −9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

unsigned long 8 bytes 0 to 18,446,744,073,709,551,615


Declaration
Variables always have a type.

Syntax:

Example:

Aewe\

Names of variables: [a-zA-z_][a-zA-Z_0-9]*


Arrays
A succession of items of the same type in memory

Declaration:

Where number_of_items is a constant number (not a variable)

Example:
Structures
A complex data type declaration that Example
defines a physically grouped list of
variables to be placed under one name Creation of a new type called new_struct
in a block of memory

Creation and declaration

Declaring a variable of type new_struct


Arrays and structures
Arrays of structures

Structures with structures elements


Functions, Programs
#cisfun
Functions
A program is a collection of functions

Functions:

● Sequence of program instructions that perform a specific task,


packaged as a unit
● May take arguments as input information
● Compute something
● May return a result
● Functions can call functions

Parameters and return values must have a type


Syntax

Example
The entry point | main
Program starts with the entry point. In C it’s the main function

From the main function you can call other functions

When main returns, the program stops


Blocks
Blocks or code blocks are sections of code which are grouped together. Blocks
consist of one or more declarations and statements.

Blocks are delimited by curly braces { and }

Syntax
Blocks
Declarations Statements

Declare variables Executed in order

Contains:

● Blocks
● Instructions (assigning values,
computing)
● Control structures (conditional
statements, loops)
Blocks | Example
Instructions, Expressions
#cisfun
Instructions, expressions
Expression, followed by ;
; (semicolon) is a statement terminator (indicates the end of one logical entity)

Expressions always have a value

Types of expressions:

● Basic expressions (i.e. arithmetic operations)


● Affectations (i.e. assigning a value to a variable)
● Comparisons (i.e. checking if a variable is less than a number)
● Logical operators (i.e. checking two things at the same time)
● Binary operators
Basic expressions
Basic expressions: Numbers, variables, arithmetics, function calls

Numbers can be written in decimal (32), octal (032) or hexadecimal (0x32)

The value of a variable is the value it contains

The value of a function call is its return value (after execution)


Characters
‘letter’ (single quotes)

The value of a character is its ASCII code (man ascii)

Example:
Strings
“string” (double quotes)

Evaluates to the address in memory of the first character of the string

Example:

When using strings, the computer creates an array of chars, containing the same
number of cells as the length of the string + 1, and then fills the array with the
ASCII codes of each letter of the string. In the extra cell (the last one) the
computer will store the ASCII code of the special character ‘\0’
Arithmetic operators
+ addition, - subtraction, * multiplication, / division, % modulo (integer remainder)

expression operator expression

++ increment, -- decrement
Basic expressions | example
Using parentheses with operators
Affectations
Change the content of a variable (update the value in memory)

Syntax:

Examples:

If the expression is another affectation, then its value is the value affected to the
previous variable
Affectations | elements of arrays and structures
Syntax:

Examples:
Variables assignment | example
Comparisons
< Less than expression operator expression
<= Less or equal to
The value of a comparison is 0 if false, and
== Equal to
something else if true.
>= Greater or equal
to Examples:
> Greater than

!= Different than
Logical operators
|| OR, && AND, ! NOT

expression1 || expression2 - if one of the 2 expressions is true, then the whole


expression is true

expression1 && expression2 - if one of the 2 expressions is false, then the whole
expression is false

!expression - if the expression is false, then the whole expression is true. If the
expression is true, then the whole expression is false.

The value of the whole expression is 0 if false, something else if true


Logical operators
AND && OR || NOT !

0 1 0 1
0 1
0 0 0 0 0 1
1 0
1 0 1 1 1 1
Can you solve this?
Logical operators | examples
Binary operators
Bitwise operations

| OR, & AND, << LEFT SHIFT, >> RIGHT SHIFT, ^ XOR
~ NOT

expression operator expression


~expression
Can you solve this?
More affectations: Compound assignment operators
Perform the operation specified by the additional operator, then assign the result
to the left operand

Example:
expression1 += expression2, is equivalent to:
expression1 = expression1 + expression2

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Comma operator
expression1, expression2

The value of the whole expression is the value of expression2

Example:

Don’t use this at home


Ternary conditional
expression1? expression2: expression3

Evaluates to expression2 if expression1 is true, and to expression3 otherwise

Example:
sizeof
Unary operator that evaluates to the size in memory of the variable or type, in
bytes

Example:
&
&var_name

The address in memory of the variable var_name

Example:
Can you solve this?
Control structures
#cisfun
if if … else

If the expression is true (the value of


the expression is not 0) then the
block is executed If the expression is true, then block1 is
executed. Otherwise, block2 is executed
if … else | examples
while loops

The while loop lets you repeat a block until a specified expression becomes false.
for loops
The for statement lets you repeat a block a specified number of times. The block
of a for statement is executed zero or more times until an optional condition
becomes false. You can use optional expressions within the for statement to
initialize and change values during the for statement's execution.

Initialize, conditions and


update are optional.
Can you solve this?
return

Ends the function and returns to the calling function

If used with an expression, the expression becomes the return value of the
function

The type of expression must match the return type of the function

Any code after return will never be executed


return | example
{ } syntax
Example
#cisfun
Source code flow of execution
1) Start by the main() function
2) Allocate 4 bytes for i
3) Allocate 4 bytes for j
4) Allocate 4 bytes for res
5) Set the value 2 to i
6) Set the value 4 to j
7) Call the function add():
a) Allocate frame memory for the function
b) Copy the value of i and set to a
c) Copy the value of j and set to b
8) Allocate 4 bytes for sum
9) Compute a + b expression
10) Set the value of the expression a + b to sum
11) Return the value of sum and go back to the main()
12) Destroy the function add() in memory
13) Set the result (return value) of add() to res
14) Call the function printf() (standard library function)
15) return 0 = stop the program
return (0);
#cisfun

You might also like