0% found this document useful (0 votes)
20 views10 pages

Handout 04 - C Program Structure

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)
20 views10 pages

Handout 04 - C Program Structure

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

C Program Structure

Programming in C

Shirley B. Chu
[email protected]
College of Computer Studies
De La Salle University

November 4, 2021
Writing C programs

What you need...


› editor such as Notepad++, or
› compiler
› IDE (Integrated development environment); and

Saving your source code...


› ˛le name without space/s
› ˛le extension: .c

S. B. Chu (DLSU) C Program Structure November 4, 2021 2 10


Semicolon ;

› ; is used to mark the end of a statement, and the


beginning of another statement.
› All C statements must end with a semi-colon.

S. B. Chu (DLSU) C Program Structure November 4, 2021 3 10


Sample C Program Comments
/* This is a sample program. */
› enclosed in /* and */
#include<stdio.h>
› starts with //
#define MSG "Hi There" › can be placed anywhere in
#define SIZE 50 the program
› are treated as white
void displayMessage () spaces by the compiler
{
printf (MSG); /*displays Hi*/ › used to put notes
} › used to temporarily
remove a statement or
int main () groups of statements
{ › Introductory comment :
int nVal = 5; usually contains the name
displayMessage (); of the author/s, date,
printf ("Number is %d\n", nVal); assumptions, and
return 0; description of the program
}

S. B. Chu (DLSU) C Program Structure November 4, 2021 4 10


Sample C Program #include
/* This is a sample program. */
› Syntax:
#include<stdio.h> #include<header ˛le>
› #include
#define MSG "Hi There"
#define SIZE 50 a preprocessor
directive;
void displayMessage () compiler inserts the
{ contents of the
printf (MSG); /*displays Hi*/ indicated ˛le
} › header ˛le
˛le extension: .h
int main () contains de˛nitions
{
that are required to
int nVal = 5; run the program
displayMessage ();
printf ("Number is %d\n", nVal); › All necessary header ˛les
return 0; must be explicitly
} included.

S. B. Chu (DLSU) C Program Structure November 4, 2021 5 10


Sample C Program #de˛ne
/* This is a sample program. */

#include<stdio.h>

#define MSG "Hi There"


#define SIZE 50 › allows constant values to
be declared and used
void displayMessage () throughout the code
{
› value cannot be changed
printf (MSG); /*displays Hi*/ by the code in the
}
program
› convention: identi˛ers are
int main ()
{ in all uppercase letters
and may have underscores
int nVal = 5;
displayMessage ();
printf ("Number is %d\n", nVal);
return 0;
}

S. B. Chu (DLSU) C Program Structure November 4, 2021 6 10


Sample C Program Function de˛nitions
/* This is a sample program. */

#include<stdio.h>

#define MSG "Hi There"


#define SIZE 50

void displayMessage () › subroutines created by the


{ programmer
printf (MSG); /*displays Hi*/ › performs a speci˛c task
}

int main ()
{
int nVal = 5;
displayMessage ();
printf ("Number is %d\n", nVal);
return 0;
}

S. B. Chu (DLSU) C Program Structure November 4, 2021 7 10


Sample C Program main() Function
/* This is a sample program. */

#include<stdio.h>

#define MSG "Hi There"


#define SIZE 50

void displayMessage () › entry point of a C program


{
› may contain:
printf (MSG); /*displays Hi*/
} variable declarations;
statements;
int main () function calls
{
int nVal = 5;
displayMessage ();
printf ("Number is %d\n", nVal);
return 0;
}

S. B. Chu (DLSU) C Program Structure November 4, 2021 8 10


Compiling and Running a C Program

1. Run Notepad++
2. Start typing.
3. Save your ˛le as MyFirstProgram.c
4. Run the Command Prompt.
5. Change directory to where your source code is saved.
6. To compile your program, type
gcc -Wall MyFirstProgram.c -o MyFirstProgram
7. If there are errors, go back to your code and ˛x them.
Compile again.
8. Once compilation is successful, i.e. no errors, run the
program:
MyFirstProgram

S. B. Chu (DLSU) C Program Structure November 4, 2021 9 10


, Thank you! ,

You might also like