Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C programming language, Slides of Computer Science

C programming language for beginners

Typology: Slides

2020/2021

Uploaded on 10/11/2021

unknown user
unknown user 🇬🇧

1 document

Partial preview of the text

Download C programming language and more Slides Computer Science in PDF only on Docsity! Lecture 06: C Language Ai UNIVERSITY of @s7 GREENWICH @ Pearson Alliance with ago. Education BT E C (Bil UNIVERSITY of @eneeNwicH © Pearson Objectives * Computer program/Memory architecture * Typical C program elements ¢ C statements and comments ¢ Variables and Data types ¢ Variables vs. Constants/Literals Standard Inputs/Outputs INTRODUCTION TO C PROGRAMMING LANGUAGE v2 GD UNIVERSITY of ee os @ Pearson C statements/comments * Every C statement is terminated with semicolon ‘;’ ¢ We could write comments to explain code, and comments will not be executed — Comment a single line with // — Comment a block with /*.......... */ INTRODUCTION TO C PROGRAMMING LANGUAGE 5) GD UNIVERSITY of is? GREEN WICH Alliance with ago. Education INTROD Pearson BTEC Librarnes Pre processor (Compiler #.ssembler Soure Code Assembly Coce Link Exqitor Object Code Executable Code C Compilation model Gi UNIVERSITY o Gi) GREENWICH @ Pearson Components of a computer Alla vcewih ID. Education prog ram S-fa-o INTRODUCTION TO C PROGRAMMING LANGUAGE 7 GD UNIVERSITY of ee Pe = GD GREENWICH @ Pearson Variable Variable is used to store data INTRODUCTION TO C PROGRAMMING LAI GD UNIVERSITY of ee Pe = GD GREENWICH @ Pearson Variable Variable is used to store data “Variable” means it could store different data at different time INTRODUCTION TO C PROGRAMMING LAN (Bil UNIVERSITY of GD GREENWICH @ Pearson Constants/Literals ¢ Constants refer to fixed values — These fixed values are also called literals ° E.g., — Integer constants: 1, 2, 3,... — Floating constants: 1.1, 2.3, 4.0... — Character constants: ‘a’, ‘b’, ‘c’,... — String literals: “Hello, there”, “Hi, there”, ... ¢ Variables are used to hold constant values — But at different time it can hold different constant values INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of GD GREENWICH @ Pearson Variable declaration ¢ Before using a variable, we need to declare it ¢ Declaring a variable: — The name of the variable is preceded with the data type ¢ The name should follow naming conventions — No white spaces, special characters (*, !, ?, etc.) — Could include underscore (‘_’) — Could include digits (but not at the start of the name) — Etc., INTRODUCTION TO C PROGRAMMING LANGUAGE (Ql UNIVERSITY of 7 GREEN WICH Pearson Alliance with ago. Education BTEC int Size: 4 bytes E.g., 1, 2, 3, etc. Basic data types float Size: 4 bytes E.g., 1.1, 1.2, etc. double Size: 8 bytes E.g., 1.234, 2.3, Basic data types char Size: 1 byte E.g., ‘a’, ‘b, ‘c’, etc. Usage: datatype varName; E.g., int x; E.g., with initialization int x = 10; INTRODUCTION TO C PROGRAMMING LANGUAGE void (Bil UNIVERSITY of qs GREEN WICH @ Pearson Alliance with ago. Education BTEC Ty p e i n t ¢ Stores numeric data — Cannot store other type e.g., “Alan” or “abc” ¢ Declaration: int num; ¢ Size (Depends on the Operating System) — Normally 32 bits (4 bytes) ¢ Integers in the range: -32768 to 32767 — Examples: 12322, 0, -232 INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of ae Pe GD GREENWICH @ Pearson Type char ¢ Stores single character information — Cannot store other type e.g., “Alan” or “abc” ¢ Declaration: char c; * Size (Depends on the Operating System) — Normally 8 bits (1 byte) * Examples: ‘a’, ‘b’, ‘$’, ‘%’, ‘1’, ‘2’ INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of ee Pe = @eneeNwicH © Pearson Type void * Stores nothing — This is used to indicate the compiler that there is nothing to expect INTRODUCTION TO C PROGRAMMING LANGUAGE 5 UNIVERSITY of 7 GREEN WICH @ Pearson ecugeoa: " BTEC Derived data types Derived data type Data type _|_ | Basic Data Modifiers types j —- j — unsigned int u NSIg ned Int —, (Pemit only positive numbers) . — short int short TT int — (Occupies less memory space) . — Long int /long double long + int/double eo Oceanis more o sce) INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of GD GREENWICH @ Pearson Variable initialization ¢ You could initialize values to variables at the time of declaring them — Usage: datatype varName = value; INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of ee Pe = = @oneenwicd © Pearson Activity * Declare a variable for each of the following data types and initialize each with a value — char — int — float — double — long — short INTRODUCTION TO C PROGRAMMING LANGUAGE (St UNIVERSITY of qs GREEN WICH @ Pearson een ifeen BTEC Type casting ¢ An expression can be forced to be of a certain type by using a cast ¢ The general syntax of cast — dataType varName = (dataType) expression * Example — float x, y; — y=45.6; —-int i = (int)y; —x = (int)y;//Value of x will be 45 INTRODUCTION TO C PROGRAMMING LANGUAGE Gi UNIVERSITY o . GD GREENWICH @ Pearson Standard output (basics) ¢ The printf£(“”, value) function allows the user to write values of various types * To use the printf method, you need to have the #include <stdio.h> preprocessor directive Usage, example: printf(“Hello”); Or: printf(“%d”, value); INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of @oneenwicd © Pearson Standard Input (basics) ¢ The scanf(“”, &var) function allows the user to read values of various types ¢ To use the scanf method, you need to have the #include <stdio.h> preprocessor directive Usage, example: int x; scanf(“%d”", &X); INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of GD GREENWICH @ Pearson The header file stdio.h ¢ #include<stdio.h> is a preprocessor directive * stdio.h is the header file — It contains macros for many I/O functions — printf), scanf(), putch(), getch(), puts(), gets() functions require the macros in stdio.h CHC) INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of @Poneenwicd © Pearson Special characters * They are meta characters (used to represent about other characters) \\ to print \ character \“ to print “ character %% to print % character INTRODUCTION TO C PROGRAMMING LANGUAGE GD UNIVERSITY of - . @eneeNwicH © Pearson Activity: printf example #include <stdio.h> void main() { int a = 10; float b 24.67892345; char ch ‘A’; printf(“Integer data = %d”, a); printf(“Float Data = %f”,b); printf (“Character = %c”,ch); printf(“This prints the string”); printf (“%s”,”This also prints a string”); INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of GD GREENWICH @ Pearson printf modifiers Field width modifier — It’s an integer, defines minimum field with for displaying the data Alignment modifier (‘-’) — The item will be printed beginning from the leftmost position of its field Precision modifier — It indicates the maximum number of digits to be printed after the floating point Zero padding modifier (‘0’) — Used to pad ‘0’ to values if there are more space for its displaying field (field width) INTRODUCTION TO C PROGRAMMING LANGUAGE Gi UNIVERSITY o - os es ap caewice @ Pearson Activity: Example for modifiers /* This program demonstrate the use of Modifiers in printf() */ #include <stdio.h> void main() { printf(“The number 555 in various forms:\n”); printf(“without any modifier: \n”); printf (“[%d]\n”,555); printf(“with - modifier :\n”); printf (“[%-d]\n”,555); printf(“with digit string 10 as modifier :\n”); printf (“[%10d]\n”, 555); printf(“with © as modifier : \n”); printf (“[%0d]\n”,555); printf(“with © and digit string 10 as modifiers :\n”); printf (“[%010d]\n”, 555); printf(“with -, 0 and digit string 10 as modifiers: \n”); printf (“[%-010d]\n”,555); INTRODUCTION TO C PROGRAMMING LANGUAGE GD UNIVERSITY of «7 GREENWICH @ Pearson ee Bbe | BTEC scanf() ¢ Used to accept data from standard Input ¢ General format of scanf () function —scanf(“control string”, arg. list) ¢ The format codes used in printf () are the same as in the scanf (), with some notices printf() scanf() Argument list uses variable names, constants, symbolic uses pointers to variables constants and expressions %g Yes No %f and %e Are different Are the same INTRODUCTION TO C PROGRAMMING LANGUAGE Gi UNIVERSITY o _ . . po a @ Pearson Activity: scanf()/printf() ¢ Make a request to ask user to input — 2 numbers a & b of type int — Then display greeting message to the user using the input name/age —Welcome to C class, sum of [a] & [b] is [Sum]. INTRODUCTION TO C PROGRAMMING LANGUAGE (Bil UNIVERSITY of ee Pe = = @oneenwicd © Pearson Activity ¢ Ask user to input his/her name * Display greeting message to the person ° E.g., — Please input your name: Mr. A — Welcome Mr. A to Procedural Programming! — In which, Mr. A is the user input value INTRODUCTION TO C PROGRAMMING LANGUAGE BARI © Pearson . asegliquace |" ETE Summaries * Computer program/Memory architecture * Typical C program elements ¢ C statements and comments ¢ Variables and Data types ¢ Variables vs. Constants/Literals Standard Inputs/Outputs INTRODUCTION TO C PROGRAMMING LANGUAGE
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved