The document provides an overview of a C/C++ tutorial, including:
- Contact information for the teaching assistant and class website.
- An outline of topics covered, such as Hello World programs, data types, operators, conditionals, loops, arrays, strings, pointers, functions, command line arguments, data structures, and memory allocation.
- Brief explanations and examples of key concepts like variable types, arithmetic operations, conditional statements, loops, functions, pointers, and dynamic memory allocation. It also discusses differences between C and C++.
The document provides an outline of topics for a C/C++ tutorial, including a "Hello World" program, data types, variables, operators, conditionals, loops, arrays, strings, functions, pointers, command-line arguments, data structures, and memory allocation. It gives examples and explanations of key concepts in C/C++ programming.
The C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system.
The C is the most widely used computer language, it keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.
The document provides an overview of key concepts in programming with examples, including:
- Identifiers, variables, data types, literals and constants
- Expressions, operators, precedence and order of evaluation
- Input/output using dialog boxes
- Common errors like syntax, runtime, logic errors
- Style and documentation guidelines
The document discusses various topics related to tokens, variables, data types, and operators in C programming. It defines tokens as the smallest elements identified by the compiler, such as keywords, identifiers, string literals, and operators. It describes different variable types like local variables, global variables, and static variables. It also explains various data types in C like integer, float, char, etc and their sizes and ranges. Finally, it discusses various arithmetic, relational, logical, and assignment operators in C and their precedence.
The document provides an overview of the C programming language. It discusses that C is commonly used for embedded systems and systems programming tasks like operating systems and compilers. It was developed between 1969-1973 along with Unix. The "Hello World" example program is shown to demonstrate the basic structure of a C program with main() as the entry point. Data types, variables, and basic I/O functions like printf() and scanf() are described. Operators for arithmetic, comparison, logic, and assignment are also covered.
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
The document provides an overview of the C programming language. It begins by explaining that Objective-C extends standard ANSI C with object-oriented capabilities. It then discusses why C remains important today due to its use in libraries, operating systems, and as the base for many other popular languages. The document proceeds to cover basic C concepts like variables, data types, functions, flow control, pointers, memory allocation, and I/O parameters. It emphasizes that C provides high performance with a minimal footprint while abstracting away the CPU and memory.
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
The document discusses Java syntax and concepts including:
1. It introduces primitive data types in Java like int, float, boolean and String.
2. It covers variables, operators, and expressions - how they are used to store and manipulate data in Java.
3. It explains console input and output using Scanner and System.out methods for reading user input and printing output.
4. It provides examples of using conditional statements like if and if-else to control program flow based on conditions.
The document provides an outline of topics for a C/C++ tutorial, including a "Hello World" program, data types, variables, operators, conditionals, loops, arrays, strings, functions, pointers, command-line arguments, data structures, and memory allocation. It gives examples and explanations of key concepts in C/C++ programming.
The C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system.
The C is the most widely used computer language, it keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.
The document provides an overview of key concepts in programming with examples, including:
- Identifiers, variables, data types, literals and constants
- Expressions, operators, precedence and order of evaluation
- Input/output using dialog boxes
- Common errors like syntax, runtime, logic errors
- Style and documentation guidelines
The document discusses various topics related to tokens, variables, data types, and operators in C programming. It defines tokens as the smallest elements identified by the compiler, such as keywords, identifiers, string literals, and operators. It describes different variable types like local variables, global variables, and static variables. It also explains various data types in C like integer, float, char, etc and their sizes and ranges. Finally, it discusses various arithmetic, relational, logical, and assignment operators in C and their precedence.
The document provides an overview of the C programming language. It discusses that C is commonly used for embedded systems and systems programming tasks like operating systems and compilers. It was developed between 1969-1973 along with Unix. The "Hello World" example program is shown to demonstrate the basic structure of a C program with main() as the entry point. Data types, variables, and basic I/O functions like printf() and scanf() are described. Operators for arithmetic, comparison, logic, and assignment are also covered.
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
The document provides an overview of the C programming language. It begins by explaining that Objective-C extends standard ANSI C with object-oriented capabilities. It then discusses why C remains important today due to its use in libraries, operating systems, and as the base for many other popular languages. The document proceeds to cover basic C concepts like variables, data types, functions, flow control, pointers, memory allocation, and I/O parameters. It emphasizes that C provides high performance with a minimal footprint while abstracting away the CPU and memory.
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
The document discusses Java syntax and concepts including:
1. It introduces primitive data types in Java like int, float, boolean and String.
2. It covers variables, operators, and expressions - how they are used to store and manipulate data in Java.
3. It explains console input and output using Scanner and System.out methods for reading user input and printing output.
4. It provides examples of using conditional statements like if and if-else to control program flow based on conditions.
This document provides an overview of various data types, variables, and control flow structures in C programming. It defines common data types like char, int, float, and double. It also explains variable declaration and definition, and different types of variables like local, static, and global. The document further discusses arithmetic, logical, and comparison operators. Finally, it covers conditional statements like if-else and switch, and iterative structures like for and while loops.
introduction to c programming and C History.pptxManojKhadilkar1
C programming was developed by Dennis Ritchie at Bell Labs in 1972. It was derived from an earlier language called BCPL. A C program typically includes source code, which is compiled into object code and linked to produce an executable file. The structure of a C program includes sections for documentation, definitions, global declarations, functions, and the main function.
C programming is a widely used programming language. The document provides an overview of key concepts in C programming including variables, data types, operators, decision and loop control statements, functions, pointers, arrays, strings, structures, and input/output functions. It also provides examples to illustrate concepts like arrays, strings, functions, pointers, and structures. The main function is the entry point for all C programs where code execution begins.
The document defines various tokens in the C programming language including keywords, identifiers, constants, string literals, operators, data types, and variables. It discusses the basic data types like integers, floating point numbers, and characters. It also covers topics like declarations, global and local variables, type conversions, precedence and order of evaluation, and various operators used in C.
This document provides an overview of basic C# programming concepts. It covers topics such as program structure, data types, variables, operators, decision making statements, loops, classes and methods. Specifically, it discusses if/else statements, switch cases, ternary operators, for, while and do-while loops. It also provides examples of basic C# programs and explanations of concepts like classes, methods, constructors and namespaces.
C is a middle-level general purpose programming language developed in 1972. It uses characters, keywords, variables, constants, data types, expressions and operators. Variables are named locations used to store and manipulate data during execution. C supports several data types including integer, float, character and others. Operators perform actions like arithmetic, relational, logical and bitwise operations on variables and constants.
This document provides an overview of an intermediate computer programming course at the University of Gondar in Ethiopia. The course code is CoEng2111 and it is taught by instructor Wondimu B. Topics that will be covered in the course include C++ basics, arrays, strings, functions, and recursion. The course materials are presented over several pages that define concepts, provide code examples, and explain key ideas like variables, data types, operators, and array implementation in C++.
guia de referencia para a linguagem do fabricante CCS info_syntax.pdfSilvanildoManoeldaSi
This document provides a quick reference guide for the C programming language. It introduces C syntax for microcontrollers and other applications. C offers power and flexibility for programming microcontrollers. The guide provides examples of C language elements like comments, constants, variables, operators, and statements to get users started with basic C programming.
The document discusses the topics that will be covered in a C programming course, including:
- Introduction to C programming components like header files, preprocessor directives, main function, comments etc.
- Variable declaration and initialization, data types, operators, input/output functions like printf and scanf.
- Common statements like if-else, switch case, for, while and do-while loops.
- Functions, arrays, strings and pointers will also be covered along with evaluation methods like exams, assignments and attendance.
This document provides a quick introduction to the C programming language. It discusses basic C syntax like main functions, header files, comments, and variables. It also covers data types, memory, functions, scopes, expressions, control flow statements like if/else and loops. Functions can access arguments and global variables from their own scope but not variables from outer scopes unless passing their addresses.
The document provides a quick introduction to C programming, covering topics like functions, variables, memory, expressions, and recursion. It uses a recursive pow() function to demonstrate recursion and the call stack. Each function call gets its own copy of arguments and local variables, stored on the call stack.
The document provides a quick introduction to C programming. It discusses some key concepts in C including functions, variables, memory, data types, expressions and evaluation, and recursion. It provides an example of a recursive pow() function to calculate powers and discusses how function calls are handled through the stack.
This document provides information about C++ programming language tokens and input/output operations. It defines various tokens like keywords, identifiers, literals, punctuators, and operators. It explains the basic structure of a C++ program and defines important headers like iostream. It also describes input/output streams like cout and cin, and input/output operators like << and >>.
This document provides an introduction to Python programming concepts including data types, operators, control flow statements, functions and modules. It discusses the basic Python data types like integers, floats, booleans, strings, lists, tuples, dictionaries and sets. It also covers Python operators like arithmetic, assignment, comparison, logical and identity operators. Additionally, it describes control flow statements like if/else and for loops. Finally, it touches on functions, modules and input/output statements in Python.
The document is a student submission from Dinobandhu Thokdar of Kaliacchak Government Polytechnic. It includes the student's personal details such as name, registration number, semester, and branch of study. The submission is for the subject "Basics of C".
The document provides information about Bjarne Stroustrup, the creator of C++ programming language, and introduces some basic concepts of C++ like tokens, keywords, identifiers, literals, character set, and a simple "Hello World" program. It mentions that Stroustrup added object-oriented programming features to the C language and named it C++. The document also lists different types of tokens, literals, and operators used in C++.
The document outlines the key components of a C++ program including basic input/output operations, data types, operators, control structures like decision making statements and looping statements, and provides examples of basic programs using these elements like checking if one number is greater than another or iterating through a loop. It also explains various tokens, stream objects like cout and cin, and control flow statements like if, else if, switch, while and for loops used for decision making and repetition in C++ programs.
This document provides an overview of the C programming language. It discusses that C was developed in 1972 at Bell Labs and is a popular systems and applications programming language. The document then covers various C language concepts like data types, variables, operators, input/output functions, and provides examples of basic C programs and code snippets.
The document discusses various operators in C++ including arithmetic, increment/decrement, assignment, relational, logical, and bitwise operators. It also covers topics such as loops, arrays, functions, pointers, classes, and objects. Key operators and concepts covered include addition, subtraction, multiplication, division, increment, decrement, assignment, comparison, logical AND/OR/NOT, bitwise AND/OR/XOR/complement, for/while/do-while loops, one and two dimensional arrays, user-defined and recursive functions, regular and double pointers, dynamic memory allocation, and the basics of classes and objects in C++.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
This document provides an overview of various data types, variables, and control flow structures in C programming. It defines common data types like char, int, float, and double. It also explains variable declaration and definition, and different types of variables like local, static, and global. The document further discusses arithmetic, logical, and comparison operators. Finally, it covers conditional statements like if-else and switch, and iterative structures like for and while loops.
introduction to c programming and C History.pptxManojKhadilkar1
C programming was developed by Dennis Ritchie at Bell Labs in 1972. It was derived from an earlier language called BCPL. A C program typically includes source code, which is compiled into object code and linked to produce an executable file. The structure of a C program includes sections for documentation, definitions, global declarations, functions, and the main function.
C programming is a widely used programming language. The document provides an overview of key concepts in C programming including variables, data types, operators, decision and loop control statements, functions, pointers, arrays, strings, structures, and input/output functions. It also provides examples to illustrate concepts like arrays, strings, functions, pointers, and structures. The main function is the entry point for all C programs where code execution begins.
The document defines various tokens in the C programming language including keywords, identifiers, constants, string literals, operators, data types, and variables. It discusses the basic data types like integers, floating point numbers, and characters. It also covers topics like declarations, global and local variables, type conversions, precedence and order of evaluation, and various operators used in C.
This document provides an overview of basic C# programming concepts. It covers topics such as program structure, data types, variables, operators, decision making statements, loops, classes and methods. Specifically, it discusses if/else statements, switch cases, ternary operators, for, while and do-while loops. It also provides examples of basic C# programs and explanations of concepts like classes, methods, constructors and namespaces.
C is a middle-level general purpose programming language developed in 1972. It uses characters, keywords, variables, constants, data types, expressions and operators. Variables are named locations used to store and manipulate data during execution. C supports several data types including integer, float, character and others. Operators perform actions like arithmetic, relational, logical and bitwise operations on variables and constants.
This document provides an overview of an intermediate computer programming course at the University of Gondar in Ethiopia. The course code is CoEng2111 and it is taught by instructor Wondimu B. Topics that will be covered in the course include C++ basics, arrays, strings, functions, and recursion. The course materials are presented over several pages that define concepts, provide code examples, and explain key ideas like variables, data types, operators, and array implementation in C++.
guia de referencia para a linguagem do fabricante CCS info_syntax.pdfSilvanildoManoeldaSi
This document provides a quick reference guide for the C programming language. It introduces C syntax for microcontrollers and other applications. C offers power and flexibility for programming microcontrollers. The guide provides examples of C language elements like comments, constants, variables, operators, and statements to get users started with basic C programming.
The document discusses the topics that will be covered in a C programming course, including:
- Introduction to C programming components like header files, preprocessor directives, main function, comments etc.
- Variable declaration and initialization, data types, operators, input/output functions like printf and scanf.
- Common statements like if-else, switch case, for, while and do-while loops.
- Functions, arrays, strings and pointers will also be covered along with evaluation methods like exams, assignments and attendance.
This document provides a quick introduction to the C programming language. It discusses basic C syntax like main functions, header files, comments, and variables. It also covers data types, memory, functions, scopes, expressions, control flow statements like if/else and loops. Functions can access arguments and global variables from their own scope but not variables from outer scopes unless passing their addresses.
The document provides a quick introduction to C programming, covering topics like functions, variables, memory, expressions, and recursion. It uses a recursive pow() function to demonstrate recursion and the call stack. Each function call gets its own copy of arguments and local variables, stored on the call stack.
The document provides a quick introduction to C programming. It discusses some key concepts in C including functions, variables, memory, data types, expressions and evaluation, and recursion. It provides an example of a recursive pow() function to calculate powers and discusses how function calls are handled through the stack.
This document provides information about C++ programming language tokens and input/output operations. It defines various tokens like keywords, identifiers, literals, punctuators, and operators. It explains the basic structure of a C++ program and defines important headers like iostream. It also describes input/output streams like cout and cin, and input/output operators like << and >>.
This document provides an introduction to Python programming concepts including data types, operators, control flow statements, functions and modules. It discusses the basic Python data types like integers, floats, booleans, strings, lists, tuples, dictionaries and sets. It also covers Python operators like arithmetic, assignment, comparison, logical and identity operators. Additionally, it describes control flow statements like if/else and for loops. Finally, it touches on functions, modules and input/output statements in Python.
The document is a student submission from Dinobandhu Thokdar of Kaliacchak Government Polytechnic. It includes the student's personal details such as name, registration number, semester, and branch of study. The submission is for the subject "Basics of C".
The document provides information about Bjarne Stroustrup, the creator of C++ programming language, and introduces some basic concepts of C++ like tokens, keywords, identifiers, literals, character set, and a simple "Hello World" program. It mentions that Stroustrup added object-oriented programming features to the C language and named it C++. The document also lists different types of tokens, literals, and operators used in C++.
The document outlines the key components of a C++ program including basic input/output operations, data types, operators, control structures like decision making statements and looping statements, and provides examples of basic programs using these elements like checking if one number is greater than another or iterating through a loop. It also explains various tokens, stream objects like cout and cin, and control flow statements like if, else if, switch, while and for loops used for decision making and repetition in C++ programs.
This document provides an overview of the C programming language. It discusses that C was developed in 1972 at Bell Labs and is a popular systems and applications programming language. The document then covers various C language concepts like data types, variables, operators, input/output functions, and provides examples of basic C programs and code snippets.
The document discusses various operators in C++ including arithmetic, increment/decrement, assignment, relational, logical, and bitwise operators. It also covers topics such as loops, arrays, functions, pointers, classes, and objects. Key operators and concepts covered include addition, subtraction, multiplication, division, increment, decrement, assignment, comparison, logical AND/OR/NOT, bitwise AND/OR/XOR/complement, for/while/do-while loops, one and two dimensional arrays, user-defined and recursive functions, regular and double pointers, dynamic memory allocation, and the basics of classes and objects in C++.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
How to Build a Module in Odoo 17 Using the Scaffold MethodCeline George
Odoo provides an option for creating a module by using a single line command. By using this command the user can make a whole structure of a module. It is very easy for a beginner to make a module. There is no need to make each file manually. This slide will show how to create a module using the scaffold method.
Main Java[All of the Base Concepts}.docxadhitya5119
This is part 1 of my Java Learning Journey. This Contains Custom methods, classes, constructors, packages, multithreading , try- catch block, finally block and more.
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPRAHUL
This Dissertation explores the particular circumstances of Mirzapur, a region located in the
core of India. Mirzapur, with its varied terrains and abundant biodiversity, offers an optimal
environment for investigating the changes in vegetation cover dynamics. Our study utilizes
advanced technologies such as GIS (Geographic Information Systems) and Remote sensing to
analyze the transformations that have taken place over the course of a decade.
The complex relationship between human activities and the environment has been the focus
of extensive research and worry. As the global community grapples with swift urbanization,
population expansion, and economic progress, the effects on natural ecosystems are becoming
more evident. A crucial element of this impact is the alteration of vegetation cover, which plays a
significant role in maintaining the ecological equilibrium of our planet.Land serves as the foundation for all human activities and provides the necessary materials for
these activities. As the most crucial natural resource, its utilization by humans results in different
'Land uses,' which are determined by both human activities and the physical characteristics of the
land.
The utilization of land is impacted by human needs and environmental factors. In countries
like India, rapid population growth and the emphasis on extensive resource exploitation can lead
to significant land degradation, adversely affecting the region's land cover.
Therefore, human intervention has significantly influenced land use patterns over many
centuries, evolving its structure over time and space. In the present era, these changes have
accelerated due to factors such as agriculture and urbanization. Information regarding land use and
cover is essential for various planning and management tasks related to the Earth's surface,
providing crucial environmental data for scientific, resource management, policy purposes, and
diverse human activities.
Accurate understanding of land use and cover is imperative for the development planning
of any area. Consequently, a wide range of professionals, including earth system scientists, land
and water managers, and urban planners, are interested in obtaining data on land use and cover
changes, conversion trends, and other related patterns. The spatial dimensions of land use and
cover support policymakers and scientists in making well-informed decisions, as alterations in
these patterns indicate shifts in economic and social conditions. Monitoring such changes with the
help of Advanced technologies like Remote Sensing and Geographic Information Systems is
crucial for coordinated efforts across different administrative levels. Advanced technologies like
Remote Sensing and Geographic Information Systems
9
Changes in vegetation cover refer to variations in the distribution, composition, and overall
structure of plant communities across different temporal and spatial scales. These changes can
occur natural.
This presentation was provided by Steph Pollock of The American Psychological Association’s Journals Program, and Damita Snow, of The American Society of Civil Engineers (ASCE), for the initial session of NISO's 2024 Training Series "DEIA in the Scholarly Landscape." Session One: 'Setting Expectations: a DEIA Primer,' was held June 6, 2024.
2. Who is the TA?
Name: Jingjing Duan
Office Hours: (might change)
Tue & Fri 2:00-4:00
Office: Room 266, WVH
Email: duanjj@ccs.neu.edu
Class web: www.ccs.neu.edu/course/csu480
3. Outline
“Hello World" Program
Data Types & Variables
printf()
Arithmetic & Logical
Operations
Conditionals
Loops
Arrays & Strings
Pointers
Functions
Command-Line Argument
Data Structure
Memory Allocation
Programming Tips
C vs. C++
Books recommended
4. Hello World Program
The source code
#include <stdio.h>
int main()
{
printf("Hello Worldn");
return(0);
}
5. Hello World Program
How to compile?
$ gcc hello.c –o hello
gcc compiling command
hello.c source file
hello compiler-generated executable file
Note: the default output filename is “a.out”
6. How to execute?
./hello
“./ ” indicates the following file “hello” resides under the
current directory.
Hello World Program
Q: why “.” is not included in $PATH
environment variable?
7. Hello World Program
A: security consideration.
Command Location Comment
ls /bin/ls provided by the
system
ls current directory virus
8. Name Description Size* Range*
char Character or small
integer
1 byte signed: -128 to 127
unsigned: 0 to 255
short int
(short)
Short integer 2 bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer 4 bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
long int
(long)
Long integer 4 bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
float Floating point
number
4 bytes 3.4e +/- 38 (7 digits)
double Double precision
floating point number
8 bytes 1.7e +/- 308 (15 digits)
long
double
Long double
precision floating
point number
8 bytes 1.7e +/- 308 (15 digits)
Data types
9. Variable Declaration
int length = 100;
char num = ‘9’; //The actual value is 57
float deposit = 240.5;
unsigned short ID = 0x5544;
Try the following statements, and see what happens
unsigned char value = -1;
printf(“The value is %d n”, value);
unsigned char value = 300;
printf(“The value is %d n”, value);
10. Result
Definition Memory layout Display comment
unsigned char
value = -1
11111111 255
unsigned char
value = 300
00101100 44 overflow
11. Local variable
Local variables are declared within the body of a function, and can
only be used within that function.
Static variable
Another class of local variable is the static type. It is specified by the
keyword static in the variable declaration.
The most striking difference from a non-static local variable is, a static
variable is not destroyed on exit from the function.
Global variable
A global variable declaration looks normal, but is located outside any
of the program's functions. So it is accessible to all functions.
Variable types
12. An example
int global = 10; //global variable
int func (int x)
{
static int stat_var; //static local variable
int temp; //(normal) local variable
int name[50]; //(normal) local variable
……
}
13. Variable Definition vs Declaration
Definition Tell the compiler about the variable: its type and
name, as well as allocated a memory cell for the
variable
Declaration Describe information ``about'' the variable,
doesn’t allocate memory cell for the variable
15. printf()
The printf() function can be instructed to print
integers, floats and string properly.
The general syntax is
printf( “format”, variables);
An example
int stud_id = 5200;
char * name = “Mike”;
printf(“%s ‘s ID is %d n”, name, stud_id);
16. Format Identifiers
%d decimal integers
%x hex integer
%c character
%f float and double number
%s string
%p pointer
How to specify display space for a variable?
printf(“The student id is %5d n”, stud_id);
The value of stud_id will occupy 5 characters space in the
print-out.
17. Why “n”
It introduces a new line on the terminal screen.
a alert (bell) character backslash
b backspace ? question mark
f formfeed ’ single quote
n newline ” double quote
r carriage return 000 octal number
t horizontal tab xhh hexadecimal number
v vertical tab
escape sequence
21. Example
Arithmetic operators
int i = 10;
int j = 15;
int add = i + j; //25
int diff = j – i; //5
int product = i * j; // 150
int quotient = j / i; // 1
int residual = j % i; // 5
i++; //Increase by 1
i--; //Decrease by 1
22. Comparing them
int i = 10;
int j = 15;
float k = 15.0;
j / i = ?
j % i = ?
k / i = ?
k % i = ?
23. The Answer
j / i = 1;
j % i = 5;
k / i = 1.5;
k % i It is illegal.
Note: For %, the operands can only be integers.
24. Logical Operations
What is “true” and “false” in C
In C, there is no specific data type to represent “true” and “false”. C
uses value “0” to represent “false”, and uses non-zero value to stand
for “true”.
Logical Operators
A && B => A and B
A || B=> A or B
A == B => Is A equal to B?
A != B => Is A not equal to B?
25. A > B => Is A greater than B?
A >= B => Is A greater than or equal to B?
A < B => Is A less than B?
A <= B => Is A less than or equal to B?
Don’t be confused
&& and || have different meanings from & and |.
& and | are bitwise operators.
26. Short circuiting
Short circuiting means that we don't
evaluate the second part of an AND or OR
unless we really need to.
Some practices
Please compute the value of the following
logical expressions?
27. int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) =>
if( i != j || k < j) =>
if( j<= k || i > k) =>
if( j == k && m) =>
if(i) =>
if(m || j && i ) =>
28. int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true
Did you get the correct answers?
31. An example
if(score >= 90){
a_cnt ++;
}else if(score >= 80){
b_cnt++;
}else if(score >= 70){
c_cnt++;
}else if (score>= 60){
d_cnt++
}else{
f_cnt++
}
32. The switch statement
switch (expression)
{
case item1:
statement;
break;
case item2:
statement;
break;
default:
statement;
break;
}
33. Loops
for statement
for (expression1; expression2; expression3){
statement…
}
expression1 initializes;
expression2 is the terminate test;
expression3 is the modifier;
34. An example
int x;
for (x=0; x<3; x++)
{
printf("x=%dn",x);
}
First time: x = 0;
Second time: x = 1;
Third time: x = 2;
Fourth time: x = 3; (don’t execute the body)
35. The while statement
while (expression) {
statement …
}
while loop exits only when the expression is false.
An example
int x = 3;
while (x>0) {
printf("x=%d n",x);
x--;
}
36. for <==> while
for (expression1;
expression2;
expression3){
statement…
}
expression1;
while (expression2)
{
statement…;
expression3;
}
equals
37. Arrays & Strings
Arrays
int ids[50];
char name[100];
int table_of_num[30][40];
Accessing an array
ids[0] = 40;
i = ids[1] + j;
table_of_num[3][4] = 100;
Note: In C Array subscripts start at 0 and end one less than
the array size. [0 .. n-1]
38. Strings
Strings are defined as arrays of characters.
The only difference from a character array is, a symbol “0”
is used to indicate the end of a string.
For example, suppose we have a character array, char
name[8], and we store into it a string “Dave”.
Note: the length of this string 4, but it occupies 5 bytes.
D a v e 0
39. Functions
Functions are easy to use; they allow complicated programs to be broken
into small blocks, each of which is easier to write, read, and maintain.
This is called modulation.
How does a function look like?
returntype function_name(parameters…)
{
local variables declaration;
function code;
return result;
}
40. Sample function
int addition(int x, int y)
{
int add;
add = x + y;
return add;
}
How to call a function?
int result;
int i = 5, j = 6;
result = addition(i, j);
41. Pointers
Pointer is the most beautiful (ugliest) part of C, but also
brings most trouble to C programmers. Over 90% bugs in
the C programs come from pointers.
“The International Obfuscated C Code Contest ”
(http://www.ioccc.org/)
What is a pointer?
A pointer is a variable which contains the address in memory
of another variable.
In C we have a specific type for pointers.
42. Declaring a pointer variable
int * pointer;
char * name;
How to obtain the address of a variable?
int x = 0x2233;
pointer = &x;
where & is called address of operator.
How to get the value of the variable indicated by the
pointer?
int y = *pointer;
43. 33 22 00 00
0x5200 0x5203
0x5200
pointer
What happens in the memory?
Suppose the address of variable x is 0x5200 in the above
example, so the value of the variable pointer is 0x5200.
X
47. Pointers and Arrays
Pointers and arrays are very closely linked in C.
Array elements arranged in consecutive memory locations
Accessing array elements using pointers
int ids[50];
int * p = &ids[0];
p[i] <=> ids[i]
Pointers and Strings
A string can be represented by a char * pointer.
48. Char name[50];
name[0] = ‘D’;
name[1] = ‘a’;
name[2] = ‘v’;
name[3] = ‘e’;
name[4] = ‘0’;
char * p = &name[0];
printf(“The name is %s n”, p);
Note: The p represents the string “Dave”, but not the array
name[50].
49. Command-Line Argument
In C you can pass arguments to main() function.
main() prototype
int main(int argc, char * argv[]);
argc indicates the number of arguments
argv is an array of input string pointers.
How to pass your own arguments?
./hello 10
50. What value is argc and argv?
Let’s add two printf statement to get the value of argc and
argv.
#include <stdio.h>
int main(int argc, char * argv[]);)
{
int i=0;
printf("Hello Worldn");
printf(“The argc is %d n”, argc);
for(i=0; i < argc; i++){
printf(“The %dth element in argv is %sn”, i, argv[i]);
}
return(0);
}
51. The output
The argc is 2
The 0th element in argv is ./hello
The 1th element in argv is 10
The trick is the system always passes the name of the
executable file as the first argument to the main() function.
How to use your argument?
Be careful. Your arguments to main() are always in string format.
Taking the above program for example, the argv[1] is string “10”,
not a number. You must convert it into a number before you can
use it.
52. Data Structure
A data structure is a collection of one or more variables, possibly of
different types.
An example of student record
struct stud_record{
char name[50];
int id;
int age;
int major;
……
};
53. A data structure is also a data type
struct stud_record my_record;
struct stud_record * pointer;
pointer = & my_record;
Accessing a field inside a data structure
my_record.id = 10; “.”
or
pointer->id = 10; “->”
54. Memory Allocation
Stack memory allocation
Non-static local variable is an example of stack memory
allocation.
Such memory allocations are placed in a system memory
area called the stack.
Static memory allocation
Static local variable and global variable require static
memory allocation. Static memory allocation happens
before the program starts, and persists through the entire
life time of the program.
55. Dynamic memory allocation
It allows the program determine how much memory it
needs at run time, and allocate exactly the right amount of
storage.
The region of memory where dynamic allocation and
deallocation of memory can take place is called the heap.
Note: the program has the responsibility to free the
dynamic memory it allocated.
57. Functions for the dynamic memory allocation
void *malloc(size_t number_of_bytes);
allocates dynamic memory
size_t sizeof(type);
returns the number of bytes of type
void free(void * p)
releases dynamic memory allocation
An example of dynamic memory allocation
int * ids; //id arrays
int num_of_ids = 40;
ids = malloc( sizeof(int) * num_of_ids);
…….. Processing …...
free(ids);
58. Allocating a data structure instance
struct stud_record * pointer;
pointer = malloc(sizeof(struct stud_record));
pointer->id = 10;
Never calculate the size of data structure yourself. The
reason is the size of data types is machine-dependent. Give
it to sizeof() function.
size of int
32-bytes machines 32
64-bytes machines 64
59. Programming Tips
Replacing numbers in your code with macros
- don’t use magic numbers directly
#define MAX_NAME_LEN 50;
char name[MAX_NAME_LEN];
Avoiding global variables
- modulation is more important
Giving variables and functions a nice name
- a meaning name
Don’t repeat your code
- make a subroutine/function
Don’t let the function body to exceed one screen
- hard to debug
60. Indenting your code (clearance)
if(expression)
{
if(expression)
{
……
}
}
Commenting your code
Don’t rush into coding. Plan first.
Printing out more debugging information
Using debugger (gdb)
61. C vs. C++
C++ is a superset of C
C++ has all the characteristics of C
Using g++ to compile your source code
62. Books recommended
The C Programming Language, Brian Kernighan
and Dennis Ritchie. Second edition. Prentice-Hall,
1988. (C Bible)
The C++ Programming Language, Bjarne
Stroustrup. Third edition. Addison-Wesley, 1997.
(C++ Bible)
Advanced Programming in the UNIX
Environment, W. Richard Stevens, Addison-
Wesley, 1992. (APUE)