A Comprehensive Guide to C Programing Basics, Variable Declarations, Input/O...Muhammad Khubaib Awan
Title: Program Basics in C Language
Description:
This comprehensive presentation delves into the foundational concepts of programming in C, designed for both novice and experienced programmers seeking to strengthen their understanding of the language's core principles.
Outline:
Program Skeleton: Explore the essential structure of a C program, including preprocessor directives, global declarations, functions, local declarations, and statements that form the backbone of any C program.
Comments and Documentation: Learn the importance of comments in enhancing code readability and maintaining documentation for effective programming practices.
Identifiers and Reserved Words: Gain insights into the naming conventions in C, distinguishing between identifiers and reserved keywords that have predefined meanings within the language.
Variable Declarations: Understand how to allocate memory and declare variables using atomic types such as void, int, float, and char, ensuring efficient data management in your programs.
Constants: Discover the various types of constants, including literals and defined constants, and their role in programming.
Formatted Input/Output: Master the techniques for handling input and output in C, covering file operations, the printf function for monitor output (including format strings and field specifications), and the scanf function for keyboard input (including format strings and address lists).
Prompting for Input: Learn effective strategies for prompting users for input, enhancing user interaction within your programs.
This presentation serves as an essential resource for anyone interested in mastering the basics of C programming, equipping you with the knowledge to write effective and efficient C programs.
The document provides an introduction to the C programming language, including its history, features, character sets, tokens, data types, operators, and the basic structure of a C program. It discusses key concepts such as variables, constants, comments, functions, preprocessing directives, and how to compile and execute a C program.
The document provides an introduction to the C programming language, including its history, features, character sets, tokens, data types, operators, and the basic structure of a C program. It discusses key concepts such as variables, constants, comments, functions, input/output, and how to compile and execute a C program.
Overview:
C is a general-purpose computer programming language created in the 1970s by Dennis Ritchie.
It remains widely used and influential due to its clean reflection of CPU capabilities.
Originally developed at Bell Labs, it was applied to re-implement the kernel of the Unix operating system.
C is commonly used on computer architectures ranging from the largest supercomputers to the smallest microcontrollers and embedded systems.
Features and Characteristics:
Imperative Procedural Language: C follows a procedural paradigm, emphasizing step-by-step instructions.
Structured Programming: Programs are divided into modules, making testing, maintenance, and debugging easier.
Low-Level Access to Memory: C provides direct memory manipulation.
Static Type System: Type checking is done at compile time.
Recursion Support: Functions can call themselves.
Cross-Platform Capability: C compilers are available for various architectures and operating systems.
Applications:
Operating Systems: C is widely used in OS development.
Device Drivers: C is essential for hardware interaction.
Protocol Stacks: Networking protocols are often implemented in C.
Embedded Systems: C is popular for microcontrollers and firmware.
Utilities and Tools: Many system utilities are written in C.
History:
ALGOL: The base language for C, introduced in 1960.
BCPL: Developed in 1967 for system software.
B: Created by Ken Thompson in 1970, influenced by BCPL.
C: Developed by Dennis Ritchie in 1972, combining features from ALGOL, BCPL, and B.
Standardization: ANSI and ISO standardized C in 1989.
Example “Hello, World!” Program:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
This simple program prints “Hello, World!” to the screen using the printf() function from the standard input-output library.
Remember, C’s legacy extends beyond application software, making it a foundational language for understanding programming concepts and system-level development.C is a general-purpose programming language that was developed in the early 1970s by Dennis Ritchie at Bell Laboratories. It is known for its efficiency and is widely used for system programming, including developing operating systems like UNIX. C is a procedural language, which means it follows a set of instructions step by step to perform a task. It allows low-level access to memory and provides a clean and simple syntax, making it a fundamental language in the field of computer science.These tags cover various aspects of C programming, from its use in system programming and embedded systems to its importance in learning coding fundamentals. Feel free to use these tags on social media platforms, blog posts, or other content related to C programming!
Btech i pic u-2 datatypes and variables in c languageRai University
This document discusses various topics related to C programming language including basic structure of a C program, programming rules, comments, main method, declaration, data types, variables, operators, and input/output functions. It explains that a C program contains functions organized into sections like main, documentation, definitions, and subprograms. It provides rules for writing comments, declaring and initializing variables, and defining valid identifiers. It also describes different data types in C like integer, float, character, and string. Finally, it discusses various operators supported in C for arithmetic, relational, logical, assignment, increment/decrement operations.
Bsc cs i pic u-2 datatypes and variables in c languageRai University
The document discusses various topics related to C programming language including basic structure of a C program, programming rules, comments, main method, declaration, data types, variables, operators, and conditional operators. It provides explanations and examples for each topic. The key points are that a C program contains functions organized into sections, all statements must end with a semicolon, comments are enclosed in /* */ , the main() function contains the main program logic, variables must be declared before use with a specified data type, and operators are used to manipulate variables and perform tasks like arithmetic, comparisons, assignments, and conditionals.
Diploma ii cfpc u-2 datatypes and variables in c languageRai University
The document discusses various topics related to C programming language such as basic structure of a C program, programming rules, comments, main method, declaration, data types, variables, operators, and conditional operators. It explains that a C program consists of functions organized into sections like documentation, definitions, global declarations, main function, and subprograms. It provides rules for writing comments, declaring variables, and defining valid identifiers and constants. It also describes various data types in C including integer, floating point, character, and string types.
Mca i pic u-2 datatypes and variables in c languageRai University
This document discusses various concepts related to data types and variables in the C programming language. It covers the basic structure of a C program and sections like documentation, definition, global declaration, main function, and subprogram sections. It describes rules for comments, the main method, declaration, data types like integer, floating point, character, and string. It also discusses variables, assigning values, and declaring variables with different storage classes.
This document provides an overview of basic concepts in C programming including data types, variables, constants, and storage classes. It discusses the basic structure of a C program and sections like main(), functions, and declarations. It describes various data types in C like integer, float, character, and string. Rules for identifiers, keywords, and comments are also covered. The document explains variable declaration and assignment as well as constants like integer, real, character, string and escape sequences. Storage classes and scope of variables are discussed.
structure of c program. everything about the structure is in this ppt...................................................................viearhgviuehdrgbvkejfsdbvaerhbgf;oiweHFGIO;WENEGV;KLADFN;OVIBNA;OINVO;IRANV;OINDF;LNVOIASRDNGVIOERNAVB EOANGVV ERNGOEWN
Here are the values of c in each case:
1. int a = 10, b = 2;
c = 12, 8, 20, 5
2. float a = 10, b = 2;
c = 12, 8, 20, 5
3. int a = 10; float b = 2;
c = 12, 8, 20, 5
The data types of the operands determine the result. For integer operands, the result is an integer. For floating point operands, the result is floating point.
C was developed by Dennis Ritchie at Bell Labs in 1972. It is a structured, portable, middle-level programming language that uses a top-down approach. A C program consists of functions, with one function named main serving as the starting point. C programs can include comments, preprocessor directives, variables, constants, data types and expressions connected with operators.
This document provides an overview of C programming basics including problem formulation, problem solving, and the fundamentals of C programming. It discusses problem formulation as an iterative process involving decomposing problems into appropriate dimensions. It then covers problem solving methods and introduces C programming, describing its features, character set, tokens, variables, data types, operators, and the structure of a basic C program. It provides examples of programming concepts like input/output operations, decision making, looping, and solving simple problems in C.
This document provides an overview of C programming and data structures. It begins with an introduction to C language concepts like data types, variables, constants, I/O functions, operators, and control statements. It then discusses the history and evolution of C from earlier languages like ALGOL and BCPL. The document outlines characteristics of C and its applications. It also covers topics like keywords, identifiers, data type sizes, variable naming rules, and comment syntax. Library functions for input/output like scanf and printf are explained. The different types of constants in C like integer, real, character, and string constants are defined along with their syntax rules.
The document provides information on the C programming language, including its history, structure, and key concepts. It discusses how C was developed at Bell Labs in the 1970s and influenced by other languages. It also describes the basic structure of C programs, which typically include header files, main functions, and statements organized into functions. The document also covers important C concepts like data types, variables, constants, and tokens.
C is a general-purpose programming language developed at Bell Labs in the 1970s. It discusses the basics of C programming, including its history, structure, keywords, variables, data types, and provides an example "Hello World" program. The document provides an overview of the key elements of C for a beginner programmer to understand how to get started with C.
The document discusses data types in C programming. It covers the basic data types like int, float, char, etc. It also discusses variables, constants, keywords, operators and functions in C. The document provides examples of integer, real and character constants. It explains variable naming rules and different data types supported in C like char, short, int, float, double etc along with their sizes and ranges. It also provides a simple "Hello World" example C program.
The document provides an overview of the C programming language. It discusses that C was developed at Bell Labs in the 1970s and influenced by other languages like BCPL. The core concepts covered include C's character set, keywords, basic program structure with main function, header files, data types, variables, constants, and declarations. An example "Hello World" C program is also given.
This document provides an overview of constants, variables, and data types in the C programming language. It discusses the different categories of characters used in C, C tokens including keywords, identifiers, constants, strings, special symbols, and operators. It also covers rules for identifiers and variables, integer, floating point, void, and character data types, and describes constants like integer, real, single character, string, and backslash character constants.
This document provides an overview of constants, variables, and data types in the C programming language. It discusses the different categories of characters used in C, C tokens including keywords, identifiers, constants, strings, special symbols, and operators. It also covers rules for identifiers and variables, integer constants, real constants, single character constants, string constants, and backslash character constants. Finally, it describes the primary data types in C including integer, character, floating point, double, and void, as well as integer, floating point, and character types.
The document discusses the C programming language. It provides the definition of a program and source code for a simple "Hello World" program in C. It describes how C was developed in 1972 and some key characteristics of C like it being a structured, portable, middle-level language. The document also provides information on variables, data types, constants, and how to compile and run a C program.
The document discusses the C programming language. It provides the definition of a program and source code for a simple "Hello World" program in C. It describes how C was developed by Dennis Ritchie in 1972 and some key features of C like it being a structured, portable, middle-level language. The document also provides information on program structure in C, writing, compiling and running a C program, identifiers, constants, data types and more.
Struggling with complex aerospace engineering concepts? This comprehensive guide is designed to support students tackling assignments, homework, and projects in Aerospace Engineering. From aerodynamics and propulsion systems to orbital mechanics and structural analysis, we cover all the essential topics that matter.
Whether you're facing challenges in understanding principles or simply want to improve your grades, this guide outlines the key areas of study, common student hurdles, tips for success, and the benefits of professional tutoring and assignment help services.
WhatsApp:- +91-9878492406
Email:- [email protected]
Visit:- https://onlinecollegehomeworkhelp.com/aerospace-engineering-assignment-help
Ad
More Related Content
Similar to C language ppt is a presentation of how to explain the introduction of a c languguage (20)
Diploma ii cfpc u-2 datatypes and variables in c languageRai University
The document discusses various topics related to C programming language such as basic structure of a C program, programming rules, comments, main method, declaration, data types, variables, operators, and conditional operators. It explains that a C program consists of functions organized into sections like documentation, definitions, global declarations, main function, and subprograms. It provides rules for writing comments, declaring variables, and defining valid identifiers and constants. It also describes various data types in C including integer, floating point, character, and string types.
Mca i pic u-2 datatypes and variables in c languageRai University
This document discusses various concepts related to data types and variables in the C programming language. It covers the basic structure of a C program and sections like documentation, definition, global declaration, main function, and subprogram sections. It describes rules for comments, the main method, declaration, data types like integer, floating point, character, and string. It also discusses variables, assigning values, and declaring variables with different storage classes.
This document provides an overview of basic concepts in C programming including data types, variables, constants, and storage classes. It discusses the basic structure of a C program and sections like main(), functions, and declarations. It describes various data types in C like integer, float, character, and string. Rules for identifiers, keywords, and comments are also covered. The document explains variable declaration and assignment as well as constants like integer, real, character, string and escape sequences. Storage classes and scope of variables are discussed.
structure of c program. everything about the structure is in this ppt...................................................................viearhgviuehdrgbvkejfsdbvaerhbgf;oiweHFGIO;WENEGV;KLADFN;OVIBNA;OINVO;IRANV;OINDF;LNVOIASRDNGVIOERNAVB EOANGVV ERNGOEWN
Here are the values of c in each case:
1. int a = 10, b = 2;
c = 12, 8, 20, 5
2. float a = 10, b = 2;
c = 12, 8, 20, 5
3. int a = 10; float b = 2;
c = 12, 8, 20, 5
The data types of the operands determine the result. For integer operands, the result is an integer. For floating point operands, the result is floating point.
C was developed by Dennis Ritchie at Bell Labs in 1972. It is a structured, portable, middle-level programming language that uses a top-down approach. A C program consists of functions, with one function named main serving as the starting point. C programs can include comments, preprocessor directives, variables, constants, data types and expressions connected with operators.
This document provides an overview of C programming basics including problem formulation, problem solving, and the fundamentals of C programming. It discusses problem formulation as an iterative process involving decomposing problems into appropriate dimensions. It then covers problem solving methods and introduces C programming, describing its features, character set, tokens, variables, data types, operators, and the structure of a basic C program. It provides examples of programming concepts like input/output operations, decision making, looping, and solving simple problems in C.
This document provides an overview of C programming and data structures. It begins with an introduction to C language concepts like data types, variables, constants, I/O functions, operators, and control statements. It then discusses the history and evolution of C from earlier languages like ALGOL and BCPL. The document outlines characteristics of C and its applications. It also covers topics like keywords, identifiers, data type sizes, variable naming rules, and comment syntax. Library functions for input/output like scanf and printf are explained. The different types of constants in C like integer, real, character, and string constants are defined along with their syntax rules.
The document provides information on the C programming language, including its history, structure, and key concepts. It discusses how C was developed at Bell Labs in the 1970s and influenced by other languages. It also describes the basic structure of C programs, which typically include header files, main functions, and statements organized into functions. The document also covers important C concepts like data types, variables, constants, and tokens.
C is a general-purpose programming language developed at Bell Labs in the 1970s. It discusses the basics of C programming, including its history, structure, keywords, variables, data types, and provides an example "Hello World" program. The document provides an overview of the key elements of C for a beginner programmer to understand how to get started with C.
The document discusses data types in C programming. It covers the basic data types like int, float, char, etc. It also discusses variables, constants, keywords, operators and functions in C. The document provides examples of integer, real and character constants. It explains variable naming rules and different data types supported in C like char, short, int, float, double etc along with their sizes and ranges. It also provides a simple "Hello World" example C program.
The document provides an overview of the C programming language. It discusses that C was developed at Bell Labs in the 1970s and influenced by other languages like BCPL. The core concepts covered include C's character set, keywords, basic program structure with main function, header files, data types, variables, constants, and declarations. An example "Hello World" C program is also given.
This document provides an overview of constants, variables, and data types in the C programming language. It discusses the different categories of characters used in C, C tokens including keywords, identifiers, constants, strings, special symbols, and operators. It also covers rules for identifiers and variables, integer, floating point, void, and character data types, and describes constants like integer, real, single character, string, and backslash character constants.
This document provides an overview of constants, variables, and data types in the C programming language. It discusses the different categories of characters used in C, C tokens including keywords, identifiers, constants, strings, special symbols, and operators. It also covers rules for identifiers and variables, integer constants, real constants, single character constants, string constants, and backslash character constants. Finally, it describes the primary data types in C including integer, character, floating point, double, and void, as well as integer, floating point, and character types.
The document discusses the C programming language. It provides the definition of a program and source code for a simple "Hello World" program in C. It describes how C was developed in 1972 and some key characteristics of C like it being a structured, portable, middle-level language. The document also provides information on variables, data types, constants, and how to compile and run a C program.
The document discusses the C programming language. It provides the definition of a program and source code for a simple "Hello World" program in C. It describes how C was developed by Dennis Ritchie in 1972 and some key features of C like it being a structured, portable, middle-level language. The document also provides information on program structure in C, writing, compiling and running a C program, identifiers, constants, data types and more.
Struggling with complex aerospace engineering concepts? This comprehensive guide is designed to support students tackling assignments, homework, and projects in Aerospace Engineering. From aerodynamics and propulsion systems to orbital mechanics and structural analysis, we cover all the essential topics that matter.
Whether you're facing challenges in understanding principles or simply want to improve your grades, this guide outlines the key areas of study, common student hurdles, tips for success, and the benefits of professional tutoring and assignment help services.
WhatsApp:- +91-9878492406
Email:- [email protected]
Visit:- https://onlinecollegehomeworkhelp.com/aerospace-engineering-assignment-help
This presentation has been made keeping in mind the students of undergraduate and postgraduate level. To keep the facts in a natural form and to display the material in more detail, the help of various books, websites and online medium has been taken. Whatever medium the material or facts have been taken from, an attempt has been made by the presenter to give their reference at the end.
The Lohar dynasty of Kashmir is a new chapter in the history of ancient India. We get to see an ancient example of a woman ruling a dynasty in the Lohar dynasty.
As of 5/17/25, the Southwestern outbreak has 865 cases, including confirmed and pending cases across Texas, New Mexico, Oklahoma, and Kansas. Experts warn this is likely a severe undercount. The situation remains fluid, though we are starting to see a significant reduction in new cases in Texas. Experts project the outbreak could last up to a year.
CURRENT CASE COUNT: 865 (As of 5/17/2025)
- Texas: 720 (+2) (62% of cases are in Gaines County)
- New Mexico: 74 (+3) (92.4% of cases are from Lea County)
- Oklahoma: 17
- Kansas: 54 (38.89% of the cases are from Gray County)
HOSPITALIZATIONS: 102
- Texas: 93 - This accounts for 13% of all cases in Texas.
- New Mexico: 7 – This accounts for 9.47% of all cases in New Mexico.
- Kansas: 2 - This accounts for 3.7% of all cases in Kansas.
DEATHS: 3
- Texas: 2 – This is 0.28% of all cases
- New Mexico: 1 – This is 1.35% of all cases
US NATIONAL CASE COUNT: 1,038 (Confirmed and suspected)
INTERNATIONAL SPREAD (As of 5/17/2025)
Mexico: 1,412 (+192)
- Chihuahua, Mexico: 1,363 (+171) cases, 1 fatality, 3 hospitalizations
Canada: 2,191 (+231) (Includes
Ontario’s outbreak, which began in November 2024)
- Ontario, Canada – 1,622 (+182), 101 (+18) hospitalizations
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...EduSkills OECD
Andreas Schleicher, Director for Education and Skills at the OECD, presents at the launch of the OECD report 'The State of Global Teenage Career Preparation' on the 20 May 2025. You can check out the video recording of the launch on the OECD website - https://oecdedutoday.com/webinars/
The Quiz Club of PSGCAS brings to you a battle...
Get ready to unleash your inner know-it-all! 🧠💥 We're diving headfirst into a quiz so epic, it makes Mount Everest look like a molehill! From chart-topping pop sensations that defined generations and legendary sports moments that still give us goosebumps, to ancient history that shaped the world and, well, literally EVERYTHING in between! Prepare for a whirlwind tour of trivia that will stretch your brain cells to their absolute limits and crown the ultimate quiz champion. This isn't just a quiz; it's a battle of wits, a test of trivia titans! Are you ready to conquer it all?
QM: VIKASHINI G
THE QUIZ CLUB OF PSGCAS(2022-25)
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleCeline George
One of the key aspects contributing to efficient sales management is the variety of views available in the Odoo 18 Sales module. In this slide, we'll explore how Odoo 18 enables businesses to maximize sales insights through its Kanban, List, Pivot, Graphical, and Calendar views.
How to Add Button in Chatter in Odoo 18 - Odoo SlidesCeline George
Improving user experience in Odoo often involves customizing the chatter, a central hub for communication and updates on specific records. Adding custom buttons can streamline operations, enabling users to trigger workflows or generate reports directly.
Slides from a Doctoral Virtual Information Session presented by staff and faculty of Capitol Technology University. Covers program details, admissions, tuition, financial aid and other information needed to consider earning a doctorate from Capitol. Presented May 18, 2025.
This presentation covers the conditions required for the application of Boltzmann Law, aimed at undergraduate nursing and allied health science students studying Biophysics. It explains the prerequisites for the validity of the law, including assumptions related to thermodynamic equilibrium, distinguishability of particles, and energy state distribution.
Ideal for students learning about molecular motion, statistical mechanics, and energy distribution in biological systems.
How to Manage Manual Reordering Rule in Odoo 18 InventoryCeline George
Reordering rules in Odoo 18 help businesses maintain optimal stock levels by automatically generating purchase or manufacturing orders when stock falls below a defined threshold. Manual reordering rules allow users to control stock replenishment based on demand.
PREPARE FOR AN ALL-INDIA ODYSSEY!
THE QUIZ CLUB OF PSGCAS BRINGS YOU A QUIZ FROM THE PEAKS OF KASHMIR TO THE SHORES OF KUMARI AND FROM THE DHOKLAS OF KATHIAWAR TO THE TIGERS OF BENGAL.
QM: EIRAIEZHIL R K, THE QUIZ CLUB OF PSGCAS
How to Change Sequence Number in Odoo 18 Sale OrderCeline George
In this slide, we’ll discuss on how to change sequence number in Odoo 18 Sale Order. In Odoo, sequences are used to generate unique identifiers for records. These identifiers are often displayed as reference numbers, such as invoice numbers, purchase order numbers, or customer numbers.
How to Use Upgrade Code Command in Odoo 18Celine George
In this slide, we’ll discuss on how to use upgrade code Command in Odoo 18. Odoo 18 introduced a new command-line tool, upgrade_code, designed to streamline the migration process from older Odoo versions. One of its primary functions is to automatically replace deprecated tree views with the newer list views.
Unleash your inner trivia titan! Our upcoming quiz event is your chance to shine, showcasing your knowledge across a spectrum of fascinating topics. Get ready for a dynamic evening filled with challenging questions designed to spark your intellect and ignite some friendly rivalry. Gather your smartest companions and form your ultimate quiz squad – the competition is on! From the latest headlines to the classics, prepare for a mental workout that's as entertaining as it is engaging. So, sharpen your wits, prepare your answers, and get ready to battle it out for bragging rights and maybe even some fantastic prizes. Don't miss this exciting opportunity to test your knowledge and have a blast!
QUIZMASTER : GOWTHAM S, BCom (2022-25 BATCH), THE QUIZ CLUB OF PSGCAS
GUESS WHO'S HERE TO ENTERTAIN YOU DURING THE INNINGS BREAK OF IPL.
THE QUIZ CLUB OF PSGCAS BRINGS YOU A QUESTION SUPER OVER TO TRIUMPH OVER IPL TRIVIA.
GET BOWLED OR HIT YOUR MAXIMUM!
2. Outline
II. Program Basics
A. Program skeleton
preprocessor directives
global declarations
functions
local declarations
statements
B. Comments and Documentation
C. Names (identifiers)
reserved words
3. Outline (cont)
II. Program Basics (cont)
D. Variable declarations
1. Memory allocation
2. Atomic types
void, int, float, char
E. Constants
1. literal
2. defined
3. memory
4. Outline (cont)
II. Program Basics (cont)
F. Formatted input/output
1. Files
2. Printf (monitor output)
a. format strings
field specifications
b. data list
3. Scanf (keyboard input)
a. format strings
b. address list
4. Prompting for Input
5. History of C
1960: ALGOL (ALGOrithmic Language)
1967: BCPL (Basic Combined Programming
Language)
1970: B programming language (typeless)
1972: C: BCPL plus B with types
1978: Kernighan + Ritchie standard for C
1989: ANSI standard for C
6. C Program Structure
• Program defined by:
– global declarations
– function definitions
• May contain preprocessor
directives
• Always has one function
named main, may contain
others
Preprocessor Directives
Global Declarations
Function Definitions
int main () {
}
Local Declarations
Statements
7. Parts of a Program
#include <stdio.h>
int x;
int main () {
int y;
printf("Enter x and y: ");
scanf(&x,&y);
printf("Sum is %dn",x+y);
}
Preprocessor Directive
Global Declaration
Function
Local Declaration
Statements
8. Preprocessor Directives
• Begin with #
• Instruct compiler to perform some
transformation to file before compiling
• Example: #include <stdio.h>
– add the header file stdio.h to this file
– .h for header file
– stdio.h defines useful input/output functions
9. Declarations
• Global
– visible throughout program
– describes data used throughout program
• Local
– visible within function
– describes data used only in function
10. Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later
11. Main Function
• Every program has one function main
• Header for main: int main ()
• Program is the sequence of statements
between the { } following main
• Statements are executed one at a time from
the one immediately following to main to
the one before the }
12. Comments
• Text between /* and */
• Used to “document” the code for the human
reader
• Ignored by compiler (not part of program)
• Have to be careful
– comments may cover multiple lines
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
13. Comment Example
#include <stdio.h>
/* This comment covers
* multiple lines
* in the program.
*/
int main () /* The main header */ {
/* No local declarations */
printf(“Too many commentsn”);
} /* end of main */
14. Documentation
• Global - start of program, outlines overall
solution, may include structure chart
• Module - when using separate files, indication
of what each file solves
• Function - inputs, return values, and logic used
in defining function
• Add documentation for key (tough to
understand) comments
• Names of variables - should be chosen to be
meaningful, make program readable
15. Syntax of C
• Rules that define C language
– Specify which tokens are valid
– Also indicate the expected order of tokens
• Some types of tokens:
– reserved words: include printf int ...
– identifiers: x y ...
– literal constants: 5 ‘a’ 5.0 ...
– punctuation: { } ; < > # /* */
16. Identifier
• Names used for objects in C
• Rules for identifiers in C:
– first char alphabetic [a-z,A-Z] or underscore (_)
– has only alphabetic, digit, underscore chars
– first 31 characters are significant
– cannot duplicate a reserved word
– case (upper/lower) matters
17. Reserved Words
• Identifiers that already have meaning in C
• Examples:
– include, main, printf, scanf, if, else, …
– more as we cover C language
19. Program Execution
• Global declarations set up
• Function main executed
– local declarations set up
– each statement in statement section executed
• executed in order (first to last)
• changes made by one statement affect later
statements
20. Variables
• Named memory location
• Variables declared in global or local declaration
sections
• Syntax: Type Name;
• Examples:
int sum;
float avg;
char dummy;
21. Variable Type
• Indicates how much memory to set aside for
the variable
• Also determines how that space will be
interpreted
• Basic types: char, int, float
– specify amount of space (bytes) to set aside
– what can be stored in that space
– what operations can be performed on those vars
22. Variable Name
• Legal identifier
• Not a reserved word
• Must be unique:
– not used before
– variable names in functions (local declarations)
considered to be qualified by function name
– variable x in function main is different from x in
function f1
23. Multiple Variable Declarations
• Can create multiple variables of the same
type in one statement:
int x, y, z;
is a shorthand for
int x;
int y;
int z;
- stylistically, the latter is often preferable
24. Variable Initialization
• Giving a variable an initial value
• Variables not necessarily initialized when
declared (value is unpredictable - garbage)
• Can initialize in declaration:
• Syntax: Type Name = Value;
• Example:
int x = 0;
25. Initialization Values
• Literal constant (token representing a value,
like 5 representing the integer 5)
• An expression (operation that calculates a
value)
• Function call
• The value, however specified, must be of the
correct type
26. Multiple Declaration Initialization
• Can provide one value for variables
initialized in one statement:
int x, y, z = 0;
• Each variable declared and then initialized
with the value
27. Type
• Set of possible values
– defines size, how values stored, interpreted
• Operations that can be performed on those
possible values
• Data types are associated with objects in C
(variables, functions, etc.)
28. Standard Types
• Atomic types (cannot be broken down)
– void
– char
– int
– float, double
• Derived types
– composed of other types
29. Literal Constants
• Sequences of characters (tokens) that
correspond to values from that type
-35 is the integer -35
3.14159 is the floating pointer number 3.14159
‘A’ is the character A
• Can be used to initialize variables
30. Void Type
• Type name: void
• Possible values: none
• Operations: none
• Useful as a placeholder
31. Integer Type
• Type name:
– int
– short int
– long int
• Possible values: whole numbers (within given
ranges) as in 5, -35, 401
• Operations: arithmetic (addition, subtraction,
multiplication, …), and others
32. Integer Types/Values
Type Bytes Bits Min Val Max Val
short int 2 16 -32768 32767
int 4 32 -2147483648 2147483647
long int 4 32 -2147483648 2147483647
33. Why Limited?
• With a fixed number of bits, only a certain
number of possible patterns
• 16 bits, 65,536 possible patterns
– 32768 negative numbers
– 1 zero
– 32767 positive numbers
• Overflow: attempt to store a value to large in
a variable (40000 in short int)
34. Two’s Complement
Integers:
positive number: 0, number in binary
97 in binary 1*64 + 1*32 + 1*1 (1100001)
pad with leading zeroes (0 00000001100001) - 16 bits
zero: 0, all zeroes
negative number: 1, (inverse of number + 1)
-97 (1, 111111110011110 + 1)
1 111111110011111
35. Unsigned Integers
• Type: unsigned int
• No negative values
• unsigned int:
– possible values: 0 to 65536
• Representation: binary number
36. Integer Literal Constants
Syntax:
1 or more digits
Optional leading sign (+ or -)
Optional l or L at the end for long
Optional u or U for unsigned
Examples:
5, -35, 401, 4010L, -350L, 2000UL
37. Floating-Point Type
• Type names:
– float
– double
– long double
• Possible values: floating point numbers, 5.0 -
3.5, 4.01
• Operations: arithmetic (addition, subtraction,
multiplication, …), and others
38. Floating-Point Representation
• float: 4 bytes, 32 bits
• double: 8 bytes, 64 bits
• long double: 10 bytes, 80 bits
• Representation:
– magnitude (some number of bits) plus exponent
(remainder of bits)
– 3.26 * 10^4 for 32600.0
39. Floating-Point Limitations
• Maximum, minimum exponents
– maximum possible value (largest positive magnitude,
largest positive exponent)
– minimum value (largest negative magnitude, largest
positive exponent)
– can have overflow, and underflow
• Magnitude limited
– cannot differentiate between values such as
1.00000000 and 1.00000001
40. Floating-Point Literals
• Syntax:
– Zero or more digits, decimal point, then zero or
more digits (at least one digit)
– Whole numbers also treated as float
– Optional sign at start
– Can be followed by e and whole number (to
represent exponent)
– f or F at end for float
– l or L at end for long double
• Examples: 5, .5, 0.5, -1.0, 2.1e+3, 5.1f
41. Character Type
• Type name: char
• Possible values: keys that can be typed at the
keyboard
• Representation: each character assigned a value
(ASCII values), 8 bits
– A - binary number 65
– a - binary number 97
– b - binary number 98
– 2 - binary number 50
42. Character Literals
• Single key stroke between quote char ‘
• Examples: ‘A’, ‘a’, ‘b’, ‘1’, ‘@’
• Some special chars:
– ‘0’ - null char
– ‘t’ - tab char
– ‘n’ - newline char
– ‘’’ - single quote char
– ‘’ - backslash char
43. String Literals
• No string type (more later)
• Contained between double quote chars (“)
• Examples:
“” - null string
“A string”
“String with newline n char in it”
“String with a double quote ” in it”
44. Constants
• Literal constants - tokens representing
values from type
• Defined constants
– syntax: #define Name Value
– preprocessor command, Name replaced by
Value in program
– example: #define MAX_NUMBER 100
45. Constants (cont)
• Memory constants
– declared similar to variables, type and name
– const added before declaration
– Example: const float PI = 3.14159;
– Can be used as a variable, but one that cannot
be changed
– Since the value cannot be changed, it must be
initialized
46. Formatted Input/Output
• Input comes from files
• Output sent to files
• Other objects treated like files:
– keyboard - standard input file (stdin)
– monitor - standard output file (stdout)
• Generally send/retrieve characters to/from
files
47. Formatted Output
• Command: printf - print formatted
• Syntax: printf(Format String, Data List);
– Format string any legal string
– Characters sent (in order) to screen
• Ex.: printf(“Welcome tonCS 1621!n”);
causes
Welcome to
CS 1621!
to appear on monitor
48. Formatted Output (cont)
• Successive printf commands cause output to be
added to previous output
• Ex.
printf(“Hi, how “);
printf(“is it goingnin 1621?”);
prints
Hi, how is it going
in 1621?
To the monitor
49. Field Specifications
• Format string may contain one or more field
specifications
– Syntax: %[Flag][Width][Prec][Size]Code
– Codes:
• c - data printed as character
• d - data printed as integer
• f - data printed as floating-point value
– For each field specification, have one data value
after format string, separated by commas
50. Field Specification Example
printf(“%c %d %fn”,’A’,35,4.5);
produces
A 35 4.50000
(varies on different computers)
Can have variables in place of literal
constants (value of variable printed)
51. Width and Precision
• When printing numbers, generally use
width/precision to determine format
– Width: how many character spaces to use in
printing the field (minimum, if more needed,
more used)
– Precision: for floating point numbers, how many
characters appear after the decimal point, width
counts decimal point, number of digits after
decimal, remainder before decimal
53. Left Justification (Flags)
Put - after % to indicate value is left justified
printf(“%-5d%-8.3fXn”,753,4.1678);
produces
753 4.168 X
For integers, put 0 after % to indicate should pad
with 0’s
printf(“%05d”,753);
produces
00753
54. Size Indicator
• Use hd for small integers
• Use ld for long integers
• Use Lf for long double
• Determines how value is treated
55. Printf Notes
• Important to have one value for each field
specification
– some C versions allow you to give too few
values (garbage values are formatted and
printed)
• Values converted to proper type
– printf(“%c”,97); produces the character a on the
screen
56. Formatted Input
• Command: scanf - scan formatted
• Syntax: scanf(Format String, Address List);
– Format string a string with one or more field
specifications
– Characters read from keyboard, stored in
variables
• scanf(“%c %d %f”,&cVar,&dVar,&fVar);
attempts to read first a single character, then a
whole number, then a floating point number
from the keyboard
57. Formatted Input (cont)
• Generally only have field specifications and
spaces in string
– any other character must be matched exactly (user
must type that char or chars)
– space characters indicate white-space is ignored
– “white-space” - spaces, tabs, newlines
– %d and %f generally ignore leading white space
anyway (looking for numbers)
– %d and %f read until next non-number char reached
58. Formatted Input (cont)
• More notes
– can use width in field specifications to indicate max
number of characters to read for number
– computer will not read input until return typed
– if not enough input on this line, next line read, (and
line after, etc.)
– inappropriate chars result in run-time errors (x when
number expected)
– if end-of-file occurs while variable being read, an
error occurs
59. Address Operator
• & - address operator
• Put before a variable (as in &x)
• Tells the computer to store the value read at
the location of the variable
• More on address operators later
60. Scanf Rules
• Conversion process continues until
– end of file reached
– maximum number of characters processed
– non-number char found number processed
– an error is detected (inappropriate char)
• Field specification for each variable
• Variable address for each field spec.
• Any character other than whitespace must be
matched exactly
62. Prompting for Input
• Using output statements to inform the user
what information is needed:
printf(“Enter an integer: “);
scanf(“%d”,&intToRead);
• Output statement provides a cue to the user:
Enter an integer: user types here