#include - preprocessing directive: instructions to the compiler that our program will need to include
main () - main entry point of the program, must have only one.
{ - open curly brace to begin statements inside the main.
} - ends statements
printf(“hello world.”);
example of program statement
allows us to display strings to the screen
Note:
C programming is case-sensitive
spacing and indention do not affect execution. only makes it more readable
The main ()
C defaults to int. C is not strict on return statement.
void main () - no need for return statement
Comments
// - single line
/* */ - multi-line
inserting explanations
hide code
Special character: \n, \t
\n - adds new lines to the display
\t - adds 5 empty spaces instead of new line
Displaying variables - DATA TYPES
Integer - whole number positive or negative and 0
Float - rational numbers (decimal) except 0 but 0.0 included
Character - single symbol ‘ ‘
String - set of characters “ “
Data type | Data format |
integer | %d, %i |
float | %f |
character | %c |
string | %s |
Variables
placeholders for values.
must not have the same name as predefined words in the language
cannot have numbers as first character
no spaces or special characters
unique within the same program
assigning variables - left gets right
Arithmetic operators
addition, subtraction, multiplication, division, modulo
precedence - parentheses, multi and divi, add and subtract
assignment ops
+=, -=, *=, /=, %=
constant declaration #define <variable> <value>
increment decrement: ++ --
a++ postfix compiler sees ‘a’ first
++a prefix compiler sees ++ first
scanf
string: contains data format “%d” and argument: corresponds data type of variable &num
Making programs that solve problems
G – Given
R – Required
E – Equation
S – Solution
A – Answer
oval - terminal, start or end
rectangle - process symbol
diamond - decision symbol
parallelogram - input/output symbol
arrows - connect the symbols, show relationship
connectors
On-page connector - replace long lines on flowchart page, circle
Off-page connector - target is on other page, five-pointed polygon
used in pairs
first connectors is the end, second is the continuation
If
specifies condition and executes statements only if condition is true
If-else
condition is true, first part executes, otherwise second part executes\
Else if
specify new conditions if first condition is false
nested if
multple conditions apply
condition depends on another condition
shorthand ternary
takes less space writes in shortest way possible
first is comparison, second is result if true, third is result if false
Switch
usefule if we have to choose between n numbers of alternatives
execute one code block among many alternatives, if-else-if ladder statement
Loops
used to repeat block of code until condition is met
entry controlled
for and while
exit controlled
evaluated at the end of loop
do-while
•Start
•Stop
•Repeating Action(s)
•condition to continue
•Process for the loop to continue and eventually reach the “stop”
Do-While
the body of do is executed once before testing expression
For
for (initializationStatement; testExpression; updateStatement){
// statements inside the body of loop
}
Jumping Statements
Jumping statements are generally used within a branching statement. used to jump out of loops
break
ends the loop immediately when it is encountered.
terminates only the nested loop if used in nested loops
Continue
used to skip the statements if we dont want to write any statement inside a condition.
Goto
can be used anywhere unconditionally in between the program. Jump the control anywhere in between the program
Exit
use to terminate a program
gotoxy sets curosr to a pos
Functions
block of statements to perform tasks
instead of writing a block of code again and again, we can create a function and call it as many times as required.
when the function is called the controller is passed to the function then return back from where it was called
reduces the size of a program