0% found this document useful (0 votes)
8 views

Lab. 01

The document discusses various C programming concepts including variables, constants, input/output functions, arithmetic, relational and logical operators. It provides examples and explanations of data types, variable naming rules, and how to declare and initialize variables and constants in C.

Uploaded by

drwa6bp
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)
8 views

Lab. 01

The document discusses various C programming concepts including variables, constants, input/output functions, arithmetic, relational and logical operators. It provides examples and explanations of data types, variable naming rules, and how to declare and initialize variables and constants in C.

Uploaded by

drwa6bp
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/ 21

Content:

1. Variables in C
2. Constants in C
3. Display in C
4. Inputs in C
5. Expressions & Operators

C Programming 6. Arithmetic Operations


7. Relational Operations
8. Logical Operators
9. Bitwise Operators
Language 10. Assignment Operators
11. Miscellaneous Operators
12. X++ Vs ++X

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 1


➢ Variables in C
• A variable is nothing but a name given to a storage area that our programs can manipulate.

• Each variable in C has a specific type (data type).

• Syntax:

<Data_Type> Variable_Name ;

<Data_Type> Variable_Name = Initial_Value;

• Examples:

char X , Y = 10; char Speed = 57; char Count;

• If there is no initial value, it will contain garbage value.


Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 2
➢ Variables in C
• A variable name can have letters (uppercase and lowercase), digits and underscore only.

EX: VAR , vAr5 , var_5

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

EX: _var , var5 , var_5 , 5_var

• There is no rule on how long a variable can be. However, only the first 31 characters are

checked. auto double int struct break else long switch


case enum register typedef char extern return union
• Not be keyword. continue for signed void do if static while
default goto sizeof volatile const float short unsigned

C89 keywords
Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 3
➢ Variables in C

• Variable Name Styles:


o Camel case notation:
EX: MaxLength
o Lower upper:
EX: maxLength
o Under bar:
EX: max_length

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 4


➢ Variables in C

• Datatype:
o Determines amount of memory (bytes) a variable requires and range of valid
values that can be stored.

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 5


➢ Variables in C
Datatype Size (Bytes) Range Format
char 1 0 to 255 (compiler-based) %c
char signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
short int 2 0 to 65,535 (compiler-based) %hd
Short int signed short int 2 -32,768 to 32,767 %hi
unsigned short int 2 0 to 65,535 %hu
int / long int 4 0 to 4,294,967,295 (compiler-based) %d or %i
int signed int / signed long int 4 -2,147,483,648 to 2,147,483,647 %d
unsigned int / unsigned long int 4 0 to 4,294,967,295 %u
long long int 8 0 to 4,294,967,295 (compiler-based) %lld
long long
signed long long int 8 -2,147,483,648 to 2,147,483,647 %lld
int
unsigned long long int 8 0 to 4,294,967,295 %llu
float 4 1.2E-38 to 3.4E+38 %f

floating double 8 1.7E-308 to 1.7E+308 %lf

long double 16 Eng.Ahmed Abdel-Gawad


Microprocessor -Lab.1- 3.4E-4932 to 1.1E+4932 6 %Lf
➢ Constants in C

• A constant is a value or an identifier whose value cannot be altered in a program.

• Syntax:

const<data_type> variable_name =value;

• Example:

const char maxLength=50; OR char const maxLength=50;

const float pi=3.14; OR float const pi=3.14;

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 7


➢ Displaying ( printf() )

• Syntax:

printf(“ String Format ”, Variable_Name);

• Example:

int age = 10;

printf(“The age is %d years “,age);

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 8


➢ Inputs ( scanf() )

• Syntax:

scanf(“Format”, &Variable_Name);

• Example:

char age;

printf("Enter your age: ");

scanf("%d", &age);

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 9


➢ Expressions & Operators :

• Expression: is a statement that uses logical operators to link identifiers to create a

true/false result or numeric value.

o Example:

Z = X + Y / I; → Expression.

Z,X,Y,I → Operands.

=,+,/ → Operators.

• Operators: Symbols that indicate to the compiler which operation to perform using

surrounding identifiers. Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 10


➢ Arithmetic Operations :

• Example: Operator Description


+ Addition
5/2=2.
- Subtraction
5%2=1. * Multiplication
/ Division
6%2=0.
% Reminder
11%3=2.

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 11


➢ Relational Operations :
• Here, the result will be TRUE (1) or FALSE (0).

Example: Operator Description Associativity


== Equal to
int X =10 , Y=0 , result;
!= Not Equal to
result = X > Y;
> Greater than
printf(" Result = %d \n",result);
< Lesser than
L to R
result = Y > X; >= Greater than or Equal
printf(" Result = %d \n",result); <= Lesser than or Equal

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 12


➢ Logical Operators :
Operator Description Associativity
! Logical Not R to L
&& Logical AND
L to R
|| Logical OR

X Y || X Y &&
X ! F F F F F F
F T F T T F T F
T F T F T T F F
T T T T T T

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 13


➢ Logical Operators :

Example:

int X =10,Y=0,result;

result = !(Y>X) && (X>Y);

printf(" Result = %d \n",result);

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 14


➢ Bitwise Operators :
• These operations applied at level of bit.
• They also applied only with integer data types.

Operator Description Associativity


X Y & X Y | X Y ^
& AND
0 0 0 0 0 0 0 0 0
| OR
0 1 0 0 1 1 0 1 1
^ X-OR
~ Complement
L to R 1 0 0 1 0 1 1 0 1
1 1 1 1 1 1 1 1 0
<< Shift Left
>> Shift Right X ~
0 1
Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 1 0 15
➢ Bitwise Operators :

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 16


➢ Bitwise Operators :

Example:
int num1 = 10;
int num2 = 1;
int result;
result = num1 & num2;
printf("num1 & num2 = %d \n", result);
result = num1 | num2;
printf("num1 | num2 = %d \n", result);
result = num1 ^ num2;
printf("num1 ^ num2 = %d \n", result);

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 17


➢ Assignment Operators : Operator Description Associativity
Assignment
=
Operator
+= Add and Assign
Example:
-= Subtract and Assign
num += 5; → num = num + 5;
*= Multiply and Assign

num /= 2; → num = num / 2; /= Divide and Assign


L to R
%= Modulus and assign
num %= 2; → num = num % 2;
<<= Left shift and assign
num *= 2; → num = num * 2;
Right shift and
>>=
assign
&= AND and assign
^= X_OR and assign
|= OR and assign
Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 18
➢ Miscellaneous Operators :

Operator Description Example


sizeof() Get size of data type. sizeof(int)
int var;
& Get address of a variable.
&var
* Get content of a variable. *var

?: Conditional Expression. z=x>y?1:0;

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 19


➢ X++ Vs ++X :

Operator Example Explanation

Increment/decrement a by 1, then use the new value


++/--(prefix) ++x / --x
of a in the expression in which a resides.

++ / -- Use the current value of a in the expression in which


x++ / x--
(postfix) a resides, then increment/decrement a by 1.

Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 20


➢ X++ Vs ++X :
• Example:
int num=5;
printf("num=%d\n", num);
printf("num=%d\n", num++);
printf("num=%d\n", num);
• Example:
int num=5;
printf("num= %d\n", num);
printf("num= %d\n", num--);
printf("num= %d\n", --num);
Microprocessor -Lab.1- Eng.Ahmed Abdel-Gawad 21

You might also like