0% found this document useful (0 votes)
10 views33 pages

Lecture 1 - Fundamentals of C

The document provides an introduction to C programming, covering basic concepts such as writing simple programs, printing strings, comments, variables, and input/output functions. It emphasizes best practices for coding, including variable declaration, assignment, and the use of format specifiers in printf and scanf. Additionally, it outlines rules for identifiers and keywords in C, along with recommended reading materials for further learning.

Uploaded by

leaderatelast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views33 pages

Lecture 1 - Fundamentals of C

The document provides an introduction to C programming, covering basic concepts such as writing simple programs, printing strings, comments, variables, and input/output functions. It emphasizes best practices for coding, including variable declaration, assignment, and the use of format specifiers in printf and scanf. Additionally, it outlines rules for identifiers and keywords in C, along with recommended reading materials for further learning.

Uploaded by

leaderatelast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Fundamentals of C

CSE 4373: Computer Programming and Applications


Ajwad Abrar
Junior Lecturer, CSE
Writing a simple program
#include<stdio.h>

int main()

printf(“To C or not to C, that is the question.”);

return 0;

What really happens when you run this program?

2
Printing strings

3
Printing strings
printf(“To C or not to C, that is the question.”);
● Will print anything enclosed between double quotation marks (“ ”)
● Things to be careful about:
○ Don’t forget the closing brackets
○ Don’t forget the semicolon (;)

Yes it’s that simple !

4
Pop Quiz

5
Printing strings

What is the output for the given line?


printf(“Why did the turkey cross the road?”);

printf(“To prove it wasn’t a chicken!”);

Will it be printed on a new line?

6
Escape sequences
Sometimes, it is necessary to use characters that cannot be typed or has some special
meaning in C programming. To use those, escape sequences are used.

\n New line \’ Single


quotation

\t Horizontal \” Double
tab quotation

\v Vertical \0 Null
tab

\\ Backslash \b backspace

7
Comments

8
Comments
Multiline Comments (C89 Style)

/* int main()
{
This is an example of a printf(“Hello!”); /*Comment*/
}
multiline comment.

*/

printf(“Very insightful comments”);

● Used for documenting code


● Starts with /* and ends with */

9
Comments
Single line Comments (C99 Style)

//This is a comment int main()


{
//It only spans a single line printf(“Hello!”); //comment
}
printf(“Very insightful comments”);

● Begins with //
● Ends when the line ends
● No need for a closing symbol

10
Comments
Write comments that are helpful. Do not write comments that state the obvious

● Follow a convention and stick to it


● Be expressive
● Increases
○ clarity,
○ readability and
○ maintainability of the code

11
Variables

12
Just a fancy name for
containers

13
Variables

Numbers Words Images Anything

Each container must have a unique name


14
Variables in C
● Each variable must have a type and an identifier
● Data type specifies what kind of data will be stored

There are a lot of data types in C:

int, float, char, double, short int, unsigned int, long int

And many more. But for now, we are concerned with: int, float

int float

● Holds integers ● Holds floating point numbers


● Limited range (-32,768 to +32767) ● Larger range
● Might cause round-off error

15
Declaring variables
All variables must be declared before usage

datatype identifier;
Examples:

int count;

float salary;

Several same type variable declarations can be combined:

int count, steps, length, width;

float salary, volume, profit, loss;

16
Assigning variables
Variables can be given a value to store through assignment

int count; int count = 10, steps = count;


int count = 10;
float salary; float salary = 12.4f, volume = 0.93f;
float salary = 12.4f;
count = 10;

salary = 12.4f;

17
Best practices

It’s best practice to append Whenever assigning values to


the letter f to a floating-point float without any digits after
assignment: the decimal point, make sure to
float salary = 12.4f; add a zero after the decimal.

float sum, angle;

sum = 0.0;

angle = 45.0;

18
Pop Quiz

19
Printing strings
What is stored in the variable?
height = 8;
int height;

What about this?


int length, width = 10;

Is this even possible?


int length = 12.4; float profit = 2150;
20
Assigning variables

● Some variables are automatically set to zero when a


program begins to execute, but most are not
● A variable is said to be uninitialized
○ Doesn’t have a default value, or
○ Hasn’t yet been assigned a value by the program
● Attempting to access uninitialized variable can yield
unpredictable result, even a program crash

21
Printing variables

22
Printing variables

● Use printf function to display the current value of a variable


int total = 100;

printf(“%d”, total);

%d is placeholder indicating where the value of total should be


printed. It is called a format specifier.
● %d is for int %f is for float

23
Printing variables

● By default, %f prints six digits after the decimal point


● To force %f to print p digits after the decimal point, we put .p
between % and f
● There’s no limit to the number of variables that can be printed
by a single printf

float volume = 131.45123;

printf(“%.3f”, volume)
24
Taking input

25
Taking input

● scanf is the C library’s counterpart to printf


● Similar to printf, scanf requires a format string to
specify the type of the input
To read an integer from the user:
scanf("%d", &i); // reads an integer; stores it in i

Reading a float value requires:


scanf("%f", &j); // reads a float; stores it in j

26
Identifiers

27
Rules of Identifier Specification
Identifiers are just the names of the variables
● May contain letters, digits, and underscores
● Must begin with a letter or underscore
● C identifiers are Case Sensitive. Age, age, AGE, agE are
all different identifiers
● C places no limit on the maximum length of an identifier

28
Pop Quiz

29
Rules of Identifier Specification
Are these identifiers legal?

times10, _done, TotalProfit$, HOUSE NUMBER,


1st_Slot, __memberValue, last<>Quest, int

30
Keywords
● Keywords are words that have special meaning to the C compiler
● They can’t be used as identifiers
● Some keywords are

auto, enum, restrict, unsigned, break, extern, return,


void, case, float, short, volatile, char, for, signed,
while, do, int, switch, double, long, typedef, else,
register, union

31
Reading Tasks
● C Programming - A Modern Approach | Section 2.1 ~ 2.7
● Teach yourself C | Section 1.1, 1.3 ~ 1.4

32
Thank You

33

You might also like