[go: up one dir, main page]

0% found this document useful (0 votes)
14 views52 pages

4 - Get Started With C (F)

Uploaded by

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

4 - Get Started With C (F)

Uploaded by

henryshmrri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

< >

Get Started
with C
Source: W3Schools
Get Started with C < >
< >

C SYNTAX
C Syntax < >
C Syntax < >
In line 1, #include <stdio.h> is a header file library that lets us work
with input and output functions, such as printf() (used in line 4).
Header files add functionality to C programs.

In line 2, a blank line. C ignores white space. But we use it to make the
code more readable.

In line 3, main( ). This is called a function. Any code inside its curly
brackets {} will be executed.
C Syntax < >
In line 4, printf() is a function used to output/print text to the screen.
In our example it will output "Hello World!".

Note that: Every C statement ends with a semicolon “;”.

In line 5, return 0 ends the main( ) function.

In line 6, the curly bracket. Do not forget to add the closing bracket } to
actually end the main function.
< >

C OUTPUT
C Output (Print Text) < >
To output values or print text in C, you can use the printf() function:
C Output (Print Text) < >
You can use as many printf() functions as you want. However, note
that it does not insert a new line at the end of the output.

Output
C Output (Print Text) < >
But using \n , we could add new lines.
C Output (Print Text) < >
What is \n exactly?

The newline character (\n) is called an escape sequence, and it forces


the cursor to change its position to the beginning of the next line on
the screen. This results in a new line.
< >

C VARIABLES
What is C Variable? < >
Variables are containers for storing data values, like numbers and
characters. These are the names you give to computer memory
locations which are used to store values in a computer program.

They are called variables because the represented information can


change but the operations on the variable remain the same.

variable

Note: The value of a variable can be changed, hence the name variable.
What is C Variable? < >
Rules in naming variables:

1. A variable name can only have letters (both uppercase and


lowercase letters), digits and underscore.

2. The first letter of a variable should be either a letter or an


underscore.

3. There is no rule on how long a variable name (identifier) can be.


However, you may run into problems in some compilers if the
variable name is longer than 31 characters.
What is C Variable? < >
Note: You should always try to give meaningful names to variables.
For example: firstName is a better variable name than fn.

C is a strongly typed language. This means that the variable type


cannot be changed once it is declared. For example:

Here, the type of number variable is int . You cannot assign a


floating-point (decimal) value 5.5 to this variable.
< >

C DATA TYPES
What is Data Type? < >
In C programming, data types are declarations for variables. This
determines the type and size of data associated with variables.
For example,

Another example, many programming languages use the data


type string to classify text, integer to identify whole numbers
and floating point to designate numbers with decimal points.
What is Data Type? < >
DIFFERENT DATA TYPES:

1. Integer - stores integers (whole numbers), without decimals, such


as 123 or -123.

2. Float (floating point) - stores floating point numbers, with


decimals, such as 19.99 or -19.99.

3. Character - stores single characters, such as 'a' or 'B' . Char values


are surrounded by single quotes.
What is Data Type? < >
4. Boolean - is a data type with two possible values:
true (1) or false (0). The two values help represent
truth conditions found in logic control structures.

5. String - is a sequence of characters enclosed between the double


quotes.
What is Data Type? < >
Data Type Symbol Used for Examples

Integer int Stores whole numbers, without 7


decimals 12
Float (Floating point) float Stores fractional numbers, containing 3.15
one or more decimals. Sufficient for 9.06
storing 6-7 decimal digits 00.13
Character char Stores a single ‘a’ ‘A’ ‘B’
character/letter/number, or ASCII
values
Boolean bool Representing logical values; conditions TRUE
FALSE
String string Alphanumeric characters hello world
Alice
Double double Stores fractional numbers, containing
one or more decimals. Sufficient for
storing 15 decimal digits
< >

DECLARING
VARIABLES
Declaring (Creating) Variables < >
To create a variable, specify the type and assign it a value:

Where type is one of C types (such as int ), and variableName is the


name of the variable (such as x or myName). The equal sign is used to
assign a value to the variable.
Declaring (Creating) Variables < >
TWO WAYS TO DECLARE VARIABLES:
1. Create a variable called myNum of type int and assign the value 15
to it.

2. You can also declare a variable without assigning the value, and
assign the value later:
< >

OUTPUT
VARIABLES
Output Variables < >
Generally, to output values / print text, use printf() function.

In many other programming languages (like Python, Java, and C++),


you would normally use a print function to display the value of a
variable. However, this is not possible in C:

Note: To output variables in C, you


must get familiar with something
called "format specifiers".
What is Format Specifiers? < >
Format specifiers are used together with the printf() function
to tell the compiler what type of data the variable is storing. It
is basically a placeholder for the variable value.

A format specifier starts with a percentage sign %, followed by


a character.
What is Format Specifiers? < >
For example, to output the value of an int variable, you must use the
format specifier %d or %i surrounded by double quotes, inside the
printf() function.
What is Format Specifiers? < >
To print other types, use %c for char and %f for float:
What is Format Specifiers? < >
To combine both text and variable, you can use the following:

To print different types in a single printf() function, you can use the
following:
What is Format Specifiers? < >
What is Format Specifiers? < >
Set Decimal Precision

You have probably already noticed that if you print a floating-point


number, the output will show many digits after the decimal point:
What is Format Specifiers? < >
To remove the extra zeros, use dot ( . ) followed by a number that
specifies how many digits that should be shown after the decimal point.
< >
CHANGE
VARIABLE
VALUES
Change Variable Values < >
To assign a new value to an
existing variable, it will overwrite
the previous value:

To assign the value of one variable


to another:
< >

ADD VARIABLES
TOGETHER
Add Variables Together < >
To add a variable to another variable, use the + operator:
< >

DECLARE MULTIPLE
VARIABLES
Declare Multiple Variables < >
To declare more than one variable of the same type, use a comma-
separated list:

You can also assign the same value to multiple variables of the same
type:
< >

COMMENTS IN
C
Comments in C < >
Comments can be used to explain code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code.

Comments can be:


• singled-lined
• multi-lined
Comments in C < >
Single-line Comments
- Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the
compiler (will not be executed).
Comments in C < >
Multi-line Comments
- Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler.


< >

BASIC OPERATORS
IN C
Basic Operators in C < >
Operators
- Operators are used to perform operations on variables and
values.

C divides the operators into the following groups:


• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Basic Operators in C < >
Arithmetic Operators
- are used to perform common mathematical operations.
Basic Operators in C < >
Assignment Operators
- are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign


the value 10 to a variable called x:
Basic Operators in C < >
The addition assignment operator (+=) adds a value to a variable:

The output is 15.


Basic Operators in C < >
Comparison Operators
- are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and
make decisions.

The return value of a comparison is either 1 or 0, which means true


(1) or false (0). These values are known as Boolean values.
Basic Operators in C < >
< >

USER INPUT
IN C
User Input in C < >
In C programming, scanf() is one of the commonly used function to
take input from the user. The scanf() function reads formatted input
from the standard input such as keyboards.
User Input in C < >
Example:

You might also like