04 Introduction To C
04 Introduction To C
C Programming
CEN111 Algorithms and Programming I
A Simple C Program
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. return 0;
8. } // end function main
• OUTPUT:
• Welcome to C!
A Simple C Program
Comments
• Begin with //
• Insert comments to document programs and improve program
readability
• Comments do not cause the computer to perform actions
• Help other people read and understand your program
• Can also use /* … */ multi-line comments
• Everything between /* and */ is a comment
A Simple C Program
#include Preprocessor Directive
• #include <stdio.h>
• C preprocessor directive
• Preprocessor handles lines beginning with # before compilation
• This directive includes the contents of the standard input/output
header (<stdio.h>)
• Contains information the compiler uses to ensure that you correctly use
standard input/output library functions such as printf
A Simple C Program
Blank Lines and White Space
• Blank lines, space characters and tab characters make programs
easier to read
• Together, these are known as white space
• Generally ignored by the compiler
A Simple C Program
The main Function
• Begins execution of every C program
• Parentheses after main indicate that main is a function
• C programs consist of functions, one of which must be main
• Precede every function by a comment stating its purpose
• Functions can return information
• The keyword int to the left of main indicates that main “returns” an
integer value
A Simple C Program
The main Function
• Functions can receive information
• void in parentheses means main does not receive any information
• A left brace, {, begins each function’s body
• A corresponding right brace, }, ends each function’s body
• A program terminates upon reaching main’s closing right brace
• The braces form a block
A Simple C Program
An Output Statement
• printf("Welcome to C!\n");
• Performs an action—displays the string in the quotes
• A string is also called a character string, a message or a literal
• The entire line is called a statement
• Every statement ends with a semicolon statement terminator
• Characters usually print as they appear between
• Notice the characters \n were not displayed.
A Simple C Program
Escape Sequences
• In a string, backslash (\) is an escape character
• Compiler combines a backslash with the next character to form an
escape sequence
• \n means newline
• When printf encounters a newline in a string, it positions the
output cursor to the beginning of the next line
A Simple C Program
Escape Sequence Description
Moves the cursor to the beginning of the next
\n
line.
\t Moves the cursor to the next horizontal tab stop.
Produces a sound or visible alert without
\a
changing the current cursor position.
Because the backslash has special meaning in a
\\ string, \\ is required to insert a backslash
character in a string.
Because strings are enclosed in double quotes,
\” \” is required to insert a double-quote character
in a string.
A Simple C Program
The Linker and Executables
• When compiling a printf statement, the compiler merely provides
space in the object program for a “call” to the function
• The compiler does not know where the library functions are—the
linker does
• When the linker runs, it locates the library functions and inserts the
proper calls to these functions in the object program
• Now the object program is complete and ready to execute
• The linked program is called an executable
A Simple C Program
The return Statement
• OUTPUT:
Welcome
to
C!
A Simple C Program
Displaying Multiple Lines with a Single printf
• One printf can display several lines
• Each \n moves the output cursor to the beginning of the next line
Adding Two Integers
• scanf standard library function obtains information from the user at
the keyboard
• Next program obtains two integers, then computes their sum and
displays the result
Adding Two Integers
1.// Addition program.
2.#include <stdio.h>
• Invalid Identifiers
1Letter double int TWO*FOUR joe’s
Variable Declarations
• Variable
• a name associated with a memory cell whose value can change
• Variable declarations
• statements that communicate to the compiler the names of variables in the
program and the kind of information stored in each variable
• Variables must be declared before use, a syntax (compile-time) error if these
are violated
• Every variable has a name, a type, a size and a value
Basic Datatypes in C
• Integer int
• Character char
• Floating Point float
• Double precision double
floating point
• Datatype modifiers
• signed / unsigned (for int and char)
• short / long
Basic Datatypes in C
• signed char (8 bits) -127 to +127
• unsigned char 0 to 255
• short int (16 bits) -32,767 to +32,767
• unsigned short int 0 to 65,535
• int (32 bits) -2,147,483,647 to +2,147,483,647
• unsigned int 0 to 4,294,967,295
• long int (32-64 bits) -2,147,483,647 to +2,147,483,647
• unsigned long int 0 to 4,294,967,295
• float ~10^-37 to ~10^38
• double ~10-^307 to ~10^308
• long double ~10^-4931 to ~10^4932
Basic Datatypes in C
• Placeholders in format string
• %c: the argument is taken to be a single character
• %d: the argument is taken to be an integer
• %f: the argument is taken to be a floating point (float or double)
• %s: the argument is taken to be a string
Variable Declarations
• A declaration consists of a data type name followed by a list of (one
or more) variables of that type:
• char c;
• int num1, num2;
• float rate;
• double avarage;
Variable Declarations
• A variable may be initialized in its declaration.
• char c = ‘a’;
• int a = 220, b = 448;
• float x = 0.00123;
• double y = 123.00;
Variable Declarations
C: m = (a + b + c + d + e)/5;
y = mx + b
C: 𝑦 = 𝑚 ∗ 𝑥 + 𝑏;
Arithmetic in C
z = pr mod q + w /x − y
C:
Arithmetic in C
Using Parentheses for Clarity
• Redundant parentheses can make an expression clearer
y = ( a x x ) + ( b x ) + c;
Assignment Operator
• variable = expression; x = 5*y + (y-1)*44 ;
• expressions:
• operations expression
• variables
• constants
• function calls statement