Document Sample
lecture4 Powered By Docstoc
					  C Programming

    Strings, Arrays,
Input, Output Statements

A string is a sequence of zero or more
characters surrounded by double quotes
• Eg: “I am a string”
• Quotes are not part of the string
• String constants can be concatenated at compile
  time: “hello,” “world” = “hello,world”

Array is an indexed sequence of elements belonging
to the same type and has a single name.
Eg: int total[5];     declares that total is an int
array having 5 int elements.
  total[0] is the first element, total[1] is the second
  element and … total[4] is the last element.
  total[index] is an int variable where index gives the
  position of the element in the array.
 Character strings

Array of characters where last character is
‘\0’ (null character).
Eg: char name[10] = “cat”;
     name[0] ‘c’ name[1] ‘a’
     name[2] ‘t’ name[3] ‘\0’

    name[4] to name[9] contains garbage.
character strings …

int i;
char name[10];
i = 123;       /* OK */
name = “cat”; /* this assignment will not
                  work */
 character strings …

 char name[10] = “cat”; /* initialization*/
 char name[10];
 name = “cat”; /* wrong assignment*/
 assign each character as
 name[0] = ‘c’; name[1] = ‘a’; … name[3] = ‘\0’;
Or one can use strcpy(name, “cat”); a library
function in string.h (Discussion of this is deferred)
 Input and Output

Input and output facilities are not part of the
C language itself !
These are supported by a set of library
functions in stdio.h
   getchar( ) and putchar( )

char c;         /* declaration */
c = getchar( ); /* reads a character from
                   keyboard (stdin)*/
putchar(c);     /* writes the character stored
                   in the variable c on the
                   screen (stdout)*/
  scanf (formatted input)

int i;
char ch;
scanf(“%d”, &i); /* read an integer from
                    stdin into i */
scanf(“%c”, &ch); /* read a char from stdin
                      into ch */
     scanf (formatted input)

  /* both can be read in a single scanf */
  scanf(“%d %c”, &i, &ch);

                                  char variable
  Format string
                   int variable

Why there is ‘&’ character before i and ch ?
 Why ‘&’ in scanf ?

Every variable should have a location in the
If i is variable’s name then &i is its address
in the memory.
scanf should be given the addresses of the
locations where it should store the read

Different values should be separated with a
space or newline.
  More about this is deferred.
    printf (formatted output)

int i; char ch;
i = 125;
ch = ‘a’;
printf(“%d %c \n”, i, ch);    125 a
printf(“%c\n%d”,ch,i);        a

                             Output on the screen

char ch = ‘a’;
printf(“%d”, ch);

What will be the output?
   How to read or write a float?

%f         float

float num;
scanf(“%f ”, &num);
printf(“%f ”, num);
    How to read or write a string?

 char str[64];
 printf(“what is your name:”);
 scanf(“%s”, str);
 printf(“Hello … %s \n”, str);

                           What is your name: Ram
Output on screen
                           Hello … Ram

Why we have not used ‘&’ while reading a
string ?
Findout the answer.

        More about scanf, printf, etc later ……

Shared By: