Você está na página 1de 5

COMPUTER PROGRAMMING

Name: PASCUAL, Michael Angel B.


Course/Year/Section: CEE23

July 4, 2015

Assignment#1 (Prelims)
1. Define
a.) Comments
In computer programming, a comment is a programmer-readable annotation in
the source code of a computer program. They are added with the purpose of making the source
code easier to understand, and are generally ignored by compilers and interpreters. The syntax of
comments in various programming languages varies considerably.
b.) Variables
In computer programming, a variable or scalar is a storage location paired with
an associated symbolic name (an identifier), which contains some known or unknown quantity or
information referred to as a value. The variable name is the usual way to reference the stored
value; this separation of name and content allows the name to be used independently of the exact
information it represents. The identifier in computer source code can be bound to
a value during run time, and the value of the variable may thus change during the course of
program execution.
c.) Constants
In computer
programming,
a constant is
an identifier with
an
associated value which cannot be altered by the program during normal execution the value
is constant.[a] This is contrasted with a variable, which is an identifier with a value that can be
changed during normal execution the value is variable. Constants are useful for both
programmers and compilers: for programmers they are a form of self-documenting code and
allow reasoning about correctness; while for compilers they allow compile-time and runtime checks that constancy assumptions are not violated, and allow or simplify some compiler
optimizations.
d.) Keywords
In a computer language, a reserved word (also known as a reserved identifier)
is a word that cannot be used as an identifier, such as the name of a variable, function, or label
it is "reserved from use". This is a syntactic definition, and a reserved word may have no
meaning.
A closely related and often conflated notion is a keyword which is a word with
special meaning in a particular context. This is a semantic definition. By contrast, names in
a standard library but not built into the language are not considered reserved words or keywords.
The terms "reserved word" and "keyword" are often used interchangeably one may say that a
reserved word is "reserved for use as a keyword" and formal use varies from language to
language; for this article we distinguish as above.

2. Give the basic format for creating C programs

3. Give the syntax (format) and 2 examples each of:


The syntax of the C programming language, the rules governing writing of software in
the language, is designed to allow for programs that are extremely terse, have a close relationship
with the resulting object code, and yet provide relatively high-level data abstraction. The
development of this syntax was a major milestone in the history of computer science as it was
the first widely successful high-level language for operating-system development.
C syntax makes use of the maximal munch principle.
printf() and scanf() functions are inbuilt library functions in C which are available in C
library by default. These functions are declared and related macros are defined in
stdio.h which is a header file.
We have to include stdio.h file as shown in below C program to make use of these
printf() and scanf() library functions.
a.) printf () statement
printf() function is used to print the character, string, float, integer, octal and
hexadecimal values onto the output screen.
We use printf() function with %d format specifier to display the value of an integer
variable.
Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
To generate a newline,we use \n in C printf() statement.
Note:
C language is case sensitive. For example, printf() and scanf() are different from Printf()
and Scanf(). All characters in printf() and scanf() functions must be in lower case.

Example program for C printf() function:


#include <stdio.h>
int main()
{
char ch = A;
char str[20] = fresh2refresh.com;
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf(Character is %c \n, ch);
printf(String is %s \n , str);
printf(Float value is %f \n, flt);
printf(Integer value is %d\n , no);
printf(Double value is %lf \n, dbl);
printf(Octal value is %o \n, no);
printf(Hexadecimal value is %x \n, no);
return 0;
}
.
Output:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
.
You can see the output with the same data which are placed within the double quotes of printf
statement in the program except
%d got replaced by value of an integer variable (no),
%c got replaced by value of a character variable (ch),
%f got replaced by value of a float variable (flt),
%lf got replaced by value of a double variable (dbl),
%s got replaced by value of a string variable (str),
%o got replaced by a octal value corresponding to integer variable (no),
%x got replaced by a hexadecimal value corresponding to integer variable
\n got replaced by a newline.
b.) scanf () statement
scanf() function is used to read character, string, numeric data from keyboard
Consider below example program where user enters a character. This value is assigned to
the variable ch and then displayed.

Then, user enters a string and this value is assigned to the variable str and then
displayed.
Example program for printf() and scanf() functions in C:
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf(Enter any character \n);
scanf(%c, &ch);
printf(Entered character is %c \n, ch);
printf(Enter any string ( upto 100 character ) \n);
scanf(%s, &str);
printf(Entered string is %s \n, str);
}
.
Output:
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
.
The format specifier %d is used in scanf() statement. So that, the value entered is
received as an integer and %s for string.
Ampersand is used before variable name ch in scanf() statement as &ch.
It is just like in a pointer which is used to point to the variable.
References:
(1) Comment (computer programming). (n.d.). Retrieved from,
https://en.wikipedia.org/wiki/Comment_(computer_programming)
(2) Variable (computer science). (n.d.). Retrieved from,
https://en.wikipedia.org/wiki/Variable_(computer_science)
(3) Constant (computer programming). (n.d.). Retrieved from,
https://en.wikipedia.org/wiki/Constant_(computer_programming)
(4) Reserved word. (n.d.). Retrieved from,
https://en.wikipedia.org/wiki/Reserved_word
(5) C syntax. (n.d.). Retrieved from,
https://en.wikipedia.org/wiki/C_syntax

(6) C printf and scanf. (n.d.). Retrieved from,


https://fresh2refresh.com/c/c-printf-and-scanf/

Você também pode gostar