escape sequence new line /n question

This is a discussion on escape sequence new line /n question within the C Programming forums, part of the General Programming Boards category; In the below code, why is it that if /n, it accepts 6 values, but if /n is not there, ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    escape sequence new line /n question

    In the below code, why is it that if /n, it accepts 6 values, but if /n is not there, it accepts 5. The full code is below so that you may run the program

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int temp[5]; //4 elements numbered 0 to 4
    	int input;
    
    	printf("How many degrees has it been the past 5 days?\n");
    	
    	for(input=0;input < 5;input++)
    	{
    		scanf("%f\n", &temp[input]);	
    	}
    
    
    
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    93
    Quote Originally Posted by bos1234 View Post
    In the below code, why is it that if /n
    i am no expert in C but i found the following errors in your code. when you are saying /n i think you are actually referring \n


    Code:
    #include <stdio.h>
    
    int main()
    {
        int temp[5]; //4 elements numbered 0 to 4
       
       for(input=0;input < 5;input++)
        {
            scanf("%f\n", &temp[input]);    
        }
    }
    the temp variable you are declaring as int but in scanf you are reading it as float, i think obviously you might have got warning in that and need to rectify it.

    and the final part the scanf format specifier is of the following type
    %[*][width][modifiers]type

    scanf - C++ Reference
    so it is improper if you put a \n in format specifier.

    experts if i am wrong please correct me.

    thanks and regards,
    satya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2011, 06:59 PM
  2. Weird problem: error C2017: illegal escape sequence
    By h3ro in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2008, 07:29 PM
  3. Escape sequence for &
    By 3saul in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 10:01 PM
  4. Escape sequence of interest?
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 10:28 AM
  5. escape sequence
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 08-01-2002, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21