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

The C Programming Language, Study notes of C programming

C - C - C - C

Typology: Study notes

2012/2013

Uploaded on 01/29/2013

bunkeraditya
bunkeraditya 🇮🇳

4

(2)

8 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
################################################## Basics
##################################################
your program begins executing at the beginning of main. This means that every program
must have a main somewhere.
mainwill usually call other functions to help performits job, somethat you wrote, and others
from libraries that are provided for you.
#include <stdio.h> -- tells the compiler to include information about the standard input/output
library
Variables and constants are the basic data objects manipulated in a program.
Declarations list the variables to be used, and state what type they have and perhaps what
their initial values are .
Operators specify what is to be done to them.
Expressions combine variables and constants to produce new values.
The standard library provides several functions for reading or writing one character at a time,
of which getchar and putchar are the simplest. Each time it is called, getchar reads the
next input character from a text stream and returns that as its value. That is, after
c = getchar();
the variable ccontains the next character of input. The characters normally come from the
keyboard;
The function putchar prints a character each time it is called:
putchar(c);
prints the contents of the integer variable cas a character, usually on the screen.
################################################## Data Types and Sizes
##################################################
There are only a few basic data types in C:
1. char :
a single byte, capable of holding one character in the local character set
2. int :
an integer, typically reflecting the natural size of integers on the host machine...short and
long apply to integers
'shorts' and ints are at least 16 bits , 'longs' are at least 32 bits , and 'short' is no longer than
int, which is no longer than 'long'.
The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers
are always positive or zero, and obey the laws of arithmetic modulo 2^n, where n is the
number
of bits in the type. So, for instance, if chars are 8 bits, unsigned charvariables have values
between 0 and 255,
while signed chars have values between -128 and 127
3. float :
pf3
pf4
pf5

Partial preview of the text

Download The C Programming Language and more Study notes C programming in PDF only on Docsity!

################################################## Basics ##################################################

your program begins executing at the beginning of main. This means that every program must have a main somewhere. mainwill usually call other functions to help performits job, somethat you wrote, and others from libraries that are provided for you.

#include <stdio.h> -- tells the compiler to include information about the standard input/output library

Variables and constants are the basic data objects manipulated in a program. Declarations list the variables to be used, and state what type they have and perhaps what their initial values are. Operators specify what is to be done to them. Expressions combine variables and constants to produce new values.

The standard library provides several functions for reading or writing one character at a time, of which getchar and putchar are the simplest. Each time it is called, getchar reads the next input character from a text stream and returns that as its value. That is, after c = getchar(); the variable ccontains the next character of input. The characters normally come from the keyboard; The function putchar prints a character each time it is called: putchar(c); prints the contents of the integer variable cas a character, usually on the screen.

################################################## Data Types and Sizes ##################################################

There are only a few basic data types in C:

  1. char : a single byte, capable of holding one character in the local character set
  2. int : an integer, typically reflecting the natural size of integers on the host machine...short and long apply to integers 'shorts' and ints are at least 16 bits , 'longs' are at least 32 bits , and 'short' is no longer than int, which is no longer than 'long'. The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2^n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned charvariables have values between 0 and 255, while signed chars have values between -128 and 127
  3. float :

single-precision floating point

  1. double : double-precision floating point

################################################## Constants ##################################################

A long constant is written with a terminal l(ell) or L, as in 123456789L;

Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long

The suffixes for F indicate a float constant ; l or L indicate a long double.

A character constantis an integer, written as one character within single quotes, such as 'x'

The complete set of escape sequences is :

\a alert (bell) character \ backslash \b backspace ? question mark \f formfeed ' single quote \n newline " double quote \r carriage return \ooo octal number \t horizontal tab \xhh hexadecimal number \v vertical tab

Be careful to distinguish between a character constant and a string that contains a single character: 'x'is not the same as "x". The former is an integer, used to produce the numeric value of the letter xin the machine's character set. The latter is an array of characters that contains one character (the letter x) and a '\0'.

There is one other kind of constant, the enumeration constant. An enumeration is a list of constant integer values...

enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' };

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };/* FEB = 2, MAR = 3, etc. */

If not all values are specified, unspecified values continue the progression from the last specified value, as the second of these example

3.Equality 4.Logical : && g.t. ||

3.Increment and Decrement Operators :

C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1 aspect is that ++ and -- may be used either as prefix operators or postfix operators But the expression ++n increments n before its value is used, while n++ increments n after its value has been used.

for (i = j = 0; s[i] != '\0'; i++) if (s[i] != c) s[j++] = s[i]; s[j] = '\0';

this is equivalent to :

if (s[i] != c) { s[j] = s[i]; j++;

  1. Bitwise Operators

C provides six operators for bit manipulation; these may only be applied to integral operands, that is, char, short, int, and long, whether signed or unsigned. & bitwise AND | bitwise inclusive OR ^ bitwise exclusive OR << left shift

right shift ~ one's complement (unary)

The bitwise exclusive OR operator ^sets a one in each bit position where its operands have different bits, and zero where they are the same

The shift operators <>perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non-negative

Right shifting an unsignedquantity always fits the vacated bits with zero. Right shifting a signed quantity will fill with bit signs

################################################## 5.Assignment Operators and Expressions##################################################

An expression as i = i + 2

in which the variable on the left side is repeated immediately on the right, can be written in the compressed form as i += 2 The operator +=is called an assignment operator. If expr1 and expr2 are expressions , then expr1 op= expr is equivalent to -- expr1 = (expr1) op (expr2)

x *= y + 1 means x = x * (y + 1) rather than x = x * y + 1

Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op= , where op is one of + - * / % << >> & ^ |

################################################## Type Conversions ##################################################

In general, if an operator like + or * that takes two operands (a binary operator) has operands of different types, the lower'' type is promotedto thehigher'' type before the operation proceeds

################################################## Conditional Expressions ##################################################

The conditional expression, written with the ternary operator ``?:'', provides an alternate way to write this and similar constructions. In the expression expr1? expr2: expr3 the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3is evaluated Thus to set z to the maximum of a and b, z = (a > b)? a : b;

################################################## If-Else and Else-If ##################################################

The if-elsestatement is used to express decisions. Formally the syntax is if (expression) statement else statement

where the elsepart is optional. The expressionis evaluated; if it is true (that is, if expression has a non-zero value), statement1is executed. If it is false (expressionis zero) and if there is an elsepart, statement2is executed instead.