Lecture 1 - Intro to C 1
Lecture 1 - Intro to C 1
http://lol-rofl.com/computer-cartoon/
1
References
Clark Barrett
“Machine Architecture and Organization” lectures, University of Minnesota,
https://cse.umn.edu/
Prof. Mohamed Zahran lectures
2
C Invention
In early 1970s, Richie started creating the C
programming language.
Brian Kernighan 2
1 https://spectrum.ieee.org/dennis-ritchie-1941-2011
3
2 https://www.cs.princeton.edu/~bwk/
C is everywhere!
C - language
is the mother of them all …
4
C is everywhere!
Fast
Pointers
It produces code that runs nearly
Simple Direct contact
as fast as code written in assembly
Easy to learn with memory
language
Main features of
C – language1
5
1 https://www.simplilearn.com/tutorials/c-tutorial/features-of-c-language
C is everywhere!
1 https://www.quora.com/Financially-speaking-which-computer-languages-can-earn-the-most-for-the-
6
programmer/answer/Carter-Page-1
C vs C++: Hello World
C C++
#include <stdio.h> #include <iostream>
using namespace std;
{ {
printf("Hello World!\n"); cout<<"Hello World!“<<endl;
return 0; return 0;
} }
7
C vs C++: I/O
C C++
#include <stdio.h> #include <iostream>
using namespace std;
int main() int main()
{ {
int num1 = 5; int num1 = 5;
int num2; int num2;
char ch; char ch;
printf("Number = %d\n", num1); cout<<"Number = "<<num1<<endl;
printf("Enter a number: "); cout<<"Enter a number = ";
scanf("%d",&num2); cin>>num2;
printf("You entered %d.\n",num2); cout<<"You entered "<<num2<<"."<<endl;
printf("Enter a character: "); cout<<"Enter a character = ";
scanf(" %c",&ch); cin>>ch;
printf("You entered %c.\n",ch); cout<<"You entered "<<ch<<"."<<endl;
return 0; return 0;
} } 8
C vs C++: I/O
To print a value stored in a variable, printf should know it type, using one
of the following syntax
%d Integer values
%c Characters
%s String values
%f Float values
9
C vs C++: I/O
10
C vs C++: I/O
11
C Syntax and Hello World!
#include inserts another file. “.h” files are called “header” files.
They contain stuff needed to interface to libraries and code in
other “.c” files.
return 0;
Return ‘0’from this function. Print out a message. ‘\n’means “new line”.
Exit 12
Comments in C
13
Using escape parameters in C
Symbol Meaning
\n New line
\t Tab
\b Back space \x is a request to display the
\x Hexadecimal next argument in hexadecimal.
\xeb would mean character eb
\\ \ in hex, or 235 in decimal.
\? ?
\’ ‘
\” “
14
Writing and Running Programs
#include <stdio.h>
/* The simplest C Program */
int main(int argc, char * argv[])
{
printf(“Hello World!\n”);
return 0;
}
Default filename 15
1 https://gcc.gnu.org/
What happens when you call “gcc”?
Preprocessor Preprocessed
Source code Compiler
Source code
16
1 https://people.sc.fsu.edu/~jburkardt/classes/isc_2012/c_program_compilation.pdf
Writing and Running Programs
#include <stdio.h>
/* The simplest C Program */ Main steps
int main(int argc, char * argv[])
1. Write text of program (source code) using a text editor,
{ save as text file e.g. my_program.c
printf(“Hello World!\n”);
return 0;
2. Run the compiler, assembler, and linker to convert
}
program from source to an “executable” or “binary”:
19
Writing and Running Programs
Note 3: If your program doesn’t work, you can use
debugging tools.
20
What happens when you call “gcc”?
Preprocessor Preprocessed
Source code Compiler
Source code
21
1 https://people.sc.fsu.edu/~jburkardt/classes/isc_2012/c_program_compilation.pdf
Preprocessing
#include <stdio.h>
/* The simplest C Program */
int main(int argc, char * argv[]) Preprocess
{
printf(“Hello World!\n”);
return 0;
}
my_program
22
Compile
Preprocessing
#include <stdio.h>
/* The simplest C Program */
int main(int argc, char * argv[])
Preprocess
{
printf(“Hello World!\n”);
return 0;
} In Preprocessing, source code is “expanded” into a
extension
dev_t;
typedef unsigned long long int larger form that is simpler for the compiler to
extension
extension
typedef
typedef
unsigned int
unsigned int
uid_t;
gid_t;
understand.
extension typedef unsigned long int ino_t;
extension typedef unsigned long long int
ino64_t;
extension typedef unsigned int nlink_t; Any line that starts with ‘#’is a line that is interpreted
by the Preprocessor.
extension typedef long int off_t;
extension typedef long long int off64_t;
extern void flockfile (FILE * stream) ;
extern int ftrylockfile (FILE * stream) ;
extern void funlockfile (FILE * stream) ;
int main(int argc, char * argv[]) • Include files are “pasted in” (#include)
• Macros are “expanded” (#define)
{
printf(“Hello World!\n”);
return 0;
} • Comments are stripped out ( /* */ , // )
• Continued lines (i.e. very long lines ) are joined ( \ )
my_program
Compile 23
What happens when you call “gcc”?
Preprocessor Preprocessed
Source code Compiler
Source code
24
1 https://people.sc.fsu.edu/~jburkardt/classes/isc_2012/c_program_compilation.pdf
Compiling
#include <stdio.h>
/* The simplest C Program */
int main(int argc, char * argv[])
Preprocess
{
printf(“Hello World!\n”);
return 0;
} The compiler then converts the resulting text into
extension
dev_t;
typedef unsigned long long int binary code the CPU can run directly.
extension typedef unsigned int uid_t;
extension typedef unsigned int gid_t;
extension
extension
typedef
typedef
unsigned long int
unsigned long long int
ino_t;
The compilation process involves really several steps:
ino64_t;
extension typedef unsigned int nlink_t; • Compiler: high level language assembly
• Assembler: assembly machine code
extension typedef long int off_t;
extension typedef long long int off64_t;
my_program
Compile 25
Memory
26
What is “Memory”?
Addr Value
The first 32 values, 0 through 31, are reserved for
0
terminal control codes, such as Escape, New Line,
1
Carriage Return, and Acknowledge.
2
The values from 32 to 64 are reserved for numbers 3
and special characters, such as space, Exclamation 4 ‘H’ (72)
mark, Per cent sign, Colon, and Semicolon. 5 ‘e’ (101)
6 ‘l’ (108)
The reason the characters are encoded as 7-bit values 7 ‘l’ (108)
is because early computers handled data in bytes — 8 ‘o’ (111)
blocks of 8 bits. The extra, eighth bit was originally 9 ‘\n’ (10)
reserved for error checking. 10 ‘\0’ (0)
11
12
29
1 https://www.ascii-code.com/
Variable
30
What is a “Variable”?
33
Expressions in C
What is an “expression”?
It is a combination of operands (variables) and operators, according
to certain precedence rules.
It computes a single value, which is stored in a variable.
operands
variable z=x+y
operator
34
Comparison and Mathematical Operators
Types of Expressions 1
Relational operators
Arithmetic operators Logical operators
+ plus && logical and == equal to
- minus || logical or < less than
* mult ! logical not <= less than or equal
/ divide > greater than
% modulo Remainder of a division >= greater than or equal
!= not equal
Bitwise operators
Beware in division:
& bitwise and If second argument is integer, the
| bitwise or
result will be integer (rounded):
^ bitwise xor
~ bitwise not
5 / 10 0 whereas 5 / 10.0 0.5
<< shift left
>> shift right Don’t confuse & and &&
1 & 2 0 whereas 1 && 2 <true>
35
1 https://www.tutorialspoint.com/cprogramming/c_logical_operators.htm
Operator Precedence in C
1+2*2 1+4 5
(1 + 2) * 2 3 * 2 6
int x=1;
int y=2;
x+y*y x+2*2 x+4 1+4 5
int main(){
//Arithmetic Expression
int a = (6 * 2) + 7 - 9;
Output
printf("The arithmetic expression returns: %d\n", a); The arithmetic expression returns: 10
//Relational Expression
int b = 10; Output
printf("The relational expression returns: %d\n", b % 2 == 0); The relational expression returns: 1
//Logical Expression
int c = (7 > 9) && ( 5 <= 9);
printf("The logical expression returns: %d\n", c);
Output
The logical expression returns: 0
//Conditional Expression
int d = (34 > 7) ? 1 : 0;
printf("The conditional expression returns: %d\n", d); Output
The conditional expression returns: 1
//Pointer Expression
int e = 20;
int *addr ; Output
addr = &e;
printf("The pointer expression returns: %p\n", addr); The pointer expression returns: 0x7ffd75e944bc
//Bitwise Expression
int f = 10;
Output
int shift = f >> 1; The bitwise expression returns: 5
printf("The bitwise expression returns: %d\n", shift);
return 0;
}
38
1 https://educative.io/answers/what-are-expressions-in-c
C Operator Precedence and Associativity Note 1
The associativity
indicates in what
order operators of
equal precedence
in an expression
are applied.
Note 2
Order of precedence When in doubt,
(highest to lowest) use parentheses.
39
1 http://www.eecs.northwestern.edu/~wkliao/op-prec.htm
Assignment Operators
40