You are on page 1of 20

Basic Programming

2/22/2017 CUIT114/111 1
Outline
• C program structure
• The main() method
• Pre-processor directives
• Console output
• Comments
• Reserved words

2/22/2017 CUIT114/111 2
C Program Structure
// Pre-processor directives

// global declarations or variables

// function declarations (optional)

// main function
main()
{
//local declarations or variables
//statements associated with main logic of algorithm
}

2/22/2017 CUIT114/111 3
main()
• main
– Only function which denotes where program running starts and ends
• ()
– Used with all function names
– Encases the list of parameters of the function, separated by commas
• {}
– Used to delimit the start { and the end } of a function, encasing all
function statements within.
• ;
– Used to terminate almost every C statement,
– Much like a fullstop terminates sentences, with each sentence conveying
its meaning or achieving a task

2/22/2017 CUIT114/111 4
Pre-processor Directives
• These directives invoke already existing tools
provided by C libraries to make tasks easier
and simpler, e.g.,
– Allow one to output information to the screen
– Allow one to read what is typed by used on
keyboard
• Invoked using the #include statement
#include <library_name>

2/22/2017 CUIT114/111 5
Blank program
Version 1 Version 2
#include <stdio.h> #include <stdio.h>

main() int main()


{ {

return 0;
} }

2/22/2017 CUIT114/111 6
Common C Standard Library Header Files
• <stdio.h> Input/output
• <string.h> manipulating sequences of characters
• <math.h> applying mathematical functions
• <ctype.h> manipulating characters
• <stdlib.h> General utilities: memory management, program
utilities, string conversions, random numbers, managing files
• <assert.h> Conditionally compiled macro that compares its
argument to zero
• <signal.h> Signal handling
• <time.h> Time/date utilities

2/22/2017 CUIT114/111 7
Using input/output functions
#include <stdio.h>
• Output
– int printf(char *s, const char*format, ...)
– Prints a string of characters (char) to the screen using a specific
format.
– Print out whatever is in double quotes to the screen
• Input
– int scanf(const char*format, ...)
– Reads values from the keyboard into a memory location according to
a specific format.
– Take what ever has been typed onto the screen and save it in
memory

2/22/2017 CUIT114/111 8
Output Example 1
#include <stdio.h>
main()
{
printf("my name is Sheila");
printf("and I love playing tennis.");
printf("What is your name, dear?");
}

2/22/2017 CUIT114/111 9
Output Example 2
#include <stdio.h>
main()
{
printf(“1 2 3 4 5");
printf(“once I caught a fish alive");
printf(“??%%%### shuwa here?");
}

2/22/2017 CUIT114/111 10
Manage display using escape sequences
\n Go to a new line
\t Move a fixed number of characters away (a tab space away)
\a Alert Produces an audible or visible alert.
\b Backspace Moves the cursor back one position (non-destructive).
\f Form Feed Moves the cursor to the first position of the next page.
\r Carriage Return Moves the cursor to the first position of the
current line.

\v Vertical Tab Moves the cursor to the next vertical tabular position.
\' Produces a single quote.
\" Produces a double quote.
\? Produces a question mark
\\ Produces a single backslash. \0 Produces a null character.
\ddd Defines one character by the octal digits (base-8 number).
\xdd Defines one character by the hexadecimal digit (base-16 number).
2/22/2017 CUIT114/111 11
Output Example 3
I am here to stay printf(“I am here to stay”);
I am here printf(“I am here \n to stay”);
to stay
I am here to stay printf(“I am here \t to stay”);
I
am
here
to
stay
right
?
Escape Sequences
\n moves to the beginning of a new line
\t moves one tab space away

2/22/2017 CUIT114/111 12
Comments
• Are notes to you or others in your program
• Are ignored by the compiler or linker
• Can be used to temporarily remove lines of
code without deleting them
• Written using
/* my comments here */
// ignore rest of this line

2/22/2017 CUIT114/111 13
Reserved Words
• Are words understood to have one meaning
only as interpreted by the language compiler
• Cannot be reassigned to perform another
function

2/22/2017 CUIT114/111 14
C Reserved Keywords (1)
auto
break inline (C99
case )
char int
const long
continue register
default restrict (C
do 99)
double return
else short
enum signed
extern
2/22/2017 sizeof CUIT114/111 15
C Reserved Keywords (2)
_Alignas (C11)
_Alignof (C11)
_Atomic (C11)
_Bool (C99)
_Complex (C99)
_Generic (C11)
_Imaginary (C99)
_Noreturn (C11)
2/22/2017 CUIT114/111 16
C Reserved Words (3)
if
elif
else
endif
defined
ifdef
ifndef
define
undef
2/22/2017 CUIT114/111 17
Program to Output Name and Age
#include<stdio.h> #include<stdio.h>
/* /*
This is a simple This is a simple
output-only program output-only program
*/ */

main() int main()


{ {
printf(“Name: .\n"); printf(“Name: \n");
printf(“Age: 56.\n"); printf(“Age: 56.\n");
//no need for return return 0;
} }

2/22/2017 CUIT114/111 18
Exercise
• Write a program that prints out the following:

123456789
1 2 3 4
‘9’ ‘8’ ‘7’

2/22/2017 CUIT114/111 19
Other C Standard Library Header Files
• <complex.h> (since C99)Complex • <stdatomic.h> (since C11)Atomic types
number arithmetic • <stdbool.h> (since C99)Boolean type
• <errno.h>Macros reporting error • <stddef.h>Common macro definitions
conditions
• <stdint.h> (since C99)Fixed-width
• <fenv.h> (since C99)Floating-point integer types
environment
• <stdnoreturn.h> (since
• <float.h>Limits of float types C11)noreturn convenience macros
• <inttypes.h> (since C99)Format • <tgmath.h> (since C99)Type-generic
conversion of integer types math (macros wrapping math.h and
• <iso646.h> (since C95)Alternative complex.h)
operator spellings • <threads.h> (since C11)Thread library
• <limits.h>Sizes of basic types • <uchar.h> (since C11)UTF-16 and UTF-
• <locale.h>Localization utilities 32 character utilities
• <setjmp.h>Nonlocal jumps • <wchar.h> (since C95)Extended
• <stdalign.h> (since C11)alignas and multibyte and wide character utilities
alignof convenience macros • <wctype.h> (since C95)Wide character
• <stdarg.h>Variable arguments classification and mapping utilities

2/22/2017 CUIT114/111 20

You might also like