Session 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programming Fundamentals for

2013/2014
Engineers Lab

Lab Session # 1 – Introduction to C Language

ALQUDS University
Department of Computer Engineering

Objective:
Our objective for today’s lab session is to introduce you to the C programing language .
The materials supplied during this period will teach you to:
1. Starting with simple program in C language.
2. Learn about the variable and data type .
3. Study the printf() function.
4. Study the scanf() function.
5. Investigate the different forms of printf() function.

Experiment 1.1

Step1: Simple program in C

#include <stdio.h>
int main()
{
printf (“Hello World!”);
return 0;
}
The program's executable code goes inside main, which represents the main program.
Inside the main each statement must end with semicolon.
printf is a function from the standard I/O library.
.h  header files''

Step2: Write a Simple program to display the first and family name on Screen.
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------.

1
Experiment 1.2

Variables :
- As a programmer, you will frequently want your program to "remember" a value. For example, if
your program requests a value from the user, or if it calculates a value, you will want to remember
it somewhere so you can use it later.
- All variables in C must be explicitly defined before use.
- Variable Names, have one to eight characters, every variable name in C must start with a letter;
the rest of the name can consist of letters, numbers and underscore characters.
- It is conventional to avoid the use of capital letters in variable names. These are used for names of
constants.
- Declaring variables: each variable declared with a name and a type.
Example …
int b;
this line says, "I want to create a variable called b that is able to hold one integer value".
A variable name is "b" and a variable type is integer.
- Scope of variable: A variable can be either of global or local scope. A global variable is a variable
declared in the main body of the source code, outside all functions, while a local variable is one
declared within the body of a function or a block.

Data types:
- When programming, we store the variables in our computer's memory, but the computer has to
know what kind of data we want to store in them, since it is not going to occupy the same
amount of memory to store a simple number than to store a single letter or a large number
- C uses the following standard variable types …
- int  integer variable
- short  short integer
- long  long integer
- float single precision real (floating point) variable
- double  double precision real (floating point) variable
- const  Constant
- void  no type.
- bool  true or false
- char  character variable (single byte), contained in single quotation.
- String  string is actually stored as an array of characters
- unsigned  an integer with no negative range.

2
printf function:
printf use to display the statements or the value of variables in screen .

printf (“statements” );
printf (“statements =%variable type symbol ” ,variable name );

Step 1: Compile and execute this program and record the results below:

#include <stdio.h>
int main()
{
int num = 2;
float price = 1.89;
char flavor = ‘c’;
printf(“the value of num=%d”, num);
printf(“the value of price=%f”, price);
printf(“flavor=%c”, flavor);
return 0;
}
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------.

Step 2: Replace the three print statement in Step 1 with the one line...
printf("the value of num=%d the value of price=%f the value of
flavor=%c",num,price,flavor);
Compile and execute the modified program and record the results.
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------.
Step 3: Modify the code in Step 2 so that the following sentence is printed using declered
variable. There are several ways to do this. Compile and execute the program. Record your
solution(s) and the results.
2 scoops of ice cream for this price $1.89 .
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------.

3
Step 4: Modify the program by replacing the statement
int num = 2;
with two statements
int num;
num=2;
Compile the program and record the results of this modification.
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------.
Step 5: Modify the statement in Step 2 with this line...
printf("the value of num=%d \n the value of price=%f \t the value of
flavor=%c\a",num,price,flavor);

Compile the program and record the results of this modification.


---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------.
Step 6: What does the( \n \t \a \") character combination mean?
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------.
Step7: printf("the value of num=%d \n\n the value of price=%f \tthe value of
flavor=%c\aaa",num,price,flavor);

Pridecte the results then compile the program and record the results of this modification.
---------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------.

Experiment 1.3

Step 1: check the error then compile and execute this program and record the results below:
#include <studio.h>
int main()
{
int x=10;
printf("X = %d\n", x);
printf("X = %1d\n", x);
printf("X = %5d\n", x);
printf("X = %5.4d\n", x);
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------.

4
Step 2: check the error then compile and execute this program and record the results below:
Main()
{
Float x=10.352;
Printf("X = %f\n",x)
Printf("X = %.4f\n",x);
Printf("X = %6.2f\n",x);
Printf("X = %7f\n",x);
}
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------.
Step 3:How many digits are displays by %f by default ?
----------------------------------------------------------------------------------------.

Experiment 1.4
Our programs are more interesting when the program's user can enter information from
the keyboard. For this pourpus we the c language provide scanf() function .
Scanf(“ %d,f,c,o.... ”,&variable );

Step 1: Compile and execute this program . In response to the first request enter 10, and in
response to the second request enter 2. Record the results.

#include <stdio.h>
int main()
{
int a, b, area;
printf("Enter the length of the rectangle:");
scanf ("%d", &a);
printf("Enter the width of the rectangle:");
scanf("%d", &b);
area = a * b;
printf("Area = Length * Width, %d * %d = %d\n", a, b, c);
return 0;
}
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------.
Step 2: Execute the program again, this time responding with 22 and 2.5. Record what
happens .
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------.

5
Step 3: Chang the int a,b in the first statement in main to be float then chang any needed statements
in this program , Execute the program again, this time responding with 8 and 2.5. Record
what happens .
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------.
Step 4: Write a program that calculates the perimeter and area of a rectangle whose integer
dimensions are supplied by the user.(Write the algorithm of this question befor c language
cod)
Algorithm C program
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------

Step 5: Write a program that calculates the circumference and area of a circle whose radius
is supplied by the user. (Write the algorithm of this question befor c language cod)
Algorithm C program
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- ------------------------------------------------------
---------------------------------------------------------- -----------------------------------------------------

6
Post lab#1

1- Write a program that prints the message


“ My name is Hector, I am
a vector.
I am the subject of many a
physics lecture!”
a. all on one line
b. on two lines
c. on eight lines
d. inside a box drawn with asterisks.

2- Write a program that calculates a Celsius temperaturefrom the Fahrenheit


temperature supplied by the user. Use the formula C = 5/9(F - 32).

3-Write a program that calculates a Fahrenheit temperature from the Celsius


temperature supplied by the user. Use the formula F = 9/5 C + 32.

4- Write a program to read a number as decimal and show it in hex.


Your output should take the form:
Right! 'Number read' decimal is 'number in hex' hex..

You might also like