cd csc2400In the directory csc2400, create another directory called Lab1. Change your working directory to Lab1 using the Unix command
cd Lab1Do the exercises below.
pwdThe directory listed should end in csc2400/Lab1.
Step 2. Invoke the pico editor using the command
pico test1.c
and type in the following program. Use the menu listed at the bottom of the pico editor for help
on text editting.
#include <stdio.h>
int main()
{
float x;
int y;
printf("\n Enter a float, then an int: \n");
scanf("%f %d", &x, &y);
printf("You have entered integer %d and float %f \n", y, x);
return 0;
}
Save and exit your program (CTRL-O, then CTRL-X).
Here is all you
need to know about printf and scanf.
Step 3. Open another terminal on tanner
(follow these login
steps).
In this new terminal window, compile your program from Step 2 using the gcc compiler
gcc test1.c
What files are created? Run the command
ls -l
to list all files and associated permissions ('r' stands for read,
'w' stands for write and 'x' stands for execute). Run the executable you find.
Step 4.
Compile again your program test1.c using the command:
gcc -o test1 test1.c
What new file is created? Run that file using
./test1
What does the -o flag do?
Step 5. Run your executable test1 again using
./test1 > junk
Type in the two numbers your program expects: an integer and a float, then hit ENTER.
What do you expect to happen?
What happened?
What does the operator > do in Unix?
The operator > is called redirection operator: redirect output to the file with the specified name.
Sample output:
Enter the first integer value: 6
Enter the second second integer value: 5
The sum of 5 and 6 is 11
Here is all you need to know about printf and scanf.
int main( int argc, char * argv[] )
int main( int argc, char * argv[] ) { int i; printf("\nYour program name is %s\n", argv[0]); printf("Your arguments are: \n"); for(i = 1; i < argc; i++) printf("\t \t %s starts with %c \n", argv[i], argv[i][0]); return 0; }Create an executable called test3 and run it using the command
test3 let us see what this does
man atoiYou will find out that the atoi function takes a string (char * in C) as an argument, and converts it to an integer. For instance, in the C statement
x = atoi("123")the atoi function takes as argument the string "123" and returns the integer value 123 (one hundred and twenty three) in the int variable x.
test4 7 11Print a message if the command line is invalid:
if(argc < 3) { printf("Invalid command line: please supply two integer values\n"); exit(1); }Note that you'll need to use the atoi function to convert the arguments (which are strings) to integer values.