0% found this document useful (0 votes)
3 views17 pages

C Language Variables and Instructions Guide

This document describes the basic concepts of the C language such as variables, constants, comments, arithmetic and logical operators. It also presents the assignment, display, and input instructions in C.

Translated by

ScribdTranslations
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)
3 views17 pages

C Language Variables and Instructions Guide

This document describes the basic concepts of the C language such as variables, constants, comments, arithmetic and logical operators. It also presents the assignment, display, and input instructions in C.

Translated by

ScribdTranslations
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

Chapter 2: The instructions

simple
Module: C Language
Course led by [Link]

1
I. Notion of variable
1- Definition:
A variable is a memory cell or a set of memory cells.
able to contain a data of a certain type at a given time.

2- Declaration:
Declaring a variable is done as follows:
Type name [= initial value];

3- Exemples:
Int x; int y=5; char a, b;
2
I. Notion of variable
With
Type: the type of the variable that can be:
Int or long: Integer, this type is designed to hold values
whole.
Char: Character, this type is designed to hold a character.
Float or Double: Real, this type is designed to hold values
real.
Or other types of course.

3
I. Notion of variable
Remarks:
The semicolon marks the end of each instruction, so it is mandatory;
Initializing a variable upon declaration is not mandatory;
The C language is case-sensitive, so uppercase and lowercase letters
are different;
The keyword unsigned specifies that the value that will be contained by the variable
a positive serum;
A variable can be declared anywhere in the program and
no to a well-reserved place.

4
II. Notion of constant
1- Definition:
Is a variable whose value remains unchanged during execution of the
program.

2- Declaration:
Const type name = value;

3- Example:
Const int x=3;

5
III. The comments
A comment starts with /* and ends with */.
For example, /* This is a comment */

We cannot nest comments. When we put in


to comment a piece of code, it is therefore necessary to ensure that
this one does not contain any comments.

Comments are ignored during the execution of the program.

6
IV. The assignment instruction
In C, assignment is a full-fledged operator. It is symbolized
by the sign =.
Its syntax is as follows:
variable=expression;
Example:
I = 5;
X = i + 1;

7
V. The display instruction
1-Displaying simple text:
for this, we use the following syntax:
Print("Text to display");
2- Displaying the value of a variable:
We display the value of a variable according to the following instruction:
Print("Symbol", variable_name);

The symbol is made up of the sign '%' followed by a letter referring to the
type of the variable whose value we need to display.
8
V. The display instruction

Example:
Symbol(%letter) Corresponding type

%d Int

%c character
%lf Long double
%ld Long int
%u Unsigned int in decimal
%o Unsigned int in octal
%x Unsigned int in hexadecimal

9
V. The display instruction
Combined display between text and value:
Just combine the two types of displays and you get the display.
heterogeneous.

Print the value to display: %letter, variable;


Note:
Accents are not tolerated in the display; at runtime they will be
replaced by insignificant symbols

10
V. The display instruction
Remarks:
We can display multiple variable values at the same time as follows:

Printf("%letter1 , %letter2,…….,%letter_n", var1, var2, .., var_n);

And we can combine the display of multiple variable values with text.
We have the following special characters:
new line carriage return
horizontal tabulation Page break
• vertical tabulation a warning signal
backward

11
VI. The reading instruction
It corresponds to assigning a value to a variable.
This operation is carried out using the following instruction:

SCANF (" %letter", &variable_name);


It is important to know that the symbol & preceding a variable name serves to
indicate the address of the variable in memory.

We can read several values from several variables of different types such as
suit
SCANF ("%letter1 %letter2 %letter_n", &var1, &var2, .., &var_n);
Note: The space between variables is important.

12
VI. Reading instruction
Examples:
printf("Displaying i: \n");
printf("%d \t %u \t %o \t %x", i, i, i, i);

printf("enter an integer in hexadecimal form i = ");


scanf("%x", &i);

13
VII. The Arithmetic and Logical Operators
Operators Explanation
Arithmetic The addition '+'
The subtraction '-'
These are the basic arithmetic operations
The Multiplication '*'
The division '/'

The Module ‘%’ It returns the remainder of the integer division of the first operand by the second.

Allows assigning the result of an operation to a variable or simply the value of a


The assignment '='
variable to another
Of Comparison The equality '==' It allows to check the equality of two operands (variables or values)
It allows you to check if the value on the left is strictly less than, less than, or equal to.
<,<=
to the right value.
It allows you to check if the left value is strictly greater than, greater than or
>,>=
equal to the value on the right.

!= It verifies whether the value on the left is different from the value on the right.
! Non-logic allows for obtaining the negation of an expression.
Logics && The 'et' logic allows the conjunction of two expressions.
|| The or logic allows the disjunction of two expressions. 14
VII. The Arithmetic and Logical Operators
Note:
There are mathematical functions that we often use, but first we need to call upon the
math.h library through the include directive <math.h>, these functions are:

Function Explanation

fabs It takes a single variable or value as a parameter and returns its integer value.

Pow It takes two parameters in the following form pow(number, power) and returns as
result the power of the number.

sqrt It takes a single parameter and returns its square root.

The functions They are the known trigonometric functions, they take a number of parameters such that they
trigonometric are mathematically defined and they are mainly (sin, cos, tan, arcsin....).

The other functions Also known are the other mathematical functions that one might want to use,
same thing for the parameters and they are (exp, log, log10, ………..).
15
VII. The Arithmetic and Logical Operators
There are also shortcuts for writing certain operations such as
what:

Operation Explanation

Increment It consists of adding 1 to the value of the variable.


variable = variable + 1, and we shorten it to variable++

decrement We subtract 1 from the value of the variable and write: variable--

arithmetic operations Var1 operation=Var2, here one of the operands receives the result.

Brackets and parentheses also define the order of


priority, the same as in mathematics.
16
VIII. Example of C program
#include <stdio.h>
main()
{
int i;
printf("enter an integer in hexadecimal form i = ");
scanf("%x", &i);
printf("i = %d\n", i);
}
If we enter the value 1a on the keyboard, the program displays i = 26.

17

You might also like