CH01
CH01
CRC Press 1
ISBN 9781032189819
Chapter 01 Program Execution
2
Structure of C Programs
3
/*
* first.c
*
* Created by [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
4
/* Comments
* first.c between /* and */
*
* Created by [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
5
//
Comments can
// first.c
also start with //
//
// Created by [email protected]
//
//
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
6
/*
* first.c
*
* Created by [email protected]
*
*/
#include <stdio.h>
include header files
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
7
/*
* first.c
*
* Created by [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
8
/*
* first.c
*
* Created by [email protected]
*
*/
"main" is the starting
#include <stdio.h>
point of a C program
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
9
/*
* first.c
*
* Created by [email protected]
*
"main" returns an
*/
integer, EXIT_SUCCESS
#include <stdio.h>
or EXIT_FAILURE
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
10
/*
* first.c
*
* Created by [email protected]
*
"main" takes two
*/
arguments, an integer
#include <stdio.h>
and an char * *
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
11
/*
* first.c
*
* Created by yunglu
*
Their names are always
*/
"argc" and "argv".
#include <stdio.h>
Do not change the names.
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
12
/*
* first.c
*
* Created by [email protected]
*
argc: number of arguments
*/
c = count
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
13
/*
* first.c
*
* Created by [email protected]
*
argv: values of arguments
*/
v = value
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
14
/*
* first.c
*
* Created by [email protected]
*
A function's body starts
*/
with { and ends with }
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
15
/*
* first.c
*
* Created by [email protected]
*
print a message in a given
*/
format
#include <stdio.h>
f = formatting
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
16
/*
* first.c
*
* Created by [email protected]
*
\n means a new line
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
17
/*
* first.c
*
* Created by [email protected]
*
This simple program
*/
always succeeds.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
printf("Hello C\n");
return EXIT_SUCCESS;
}
18
C Programs has three formats
Compilation + Linking
Caution:
• Do not call your executable “test”.
Test is a Linux command.
• Do not put your text file after -o.
26
Warning: unused variable
Compiler warnings are your “first-
line of defense” detecting erroneous
code.
27
Shadow Variables
without gcc
warning
29
Quick Start of Linux
30
Topics
• Why Linux?
• Where to get Linux?
• Terminal and command line
• Frequently used commands
• Pipe
Why Linux?
• Linux is "UNIX-like"
• iOS, Anroid are also "UNIX-like"
• Top 500 (fastest 500 computers in the world)
• Linux is widely used in embedded systems
• (real-time improvements) Used in autonomous vehicles
• "At least" 67% web servers run Linux (wired.com, 2016/08/25)
• Many computer courses use Linux (because grading is easier)
Get Linux
• Download Linux and install on your computer
• Brand new computer
• Dual boot if it already has an operating system
• Virtual machine (such as Virtualbox), Linux coexists with another
operating system (such as Windows)
• Cloud (Amazon EC2, Microsoft Azure, Google Cloud ...)
Why terminal? Isn’t GUI better?
• GUI (Graphical User Interfaces) are good for humans, not computers
• Terminals allow you to automate many things that are difficult if you use GUI
• Terminals allow you to scale up to manage hundreds or thousands of machines
• In data centers, everything is based on terminals, no GUI
• Knowing how to use terminals give you additional skills (needed for many
positions)
This is a "command prompt". You enter commands after the prompt.
36
Frequently Used Commands
37
ls (list)
ls: list the files and directories (also called "folders")
ls -l: list with long (more information); beginning 'd': directory
ls (list)
ls: list the files and directories (also called "folders")
ls -l: list with long (more information); beginning 'd': directory
mkdir: make new directory (i.e., folder)
cd: change (i.e. enter) directory
vim: text editor
cp src dest: copy file src to dest
diff file1 file2: compare
64
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
int ind;
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
65
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
an integer as indexes
int ind;
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
66
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ %d means the value of an integer
int ind; This line prints argc's value
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
67
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ argc is the number of arguments
int ind; it is at least one
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
68
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ the first argument is the
int ind; program's name
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
69
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{
int ind; ind is 0, 1, 2, ... argc - 1
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
70
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ print the index
int ind; and the value of the argument
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
71
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ print an integer
int ind;
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
72
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ print a string
int ind;
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
73
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * * argv)
{ ind is 0, 1, 2, ..., argc - 1
int ind; not 1, 2, ..., argc
printf("argc = %d\n", argc);
for (ind = 0; ind < argc; ind ++)
{
printf("argv[%d] = %s\n", ind, argv[ind]);
}
return EXIT_SUCCESS;
}
74
a.out is gcc’s default output name
75
0 1 2 3 4 5 6
76
How to Use argc and argv
77
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{
if (argc < 2)
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 78
}
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{ check the value
if (argc < 2) argv must be 2 or greater; othersie
{ argv[1] does not exist
printf("Need a number\n");
return EXIT_FAILURE;
use argv[1]
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS;
79
}
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{
if (argc < 2) an error message
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 80
}
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{
if (argc < 2) the program stops with an error
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 81
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{ convert argv[1] to an integer
if (argc < 2) store the integer in val
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 82
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{ add 10 to val
if (argc < 2)
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 83
}
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{ print the string argv[1]
if (argc < 2) and the integer val
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS;
84
}
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{ the program successfully
if (argc < 2) prints the value
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS; 85
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char * * argv)
{
if (argc < 2)
{
printf("Need a number\n");
return EXIT_FAILURE;
}
int val = strtol(argv[1], NULL, 10);
val += 10;
printf("argv[1] = %s\n", argv[1]);
printf("val = %d\n", val);
return EXIT_SUCCESS;
86
}
87
More Examples of
argc and argv
88
#include <string.h>
int main(int argc, char * * argv)
{
if (argc < 4)
{
printf("Need three arguments\n");
return EXIT_FAILURE;
}
int val1 = strtol(argv[1], NULL, 10);
int val2 = strtol(argv[2], NULL, 10);
if (strcmp(argv[3], "+") == 0)
{
printf("%d + %d = %d\n", val1, val2, val1 + val2);
}
89
#include <string.h>
int main(int argc, char * * argv)
{
if (argc < 4) convert string to integer
{
printf("Need three arguments\n");
return EXIT_FAILURE;
}
int val1 = strtol(argv[1], NULL, 10);
int val2 = strtol(argv[2], NULL, 10);
if (strcmp(argv[3], "+") == 0)
{
printf("%d + %d = %d\n", val1, val2, val1 + val2);
}
90
#include <string.h>
int main(int argc, char * * argv)
{ compare two strings
if (argc < 4)
{
printf("Need three arguments\n");
return EXIT_FAILURE;
}
int val1 = strtol(argv[1], NULL, 10);
int val2 = strtol(argv[2], NULL, 10);
if (strcmp(argv[3], "+") == 0)
{
printf("%d + %d = %d\n", val1, val2, val1 + val2);
}
91
#include <string.h>
int main(int argc, char * * argv)
{
if (argc < 4)
{
printf("Need three arguments\n");
return EXIT_FAILURE;
}
int val1 = strtol(argv[1], NULL, 10);
int val2 = strtol(argv[2], NULL, 10); print the sum
if (strcmp(argv[3], "+") == 0)
{
printf("%d + %d = %d\n", val1, val2, val1 + val2);
}
92
else otherwise, check whether it is "-"
{
if (strcmp(argv[3], "-") == 0)
{
printf("%d - %d = %d\n", val1, val2, val1 - val2);
}
else
{
printf("Neither + nor -\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
93
else
{
if (strcmp(argv[3], "-") == 0)
{ If it is, print the difference
printf("%d - %d = %d\n", val1, val2, val1 - val2);
}
else
{
printf("Neither + nor -\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
94
else If argv[3] is neither "+" nor "-"
{ print a message and return EXIT_FAILURE
if (strcmp(argv[3], "-") == 0)
{
printf("%d - %d = %d\n", val1, val2, val1 - val2);
}
else
{
printf("Neither + nor -\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
95
96