0% found this document useful (0 votes)
14 views

CMSC Notes

Uploaded by

mbunabia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CMSC Notes

Uploaded by

mbunabia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Don't worry if you don't understand how #include

What is C?
stdio.h> works. Just think of it as something that (almost)
lways appears in your program.
C is a general-purpose programming language
created by Dennis Ritchie at the Bell Laboratories in
1972.
Line 2: A blank line. C ignores white space. But we use it
to make the code more readable.
It is a very popular language, despite being old. The
main reason for its popularity is because it is a
Line 3: Another thing that always appear in a C program,
fundamental language in the field of computer is main(). This is called a function. Any code inside its
science. curly brackets {} will be executed.

C is strongly associated with UNIX, as it was Line 4: printf() is a function used to output/print text to
developed to write the UNIX operating system. the screen. In our example it will output "Hello World!".

Note that: Every C statement ends with a semicolon ;

Why Learn C?
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
● It is one of the most popular programming
language in the world Remember: The compiler ignores white spaces. However,
● If you know C, you will have no problem multiple lines makes the code more readable.
learning other popular programming
languages such as Java, Python, C++, C#, Line 5: return 0 ends the main() function.
etc, as the syntax is similar
● C is very fast, compared to other Line 6: Do not forget to add the closing curly bracket } to
programming languages, like Java and Python actually end the main function.
● C is very versatile; it can be used in both
applications and technologies
C OUTPUT
Output (Print Text)
Difference between C and C++
To output values or print text in C, you can use the
● C++ was developed as an extension of C, and printf() function:
both languages have almost the same syntax
● The main difference between C and C++ is
that C++ support classes and objects, while C
does not

Syntax

New Lines

To insert a new line, you can use the \n character:

Line 1: #include <stdio.h> is a header file library


that lets us work with input and output functions,
such as printf() (used in line 4). Header files add
functionality to C programs.
What is \n exactly?

The newline character (\n) is called an escape sequence,


and it forces the cursor to change its position to the
beginning of the next line on the screen. This results in a
new line.

Examples of other valid escape sequences are:

Format Specifiers

Format specifiers are used together with the printf()


function to tell the compiler what type of data the variable
is storing. It is basically a placeholder for the variable
C Variables value.

A format specifier starts with a percentage sign %, followed


by a character.
Variables are containers for storing data values, like
numbers and characters. For example, to output the value of an int variable, you
must use the format specifier %d or %i surrounded by
In C, there are different types of variables (defined with double quotes, inside the printf() function:
different keywords), for example:

● int - stores integers (whole numbers), without


decimals, such as 123 or -123
● float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or
'B'. Char values are surrounded by single quotes

Declaring (Creating) Variables

To create a variable, specify the type and assign it a value:

Where type is one of C types (such as int), and


variableName is the name of the variable (such as x or
myName). The equal sign is used to assign a value to the
variable.

So, to create a variable that should store a number, look at


the following example:
To combine both text and a variable, separate them with a
comma inside the printf() function:

The general rules for naming variables are:

To print different types in a single printf() function, you ● Names can contain letters, digits and underscores
can use the following: ● Names must begin with a letter or an underscore
(_)
● Names are case sensitive (myVar and myvar are
different variables)
● Names cannot contain whitespaces or special
characters like !, #, %, etc.
● Reserved words (such as int) cannot be used as
names

Add Variables Together


To add a variable to
another variable, you can
use the + operator:

Declare Multiple Variables


To declare more than one
variable of the same type,
use a comma-separated
list:

C Variable Names
All C variables must be identified with unique names.
These unique names are called identifiers.

Identifiers can be short names (like x and y) or more


descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order


to create understandable and maintainable code:
Assignment Operators
C Data Types
Assignment operators are
Basic Data Types used to assign values to
variables. In the example
below, we use the
The data type specifies the size and type of information
assignment operator (=) to
the variable will store.
assign the value 10 to a
variable called x:

The addition assignment


operator (+=) adds a value
to a variable:

Basic Format Specifiers

C Operators
Arithmetic Operators
Arithmetic operators are used to perform common
mathematical operations.

A list of all comparison operators:


Logical Operators

C If ... Else
Conditions and If
Statements
You have already learned that C supports the usual
logical conditions from mathematics:

● Less than: a < b




Less than or equal to: a <= b
Greater than: a > b
The else Statement
● Greater than or equal to: a >= b
● Equal to a == b Use the else statement to specify a block of code
● Not Equal to: a != b to be executed if the condition is false.

You can use these conditions to perform different


actions for different decisions.

C has the following conditional statements:

● Use if to specify a block of code to be


executed, if a specified condition is true
● Use else to specify a block of code to be
executed, if the same condition is false
● Use else if to specify a new condition to
test, if the first condition is false
● Use switch to specify many alternative blocks
of code to be executed

The if Statement
Use the if statement to specify a block of code to
be executed if a condition is true.
The else if Statement
Use the else if statement to specify a new
condition if the first condition is false.

The break Keyword


When C reaches a break keyword, it breaks out of the
switch block. This will stop the execution of more code and
case testing inside the block. When a match is found, and
the job is done, it's time for a break. There is no need for
more testing.

The default Keyword


The default keyword specifies some code to run if there is no
case match:

C Switch
Switch Statement
Instead of writing many if..else statements, you can use
the switch statement. The switch statement selects one of
many code blocks to be executed:
C While Loop C For Loop
While Loop For Loop
The while loop loops through a block of code as long as a When you know exactly how many times you want to loop
specified condition is true: through a block of code, use the for loop instead of a
while loop:

Statement 1 is executed (one time) before the execution of


the code block.
Statement 2 defines the condition for executing the code
block.
Statement 3 is executed (every time) after the code block
has been executed.

Note: Do not forget to increase the variable used in the


condition (i++), otherwise the loop will never end!

The Do/While Loop


The do/while loop is a variant of the while loop. This loop Example explained
Statement 1 sets a variable before the loop starts (int i =
will execute the code block once, before checking if the
0).
condition is true, then it will repeat the loop as long as the
Statement 2 defines the condition for the loop to run (i
condition is true.
must be less than 5). If the condition is true, the loop will
start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code
block in the loop has been executed.
Nested Loops Multiple Inputs
It is also possible to place a loop inside The scanf() function also allow multiple inputs (an
another loop. This is called a nested loop. integer and a character in the following example):
The "inner loop" will be executed one
time for each iteration of the "outer
loop":

Take String Input


C User Input You can also get a string entered by the user:

User Input
You have already learned that printf() is used to output
values in C. To get user input, you can use the scanf()
function:

Note: When working with strings in scanf(), you


must specify the size of the string/array (we used a
very high number, 30 in our example, but atleast
The scanf() function takes two arguments: the then we are certain it will store enough characters
format specifier of the variable (%d in the example for the first name), and you don't have to use the
above) and the reference operator (&myNum), which reference operator (&).
stores the memory address of the variable.
However, the scanf() function has some limitations:
it considers space (whitespace, tabs, etc) as a
terminating character, which means that it can only
display a single word (even if you type many words).
For example:
From the example above, you would expect the
program to print "John Doe", but it only prints
"John".

That's why, when working with strings, we often use


the fgets() function to read a line of text. Note that
you must include the following arguments: the name
of the string variable, sizeof(string_name), and
stdin:

TRACING

INCREMENT
A. Good Morning...
B. Good Day...
C. Good Morning...Good Day...
D. Syntax error

ANSWER: C
Explanation: Condition inside if statement, i.e. time < 12 is true,
hence both the printf statements will be executed resulting in
output as "Good Morning...Good Day..."

Program - 2

#include<stdio.h>
int main()
{
int num1=5, num2=4, num3=3;

if(num1 > num2 && num1 > num3)


{
printf("Number1.");
}
if(num2 > num1 || num2 > num3)
{
printf("Number2.");
}
if(num3 > num1 && num3 > num2)
{
printf("Number3.");
}
return 0;
}
A. Number1.
B. Number2.
C. Number3.
D. Number1.Number2
E. Number1.Number2.Number3

ANSWER: D

Program - 3

#include<stdio.h>

int main()
{
int time=0;

if(time < 12)


{
printf("Good Morning...");
}
printf("Good Day...");
return 0;
}

You might also like