Robotics 1: Getting Started with C Programming PDF
Document Details
Uploaded by AvailableRhythm
Tags
Summary
This document provides an introduction to C programming, specifically focused on topics relevant to robotics. It covers fundamental concepts such as variables, data types, and conditional statements. The document also discusses escape sequences and operators in the context of C programming.
Full Transcript
ROBOTICS 1 Getting Started with C Programming Target Outcomes: At the end of the discussion, the students should be able to: Understand and use C programming variables 2 Use data types appropriately and; Learn conditional statements effective...
ROBOTICS 1 Getting Started with C Programming Target Outcomes: At the end of the discussion, the students should be able to: Understand and use C programming variables 2 Use data types appropriately and; Learn conditional statements effectively What is C Programming C programming language is a General purpose language that has wide range of application. It was developed by Dennis Ritchie in year 2 1972 at Bell Laboratories. It was mainly developed as a system programming language to write the UNIX operating system. C Programming C programming also allows us to access the hardware of computer on a deeper level than other programming languages do not. Main features of C programming language: 2 General Purpose and Portable Low level Memory Access Fast Speed Clean Syntax VARIABLES Variables In computer programming, a variable is a name given to a memory location inside our computer where we can store data. A variable is a name associated 2 with a memory location to store data of different types. A variable is the basic building block of a C program that can be used in expressions as a substitute in a place of the value it stores. C Variables A variable in C is a memory location with some name that helps store some form of data and retrieve it when required. We can store different types of data 2 in the variable and reuse the same variable for storing some other data any number of times. C Variables Syntax The syntax to declare a variable in C specifies the name and the type of the variable Data Types Data Types Each variable in C has an associated data type. A data type defines the type of data a variable can hold and how much memory will be allocated to 2 store that data. C program provides several built in data types and those are call Primitive Data Types. Data Types Primitive Data Types are the most basic data types that are used for representing simple values such us INTEGERS, FLOAT, CHARACTER, etc. 2 Primitive Data Types int (INTEGER): Used to store whole number, both positive and negative Size: Typically 4 bytes(32bits) 2 on modern systems Example: int age = 25; Primitive Data Types float (FLOATING POINT): Used to store single precision decimal numbers. Size: Typically 4 bytes(32bits) 2 Example: temperature = 36.5; Primitive Data Types double(Double precision floating point): Used to store double precision decimal numbers with more precision than float. Size: 8 bytes 2 Example: double pi = 3.141592653589793238; Primitive Data Types char (Character): Used to store a single character, typically enclosed in single quotes (e.g., ‘A’). Size: 1 byte(8bits) 2 Example: char letter = ‘A’ Modifiers signed : Allows a variable to hold both positive and negative values(default for int and char). Example: signed int x; 2 unsigned : Allow a variable to hold only non-negative values, which doubles the upper limit of the range. Example: unsigned int = 2; Modifiers short : Reduces the size of an int(usually to 2 bytes). Example: short int z; 2 long: Increases the size of an int (usually to 8bytes or 64bit system) or double Example: long int y; Data Type int is used for counting, indexing, and performing operations involving whole numbers. float and double are used for numbers with decimal points ( ex. scientific2 calculation, temperature, distance). char is used for handling characters or small numbers. Data Type Summary In C programming, Data Types play a crucial role by defining the type and size of data that a variable can hold. The type of a variable affects what kind of operations can be performed 2on it, how much memory is allocated, and how the data is interpreted. Properly selecting and using data types ensures efficient memory usage and prevents potential bugs or errors in your program. CONDITIONAL STATEMENTS CONDITIONAL STATEMENTS Conditional Statements are used to control the normal flow of execution of the program. These statements are used to make decisions whether to execute a specific block of code or not based on the specific conditions. Types of Conditional Statements in C if Statement if else Statement switch Statement if Statement The if statement is the simplest form of conditional control. It executes a block of code if a specified condition evaluates to true. If the condition is false, the block of code inside the if statement is skipped. Syntax: if Statement Example if else Statement The if else statement adds an alternative action if the condition is false. If the condition is true, the if block is executed, otherwise, the else block runs. Syntax: if else Statement Example else if Ladder The else if ladder allows you to check multiple conditions. If the first condition is false, the program checks the next else if condition, and so on. If none of the conditions are true, the else block (if present) is executed. Syntax: else if Ladder Example switch case Statement The switch statement is used to test the value of a variable against multiple cases. It provides a cleaner and more organized way to write multiple if-else if conditions. Each case represents a possible value of the variable. If a match is found, the code associated with that case is executed. Syntax: switch case Statement The break statement is used to exit the switch after a case is executed. Without break, the code continues executing the next cases (this is called fall-through). The default case is optional and is executed if no matching case is found. switch case Statement Example C PROGRAMMING ESCAPE SEQUENCE & OPERATIONS TARGET OUTCOMES: At the end of the discussion, the students should be able to: Understand the concept of escape sequences in C programming. Use escape sequences effectively to represent different characters and control output. Understand the concept of operators in C programming. Use operators effectively to perform calculations, comparisons, and logical operations. What is Escape Sequence in C Program? The escape sequence in C is the characters or the sequence of characters that can be used inside the string literal. The purpose of the escape 2 sequence is to represent the characters that cannot be used normally using the keyboard. Some escape sequence characters are the part of ASCII charset but some are not. Escape Sequence List Escape Sequence in C Examples 1. Demonstrate how to use \n escape sequence in C Program: Output : Escape Sequence in C Examples 2. Demonstrate how to use \t escape sequence in C Program: Output : Escape Sequence in C Examples 3. Demonstrate how to use \\ escape sequence in C Program: Output : Escape Sequence in C Examples 4. Demonstrate how to use \’ and \” escape sequence in C Program: Output : What is a C Operator? In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So, we can say that the operators are the symbols that perform operations on operands. Types of Operators in C 1.Arithmetic Operators 2.Relational Operators 3.Logical Operators 4.Assignment Operators 5.Ternary or Conditional Operator Arithmetic Operations in C The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language: Arithmetic Operators in C Examples Program: Output: Relational Operators in C The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison. These are a total of 6 relational operators in C: Relational Operators in C Examples Program: Output: Logical Operator in C Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false.e result of comparison. Logical Operators in C Examples Program: Output: Assignment Operators in C Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error. In C, there are 11 assignment operators : Assignment Operators in C Examples Program: Output: Ternary or Conditional Operator The conditional operator ?: is the only ternary operator in C. Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3. It is also known as a short-hand if else because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements. Syntax: Variable = (condition)? expressionTrue : expressionFalse; Program: Output: Program & Output: THANK YOU!