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

1 IntroC

The document discusses the basics of writing C programs including compilation, the anatomy of a C program, tokens, and common elements in C code like keywords, operators, and data types. It explains how a C program is compiled in three stages and the typical structure of a simple C program file.

Uploaded by

Deep Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1 IntroC

The document discusses the basics of writing C programs including compilation, the anatomy of a C program, tokens, and common elements in C code like keywords, operators, and data types. It explains how a C program is compiled in three stages and the typical structure of a simple C program file.

Uploaded by

Deep Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to C

Topics
• Compilation
• Using the gcc/cc Compiler
• The Anatomy of a C Program

Reading
• 1st few chapters of any C book.
Writing C Programs

• A programmer uses a text editor to create or


modify files containing C code.
• Code is also known as source code.
• A file containing source code is called a source
file.
• After a C source file has been created, the
programmer must invoke the C compiler before
the program can be executed (run).
Using the C Compiler

• Invoking the compiler is system dependent.


o We have two C compilers available, cc and
gcc.
o For this class, we will use the gcc compiler as it
is the compiler available on the Linux system.
Invoking the gcc Compiler

At the prompt, type

gcc <-ansi> <-Wall> pgm.c

where pgm.c is the C program source


file.
• -ansi is a compiler option that tells the
compiler to adhere to the ANSI C standard.
• -Wall is an option to turn on all compiler
warnings (best for new programmers).
The Result : a.out
• If there are no errors in pgm.c, this
command produces an executable file,
which is one that can be executed (run).
• The gcc compiler names the executable file
a.out .
• To execute the program, at the prompt, type
./a.out
• Although we call this process “compiling a
program,” what actually happens is more
complicated.
3 Stages of Compilation

Stage 1: Preprocessing
o Performed by a program called the preprocessor
o Modifies the source code (in RAM) according to
preprocessor directives (preprocessor
commands) embedded in the source code
o Strips comments and white space from the code
o The source code as stored on disk is not modified.
3 Stages of Compilation (con’t)
Stage 2: Compilation
o Performed by a program called the compiler
o Translates the preprocessor-modified source
code into object code (machine code)
o Checks for syntax errors and warnings
o Saves the object code to a disk file, if instructed
to do so (we will not do this).
o If any compiler errors are received, no object code
file will be generated.
o An object code file will be generated if only
warnings, not errors, are received.
3 Stages of Compilation (con’t)
Stage 3: Linking
o Combines the program object code with other
object code to produce the executable file.
o The other object code can come from the Run-
Time Library, other libraries, or object files that
you have created.
o Saves the executable code to a disk file. On
the Linux system, that file is called a.out.
o If any linker errors are received, no executable file
will be generated.
Program Development Using gcc

Editor

Source File pgm.c

Preprocessor

Modified Source Code in RAM

Compiler
Program Object Code File pgm.o
Other Object Code Files (if any)

Linker

Executable File a.out


A Simple C Program
/* Filename: hello.c
Author: Brian Kernighan & Dennis Ritchie
Date written: ?/?/1978
Description: This program prints the greeting
“Hello, World!”
*/
#include <stdio.h>
int main ( void )
{
printf ( “Hello, World!\n” ) ;
return 0 ;
}
Anatomy of a C Program
program header comment

preprocessor directives (if any)

int main ( )
{
statement(s)
return 0 ;
}
Program Header Comment
• A comment is descriptive text used to help a
reader of the program understand its
content.
• All comments must begin with the characters
/* and end with the characters */
• These are called comment delimiters
• The program header comment always
comes first.
Preprocessor Directives
• Lines that begin with a # in column 1 are
called preprocessor directives
(commands).
• Example: the #include <stdio.h> directive
causes the preprocessor to include a copy of
the standard input/output header file stdio.h at
this point in the code.
• This header file was included because it
contains information about the printf ( )
function that is used in this program.
stdio.h

• When we write our programs, there are


libraries of functions to help us so that we
do not have to write the same code over
and over.
• Some of the functions are very complex and
long. Not having to write them ourselves
make it easier and faster to write programs.
• Using the functions will also make it easier
to learn to program!
int main ( void )

• Every program must have a function called


main. This is where program execution begins.
• main() is placed in the source code file as the first
function for readability.
• The reserved word “int” indicates that main()
returns an integer value.
• The parentheses following the reserved word
“main” indicate that it is a function.
• The reserved word “void” means nothing is there.
The Function Body

• A left brace (curly bracket) -- { -- begins the


body of every function. A corresponding
right brace -- } -- ends the function body.

• The style is to place these braces on


separate lines in column 1 and to indent the
entire function body 3 to 5 spaces.
printf (“Hello, World!\n”) ;

• This line is a C statement.


• It is a call to the function printf ( ) with a
single argument (parameter), namely the
string “Hello, World!\n”.
• Even though a string may contain many
characters, the string itself should be
thought of as a single quantity.
• Notice that this line ends with a semicolon.
All statements in C end with a semicolon.
return 0 ;
• Because function main() returns an integer value,
there must be a statement that indicates what this
value is.
• The statement
return 0 ;
indicates that main() returns a value of zero to
the operating system.
• A value of 0 indicates that the program successfully
terminated execution.
• Do not worry about this concept now. Just
remember to use the statement.
Another C Program
/*****************************************
** File: proj1.c
** Author: Joe Student
** Date: 9/15/01
** SSN: 123-45-6789
** Section: 0304
** E-mail: jstudent22@umbc.edu
**
** This program prompts the user for two integer values then displays
** their product.
**
***********************************************/
Another C Program (con’t)
#include <stdio.h>
int main( void )
{
int value1, value2, product ;
printf(“Enter two integer values: “) ;
scanf(“%d%d”, &value1, &value2) ;
product = value1 * value2 ;
printf(“Product = %d\n”, product) ;
return 0 ;
}
Tokens

• The smallest element in the C language is


the token.
• It may be a single character or a sequence
of characters to form a single item.
Tokens are:

• Tokens can be:


o Numeric constants
o Character constants
o String constants
o Keywords
o Names (identifiers)
o Punctuation
o Operators
Numeric Constants

• Numeric constants are an uninterrupted


sequence of digits (and may contain a
period). They never contain a comma.
• Examples:
o 123
o 98.6
o 1000000
Character Constants

• One character defined character set.


• Surrounded on the single quotation mark.
• Examples:
o ‘A’
o ‘a’
o ‘$’
o ‘4’
String Constants

• A sequence characters surrounded by


double quotation marks.
• Considered a single item.
• Examples:
o “UMBC”
o “I like ice cream.”
o “123”
o “CAR”
o “car”
Keywords

• Sometimes called reserved words.


• Are defined as a part of the C language.
• Can not be used for anything else!
• Examples:
o int
o while
o for
Names

• Sometimes called identifiers.


• Can be of any length, but on the first 31 are
significant (too long is as bad as too short).
• Are case sensitive:
o abc is different from ABC
• Must begin with a letter and the rest can be
letters, digits, and underscores.
• Name should be meaningful
Punctuation

• Semicolons, colons, commas, apostrophes,


quotation marks, braces, brackets, and
parentheses.
• ; : , ‘ “ [ ] { } ( )
Operators

• There are operators for:


o Assignments (=)
o mathematical operations (+,-,/,*,%,++,--)
o relational operations (>=,<=,>,<,==,!=)
o Boolean operations (&&,||,!)
o bitwise operations (&,|,^,~)
o shifting values (>>,<<)
o calling functions ()
o Subscripting ([ ])
o obtaining the size of an object sizeof()
o obtaining the address of an object &
o referencing an object through its address *
o choosing between alternate subexpressions e1?e2:e3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy