CMSC Notes
CMSC Notes
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!".
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
Format Specifiers
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
C Variable Names
All C variables must be identified with unique names.
These unique names are called identifiers.
C Operators
Arithmetic Operators
Arithmetic operators are used to perform common
mathematical operations.
C If ... Else
Conditions and If
Statements
You have already learned that C supports the usual
logical conditions from mathematics:
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.
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:
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:
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;
ANSWER: D
Program - 3
#include<stdio.h>
int main()
{
int time=0;